Beispiel #1
0
        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();
            }
        }
Beispiel #2
0
        /// <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);
            }
        }