Esempio n. 1
0
        private void Bind(Socket acceptSocket)
        {
            var exSocket = new TNSocket(acceptSocket);

            exSocket.LastAccessTime = DateTime.Now;
            var buffer = new byte[this.clientSettings.BufferSize * 2];

            this.sendEventArg = new SocketAsyncEventArgs();
            this.sendEventArg.SetBuffer(buffer, 0, this.clientSettings.BufferSize);
            var sendDataToken = new DataToken()
            {
                Socket = exSocket
            };

            sendDataToken.bufferOffset     = this.sendEventArg.Offset;
            this.sendEventArg.UserToken    = sendDataToken;
            this.sendEventArg.AcceptSocket = acceptSocket;
            this.sendEventArg.Completed   += new EventHandler <SocketAsyncEventArgs>(IO_Completed);

            this.receiveEventArg = new SocketAsyncEventArgs();
            this.receiveEventArg.SetBuffer(buffer, this.clientSettings.BufferSize, this.clientSettings.BufferSize);
            var receiveDataToken = new DataToken {
                Socket = exSocket, SyncSegments = new Queue <ArraySegment <byte> >()
            };

            receiveDataToken.bufferOffset     = this.receiveEventArg.Offset;
            this.receiveEventArg.UserToken    = receiveDataToken;
            this.receiveEventArg.AcceptSocket = acceptSocket;
            this.receiveEventArg.Completed   += new EventHandler <SocketAsyncEventArgs>(IO_Completed);
            if (IsSyncReceived)
            {
                DataReceived += OnReceived;
            }
            requestHandler.Bind(this);
        }
Esempio n. 2
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.ResultCallback(ResultCode.Success);
                    dataToken.Reset(true);
                    try
                    {
                        TryDequeueAndPostSend(dataToken.Socket, ioEventArgs);
                    }
                    catch
                    {
                        ResetSendFlag();
                        throw;
                    }
                }
            }
            else
            {
                dataToken.ResultCallback(ResultCode.Close);
                ResetSendFlag();
                Closing(ioEventArgs);
            }
        }
Esempio n. 3
0
        private void TryDequeueAndPostSend(TNSocket socket, SocketAsyncEventArgs ioEventArgs)
        {
            SocketAsyncResult result;

            if (socket.TryDequeueOrReset(out result))
            {
                DataToken dataToken = (DataToken)ioEventArgs.UserToken;
                dataToken.Socket              = socket;
                dataToken.AsyncResult         = result;
                dataToken.byteArrayForMessage = result.Data;
                dataToken.messageLength       = result.Data.Length;
                try
                {
                    PostSend(ioEventArgs);
                }
                catch (Exception ex)
                {
                    dataToken.ResultCallback(ResultCode.Error, ex);
                    ResetSendFlag();
                }
            }
            else
            {
                //ResetSendFlag();
            }
        }
Esempio n. 4
0
        private void IO_Completed(object sender, SocketAsyncEventArgs ioEventArgs)
        {
            try
            {
                DataToken ioDataToken = (DataToken)ioEventArgs.UserToken;

                switch (ioEventArgs.LastOperation)
                {
                case SocketAsyncOperation.Receive:
                    ProcessReceive(ioEventArgs);
                    break;

                case SocketAsyncOperation.Send:
                    ProcessSend(ioEventArgs);
                    break;

                default:
                    throw new ArgumentException("The last operation completed on the socket was not a receive or send");
                }
            }
            catch (ObjectDisposedException)
            {
                ReleaseIOEventArgs(ioEventArgs);
            }
            catch (Exception ex)
            {
                TraceLog.WriteError("IO_Completed of client error:{0}", ex);
            }
        }
Esempio n. 5
0
        private int HandlePrefix(byte[] buffer, int offset, DataToken dataToken, int remainingBytesToProcess)
        {
            if (remainingBytesToProcess >= 4 - dataToken.prefixBytesDone)
            {
                for (int i = 0; i < 4 - dataToken.prefixBytesDone; i++)
                {
                    dataToken.byteArrayForPrefix[dataToken.prefixBytesDone + i] = buffer[offset + i];
                }
                remainingBytesToProcess   = remainingBytesToProcess - 4 + dataToken.prefixBytesDone;
                dataToken.bufferSkip     += 4 - dataToken.prefixBytesDone;
                dataToken.prefixBytesDone = 4;
                dataToken.messageLength   = BitConverter.ToInt32(dataToken.byteArrayForPrefix, 0);
            }
            else
            {
                for (int i = 0; i < remainingBytesToProcess; i++)
                {
                    dataToken.byteArrayForPrefix[dataToken.prefixBytesDone + i] = buffer[offset + i];
                }
                dataToken.prefixBytesDone += remainingBytesToProcess;
                remainingBytesToProcess    = 0;
            }

            return(remainingBytesToProcess);
        }
