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?
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);