dichtfux has asked for the wisdom of the Perl Monks concerning the following question:
The function read_buffer() and skip() are used in the code above and therefor also shown here:## read a string until a '\x00' is encountered (-> null termination of + strings in C!) sub read_complete_string { my ($self) = @_; my $string = ""; ## return empty string if we're already at the end of the buffer if($self->index() >= $self->size()) { return $string; } my $end_of_string = index($self->buffer(), "\x00", $self->index()) +; if(!($end_of_string)) { ## just read the rest of the buffer if there is no termina +tion $string = $self->read_buffer($self->size() - $self->index +()); } else { $string = $self->read_buffer($end_of_string - $self->index()); ## ignore the nullbyte '\x00' $self->skip(1); } return $string; }
I do now need to parse the following C struct from the data:## read the specified number of bytes from buffer, called by the other + read_* functions sub read_buffer { my ($self , $user_length) = @_; my $length = 1; $length = $user_length if $user_length; if(($self->index() + $length) > $self->size()) { print "$tag: WARN +ING: ##### read_buffer(): not enough data left, ignoring request to r +ead $length bytes! #####\n"; return 0; } my $val = substr($self->buffer(), $self->index(), $length); $self->index($self->index() + $length); print "$tag: DEBUG: read_buffer(): read raw data '$val' of length +$length, now at $self->{_index} (" . $self->data_left() . " bytes of +" . $self->size() . " left)\n"; return $val; } ## skips the given number of bytes sub skip { my ($self, $user_length) = @_; my $default_length; my $length; $default_length = 1; $length = $default_length; $length = $user_length if $user_length; $self->index($self->index() + $length); return $length; }
And I'm trying to get the ping of this client with the following code:struct client_t { short ping; int rate; char name[32]; // NULL-terminated byte clanTagPosition; // 0: prefix, 1: suffix char clanTag[32]; // NULL-terminated byte isBot; };
I'm getting a number sometimes, but most of the time it's just empty or nonsense.sub read_short { my ($self) = @_; my $sys_short_length = $Config{shortsize}; my $short = unpack('S', $self->read_buffer($sys_short_length)); return $short; } # elsewhere in the code: print "ping: " . $self->package_reader->read_short() . "\n";
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Parsing protocol data: unpack and bytes
by BrowserUk (Patriarch) on Dec 10, 2007 at 17:11 UTC | |
by dichtfux (Sexton) on Dec 10, 2007 at 17:56 UTC | |
by ikegami (Patriarch) on Dec 10, 2007 at 18:18 UTC | |
by BrowserUk (Patriarch) on Dec 10, 2007 at 19:23 UTC | |
by dichtfux (Sexton) on Dec 10, 2007 at 20:46 UTC | |
by BrowserUk (Patriarch) on Dec 10, 2007 at 23:30 UTC | |
by tye (Sage) on Dec 10, 2007 at 23:54 UTC |