//消息处理 private void ProcessData() { //黏包分包处理 if (_buffCount < sizeof(Int32)) { return; } //包体长度 Array.Copy(_readbuff, _lenBytes, sizeof(Int32)); _msgLength = BitConverter.ToInt32(_lenBytes, 0); if (_buffCount < sizeof(Int32) + _msgLength) { return; } //协议解码 ProtocolBase protocol = _proto.Decode(_readbuff, sizeof(Int32), _msgLength); Debug.Log("收到消息" + protocol.GetDesc()); lock (msgDist.msgList){ msgDist.msgList.Add(protocol); } //清楚已处理的消息 int count = _buffCount - _msgLength - sizeof(Int32); Array.Copy(_readbuff, sizeof(Int32) + _msgLength, _readbuff, 0, count); _buffCount = count; if (_buffCount > 0) { ProcessData(); } }
private void ProcessData() //处理数据(粘包分包) { if (buffCount < sizeof(Int32)) { return; } Array.Copy(readBuff, lenBytes, sizeof(Int32)); msgLength = BitConverter.ToInt32(lenBytes, 0); if (buffCount < msgLength + sizeof(Int32)) { return; } //计算包体长度 ProtocolBase protocol = proto.Decode(readBuff, sizeof(Int32), msgLength); Debug.Log("收到消息" + proto.GetDesc()); //解码 lock (msgDist.msgList) { msgDist.msgList.Add(protocol); } //线程加锁 int count = buffCount - msgLength - sizeof(Int32); Array.Copy(readBuff, sizeof(Int32) + msgLength, readBuff, 0, count); buffCount = count; //清除已处理的消息 if (buffCount > 0) { ProcessData(); } //如果还有数据,继续处理 }
//消息处理 private void ProcessData() { //沾包分包处理 if (buffCount < sizeof(Int32)) { return; } //包体长度 Array.Copy(readBuff, lenBytes, sizeof(Int32)); msgLength = BitConverter.ToInt32(lenBytes, 0); if (buffCount < msgLength + sizeof(Int32)) { return; } //协议解码 ProtocolBase protocol = proto.Decode(readBuff, sizeof(Int32), msgLength); Debug.Log("收到消息 " + protocol.GetDesc()); lock (msgDist.msgList) { msgDist.msgList.Add(protocol); } //清除已处理的消息 int count = buffCount - msgLength - sizeof(Int32); Array.Copy(readBuff, sizeof(Int32) + msgLength, readBuff, 0, count); buffCount = count; if (buffCount > 0) { ProcessData(); } }
//消息处理 private void ProcessData() { //小于长度字节 if (buffCount < sizeof(Int32)) { return; } //消息长度 Array.Copy(readBuff, lenBytes, sizeof(Int32)); msgLength = BitConverter.ToInt32(lenBytes, 0); if (buffCount < msgLength + sizeof(Int32)) { return; } //处理消息 //ProtocolBase protocol = proto.Decode(readBuff,sizeof(Int32), msgLength); ProtocolBase proto_register = proto.Decode(readBuff, 0, buffCount); //Debug.Log("收到消息 " + protocol.GetDesc()); Debug.Log("收到消息 " + proto_register.GetDesc()); lock (msgDist.msgList) { msgDist.msgList.Add(proto_register); } buffCount = 0; // 清除已处理的消息 //int count = buffCount - msgLength - sizeof(Int32); //Array.Copy(readBuff,sizeof(Int32)+ msgLength, readBuff, 0, count); //buffCount = count; //if (buffCount > 0) //{ // ProcessData(); //} }
private void ProcessData() { //throw new NotImplementedException(); //黏包分包处理 if (buffCount < sizeof(Int32)) { return; } //包长 Array.Copy(readbuff, lenBytes, sizeof(Int32)); msgLength = BitConverter.ToInt32(lenBytes, 0); if (buffCount < msgLength + sizeof(Int32)) { return; } ProtocolBase protocol = proto.Decode(readbuff, sizeof(Int32), msgLength); Debug.Log(protocol.GetDesc() + "解码收到"); lock (msgDist.msgList) { msgDist.msgList.Add(protocol); } //清除已处理的消息 int count = buffCount - msgLength - sizeof(Int32); Array.Copy(readbuff, sizeof(Int32) + msgLength, readbuff, 0, count); buffCount = count; if (buffCount > 0) { ProcessData(); } }
//发送协议 public bool Send(ProtocolBase protocol) { if (status != Status.Connected) { Debug.LogError("[Connection]还没连接就发送数据"); return(true); } byte[] b = protocol.Encode(); byte[] length = BitConverter.GetBytes(b.Length); byte[] sendBuff = length.Concat(b).ToArray(); socket.Send(sendBuff); Debug.Log("发送消息 " + protocol.GetDesc()); return(true); }
//send 及 重载 public bool Send(ProtocolBase protocolBase) { if (_status != Status.Connected) { Debug.LogError("[Connection]: 没连接就发送数据是不好的 !!!"); return(false); } byte[] b = protocolBase.Encode(); byte[] length = BitConverter.GetBytes(b.Length); byte[] sendbuff = length.Concat(b).ToArray(); _socket.Send(sendbuff); Debug.Log("发送消息 : " + protocolBase.GetDesc()); return(true); }
public bool Send(ProtocolBase protoBase) { if (status != Status.Connected) { Console.WriteLine("[Connection.Send] Connection is not exist."); return(false); } byte[] strBytes = protoBase.Encode(); byte[] lenBytes = BitConverter.GetBytes(strBytes.Length); byte[] sendBytes = lenBytes.Concat(strBytes).ToArray(); socket.Send(sendBytes); Console.WriteLine("[Connection.Send] Send protocol: " + protoBase.GetDesc()); return(true); }
//处理粘包、分包 public void ProcessData() { if (!recvNetPachage.DecodeHeader()) { return; } if (!recvNetPachage.HasEnoughData()) { return; } ProtocolBase protocol = proto.Decode(recvNetPachage.buffer, NetPackage.HEADER_SIZE, recvNetPachage.msgLength); Console.WriteLine("收到消息 " + protocol.GetDesc()); msgDist.AddMsg(protocol); recvNetPachage.DeleteCurData(); if (recvNetPachage.HasData()) { ProcessData(); } }