Пример #1
0
        private void ProcessSend(SocketAsyncEventArgs ioEventArgs)
        {
            DataToken dataToken = (DataToken)ioEventArgs.UserToken;

            if (ioEventArgs.SocketError == SocketError.Success)
            {
                dataToken.messageBytesDone += ioEventArgs.BytesTransferred;
                if (dataToken.messageBytesDone != dataToken.messageLength)
                {
                    PostSend(ioEventArgs);
                }
                else
                {
                    dataToken.Reset(true);
                    try
                    {
                        TryDequeueAndPostSend(dataToken.Socket, ioEventArgs);
                    }
                    catch
                    {
                        dataToken.Socket.ResetSendFlag();
                        throw;
                    }
                }
            }
            else
            {
                dataToken.Socket.ResetSendFlag();
                HandleCloseSocket(ioEventArgs);
            }
        }
Пример #2
0
        /// <summary>
        /// 处理数据接收回调
        /// </summary>
        /// <param name="ioEventArgs"></param>
        private void ProcessReceive(SocketAsyncEventArgs ioEventArgs)
        {
            DataToken dataToken = (DataToken)ioEventArgs.UserToken;

            if (ioEventArgs.SocketError != SocketError.Success)
            {
                //Socket错误
                if (logger.IsDebugEnabled)
                {
                    logger.Debug("ProcessReceive:{0}", ioEventArgs.SocketError);
                }
                HandleCloseSocket(ioEventArgs);
                return;
            }

            if (ioEventArgs.BytesTransferred == 0)
            {
                //对方主动关闭socket
                if (logger.IsDebugEnabled)
                {
                    logger.Debug("对方关闭Socket");
                }
                HandleCloseSocket(ioEventArgs);
                return;
            }

            var exSocket = dataToken.Socket;

            #region 数据解析
            List <byte[]> msgs = new List <byte[]>();
            int           remainingBytesToProcess = ioEventArgs.BytesTransferred;
            bool          needPostAnother         = true;
            do
            {
                if (dataToken.prefixBytesDone < 4)
                {
                    remainingBytesToProcess = prefixHandler.HandlePrefix(ioEventArgs, dataToken, remainingBytesToProcess);
                    if (dataToken.prefixBytesDone == 4 && (dataToken.messageLength > 10 * 1024 * 1024 || dataToken.messageLength <= 0))
                    {
                        //消息头已接收完毕,并且接收到的消息长度大于10M,socket传输的数据已紊乱,关闭掉
                        logger.Warn("接收到的消息长度错误:{0}", dataToken.messageLength);
                        needPostAnother = false;
                        HandleCloseSocket(ioEventArgs);
                        break;
                    }
                    if (logger.IsDebugEnabled)
                    {
                        logger.Debug("处理消息头,消息长度[{0}],剩余字节[{1}]", dataToken.messageLength, remainingBytesToProcess);
                    }
                    if (remainingBytesToProcess == 0)
                    {
                        break;
                    }
                }

                remainingBytesToProcess = messageHandler.HandleMessage(ioEventArgs, dataToken, remainingBytesToProcess);

                if (dataToken.IsMessageReady)
                {
                    if (logger.IsDebugEnabled)
                    {
                        logger.Debug("完整封包 长度[{0}],总传输[{1}],剩余[{2}]", dataToken.messageLength, ioEventArgs.BytesTransferred, remainingBytesToProcess);
                    }
                    msgs.Add(dataToken.byteArrayForMessage);
                    if (remainingBytesToProcess != 0)
                    {
                        if (logger.IsDebugEnabled)
                        {
                            logger.Debug("重置缓冲区,buffskip指针[{0}]。", dataToken.bufferSkip);
                        }
                        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.prefixBytesDone == 4 && dataToken.IsMessageReady)
                {
                    dataToken.Reset(true);
                }
                dataToken.bufferSkip = 0;
                PostReceive(ioEventArgs);
            }

            foreach (var m in msgs)
            {
                try
                {
                    OnDataReceived(new ConnectionEventArgs {
                        Socket = exSocket, Data = m
                    });
                }
                catch (Exception ex)
                {
                    logger.Error("OnDataReceived", ex);
                }
            }
        }