⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 asyncudtsession.cs

📁 破解的飞信源代码
💻 CS
📖 第 1 页 / 共 2 页
字号:
        {
            if (log.IsInfoEnabled)
            {
                log.Info("Enter SocketBeginReceive");
            }
            try
            {
                IBuffer buffer = BufferFactory.GetBuffer(0x2000);
                this.DoBeginReceive(buffer);
            }
            catch (ObjectDisposedException exception)
            {
                base.SessionCaughtException(exception);
            }
            catch (Exception exception2)
            {
                base.SessionCaughtException(exception2);
            }
        }

        protected virtual IAsyncResult SocketBeginSend(IPacket packet, object state)
        {
            return this.DoBeginSend(packet.Content.GetInnerByteArray(), packet.Content.Position, packet.Content.Remaining, state);
        }

        private void SocketEndAccept(IAsyncResult ar)
        {
            try
            {
                this.innerSocket = this.listenSocket.EndAccept(ar);
                base.State = SessionState.Opened;
                this.SocketBeginReceive();
            }
            catch (Exception exception)
            {
                log.Error("Error in SocketEndAccept", exception);
            }
            finally
            {
                if (this.startWaitHandle != null)
                {
                    this.startWaitHandle.Set();
                }
                if (this.listenSocket != null)
                {
                    this.listenSocket.Close();
                }
            }
        }

        private void SocketEndConnect(IAsyncResult asyncResult)
        {
            AsyncFuture asyncState = (AsyncFuture) asyncResult.AsyncState;
            try
            {
                this.innerSocket.EndConnect(asyncResult);
                base.State = SessionState.Opened;
                asyncState.IsSucceeded = true;
                this.SocketBeginReceive();
                if (this.startWaitHandle != null)
                {
                    this.startWaitHandle.Set();
                }
            }
            catch (SocketException exception)
            {
                base.State = SessionState.Initial;
                base.SessionCaughtException(exception);
                if (Interlocked.Increment(ref this.tryConnectTimes) < 10)
                {
                    Thread.Sleep(0x3e8);
                    this.InternalOpen(asyncState);
                }
                else
                {
                    asyncState.IsSucceeded = false;
                    base.SessionCaughtException(exception);
                    if (this.startWaitHandle != null)
                    {
                        this.startWaitHandle.Set();
                    }
                }
            }
            catch (Exception exception2)
            {
                asyncState.IsSucceeded = false;
                base.SessionCaughtException(exception2);
                if (this.startWaitHandle != null)
                {
                    this.startWaitHandle.Set();
                }
            }
        }

        protected void SocketEndReceive(IAsyncResult asyncResult)
        {
            if (log.IsInfoEnabled)
            {
                log.Info("Enter SocketEndReceive");
            }
            if (!base.IsOpened)
            {
                if (log.IsWarnEnabled)
                {
                    log.Warn("Session has not been started.");
                }
            }
            else
            {
                try
                {
                    lock (this.recvSyncRoot)
                    {
                        IPacket packet = this.DoEndReceive(asyncResult);
                        if (base.IsOpened)
                        {
                            this.SocketBeginReceive();
                        }
                        else if (log.IsWarnEnabled)
                        {
                            log.Warn("Session has not been started.");
                        }
                        if (packet != null)
                        {
                            base.GetSessionFilterChain(FilterChainMode.Receive).PacketReceived(packet);
                        }
                    }
                }
                catch (RemoteSocketClosedException exception)
                {
                    if (base.IsOpened)
                    {
                        log.Warn("Remote Socket Was Closed.", exception);
                    }
                    this.Close();
                }
                catch (Exception exception2)
                {
                    base.SessionCaughtException(exception2);
                }
            }
        }

        protected virtual void SocketEndSend(IAsyncResult asyncResult)
        {
            if (base.IsOpened)
            {
                AsyncFuture future = asyncResult.AsyncState as AsyncFuture;
                if (future != null)
                {
                    IPacket associatedPacket = future.AssociatedPacket;
                    try
                    {
                        int num = this.innerSocket.EndSend(asyncResult);
                        try
                        {
                            IBuffer content = associatedPacket.Content;
                            content.Position += num;
                        }
                        catch (Exception exception)
                        {
                            if (log.IsWarnEnabled)
                            {
                                log.Warn("Change packet content postion failed.", exception);
                            }
                        }
                        if (associatedPacket.Content.Remaining == 0)
                        {
                            lock (this.sendQueue)
                            {
                                this.sendQueue.Dequeue();
                            }
                            future.IsSucceeded = true;
                            base.GetSessionFilterChain(FilterChainMode.Send).PacketSent(future.AssociatedPacket);
                            future.AssociatedPacket.Content.Release();
                            base.GetSessionFilterChain(FilterChainMode.Send).ObjectSent(future.AssociatedObject);
                        }
                        else
                        {
                            log.Warn("Send remaining > 0");
                            this.SendFirstOfQueue();
                        }
                    }
                    catch (Exception exception2)
                    {
                        this.SendFirstOfQueue();
                        log.Fatal("End send failed.", exception2);
                        future.IsSucceeded = false;
                        base.SessionCaughtException(exception2);
                    }
                }
            }
        }

        public bool CanSendNow
        {
            get
            {
                return (this.sendQueue.get_Count() < 0x400);
            }
        }

        public UDTSocket InnerSocket
        {
            get
            {
                return this.innerSocket;
            }
        }

        public IPEndPoint LocalEndPoint
        {
            get
            {
                return this.localEndPoint;
            }
            set
            {
                if (base.IsOpened)
                {
                    throw new InvalidOperationException("Session already opened.");
                }
                this.localEndPoint = value;
            }
        }

        public int LocalPort
        {
            get
            {
                if ((this.innerSocket != null) && (this.innerSocket.LocalEndPoint != null))
                {
                    return ((IPEndPoint) this.innerSocket.LocalEndPoint).Port;
                }
                return 0;
            }
        }

        public virtual IPEndPoint RemoteEndPoint
        {
            get
            {
                return this.remoteEndPoint;
            }
            set
            {
                this.remoteEndPoint = value;
            }
        }

        protected class SendOperateFilter : SessionFilterAdapter
        {
            protected readonly AsyncFuture future;
            protected readonly AsyncUDTSession parentSession;

            public SendOperateFilter(AsyncUDTSession session, AsyncFuture future)
            {
                this.future = future;
                this.parentSession = session;
            }

            public override void PacketSend(ISessionFilterChain filterChain, IPacket packet)
            {
                if (packet.Content.Remaining > 0)
                {
                    this.future.AsyncResult = this.parentSession.SocketBeginSend(packet, this.future);
                }
                base.PacketSend(filterChain, packet);
            }
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -