/// <summary> /// 打包并整合所有要发送的数据,从一个未打包队列里提取然后完成打包,之后直接传出 /// </summary> /// <param name="packeter"></param> internal byte[] PackSendData(IPacket packeter) { byte[][] datas = _sendQueue.GetData();//这里的数据应该已经是预打包数据 if (datas != null) { for (int i = 0; i < datas.Length; i++) { datas[i] = packeter.CompletePack(datas[i]);//完成数据打包 } byte[] SeriesData = BytesQueue.BytesArrayToBytes(datas); return(SeriesData); } return(null); }
/// <summary> /// 构造函数,应该是内部的。由内部机制创建 /// </summary> /// <param name="socket">用户对应的 Socket</param> /// <param name="sendArgs">创建出来的发送用的SocketAsyncEventArgs(将来可以放到池里)</param> /// <param name="receiveArgs">创建出来的接收用的SocketAsyncEventArgs(将来可以放到池里)</param> /// <param name="receiveBufferSize">数据接收缓存大小</param> internal Token(Socket socket, SocketAsyncEventArgs sendArgs, SocketAsyncEventArgs receiveArgs, int receiveBufferSize) { this._tokenScket = socket; this.SendArgs = sendArgs; this.ReceiveArgs = receiveArgs; ReceiveBuffer = new byte[receiveBufferSize]; _sendQueue = new BytesQueue(int.MaxValue, MAX_BYTES_SIZE, 256); _receiveQueue = new BytesQueue(int.MaxValue, MAX_BYTES_SIZE, 256); _reserveQueuePacked = new BytesQueue(int.MaxValue, MAX_BYTES_SIZE, 256); LastMsgReceTickTime = DateTime.Now.Ticks; LastMsgSendTickTime = DateTime.Now.Ticks; //设置一下timeout socket.SendTimeout = 8 * 1000;//最长发送8秒超时 socket.ReceiveTimeout = 0; userObj = new UserObj(); }