如何解析字节数组中的块
how do I parse chunks in a byte array
所以我有一个字节数组形状的大缓冲区,它是我从客户端获取的,我在服务器上接收到它,它看起来像这样。
[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]
这是有规律可循的。
这是一个充满了一些数据包的缓冲区,数据包看起来像这样。
前两个字节保留为大小 (int16) [10, 0]
然后是下一个字节是数据包的类型(5)[10, 0, 5]
然后剩下的就是payload[10, 0, 5, 11, 130, 0, 0, 0, 0, 0]
这里的目标是我想读取数据包的长度 - 长度,所以只是有效负载,对其进行处理,然后读取下一个数据包,即下一个数据包的长度。
我能想到的唯一办法就是
for (int i = 0; i < 10; i++) {
//ProcessBuffer();
}
但这只对前 10 个有效,不会将 10
更改为下一个的长度。
这样做的正确方法是什么?
假设 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);
所以我有一个字节数组形状的大缓冲区,它是我从客户端获取的,我在服务器上接收到它,它看起来像这样。
[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]
这是有规律可循的。 这是一个充满了一些数据包的缓冲区,数据包看起来像这样。
前两个字节保留为大小 (int16) [10, 0]
然后是下一个字节是数据包的类型(5)[10, 0, 5]
然后剩下的就是payload[10, 0, 5, 11, 130, 0, 0, 0, 0, 0]
这里的目标是我想读取数据包的长度 - 长度,所以只是有效负载,对其进行处理,然后读取下一个数据包,即下一个数据包的长度。
我能想到的唯一办法就是
for (int i = 0; i < 10; i++) {
//ProcessBuffer();
}
但这只对前 10 个有效,不会将 10
更改为下一个的长度。
这样做的正确方法是什么?
假设 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);