Ejemplo n.º 1
0
        void OnReceiveAsyncCompleted(SocketSession session, Socket socket, SocketAsyncEventArgs args)
        {
            var sid = session.id;

            if ((args.SocketError != SocketError.Success) || (0 == args.BytesTransferred))
            {
                this.socketResourceManager.CloseSocket(socket);
                this.socketResourceManager.FreeSocketAsyncEventArgs(args);
                return;
            }

            session.IncPendingCount(this.utcNow); // to prevent disconnect while receive data processing
            var ret = ProcessReceivedData(session, socket, args);

            session.DecPendingCount(this.utcNow);

            if (false == ret)
            {
                this.socketResourceManager.CloseSocket(socket);
                this.socketResourceManager.FreeSocketAsyncEventArgs(args);
                return;
            }

            bool asyncResult = true;

            try
            {
                session.IncPendingCount(this.utcNow);
                asyncResult = socket.ReceiveAsync(args);
            }
            catch
            {
                session.DecPendingCount(this.utcNow);
                this.socketResourceManager.CloseSocket(socket);
                this.socketResourceManager.FreeSocketAsyncEventArgs(args);
                return;
            }

            if (false == asyncResult)
            {
                OnSocketIOCompleted(socket, args);
            }
        }
Ejemplo n.º 2
0
        void OnSendAsyncCompleted(SocketSession session, Socket socket, SocketAsyncEventArgs args)
        {
            var sid = session.id;

            if ((args.SocketError != SocketError.Success) || (0 == args.BytesTransferred))
            {
                this.socketResourceManager.CloseSocket(socket);
                var list = args.BufferList as List <ArraySegment <byte> >;
                this.socketResourceManager.FreeArraySegmentList(list);
                this.socketResourceManager.FreeSocketAsyncEventArgs(args);
                return;
            }

            byte[] qitem;
            if (session.sendQueue.TryPeek(out qitem))
            {
                var nlist = args.BufferList as List <ArraySegment <byte> >;
                nlist.Clear();
                while (session.sendQueue.TryDequeue(out qitem))
                {
                    nlist.Add(new ArraySegment <byte>(BitConverter.GetBytes(qitem.Length)));
                    nlist.Add(new ArraySegment <byte>(qitem));
                }
                args.BufferList = nlist;

                bool asyncResult = true;
                try
                {
                    session.IncPendingCount(this.utcNow);
                    asyncResult = socket.SendAsync(args);
                }
                catch
                {
                    session.DecPendingCount(this.utcNow);
                    this.socketResourceManager.CloseSocket(socket);
                    var list = args.BufferList as List <ArraySegment <byte> >;
                    this.socketResourceManager.FreeArraySegmentList(list);
                    this.socketResourceManager.FreeSocketAsyncEventArgs(args);
                    return;
                }

                if (false == asyncResult)
                {
                    OnSocketIOCompleted(session.socket, args);
                }
            }
            else
            {
                Interlocked.CompareExchange(ref session.isSendAsyncCalled, 0, 1);
                this.socketResourceManager.FreeSocketAsyncEventArgs(args);
                VerifyMissingSend(session);
            }
        }
Ejemplo n.º 3
0
        void OnConnectAsyncCompleted(SocketSession session, Socket socket, SocketAsyncEventArgs args)
        {
            var sid = session.id;

            if (args.SocketError != SocketError.Success)
            {
                this.socketResourceManager.CloseSocket(socket);
                this.socketResourceManager.FreeSocketAsyncEventArgs(args);

                this.userCallback(CallbackEventType.CONNECT_FAIL, session, args.SocketError);
                DisposeSocketSession(session);
                return;
            }

            if (this.encryptLevel > 0)
            {
                // wait for public key receive
            }
            else
            {
                this.userCallback(CallbackEventType.CONNECT_SUCCESS, session, null);
                session.isUserCallbackHappen = true;
            }

            if (socket.Connected)
            {
                bool asyncResult = true;
                try
                {
                    session.IncPendingCount(this.utcNow);

                    args.SetBuffer(session.receiveBuffer, 0, session.receiveBuffer.Length);
                    asyncResult = socket.ReceiveAsync(args);
                }
                catch
                {
                    session.DecPendingCount(this.utcNow);
                    this.socketResourceManager.CloseSocket(socket);
                    this.socketResourceManager.FreeSocketAsyncEventArgs(args);
                    return;
                }

                if (false == asyncResult)
                {
                    OnSocketIOCompleted(socket, args);
                }
            }
        }
Ejemplo n.º 4
0
        ReturnCode _Send(SocketSession session, Socket socket, byte[] sendData)
        {
            var sid  = session.id;
            var args = this.socketResourceManager.AllocSocketAsyncEventArgs(OnSocketIOCompleted);

            args.UserToken = session;

            var list = this.socketResourceManager.AllocArraySegmentList();

            list.Add(new ArraySegment <byte>(BitConverter.GetBytes(sendData.Length)));
            list.Add(new ArraySegment <byte>(sendData));
            args.BufferList = list;

            bool asyncResult = true;

            try
            {
                session.IncPendingCount(this.utcNow);
                asyncResult = socket.SendAsync(args);
            }
            catch
            {
                session.DecPendingCount(this.utcNow);
                this.socketResourceManager.CloseSocket(socket);
                this.socketResourceManager.FreeSocketAsyncEventArgs(args);
                this.socketResourceManager.FreeArraySegmentList(list);
                return(ReturnCode.ERROR_SESSION_CLOSED);
            }

            if (false == asyncResult)
            {
                OnSocketIOCompleted(socket, args);
            }

            return(ReturnCode.SUCCESS);
        }