/// <summary> /// 对从网络缓冲区拿到的数据得处理函数 /// </summary> /// <param name="connect">连接类的实例</param> private void PossingData(Connect connect) { if (connect.buffCount < sizeof(Int32)) { connect.Connect_Socket.BeginReceive(connect.Buffers, connect.buffCount, connect.BuffRemain(), SocketFlags.None, AsyncReceiveCb, connect); } else { Int32 MessageLen = BitConverter.ToInt32(connect.Buffers, 0);//取包头获取单个数据包长度 if (connect.buffCount < sizeof(Int32) + MessageLen && connect.buffCount < Connect.BUFFERS_SIZE) { connect.Connect_Socket.BeginReceive(connect.Buffers, connect.buffCount, connect.BuffRemain(), SocketFlags.None, AsyncReceiveCb, connect); } else if (connect.buffCount < sizeof(Int32) + MessageLen && Connect.BUFFERS_SIZE < sizeof(Int32) + MessageLen) { try { MemoryStream stream = new MemoryStream(); stream.Write(connect.Buffers, sizeof(Int32), connect.buffCount - sizeof(Int32)); connect.MessageLenght = MessageLen; connect.BufferStream = stream; connect.RestBuffCount(); connect.Connect_Socket.BeginReceive(connect.Buffers, connect.buffCount, connect.StreamBuffLen(), SocketFlags.None, SubReceiveCb, connect); } catch (Exception e) { FDebug.Log(e.Message + " :分包接收"); } } else { try { byte[] data = new byte[MessageLen]; Array.Copy(connect.Buffers, sizeof(Int32), data, 0, MessageLen); DataTransfer(data, connect); connect.buffCount -= (MessageLen + sizeof(Int32)); if (connect.buffCount > 0) { Array.Copy(connect.Buffers, sizeof(Int32) + MessageLen, connect.Buffers, 0, connect.buffCount); PossingData(connect); } else { connect.Connect_Socket.BeginReceive(connect.Buffers, connect.buffCount, connect.BuffRemain(), SocketFlags.None, AsyncReceiveCb, connect); } } catch (Exception e) { FDebug.Log(e.Message + " pd"); } } } }
/// <summary> /// 增量接收 /// </summary> /// <param name="ar"></param> private void SubReceiveCb(IAsyncResult ar) { Connect connect = (Connect)ar.AsyncState; try { int count = connect.Connect_Socket.EndReceive(ar); connect.buffCount = count; if (count < 0) { connect.Close(); } else if (count == 0) { connect.MStreamDispose(); connect.Connect_Socket.BeginReceive(connect.Buffers, connect.buffCount, connect.BuffRemain(), SocketFlags.None, AsyncReceiveCb, connect); } else { connect.BufferStream.Write(connect.Buffers, 0, connect.buffCount); if (connect.MessageLenght == connect.BufferStream.Length) { var bs = connect.BufferStream.ToArray(); DataTransfer(bs, connect); connect.MStreamDispose(); connect.RestBuffCount(); connect.Connect_Socket.BeginReceive(connect.Buffers, connect.buffCount, connect.BuffRemain(), SocketFlags.None, AsyncReceiveCb, connect); } else if (connect.MessageLenght > connect.BufferStream.Length) { connect.RestBuffCount(); connect.Connect_Socket.BeginReceive(connect.Buffers, connect.buffCount, connect.StreamBuffLen(), SocketFlags.None, SubReceiveCb, connect); } else { FDebug.Log("MS < s.lenght"); } } } catch (Exception e) { FDebug.Log(e.Message + "SubReceiveCb err"); } }
public void AsyncAccept(IAsyncResult Ar) { try { Socket socket = Server_Socket.EndAccept(Ar); int Index = Get_Connect_Index(); if (Index < 0) { socket.Close(); FDebug.Log("警告:连接已满"); } else { Connect connect = connects[Index]; connect.Init(socket); FDebug.Log("与{0}建立连接", connect.GetAddress()); connect.Connect_Socket.BeginReceive(connect.Buffers, connect.buffCount, connect.BuffRemain(), SocketFlags.None, AsyncReceiveCb, connect); } Server_Socket.BeginAccept(AsyncAccept, null); } catch (Exception e) { FDebug.Log("AsyncAccept失败 :" + e.Message); } }