-1

So I have this big buffer in the shape of a byte array that I get from the client and I receive it on the server and it looks like this.

[10, 0, 5, 11, 130, 0, 0, 0, 0, 0, 10, 0, 5, 11, 131, 0, 0, 0, 0, 0, 10, 0, 5, 11, 132, 0, 0, 0, 0, 0, 10, 0, 5, 11, 133, 0, 0, 0, 0, 0, 10, 0, 5, 11, 134, 0, 0, 0, 0, 0, 10, 0, 5, 11, 135, 0, 0, 0, 0, 0, 10, 0, 5, 11, 136, 0, 0, 0, 0, 0, 10, 0, 5, 11, 137, 0, 0, 0, 0, 0, 10, 0, 5, 11, 138, 0, 0, 0, 0, 0, 10, 0, 5, 11, 139, 0, 0, 0, 0, 0, 10, 0, 5, 11, 140, 0, 0, 0, 0, 0, 10, 0, 5, 11, 141, 0, 0, 0, 0, 0, 10, 0, 5, 11, 142, 0, 0, 0, 0, 0, 10, 0, 5, 11, 143, 0, 0, 0, 0, 0, 10, 0, 5, 11, 144, 0, 0, 0, 0, 0, 10, 0, 5, 11, 145, 0, 0, 0, 0, 0, 10, 0, 5, 11, 146, 0, 0, 0, 0, 0, 10, 0, 5, 11, 147, 0, 0, 0, 0, 0, 10, 0, 5, 11, 148, 0, 0, 0, 0, 0, 10, 0, 5, 11, 149, 0, 0, 0, 0, 0, 10, 0, 5, 11, 150, 0, 0, 0, 0, 0, 10, 0, 5, 11, 151, 0, 0, 0, 0, 0, 10, 0, 5, 11, 152, 0, 0, 0, 0, 0, 10, 0, 5, 11, 153, 0, 0, 0, 0, 0, 10, 0, 5, 11, 154, 0, 0, 0, 0, 0, 10, 0, 5, 11, 155, 0, 0, 0, 0, 0, 10, 0, 5, 11, 156, 0, 0, 0, 0, 0, 10, 0, 5, 11, 157, 0, 0, 0, 0, 0, 10, 0, 5, 11, 158, 0, 0, 0, 0, 0, 10, 0, 5, 11, 159, 0, 0, 0, 0, 0, 10, 0, 5, 11, 160, 0, 0, 0, 0, 0, 10, 0, 5, 11, 161, 0, 0, 0, 0, 0, 10, 0, 5, 11, 162, 0, 0, 0, 0, 0, 10, 0, 5, 11, 163, 0, 0, 0, 0, 0, 10, 0, 5, 11, 164, 0, 0, 0, 0, 0, 10, 0, 5, 11, 165, 0, 0, 0, 0, 0, 10, 0, 5, 11, 166, 0, 0, 0, 0, 0, 10, 0, 5, 11, 167, 0, 0, 0, 0, 0, 10, 0, 5, 11, 168, 0, 0, 0, 0, 0, 10, 0, 5, 11, 169, 0, 0, 0, 0, 0, 10, 0, 5, 11, 170, 0, 0, 0, 0, 0, 10, 0, 5, 11, 171, 0, 0, 0, 0, 0, 10, 0, 5, 11, 172, 0, 0, 0, 0, 0, 10, 0, 5, 11, 173, 0, 0, 0, 0, 0, 10, 0, 5, 11, 174, 0, 0, 0, 0, 0, 10, 0, 5, 11, 175, 0, 0, 0, 0, 0, 10, 0, 5, 11, 176, 0, 0, 0, 0, 0, 10, 0, 5, 11, 177, 0, 0, 0, 0, 0, 10, 0, 5, 11, 178, 0, 0, 0, 0, 0, 10, 0, 5, 11, 179, 0, 0, 0, 0, 0, 10, 0, 5, 11, 180, 0, 0, 0, 0, 0, 10, 0, 5, 11, 181, 0, 0, 0, 0, 0, 10, 0, 5, 11, 182, 0, 0, 0, 0, 0, 10, 0, 5, 11, 183, 0, 0, 0, 0, 0, 10, 0, 5, 11, 184, 0, 0, 0, 0, 0, 10, 0, 5, 11, 185, 0, 0, 0, 0, 0, 10, 0, 5, 11, 186, 0, 0, 0, 0, 0, 10, 0, 5, 11, 187, 0, 0, 0, 0, 0, 10, 0, 5, 11, 188, 0, 0, 0, 0, 0, 10, 0, 5, 11, 189, 0, 0, 0, 0, 0, 10, 0, 5, 11, 190, 0, 0, 0, 0, 0, 10, 0, 5, 11, 191, 0, 0, 0, 0, 0, 10, 0, 5, 11, 192, 0, 0, 0, 0, 0, 10, 0, 5, 11, 193, 0, 0, 0, 0, 0, 10, 0, 5, 11, 194, 0, 0, 0, 0, 0, 10, 0, 5, 11, 195, 0, 0, 0, 0, 0, 10, 0, 5, 11, 196, 0, 0, 0, 0, 0, 10, 0, 5, 11, 197, 0, 0, 0, 0, 0, 10, 0, 5, 11, 198, 0, 0, 0, 0, 0, 10, 0, 5, 11, 199, 0, 0, 0, 0, 0, 10, 0, 5, 11, 200, 0, 0, 0, 0, 0, 10, 0, 5, 11, 201, 0, 0, 0, 0, 0, 10, 0, 5, 11, 202, 0, 0, 0, 0, 0, 10, 0, 5, 11, 203, 0, 0, 0, 0, 0, 10, 0, 5, 11, 204, 0, 0, 0, 0, 0, 10, 0, 5, 11, 205, 0, 0, 0, 0, 0, 10, 0, 5, 11, 206, 0, 0, 0, 0, 0, 10, 0, 5, 11, 207, 0, 0, 0, 0, 0, 10, 0, 5, 11, 208, 0, 0, 0, 0, 0, 10, 0, 5, 11, 209, 0, 0, 0, 0, 0, 10, 0, 5, 11, 210, 0, 0, 0, 0, 0, 10, 0, 5, 11, 211, 0, 0, 0, 0, 0, 10, 0, 5, 11, 212, 0, 0, 0, 0, 0, 10, 0, 5, 11, 213, 0, 0, 0, 0, 0, 10, 0, 5, 11, 214, 0, 0, 0, 0, 0, 10, 0, 5, 11, 215, 0, 0, 0, 0, 0, 10, 0, 5, 11, 216, 0, 0, 0, 0, 0, 10, 0, 5, 11, 217, 0, 0, 0, 0, 0, 10, 0, 5, 11, 218, 0, 0, 0, 0, 0, 10, 0, 5, 11, 219, 0, 0, 0, 0, 0, 3, 0, 6]

