Example #1
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)
                {
                    TraceLog.WriteError("OnDataReceived error:{0}", ex);
                }
            }
        }
Example #2
0
        private void ProcessReceive()
        {
            var dataToken  = this.receiveDataToken;
            var ioEventArg = this.receiveEventArg;

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

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

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

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

                if (dataToken.IsMessageReady)
                {
                    //if (logger.IsDebugEnabled) logger.Debug("完整封包 长度[{0}],总传输[{1}],剩余[{2}]", dataToken.messageLength, ioEventArg.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, ioEventArg.BytesTransferred, dataToken.messageBytesDone);
                }
            } while (remainingBytesToProcess != 0);
            #endregion

            //modify reason:数据包接收事件触发乱序
            // 触发收到消息事件
            foreach (var m in msgs)
            {
                try
                {
                    OnDataReceived(new SocketEventArgs {
                        Data = m
                    });
                }
                catch (Exception ex)
                {
                    logger.Error("OnDataReceived", ex);
                }
            }
            if (needPostAnother)
            {
                if (dataToken.prefixBytesDone == 4 && dataToken.IsMessageReady)
                {
                    dataToken.Reset(true);
                }
                dataToken.bufferSkip = 0;
                PostReceive();
            }
        }
Example #3
0
        /// <summary>
        /// 接收处理
        /// </summary>
        /// <param name="e"></param>
        private void ProcessReceive(SocketAsyncEventArgs e)
        {
            DataToken session = e.UserToken as DataToken;

            if (e.BytesTransferred > 0)
            {
                if (e.SocketError == SocketError.Success)
                {
                    //是否有已接收但未处理的数据
                    int remainingBytesToProcess = e.BytesTransferred;
                    do
                    {
                        if (session.prefixBytesDone < 4)
                        {
                            remainingBytesToProcess = _prefixHandler.HandlePrefix(e, session, remainingBytesToProcess);
                            if (remainingBytesToProcess == 0)
                            {
                                session.bufferSkip = 0;
                                StartReceive(e);
                                return;
                            }
                        }

                        remainingBytesToProcess = _messageHandler.HandleMessage(e, session, remainingBytesToProcess);

                        if (session.IsMessageReady)
                        {
                            // 触发收到消息事件
                            if (ReceiveCompleted != null)
                            {
                                byte[] data = session.byteArrayForMessage;
                                ReceiveCompleted.BeginInvoke(new SocketProcessEventArgs()
                                {
                                    Socket = session.Socket, Data = data
                                }, null, null);
                            }
                            if (remainingBytesToProcess != 0)
                            {
                                session.Reset(false);
                            }
                        }
                    }while (remainingBytesToProcess != 0);

                    if (session.prefixBytesDone == 4 && session.IsMessageReady)
                    {
                        session.Reset(true);
                    }
                    session.bufferSkip = 0;
                    StartReceive(e);
                }
                else
                {
                    session.Reset(true);
                    ProcessSocketError(e);
                }
            }
            else
            {
                session.Reset(true);
                CloseClientSocket(e);
            }
        }