1

This is how I read the input stream

while (!stopRequested && stream.CanRead)
{
    byte[] buffer = new byte[READ_BUFFER_SIZE];

    Int32 bytesRead = stream.Read(buffer, 0, READ_BUFFER_SIZE);
    byte[] response = new byte[bytesRead];
    Array.Copy(buffer, response, bytesRead);

    responsesStream.AddRange(response); //push the bytes onto the dataStream
    ParseStream(responsesStream); //attempt to parse
}

How do you implement an efficient binary protocol parser given that you have a spec like in following example enter image description here

Concise code samples or links highly appreciated. thx

Erik Philips
  • 53,428
  • 11
  • 128
  • 150
ruslander
  • 3,815
  • 4
  • 32
  • 34
  • possible duplicate of [Binary communications protocol parser design for serial data](http://stackoverflow.com/questions/3278227/binary-communications-protocol-parser-design-for-serial-data) – mybirthname Feb 09 '15 at 04:50
  • Is it just 10 bytes that need to be read? Why not just read 10 bytes from the buffer into a byte array of 10? Asking how to 'efficiently' read just 10 bytes is a bit overkill. We're talking about microseconds here – tofi9 Feb 09 '15 at 05:04
  • @mybirthname the message size is variable based on second byte – ruslander Feb 09 '15 at 14:41

0 Answers0