初始化obs/Unity仓库
This commit is contained in:
@@ -0,0 +1,33 @@
|
||||
void OnReceive(byte[] data, int length)
|
||||
{
|
||||
// 把收到的数据追加到缓存末尾
|
||||
Array.Copy(data, 0, cacheBuffer, cacheCount, length);
|
||||
cacheCount += length;
|
||||
|
||||
// 循环处理缓存中的完整消息
|
||||
while (true)
|
||||
{
|
||||
// 不足4字节,说明长度头还没收全
|
||||
if (cacheCount < 4)
|
||||
break;
|
||||
|
||||
// 取出前4字节的消息长度
|
||||
int msgLength = BitConverter.ToInt32(cacheBuffer, 0);
|
||||
|
||||
// 检查是否收全整个消息
|
||||
if (cacheCount - 4 < msgLength)
|
||||
break;
|
||||
|
||||
// 提取消息体
|
||||
byte[] msgBody = new byte[msgLength];
|
||||
Array.Copy(cacheBuffer, 4, msgBody, 0, msgLength);
|
||||
|
||||
// ---- 处理消息 ----
|
||||
ProcessMessage(msgBody);
|
||||
|
||||
// 将剩余数据前移
|
||||
int remain = cacheCount - 4 - msgLength;
|
||||
Array.Copy(cacheBuffer, 4 + msgLength, cacheBuffer, 0, remain);
|
||||
cacheCount = remain;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user