Esempio n. 1
0
        internal override void StartProcess()
        {
            var awaiter = HandlerSaeaPool.Take();

            awaiter.Saea.SetBuffer(_headBuffer, _packageRecvOffset, _headBuffer.Length);
            SaeaExHelper.ReceiveAsync(Socket, awaiter, HeadProcess);
        }
Esempio n. 2
0
        private void PacketPartProcess(SaeaAwaiter awaiter, SocketError error)
        {
            int bytesTransferred = awaiter.Saea.BytesTransferred;

            if (bytesTransferred == 0 ||
                error != SocketError.Success ||
                State != TcpSocketConnectionState.Connected ||
                Socket == null)
            {
                LogHelper.WriteLog("session_recv endtransfer state:" + State.ToString() + " socket_error:" + error.ToString());
                this.EndTransfer(awaiter);
                return;
            }

            this.ReceiveBytesTransferred = bytesTransferred;
            this.NotifyEventHandler?.Invoke(TcpSocketCompletionNotify.OnDataReceiveing, this);

            _heartTime          = DateTime.Now;
            _packageRecvOffset += bytesTransferred;
            if (_packageRecvOffset >= CompletedBuffer.Length)
            {
                this.PackageProcess();

                _packageRecvOffset = 0;
                awaiter.Saea.SetBuffer(_headBuffer, _packageRecvOffset, _headBuffer.Length);
                SaeaExHelper.ReceiveAsync(this.Socket, awaiter, HeadProcess);
            }
            else
            {
                awaiter.Saea.SetBuffer(_packageRecvOffset, CompletedBuffer.Length - _packageRecvOffset);
                SaeaExHelper.ReceiveAsync(this.Socket, awaiter, PacketPartProcess);
            }
        }
        internal override void StartProcess()
        {
            var awaiter = HandlerSaeaPool.Take();

            awaiter.Saea.SetBuffer(CompletedBuffer, 0, CompletedBuffer.Length);
            SaeaExHelper.ReceiveAsync(Socket, awaiter, PacketPartProcess);
        }
Esempio n. 4
0
        private void HeadProcess(SaeaAwaiter awaiter, SocketError error)
        {
            if (awaiter.Saea.BytesTransferred == 0 ||
                error != SocketError.Success ||
                State != TcpSocketConnectionState.Connected ||
                Socket == null)
            {
                LogHelper.WriteLog("session_recv endtransfer state:" + State.ToString() + " socket_error:" + error.ToString());
                this.EndTransfer(awaiter);
                return;
            }

            _heartTime          = DateTime.Now;
            _packageRecvOffset += awaiter.Saea.BytesTransferred;
            if (_packageRecvOffset >= 4)
            {
                int packBytesTransferred = BitConverter.ToInt32(_headBuffer, 0);

                if (packBytesTransferred < 0 || packBytesTransferred > Configuration.SendBufferSize)//长度包越界判断
                {
                    this.Close(true);
                    return;
                }

                if (packBytesTransferred == 0)
                {
                    if (this.Configuration._intervalWhetherService) //如果是服务端,则反馈心跳包
                    {
                        if (this._intervalIsUseChannel == 0)
                        {
                            var h_saea = HandlerSaeaPool.Take();

                            //4个字节空包头
                            h_saea.Saea.SetBuffer(_emptyHeart, 0, _emptyHeart.Length);
                            SaeaExHelper.SendAsync(this.Socket, h_saea, (a, e) => HandlerSaeaPool.Return(a));
                        }
                    }

                    //继续接收包头
                    _packageRecvOffset = 0;
                    awaiter.Saea.SetBuffer(_packageRecvOffset, _headBuffer.Length);
                    SaeaExHelper.ReceiveAsync(this.Socket, awaiter, HeadProcess);
                    return;
                }
                CompletedBuffer    = new byte[packBytesTransferred];
                _packageRecvOffset = 0;

                awaiter.Saea.SetBuffer(CompletedBuffer, _packageRecvOffset, CompletedBuffer.Length);
                SaeaExHelper.ReceiveAsync(this.Socket, awaiter, PacketPartProcess);
            }
            else
            {
                awaiter.Saea.SetBuffer(_packageRecvOffset, _headBuffer.Length - _packageRecvOffset);
                SaeaExHelper.ReceiveAsync(this.Socket, awaiter, HeadProcess);
            }
        }
        private void PacketPartProcess(SaeaAwaiter awaiter, SocketError error)
        {
            var bytesTransferred = awaiter.Saea.BytesTransferred;

            if (bytesTransferred == 0 ||
                error != SocketError.Success ||
                State != TcpSocketConnectionState.Connected ||
                Socket == null)
            {
                LogHelper.WriteLog("session_recv endtransfer state:" + State.ToString() + " socket_error:" + error.ToString());
                this.EndTransfer(awaiter);
                return;
            }
            this.ReceiveBytesTransferred = bytesTransferred;

            NotifyEventHandler?.Invoke(TcpSessionNotify.OnDataReceiveing, this);

            awaiter.Saea.SetBuffer(CompletedBuffer, 0, CompletedBuffer.Length);
            SaeaExHelper.ReceiveAsync(this.Socket, awaiter, PacketPartProcess);
        }