Esempio n. 6
0
        private void IO_Completed(object sender, SocketAsyncEventArgs ioEventArgs)
        {
            DataToken ioDataToken = (DataToken)ioEventArgs.UserToken;

            try
            {
                ioDataToken.Socket.LastAccessTime = DateTime.Now;
                switch (ioEventArgs.LastOperation)
                {
                case SocketAsyncOperation.Receive:
                    ProcessReceive(ioEventArgs);
                    break;

                case SocketAsyncOperation.Send:
                    ProcessSend(ioEventArgs);
                    break;

                default:
                    throw new ArgumentException("The last operation completed on the socket was not a receive or send");
                }
            }
            catch (ObjectDisposedException)
            {
                //modify disposed error ignore log
                //logger.Error(string.Format("IO_Completed error:{0}", error));
                ReleaseIOEventArgs(ioEventArgs);
            }
            catch (Exception ex)
            {
                TraceLog.WriteError("IP {0} IO_Completed unkown error:{1}", (ioDataToken != null && ioDataToken.Socket != null ? ioDataToken.Socket.RemoteEndPoint.ToNotNullString() : ""), ex);
            }
        }
Esempio n. 7
0
        private void PostSend(SocketAsyncEventArgs ioEventArgs)
        {
            DataToken dataToken = (DataToken)ioEventArgs.UserToken;

            if (dataToken.messageLength - dataToken.messageBytesDone <= this.clientSettings.BufferSize)
            {
                ioEventArgs.SetBuffer(dataToken.bufferOffset, dataToken.messageLength - dataToken.messageBytesDone);
                Buffer.BlockCopy(dataToken.byteArrayForMessage, dataToken.messageBytesDone, ioEventArgs.Buffer, dataToken.bufferOffset, dataToken.messageLength - dataToken.messageBytesDone);
            }
            else
            {
                this.sendEventArg.SetBuffer(dataToken.bufferOffset, this.clientSettings.BufferSize);
                Buffer.BlockCopy(dataToken.byteArrayForMessage, dataToken.messageBytesDone, ioEventArgs.Buffer, dataToken.bufferOffset, this.clientSettings.BufferSize);
            }

            bool willRaiseEvent = this.socketClient.SendAsync(this.sendEventArg);

            if (!willRaiseEvent)
            {
                ProcessSend(ioEventArgs);
            }
        }
Esempio n. 8
0
        private void Init()
        {
            this.bufferManager.InitBuffer();

            for (int i = 0; i < this.socketSettings.MaxAcceptOps; i++)
            {
                this.acceptEventArgsPool.Push(CreateAcceptEventArgs());
            }

            SocketAsyncEventArgs ioEventArgs;

            for (int i = 0; i < this.socketSettings.NumOfSaeaForRecSend; i++)
            {
                ioEventArgs = new SocketAsyncEventArgs();
                this.bufferManager.SetBuffer(ioEventArgs);
                ioEventArgs.Completed += new EventHandler <SocketAsyncEventArgs>(IO_Completed);
                DataToken dataToken = new DataToken();
                dataToken.bufferOffset = ioEventArgs.Offset;
                ioEventArgs.UserToken  = dataToken;
                this.ioEventArgsPool.Push(ioEventArgs);
            }
            _summaryTimer = new Timer(OnSummaryTrace, null, 600, 60000);
        }
Esempio n. 9
0
        private void TryDequeueAndPostSend(TNSocket socket, SocketAsyncEventArgs ioEventArgs)
        {
            bool isOwner = ioEventArgs == null;
            SocketAsyncResult result;

            if (socket.TryDequeueOrReset(out result))
            {
                if (ioEventArgs == null)
                {
                    ioEventArgs = ioEventArgsPool.Pop();
                    ioEventArgs.AcceptSocket = socket.WorkSocket;
                }
                DataToken dataToken = (DataToken)ioEventArgs.UserToken;
                dataToken.Socket              = socket;
                dataToken.AsyncResult         = result;
                dataToken.byteArrayForMessage = result.Data;
                dataToken.messageLength       = result.Data.Length;
                try
                {
                    PostSend(ioEventArgs);
                }
                catch (Exception ex)
                {
                    dataToken.ResultCallback(ResultCode.Error, ex);
                    if (isOwner)
                    {
                        ReleaseIOEventArgs(ioEventArgs);
                    }
                    socket.ResetSendFlag();
                }
            }
            else
            {
                ReleaseIOEventArgs(ioEventArgs);
                //socket.ResetSendFlag();
            }
        }
