private void UDPReceiveCb(IAsyncResult ar) { int count = UDPsocket.EndReceiveFrom(ar, ref UDPRemote); Array.Copy(UDPreadBuff, UDPlenBytes, sizeof(Int32)); int msgLength = BitConverter.ToInt32(UDPlenBytes, 0); ProtocolBase protocolBase = proto.Decode(UDPreadBuff, sizeof(Int32), msgLength); ProtocolBytes protocol = (ProtocolBytes)protocolBase; int start = 0; int conn_id = protocol.GetInt(start, ref start); string protoName = protocol.GetString(start, ref start); if (protoName == "A") { conns[conn_id].UDPRemote = UDPRemote; UDPsocket.BeginReceiveFrom(UDPreadBuff, 0, BUFFER_SIZE, SocketFlags.None, ref TempRemote, UDPReceiveCb, UDPreadBuff); return; } else if (protoName != "U") { UDPsocket.BeginReceiveFrom(UDPreadBuff, 0, BUFFER_SIZE, SocketFlags.None, ref TempRemote, UDPReceiveCb, UDPreadBuff); return; } int DataID = protocol.GetInt(start, ref start); string id = protocol.GetString(start, ref start); float x = protocol.Getfloat(start, ref start); float y = protocol.Getfloat(start, ref start); float z = protocol.Getfloat(start, ref start); ProtocolBytes Recombine = new ProtocolBytes(); Recombine.AddString(protoName); Recombine.AddInt(DataID); Recombine.AddString(id); Recombine.AddFloat(x); Recombine.AddFloat(y); Recombine.AddFloat(z); HandleMsg(conns[conn_id], Recombine); Array.Copy(UDPreadBuff, sizeof(Int32) + msgLength, UDPreadBuff, 0, count); UDPsocket.BeginReceiveFrom(UDPreadBuff, 0, BUFFER_SIZE, SocketFlags.None, ref TempRemote, UDPReceiveCb, UDPreadBuff); }
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(); } }
private void ProcessData(Conn conn) { if (conn.buffCount < sizeof(Int32)) { return; } Array.Copy(conn.readBuff, conn.lenBytes, sizeof(Int32)); conn.msgLength = BitConverter.ToInt32(conn.lenBytes, 0); if (conn.buffCount < conn.msgLength + sizeof(Int32)) { return; } ProtocolBase protocol = proto.Decode(conn.readBuff, sizeof(Int32), conn.msgLength); HandleMsg(conn, protocol); int count = conn.buffCount - conn.msgLength - sizeof(Int32); Array.Copy(conn.readBuff, sizeof(Int32) + conn.msgLength, conn.readBuff, 0, count); conn.buffCount = count; if (conn.buffCount > 0) { ProcessData(conn); } }
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; } //string str = System.Text.Encoding.UTF8.GetString(readBuff, sizeof(Int32), (int)msgLength); //recvstr = str; ProtocolBase protocol = proto.Decode(readBuff, sizeof(Int32), msgLength); HandleMsg(protocol); int count = buffCount - msgLength - sizeof(Int32); Array.Copy(readBuff, 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; } Console.WriteLine("处理消息!!"); ProtocolBase protocol = proto.Decode(readBuff, sizeof(Int32), msgLength); HandleMsg(protocol); //string str = System.Text.Encoding.UTF8.GetString (readBuff, sizeof(Int32), (int)msgLength); //recvStr = str; int count = buffCount - msgLength - sizeof(Int32); Array.Copy(readBuff, msgLength, readBuff, 0, count); buffCount = count; if (buffCount > 0) { ProcessData(); } }
private void PrcessByte(ServerConn SC) { try { if (SC.bufferCount < sizeof(Int32))//接收到消息小于包头长度 { return; } Array.Copy(SC.ReadBuffer, SC.lenbyte, sizeof(Int32)); SC.MsgLen = BitConverter.ToInt32(SC.lenbyte, 0); if (SC.bufferCount < SC.MsgLen + sizeof(Int32)) { return; } ProtocolBase Pb = PB.Decode(SC.ReadBuffer, sizeof(Int32), SC.MsgLen); ProtocolBytes pb = (ProtocolBytes)Pb; object[] vs = new object[] { SC, pb }; object O = vs; Thread Hand = new Thread(new ParameterizedThreadStart(HandMSG)); Hand.Name = DateTime.Now.ToLocalTime().ToString(); Hand.Start(O); //HandMSG(SC, pb); int count = SC.bufferCount - SC.MsgLen - sizeof(Int32); Array.Copy(SC.ReadBuffer, sizeof(Int32) + SC.MsgLen, SC.ReadBuffer, 0, count); SC.bufferCount = count; if (SC.bufferCount > 0) { PrcessByte(SC); } } catch (Exception e) { Console.WriteLine(e.Message + "115"); } }
//消息处理 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() { //黏包分包处理 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("收到消息 " + 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 (BufferCount < sizeof(Int32)) { return; } Array.Copy(BufferRead, lenBytes, sizeof(Int32)); lenMsg = BitConverter.ToInt32(lenBytes, 0); if (BufferCount < lenMsg + sizeof(Int32)) { return; } proto = proto.Decode(BufferRead, 0, BufferCount); #if DEBUG Debug.Log("收到消息:" + proto.Expression); #endif //消息处理 lock (_msgDistri.ProtoList) { _msgDistri.ProtoList.Add(proto); Debug.Log("add protocol:" + proto.Name); } //清除已处理消息 int count = BufferCount - sizeof(Int32) - lenMsg; Array.Copy(BufferRead, sizeof(Int32) + lenMsg, BufferRead, 0, count); BufferCount = count; if (BufferCount > 0) { ProcessData(); } }
/// <summary> /// 数据处理 /// </summary> /// <param name="conn"></param> private void ProcessData(Conn conn) { //小于长度字节 if (conn.buffCount < sizeof(Int32)) { return; } //将包含消息长度的4个字节复制到lenbytes字节数组中 Array.Copy(conn.readBuff, conn.lenBytes, sizeof(Int32)); //将lenbytes转为int整数,得到消息长度 conn.msgLength = BitConverter.ToInt32(conn.lenBytes, 0); if (conn.buffCount < conn.msgLength + sizeof(Int32)) { return; } //处理消息 ProtocolBase protocol = proto.Decode(conn.readBuff, sizeof(Int32), conn.msgLength); HandleMsg(conn, protocol); //清除已处理的消息 int count = conn.buffCount - conn.msgLength - sizeof(Int32); Array.Copy(conn.readBuff, sizeof(Int32) + conn.msgLength + sizeof(Int32), conn.readBuff, 0, count); conn.buffCount = count; if (conn.buffCount > 0) { ProcessData(conn); } // WbeService.instance.WriteLine("ProcessData结束"); }
//缓存区数据处理 private void ProcessData(Conn conn) { //小于长度字节 if (conn.buffCount < sizeof(Int32)) { return; } //消息长度 Array.Copy(conn.readBuff, conn.lenBytes, sizeof(Int32)); //将readBuff前4个字节复制到lenBytes conn.msgLength = BitConverter.ToInt32(conn.lenBytes, 0); //获取接收数据长度 if (conn.buffCount < conn.msgLength + sizeof(Int32)) //如果数据分包了,先不处理 { return; } //处理消息 ProtocolBase protocol = proto.Decode(conn.readBuff, sizeof(Int32), conn.msgLength); HandleMsg(conn, protocol); //清除已经处理的消息 int count = conn.buffCount - conn.msgLength - sizeof(Int32); Array.Copy(conn.readBuff, sizeof(Int32) + conn.msgLength, conn.readBuff, 0, count); conn.buffCount = count; if (conn.buffCount > 0) { ProcessData(conn); } }
private void ProcessData(Conn conn) { //小于长度字节 if (conn.buffCount < sizeof(Int32)) { return; } //消息长度 Array.Copy(conn.readBuff, conn.lenBytes, sizeof(Int32)); conn.msgLength = BitConverter.ToInt32(conn.lenBytes, 0); if (conn.buffCount < conn.msgLength + sizeof(Int32)) { return; } //处理消息, proto未实例化?? ProtocolBase protocol = proto.Decode(conn.readBuff, sizeof(Int32), conn.msgLength); //单线程处理消息??? 如果消息阻塞怎么办?? HandleMsg(conn, protocol); //清除已处理的消息 int count = conn.buffCount - conn.msgLength - sizeof(Int32); Array.Copy(conn.readBuff, sizeof(Int32) + conn.msgLength, conn.readBuff, 0, count); conn.buffCount = count; if (conn.buffCount > 0) { ProcessData(conn); } }
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); 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 void ProcessData(NL_Conn conn) { if (conn.BufferCount < sizeof(Int32)) { return; } Array.Copy(conn.Buffer, conn.LenBytes, sizeof(int)); conn.MsgLength = BitConverter.ToInt32(conn.LenBytes, 0); if (conn.BufferCount < (conn.MsgLength + sizeof(int))) { return; } //处理消息 //string message = System.Text.Encoding.UTF8.GetString(conn.Buffer, sizeof(Int32), conn.MsgLength); //Console.WriteLine("Recieve Client[{0}]'s Message: {1}", conn.GetAddress, message); //SendMessage(message); ProtocolBase tempBase = _protocolBase.Decode(conn.Buffer, sizeof(int), conn.MsgLength); HandleMessage(conn, tempBase); //清楚已处理的消息 conn.BufferCount -= (sizeof(Int32) + conn.MsgLength); Array.Copy(conn.Buffer, sizeof(Int32) + conn.MsgLength, conn.Buffer, 0, conn.BufferCount); if (conn.BufferCount > 0) { ProcessData(conn); } }
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); HandleMsg(protocol); //清除已处理的消息 int count = buffCount - msgLength - sizeof(Int32); Array.Copy(readBuff, msgLength, readBuff, 0, count); buffCount = count; if (buffCount > 0) { ProcessData(); } }
void ProcessData() { if (buffCount < sizeof(Int32)) { return; } Array.Copy(readBuff, lenBuff, sizeof(Int32)); msgLength = BitConverter.ToInt32(lenBuff, 0); if (buffCount < sizeof(Int32) + msgLength) { return; } ProtocolBase protocol = proto.Decode(readBuff, sizeof(Int32), msgLength); lock (msgDist.msgList) { msgDist.msgList.Add(protocol); } int count = buffCount - sizeof(Int32) - msgLength; 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); lock (msgDistribution.msgList) { msgDistribution.msgList.Add(protocol); } //清除已处理的消息 int count = buffCount - sizeof(Int32) - msgLength; Array.Copy(readBuff, sizeof(Int32) + msgLength, readBuff, 0, count); buffCount = count; if (buffCount > 0) { ProcessData(); } }
private void PrcessByte(ConnToClient CTC) { if (CTC.BufferCount < sizeof(Int32))//接收到消息小于包头长度 { return; } Array.Copy(CTC.ReadBuffer, CTC.Lenbyte, sizeof(Int32)); CTC.MsgLen = BitConverter.ToInt32(CTC.Lenbyte, 0); if (CTC.BufferCount < CTC.MsgLen + sizeof(Int32)) { return; } ProtocolBytes Pb = (ProtocolBytes)PB.Decode(CTC.ReadBuffer, sizeof(Int32), CTC.MsgLen); object[] vs = new object[] { CTC, Pb }; object O = vs; Thread HandMSGT = new Thread(new ParameterizedThreadStart(HandMSG)); HandMSGT.Start(O); int count = CTC.BufferCount - CTC.MsgLen - sizeof(Int32); Array.Copy(CTC.ReadBuffer, sizeof(Int32) + CTC.MsgLen, CTC.ReadBuffer, 0, count); CTC.BufferCount = count; if (CTC.BufferCount > 0) { PrcessByte(CTC); } }
private void UDPReceiveCb(IAsyncResult ar) { int count = UDPsocket.EndReceive(ar); Array.Copy(UDPreadBuff, UDPlenBytes, sizeof(Int32)); int msgLength = BitConverter.ToInt32(UDPlenBytes, 0); ProtocolBase protocol = proto.Decode(UDPreadBuff, sizeof(Int32), msgLength); //Debug.Log(protocol.GetName()); lock (msgList) { msgList.Add(protocol); } Array.Copy(UDPreadBuff, sizeof(Int32) + msgLength, UDPreadBuff, 0, count); UDPsocket.BeginReceive(UDPreadBuff, 0, BUFFER_SIZE, SocketFlags.None, UDPReceiveCb, UDPreadBuff); }
//处理粘包、分包 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(); } }
private void HandData(EndPoint Client) { if (recvLenght < sizeof(Int32)) { return; } Array.Copy(recvbytes, msglenBytes, sizeof(Int32)); msglenght = BitConverter.ToInt32(msglenBytes, 0); if (recvLenght < msglenght + sizeof(Int32)) { return; } ProtocolBase proto = MsgProto.Decode(recvbytes, sizeof(Int32), msglenght);// MsgProto.Decode(recvbytes, sizeof(Int32), msglenght); HangMsg(proto, Client); }
private void PrcessByte() { if (buffCount < sizeof(Int32))//接收到消息小于包头长度 { return; } Array.Copy(readbuff, lenBytes, sizeof(Int32)); msglenght = BitConverter.ToInt32(lenBytes, 0); if (buffCount < msglenght + sizeof(Int32)) { return; } ProtocolBytes Pb = (ProtocolBytes)PB.Decode(readbuff, sizeof(Int32), msglenght); HandMSG(Pb); int count = buffCount - msglenght - sizeof(Int32);//计算剩余未处理消息的长度 Array.Copy(readbuff, sizeof(Int32) + msglenght, readbuff, 0, count); buffCount = count; if (buffCount > 0) { PrcessByte(); } }
private void ProcessData(object con) { TCP conn = (TCP)con; if (conn.buffercount < sizeof(Int32)) { return; } // Array.Copy(conn.readbuffer, conn.lenbytes, sizeof(Int32)); conn.msgLenght = BitConverter.ToInt32(conn.lenbytes, 0); if (conn.buffercount < conn.msgLenght + sizeof(Int32)) { return; } //处理消息 ProtocolBase protoco = Proto.Decode(conn.readbuffer, sizeof(Int32), conn.msgLenght); try { HandleMsg(conn, protoco); } catch (Exception e) { Console.WriteLine("HandleMsg " + e.Message); } //发送消息 int count = conn.buffercount - conn.msgLenght - sizeof(Int32); Array.Copy(conn.readbuffer, sizeof(Int32) + conn.msgLenght, conn.readbuffer, 0, count); conn.buffercount = count; if (conn.buffercount > 0) { ProcessData(conn); } }
/// <summary> /// 数据处理 /// </summary> /// <param name="state"></param> public static void OnReceiveData(ClientState state) { ByteArray readBuff = state.readBuff; //消息长度 if (readBuff.length <= 2) { return; } //消息体长度 int readIdx = readBuff.readIdx; byte[] bytes = readBuff.bytes; Int16 bodyLength = (Int16)((bytes[readIdx + 1] << 8) | bytes[readIdx]); if (readBuff.length < bodyLength) { return; } readBuff.readIdx += 2; //解析协议名 int nameCount = 0; string protoName = ProtocolBase.DecodeName(readBuff.bytes, readBuff.readIdx, out nameCount); if (protoName == "") { Console.WriteLine("OnReceiveData MsgBase.DecodeName fail"); Close(state); return; } readBuff.readIdx += nameCount; //解析协议体 int bodyCount = bodyLength - nameCount; if (bodyCount <= 0) { Console.WriteLine("OnReceiveData fail, bodyCount <=0 "); Close(state); return; } ProtocolBase msgBase = ProtocolBase.Decode(protoName, readBuff.bytes, readBuff.readIdx, bodyCount); readBuff.readIdx += bodyCount; readBuff.CheckAndMoveBytes(); //分发消息 MethodInfo mi = typeof(MsgHandler).GetMethod(protoName); object[] o = { state, msgBase }; Console.WriteLine("Receive " + protoName); if (mi != null) { mi.Invoke(null, o); } else { Console.WriteLine("OnReceiveData Invoke fail " + protoName); } //继续读取消息 if (readBuff.length > 2) { OnReceiveData(state); } }