Re: A reference to a hash member? (to an object's member variable)



On Fri, 22 Aug 2008 21:53:40 GMT, sln@xxxxxxxxxxxxxxx wrote:

On Fri, 22 Aug 2008 14:15:24 -0700 (PDT), "A. Farber" <Alexander.Farber@xxxxxxxxx> wrote:

Hello,

I have an object, which reads a header
from a non-blocking socket and if the header
indicates, that there is more data to read
(a chat string) then it will read that too.

To handle those 2 reading states
(reading a header; reading a chat string)
I try to use 3 variables: $buf, $toread, $offset
and then call sysread($buf, $toread, $offset).

My problem is, that I need to set
$buf to _point_ to $self->{REQ_HEADER}
in the 1st state and to $self->{CHAT}
in the 2nd state. But I don't know how.

In C-code (which I'm trying to port to Perl)
this has been very easy. But in Perl I don't
know how and rereading "perlref" doesn't help.

Any hints please?
Alex

PS: My complete, but buggy code is below:

sub read {
my $self = shift;
my $fh = $self->{FH};
my ($buf, $toread, $offset, $nbytes);

# start or continue reading the request header
if ($self->{NREAD} < REQ_HEADER_LEN) {
$buf = $self->{REQ_HEADER};
$offset = $self->{NREAD};
$toread = REQ_HEADER_LEN - $offset;
# start or continue reading the chat string
} else {
$buf = $self->{CHAT};
$offset = $self->{NREAD} - REQ_HEADER_LEN;
$toread = $self->{LEN} - $offset;
}

# XXX the buf is WRONG here:
$nbytes = $fh->sysread($buf, $toread, $offset);

unless (defined $nbytes) {
# interrupted by signal or would block - retry later
return if $!{EINTR} || $!{EAGAIN} || $!{EWOULDBLOCK};
# connection interrupted - remove the Apache child
$self->remove($fh);
return;
}

if (0 == $nbytes) {
# connection closed - remove the Apache child
$self->remove($fh);
return;
}

$self->{NREAD} += $nbytes;
print STDERR "nbytes = $nbytes\n";

# not done yet - continue reading
return if $nbytes < $toread;

if (REQ_HEADER_LEN == $self->{NREAD}) {
($self->{SID}, $self->{EVENT}, $self->{MOD}, $self-
{LEN}) =
unpack 'A32xN3', $self->{REQ_HEADER};

printf STDERR "sid=%s, event=%x, mod=%x, len=%x\n",
$self->{SID}, $self->{EVENT}, $self->{MOD}, $self-
{LEN};

# not done yet - continue reading (the chat string)
return if $self->{LEN} > 0;
}

print STDERR "done reading";
#$Poll->mask($fh => POLLOUT);
}

I don't know for sure if "$fh->sysread() is valid (it may be),
but did you try this:

$nbytes = sysread($fh, $buf, $toread, $offset);

-------

sysread FILEHANDLE,SCALAR,LENGTH,OFFSET

Also, OFFSET, in this case, is not the file offset. But I don't
know what your trying to achieve.

"An OFFSET may be specified to place the read data at some place in
the string other than the beginning. A negative OFFSET specifies
placement at that many characters counting backwards from the end
of the string. A positive OFFSET greater than the length of SCALAR
results in the string being padded to the required size with "\0"
bytes before the result of the read is appended"


sln

It is possible that "reads" need a reference to a buffer, I'm not
sure.

So, something like this would be necessary:

$buf = \$self->{REQ_HEADER};

unless $self->{REQ_HEADER} is already a SCALAR reference.

Not sure.


sln

.



Relevant Pages

  • A reference to a hash member? (to an objects member variable)
    ... I have an object, which reads a header ... To handle those 2 reading states ... (reading a header; reading a chat string) ... # connection interrupted - remove the Apache child ...
    (comp.lang.perl.misc)
  • Re: A reference to a hash member? (to an objects member variable)
    ... I have an object, which reads a header ... To handle those 2 reading states ... (reading a header; reading a chat string) ... Also, OFFSET, in this case, is not the file offset. ...
    (comp.lang.perl.misc)
  • Re: How to determine integer values in a txt file?
    ... the fixed header, skip 2 bytes and read the 5th byte to determine how many ... The problem boils down to one of positioning and then reading - how it can ... the stream then the task is easy - just read the header in! ... really, quite easy to accomplish. ...
    (comp.lang.java.programmer)
  • Re: confused about word templates
    ... dialog and the ³same as previous² setting in the header and footer settings ... And congratulations on reading the documentation. ... stubborn as I am when I have a formatting problem. ... > textedit files are in word format and open in Word} ...
    (microsoft.public.mac.office.word)
  • Re: Parsing Multipart formdata
    ... Read the header, ... Start reading the part header, ... Stop reading into the string buffer when you see the boundary again. ... The array of bytes is your binary data. ...
    (microsoft.public.dotnet.languages.csharp)