Esempio n. 10
0
        private int HandleMessage(byte[] buffer, int offset, DataToken dataToken, int remainingBytesToProcess)
        {
            if (dataToken.messageBytesDone == 0)
            {
                dataToken.byteArrayForMessage = new byte[dataToken.messageLength];
            }
            var nonCopiedBytes = 0;

            if (remainingBytesToProcess + dataToken.messageBytesDone >= dataToken.messageLength)
            {
                var copyedBytes = dataToken.RemainByte;
                nonCopiedBytes = remainingBytesToProcess - copyedBytes;
                Buffer.BlockCopy(buffer, offset, dataToken.byteArrayForMessage, dataToken.messageBytesDone, copyedBytes);
                dataToken.messageBytesDone = dataToken.messageLength;
                dataToken.bufferSkip      += copyedBytes;
            }
            else
            {
                Buffer.BlockCopy(buffer, offset, dataToken.byteArrayForMessage, dataToken.messageBytesDone, remainingBytesToProcess);
                dataToken.messageBytesDone += remainingBytesToProcess;
            }

            return(nonCopiedBytes);
        }
Esempio n. 11
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="ioEventArgs"></param>
 /// <param name="opCode"></param>
 /// <param name="reason"></param>
 /// <param name="dataToken"></param>
 protected void Dispose(SocketAsyncEventArgs ioEventArgs, sbyte opCode, string reason, DataToken dataToken)
 {
     ReleaseIOEventArgs(ioEventArgs);
     Dispose(dataToken.Socket, opCode, reason);
 }
Esempio n. 12
0
        private void ProcessReceive(SocketAsyncEventArgs ioEventArgs)
        {
            DataToken dataToken = (DataToken)ioEventArgs.UserToken;

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

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

            List <DataMessage> messages;
            bool hasHandshaked;
            bool needPostAnother = requestHandler.TryReceiveMessage(ioEventArgs, out messages, out hasHandshaked);

            if (hasHandshaked)
            {
                DoOpened(new SocketEventArgs()
                {
                    Socket = dataToken.Socket
                });
            }
            // 触发收到消息事件
            if (messages != null)
            {
                foreach (var message in messages)
                {
                    try
                    {
                        if (message.OpCode == OpCode.Close)
                        {
                            var statusCode = requestHandler.MessageProcessor != null
                                      ? requestHandler.MessageProcessor.GetCloseStatus(message.Data)
                                      : OpCode.Empty;

                            if (statusCode != OpCode.Empty)
                            {
                                OnClosedStatus(statusCode);
                            }
                            Closing(ioEventArgs, OpCode.Empty);
                            needPostAnother = false;
                            break;
                        }
                        OnDataReceived(new SocketEventArgs {
                            Source = message
                        });
                    }
                    catch (Exception ex)
                    {
                        TraceLog.WriteError("OnDataReceived of client error:{0}", ex);
                    }
                }
            }
            if (needPostAnother)
            {
                PostReceive(ioEventArgs);
            }
        }
Esempio n. 13
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="dataToken"></param>
        /// <param name="buffer"></param>
        /// <param name="messageList"></param>
        /// <returns></returns>
        public override bool TryReadMeaage(DataToken dataToken, byte[] buffer, out List <DataMessage> messageList)
        {
            messageList = new List <DataMessage>();

            int  remainingBytesToProcess = buffer.Length;
            bool needPostAnother         = true;

            do
            {
                if (dataToken.prefixBytesDone < 4)
                {
                    remainingBytesToProcess = HandlePrefix(buffer, dataToken.bufferSkip, dataToken, remainingBytesToProcess);
                    if (dataToken.prefixBytesDone == 4 && (dataToken.messageLength > 10 * 1024 * 1024 || dataToken.messageLength <= 0))
                    {
                        byte[] bufferBytes = dataToken.byteArrayForMessage == null?
                                             BufferUtils.MergeBytes(dataToken.byteArrayForPrefix, BufferUtils.GetBytes(buffer, dataToken.bufferSkip, buffer.Length - dataToken.bufferSkip)) :
                                                 BufferUtils.MergeBytes(dataToken.byteArrayForPrefix, dataToken.byteArrayForMessage, buffer);

                        string data = Encoding.UTF8.GetString(bufferBytes);
                        //消息头已接收完毕,并且接收到的消息长度大于10M,socket传输的数据已紊乱,关闭掉
                        TraceLog.Write("The host[{0}] request parser head byte error, byte len={1}\r\ndata:{2}",
                                       dataToken.Socket.RemoteEndPoint.ToNotNullString(),
                                       dataToken.messageLength,
                                       data);
                        needPostAnother = false;
                        break;
                    }
                    //if (logger.IsDebugEnabled) logger.Debug("处理消息头,消息长度[{0}],剩余字节[{1}]", dataToken.messageLength, remainingBytesToProcess);
                    if (remainingBytesToProcess == 0)
                    {
                        break;
                    }
                }

                remainingBytesToProcess = HandleMessage(buffer, dataToken.bufferSkip, dataToken, remainingBytesToProcess);

                if (dataToken.IsMessageReady)
                {
                    //if (logger.IsDebugEnabled) logger.Debug("完整封包 长度[{0}],总传输[{1}],剩余[{2}]", dataToken.messageLength, ioEventArgs.BytesTransferred, remainingBytesToProcess);
                    messageList.Add(new DataMessage()
                    {
                        Data = dataToken.byteArrayForMessage, OpCode = OpCode.Binary
                    });
                    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);


            if (needPostAnother)
            {
                //继续处理下个请求包
                if (dataToken.prefixBytesDone == 4 && dataToken.IsMessageReady)
                {
                    dataToken.Reset(true);
                }
                dataToken.bufferSkip = 0;
            }
            return(needPostAnother);
        }
Esempio n. 14
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="saea"></param>
 /// <param name="dataToken"></param>
 /// <param name="remainingBytesToProcess"></param>
 /// <returns></returns>
 public int HandleMessage(SocketAsyncEventArgs saea, DataToken dataToken, int remainingBytesToProcess)
 {
     return(HandleMessage(saea.Buffer, dataToken.DataOffset, dataToken, remainingBytesToProcess));
 }
Esempio n. 15
0
        /// <summary>
        /// 处理数据接收回调
        /// </summary>
        /// <param name="ioEventArgs"></param>
        private void ProcessReceive(SocketAsyncEventArgs ioEventArgs)
        {
            DataToken dataToken = (DataToken)ioEventArgs.UserToken;

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

            if (ioEventArgs.SocketError != SocketError.Success)
            {//Socket错误
                TraceLog.Write("ProcessReceive IP {0} SocketError:{1}, bytes len:{2}",
                               (dataToken != null ? dataToken.Socket.RemoteEndPoint.ToNotNullString() : ""),
                               ioEventArgs.SocketError.ToString(),
                               ioEventArgs.BytesTransferred);
                Closing(ioEventArgs);
                return;
            }
            TNSocket           exSocket = dataToken == null ? null : dataToken.Socket;
            List <DataMessage> messages;
            bool hasHandshaked;
            bool needPostAnother = requestHandler.TryReceiveMessage(ioEventArgs, out messages, out hasHandshaked);

            if (hasHandshaked)
            {
                OnHandshaked(new ConnectionEventArgs {
                    Socket = exSocket
                });
            }

            //modify reason:数据包接收事件触发乱序
            if (messages != null)
            {
                foreach (var message in messages)
                {
                    try
                    {
                        switch (message.OpCode)
                        {
                        case OpCode.Close:
                            var statusCode = requestHandler.MessageProcessor != null
                                    ? requestHandler.MessageProcessor.GetCloseStatus(message.Data)
                                    : OpCode.Empty;

                            if (statusCode != OpCode.Empty)
                            {
                                DoClosedStatus(exSocket, statusCode);
                            }
                            Closing(ioEventArgs, OpCode.Empty);
                            needPostAnother = false;
                            break;

                        case OpCode.Ping:
                            DoPing(new ConnectionEventArgs {
                                Socket = exSocket, Message = message
                            });
                            break;

                        case OpCode.Pong:
                            DoPong(new ConnectionEventArgs {
                                Socket = exSocket, Message = message
                            });
                            break;

                        default:
                            OnDataReceived(new ConnectionEventArgs {
                                Socket = exSocket, Message = message
                            });
                            break;
                        }
                    }
                    catch (Exception ex)
                    {
                        TraceLog.WriteError("OnDataReceived error:{0}", ex);
                    }
                }
            }
            if (needPostAnother)
            {
                PostReceive(ioEventArgs);
                //是否需要关闭连接
                if (exSocket.IsClosed)
                {
                    ResetSAEAObject(ioEventArgs);
                }
            }
        }
Esempio n. 16
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="dataToken"></param>
 /// <param name="buffer"></param>
 /// <param name="messageList"></param>
 /// <returns></returns>
 public abstract bool TryReadMeaage(DataToken dataToken, byte[] buffer, out List <DataMessage> messageList);