private SendBufferPacket m_sendBufferPacket; //发送记录包 /// <summary> /// 初始化发送缓存 /// </summary> /// <param name="bufferSize">int</param> public AsyncSendBufferManager(int bufferSize) { m_dynamicBufferManager = new DynamicBufferManager(bufferSize); m_sendBufferList = new List <SendBufferPacket>(); m_sendBufferPacket.Offset = 0; m_sendBufferPacket.Count = 0; }
/// <summary> /// 协议组装/继续接收 /// </summary> /// <param name="buffer">byte[]</param> /// <param name="offset"></param> /// <param name="count"></param> /// <returns></returns> public virtual bool ProcessReceive(byte[] buffer, int offset, int count) //接收异步事件返回的数据,用于对数据进行缓存和分包 { m_activeDT = DateTime.UtcNow; DynamicBufferManager receiveBuffer = m_socketUserToken.ReceiveBuffer; receiveBuffer.WriteBuffer(buffer, offset, count);//把刚刚接收的信息加入到缓存中 bool result = true; //按照长度分包 int packetLength = BitConverter.ToInt32(receiveBuffer.Buffer, 0); //获取包长度 if ((packetLength > 10 * 1024 * 1024) | (receiveBuffer.DataCount > 10 * 1024 * 1024)) //最大Buffer异常保护 { return(false); } if (receiveBuffer.DataCount >= packetLength) //packetLength收到的数据达到包长度 { //命令 组装-如果组装失败则继续接收:如果组装成功-则删除 result = ProcessPacket(receiveBuffer.Buffer, offset, packetLength); if (result) { receiveBuffer.Clear(packetLength);//清理已经处理的协议 } else { return(result); } } return(true); }
/// <summary> /// 初始化客户端 /// </summary> /// <param name="asyncReceiveBufferSize">接收和发送缓存大小</param> public SocketUserToken(int asyncReceiveBufferSize) { m_connectSocket = null; m_receiveEventArgs = new SocketAsyncEventArgs(); SendUserTokenAll = new List <SocketUserToken>(); //异步回调函数时候的值(this) m_receiveEventArgs.UserToken = this; m_asyncReceiveBuffer = new byte[asyncReceiveBufferSize]; m_receiveEventArgs.SetBuffer(m_asyncReceiveBuffer, 0, m_asyncReceiveBuffer.Length); //异步回调函数时候的值(this) m_sendEventArgs = new SocketAsyncEventArgs(); m_sendEventArgs.UserToken = this; //初始化缓存大小 m_receiveBuffer = new DynamicBufferManager(asyncReceiveBufferSize); m_sendBuffer = new AsyncSendBufferManager(asyncReceiveBufferSize); }
/// <summary> /// 协议组装/继续接收 /// </summary> /// <param name="buffer">byte[]</param> /// <param name="offset"></param> /// <param name="count"></param> /// <returns></returns> public virtual bool ProcessReceive(byte[] buffer, int offset, int count) //接收异步事件返回的数据,用于对数据进行缓存和分包 { //m_activeDT = DateTime.UtcNow; //DynamicBufferManager receiveBuffer = m_socketUserToken.ReceiveBuffer; //receiveBuffer.WriteBuffer(buffer, offset, count);//把刚刚接收的信息加入到缓存中 //bool result = true; ////按照长度分包 //int packetLength = BitConverter.ToInt32(receiveBuffer.Buffer, 0); //获取包长度 //if ((packetLength > 10 * 1024 * 1024) | (receiveBuffer.DataCount > 10 * 1024 * 1024)) //最大Buffer异常保护 // return false; //if (receiveBuffer.DataCount >= packetLength) //packetLength收到的数据达到包长度 //{ // //命令 组装-如果组装失败则继续接收:如果组装成功-则删除 // result = ProcessPacket(receiveBuffer.Buffer, offset, packetLength); // if (result) // receiveBuffer.Clear(packetLength);//清理已经处理的协议 // else // return result; //} //return true; ///////////******下面的程序考虑粘包的情况。******////// m_activeDT = DateTime.UtcNow; DynamicBufferManager receiveBuffer = m_socketUserToken.ReceiveBuffer; receiveBuffer.WriteBuffer(buffer, offset, count); bool result = true; while (true) { if (receiveBuffer.Buffer.Length > 11) { // int packetLength = (int)((receiveBuffer.Buffer[1] << 8) | receiveBuffer.Buffer[2]); int packetLength = getLength(receiveBuffer.Buffer[11]); // Console.WriteLine(packetLength+"-------------------"); //if ((packetLength > 10 * 1024 * 1024) | (receiveBuffer.DataCount > 10 * 1024 * 1024)) if ((packetLength > 16064)) { DelegateState.ServerStateInfo("(packetLength > 16064 packetLength= " + packetLength.ToString()); return(false); } if ((receiveBuffer.DataCount > 10 * 1024 * 1024)) { DelegateState.ServerStateInfo("(receiveBuffer.DataCount> 10 * 1024* 1024 receiveBuffer.DataCount > 10 * 1024* 1024 "); return(false); } if (packetLength == 0) { return(false); } if (receiveBuffer.DataCount >= packetLength) { // while (1) { result = ProcessPacket(receiveBuffer.Buffer, offset, packetLength); if (result) { receiveBuffer.Clear(packetLength); if (receiveBuffer.DataCount > 0) { continue; } else { break; } } else { return(result); } } } else { return(true); } } else { return(true); } } return(true); }