简体   繁体   English

如何解析字节数组中的块

[英]how do I parse chunks in a byte array

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]前两个字节为大小 (int16) [10, 0]保留

Then there is the next byte which is the type of the packet (5) [10, 0, 5]然后是下一个字节,它是数据包的类型 (5) [10, 0, 5]

and then the rest is the payload [10, 0, 5, 11, 130, 0, 0, 0, 0, 0]然后 rest 是有效载荷[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.但这仅适用于前 10 个,它不会将10更改为下一个的长度。

What's the proper way of doing this?这样做的正确方法是什么?

Assuming buffer contains the Byte[] :假设buffer包含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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM