It turned out that this is not the case. The 'name' and 'clan' fields are max 32 chars, not fixed at 32 chars.

Then your struct definition is wrong.

If the strings are variable length, then use 'Z*':

print for unpack 'n N Z* C Z* C', "\x01\x01\x04\x03\x02\x01The first string\0\xffThe second string\0 +\xff";; 257 67305985 The first string 255 The second string 255
That's why I structured the code a bit more than I usually tend to in perl...

I'm not suggesting you stop structuring your code, just structure is differently. Java streams are just one possible abstraction of the problem.

I would suggest a different abstraction that uses the power of unpack format strings. Have a different class for each packet and pass the buffer into the constructor and have it remove its stuff from that buffer.

package This::Packet; sub new { my( $class, $bufRef ) = @_; my( $ping, $rate, $name, $ctpos, $clantag, $isbot ) = unpack 'n N Z* C Z* C', $$bufRef; my $nPacket = 2 + 4 + length( $name )+ 1 + 1 ## string + null + ctpos + length( $clantag ) + 1 + 1; ## string + null + IsBot # ... validate substr( $$bufRef, 0, $nPacket, '' ); ## remove this packet from th +e buffer return bless { ping => $ping, rate => $rate, name => $name, cpos => $ctpos, clantag => $clantag, isBot => $isBot, }, $class; } ## accessors go here

This way moves all the packets specific information inside a class that deals with that packet. Each class removes it's data from the buffer and the higher level only has to decide which packet to deal with next.


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
"Too many [] have been sedated by an oppressive environment of political correctness and risk aversion."

In reply to Re^5: Parsing protocol data: unpack and bytes by BrowserUk
in thread Parsing protocol data: unpack and bytes by dichtfux

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.