And there is a pattern to this. It's a buffer filled with a few packets and the packets look like this.

The first two bytes are reserved for the size (int16) [10, 0]

Then there is the next byte which is the type of the packet (5) [10, 0, 5]

and then the rest is the payload [10, 0, 5, 11, 130, 0, 0, 0, 0, 0]

And the goal here is that I want to read the length of the packet - the length so just the payload, process it and then read the next packet which is the length of the next packet.

The only way I can think of is

for (int i = 0; i < 10; i++) {
    //ProcessBuffer();
}

But that only does it for the first 10, it doesnt change that 10 to what the length of the next one is.

What's the proper way of doing this?

Riley Varga
  • 670
  • 1
  • 5
  • 15
  • So why not read the length, do processing, change the index to that length until you've hit the end of the byte array? – Ross Jacobs Apr 29 '20 at 21:40
  • @RossJacobs What do you mean? I'm not quite following – Riley Varga Apr 29 '20 at 21:52
  • Use a while loop with an index to get the first length of packet, add the packet to a packet list, and then increase the index by the packet length. Do this until the index >= the number of bytes in this byte array. – Ross Jacobs Apr 29 '20 at 23:34

1 Answers1

1

Assuming buffer contains the Byte[]:

var bufferPos = 0;

do {
    // get size of next packet
    var packetSize = buffer[bufferPos]+buffer[bufferPos+1]*256;
    // get type of next packet
    var packetType = buffer[bufferPos+2];
    var payload = new ArraySegment<Byte>(buffer, bufferPos+3, packetSize-3);
    // process packet here

    bufferPos += packetSize;
} while (bufferPos < buffer.Length);
NetMage
  • 26,163
  • 3
  • 34
  • 55
  • Oh that makes a lot of sense! I really like the way you utilized a do while loop! – Riley Varga Apr 30 '20 at 00:02
  • Do/while ignores the edge case of having an empty input array (let's pretend you capture packets on the wrong interface or with too strict a filter). Given this question, that's probably fine though. OP knows more about his requirements than we do. – Ross Jacobs Apr 30 '20 at 08:39
  • @RossJacobs Good point - I did assume the buffer wasn't empty by the time it is being parsed, but a `while` might be better after all. – NetMage Apr 30 '20 at 19:20