public override void SendBuffer(byte[] data, int offset, int count) { if (!Connected) { return; //throw new ObjectDisposedException("socket"); } int P = NetPacketParser.PREFIX_SIZE; byte[] buffer = new byte[count + P]; Buffer.BlockCopy(BitConverter.GetBytes((UInt16)count), 0, buffer, 0, P); Buffer.BlockCopy(data, offset, buffer, P, count); Queue <byte> aa = new Queue <byte>(); sendQueue.Enqueue(buffer); if (TrySetSendFlag()) { try { TryDequeueAndPostSend(); } catch { ResetSendFlag(); throw; } } }
private void ProcessAccept(SocketAsyncEventArgs acceptEventArgs) { PostAccept(); if (acceptEventArgs.SocketError != SocketError.Success) { HandleBadAccept(acceptEventArgs); return; } var newConn = new TcpConnect(this, acceptEventArgs); acceptEventArgs.AcceptSocket = null; this.acceptEventArgsPool.Push(acceptEventArgs); lock (this) { if (clientSockets.ContainsKey(newConn) == false) { clientSockets.Add(newConn, newConn); } else { Log.Log.Net.Info("怎么可能?clientSockets.ContainsKey(newConn) == false"); } newConnectQueue.Enqueue(newConn); } PostReceive(newConn.ReceiveEventArgs); }
private void ProcessReceive() { var dataToken = this.receiveDataToken; var ioEventArg = this.receiveEventArg; if (ioEventArg.SocketError != SocketError.Success) { //Socket错误 HandleCloseSocket(); return; } if (ioEventArg.BytesTransferred == 0) { //远端主动关闭socket HandleCloseSocket(); return; } #region 消息包解析 int remainingBytesToProcess = ioEventArg.BytesTransferred; bool needPostAnother = true; do { if (dataToken.prefixBytesDone < NetPacketParser.PREFIX_SIZE) { remainingBytesToProcess = NetPacketParser.HandlePrefix(ioEventArg, dataToken, remainingBytesToProcess); if (dataToken.IsPrefixReady && (dataToken.messageLength > 65535 || dataToken.messageLength <= 0)) { //消息头已接收完毕,并且接收到的消息长度大于64K,socket传输的数据已紊乱,关闭掉 Trace.TraceWarning("接收到的消息长度错误:{0}", dataToken.messageLength); needPostAnother = false; HandleCloseSocket(); break; } if (remainingBytesToProcess == 0) { break; } } remainingBytesToProcess = NetPacketParser.HandleMessage(ioEventArg, dataToken, remainingBytesToProcess); if (dataToken.IsMessageReady) { recvQueue.Enqueue(dataToken.messageBytes); if (remainingBytesToProcess != 0) { dataToken.Reset(false); } } } while (remainingBytesToProcess != 0); #endregion if (needPostAnother) { if (dataToken.IsPrefixReady && dataToken.IsMessageReady) { dataToken.Reset(true); } dataToken.bufferSkip = 0; PostReceive(); } }
/// <summary> /// 处理数据接收回调 /// </summary> private void ProcessReceive(SocketAsyncEventArgs ioEventArgs) { AsyncUserToken dataToken = (AsyncUserToken)ioEventArgs.UserToken; if (ioEventArgs.SocketError != SocketError.Success) { //Socket错误 //Trace.TraceError("ProcessReceive:{0}", ioEventArgs.SocketError); HandleCloseSocket(ioEventArgs); return; } if (ioEventArgs.BytesTransferred == 0) { HandleCloseSocket(ioEventArgs); return; } var tcpConn = dataToken.tcpConn; #region 消息包解析 int remainingBytesToProcess = ioEventArgs.BytesTransferred; //bool needPostAnother = true; do { if (dataToken.prefixBytesDone < NetPacketParser.PREFIX_SIZE) { remainingBytesToProcess = NetPacketParser.HandlePrefix(ioEventArgs, dataToken, remainingBytesToProcess); if (dataToken.IsPrefixReady && (dataToken.messageLength > 65535 || dataToken.messageLength <= 0)) { //消息头已接收完毕,并且接收到的消息长度已经超长,socket传输的数据已紊乱,关闭掉 Log.FileLog.WriteLine("{0} Receive Ip {2} message length error:{1}", DateTime.Now.ToString("HH:mm:ss"), dataToken.messageLength, ioEventArgs.RemoteEndPoint); //needPostAnother = false; HandleCloseSocket(ioEventArgs); return; } if (remainingBytesToProcess == 0) { break; } } remainingBytesToProcess = NetPacketParser.HandleMessage(ioEventArgs, dataToken, remainingBytesToProcess); if (dataToken.IsMessageReady) { RecvEventArg evtArg = new RecvEventArg(dataToken.tcpConn, dataToken.messageBytes); if (TestLag) { evtArg.ReceiveTime = System.DateTime.Now.Ticks; //IDllImportAPI.HighPrecision_GetTickCount(); } recvQueue.Enqueue(evtArg); if (remainingBytesToProcess != 0) { dataToken.Reset(false); } } else { //if (logger.IsDebugEnabled) logger.Debug("不完整封包 长度[{0}],总传输[{1}],已接收[{2}]", dataToken.messageLength, ioEventArgs.BytesTransferred, dataToken.messageBytesDone); } } while (remainingBytesToProcess != 0); #endregion //if (needPostAnother) { //继续处理下个请求包 if (dataToken.IsPrefixReady && dataToken.IsMessageReady) { dataToken.Reset(true); } dataToken.bufferSkip = 0; PostReceive(ioEventArgs); } }
internal void Enqueue(byte[] data) { mSendQueue.Enqueue(data); }