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

📄 asyncudpsession.cs

📁 破解的飞信源代码
💻 CS
字号:
namespace NCindy.Session.AIO
{
    using NCindy;
    using NCindy.Packet;
    using NCindy.Util.Logging;
    using System;
    using System.Net;
    using System.Net.Sockets;

    public class AsyncUdpSession : AsyncSocketSession
    {
        private static readonly ILogger log = LogFactory.CreateLogger(MethodBase.GetCurrentMethod().ReflectedType);

        public AsyncUdpSession(IPEndPoint remoteIPEndPoint) : base(SessionType.Udp, remoteIPEndPoint)
        {
        }

        public AsyncUdpSession(Socket sock) : base(sock)
        {
            base.sessionType = SessionType.Udp;
            base.State = SessionState.Opened;
            base.SocketBeginReceive();
        }

        protected override void DoBeginReceive(IBuffer buffer)
        {
            EndPoint remoteEP = new IPEndPoint(IPAddress.Any, 0);
            base.pendingReceiveResult = base.innerSocket.BeginReceiveFrom(buffer.GetInnerByteArray(), 0, 0x800, SocketFlags.None, ref remoteEP, base.socketEndRecvCallback, buffer);
        }

        protected override IAsyncResult DoBeginSend(byte[] content, int offset, int size, object state)
        {
            if (log.IsInfoEnabled)
            {
                log.Info(string.Format("Send content, lenght:{0}, offset:{1}, size:{2}", content.Length, offset, size));
            }
            return base.innerSocket.BeginSendTo(content, offset, size, SocketFlags.None, this.RemoteEndPoint, base.socketEndSendCallback, state);
        }

        protected override void DoClose()
        {
            base.innerSocket.Close();
        }

        protected override IPacket DoEndReceive(IAsyncResult asyncResult)
        {
            EndPoint endPoint = new IPEndPoint(IPAddress.Any, 0);
            IBuffer content = (IBuffer) asyncResult.AsyncState;
            int num = base.innerSocket.EndReceiveFrom(asyncResult, ref endPoint);
            if (log.IsInfoEnabled)
            {
                log.Info(string.Format("Received size: {0}", num));
            }
            if (num <= 0)
            {
                if (log.IsWarnEnabled)
                {
                    log.Warn("Received 0 byte from " + endPoint);
                }
                content.Release();
                return null;
            }
            content.Limit = num;
            return new DefaultPacket(content, endPoint as IPEndPoint);
        }

        protected override void InternalOpen(AsyncFuture future)
        {
            base.innerSocket.Bind(base.LocalEndPoint);
            base.State = SessionState.Opened;
            future.IsCompleted = true;
            future.IsSucceeded = true;
            base.SocketBeginReceive();
        }

        public override IPEndPoint RemoteEndPoint
        {
            set
            {
                base.remoteEndPoint = value;
            }
        }
    }
}

⌨️ 快捷键说明

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