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

📄 packetdecoderfilter.cs

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

    public class PacketDecoderFilter : SessionFilterAdapter
    {
        private static readonly ILogger log = LogFactory.CreateLogger(MethodBase.GetCurrentMethod().ReflectedType);
        protected readonly ISession session;

        protected PacketDecoderFilter(ISession session)
        {
            this.session = session;
        }

        public static PacketDecoderFilter NewInstance(ISession session)
        {
            if (session.SessionType == SessionType.Udp)
            {
                return new DiscardPacketDecoderFilter(session);
            }
            if (session.SessionType != SessionType.Tcp)
            {
                throw new ArgumentException();
            }
            return new CopyPacketDecoderFilter(session);
        }

        protected void Recognize(IBuffer content, IPEndPoint endpoint)
        {
            if (log.IsInfoEnabled)
            {
                log.Info("Enter Recognize.");
            }
            while (content.HasRemaining)
            {
                try
                {
                    IBuffer buffer = content.AsReadOnlyBuffer().Slice();
                    int position = buffer.Position;
                    if ((this.session == null) || (this.session.PacketDecoder == null))
                    {
                        break;
                    }
                    object obj2 = this.session.PacketDecoder.Decode(this.session, new DefaultPacket(buffer, endpoint));
                    if (obj2 == null)
                    {
                        if (log.IsDebugEnabled)
                        {
                            log.Debug("Decode failed.");
                        }
                        break;
                    }
                    if (position == buffer.Position)
                    {
                        throw new Exception("PacketDecoder should change the packet position.");
                    }
                    content.Skip(buffer.Position);
                    if (log.IsInfoEnabled)
                    {
                        log.Info(obj2.ToString());
                    }
                    this.session.GetSessionFilterChain(FilterChainMode.Receive).ObjectReceived(obj2);
                    continue;
                }
                catch (Exception exception)
                {
                    if (log.IsWarnEnabled)
                    {
                        log.Warn("Recognize failed.", exception);
                    }
                    break;
                }
            }
            if (log.IsInfoEnabled)
            {
                log.Info("Exit Recognize.");
            }
        }

        private class CopyPacketDecoderFilter : PacketDecoderFilter
        {
            private IBuffer content;
            private IPEndPoint endPoint;

            public CopyPacketDecoderFilter(ISession session) : base(session)
            {
            }

            public override void PacketReceived(ISessionFilterChain filterChain, IPacket packet)
            {
                if ((packet == null) || (filterChain.Session != base.session))
                {
                    base.PacketReceived(filterChain, packet);
                }
                else
                {
                    lock (this)
                    {
                        if (this.content == null)
                        {
                            this.content = packet.Content;
                            this.endPoint = packet.EndPoint;
                        }
                        else
                        {
                            IBuffer source = packet.Content;
                            if (this.content.Remaining < source.Remaining)
                            {
                                IBuffer buffer2 = BufferFactory.GetBuffer(this.content.Position + source.Remaining).Put(this.content.Flip());
                                this.content.Release();
                                this.content = buffer2;
                            }
                            this.content.Put(source);
                            this.content.Flip();
                        }
                        if (this.content != null)
                        {
                            try
                            {
                                base.Recognize(this.content, this.endPoint);
                            }
                            finally
                            {
                                if (this.content.HasRemaining)
                                {
                                    this.content.Compact();
                                }
                                else
                                {
                                    this.content.Release();
                                    this.content = null;
                                    this.endPoint = null;
                                }
                            }
                        }
                    }
                }
            }

            public override void SessionStateChanged(ISessionFilterChain filterChain)
            {
                if ((filterChain.Session == base.session) && (base.session.State == SessionState.Closed))
                {
                    lock (this)
                    {
                        if (this.content != null)
                        {
                            this.content.Release();
                            this.content = null;
                        }
                        this.endPoint = null;
                    }
                }
                base.SessionStateChanged(filterChain);
            }
        }

        private class DiscardPacketDecoderFilter : PacketDecoderFilter
        {
            public DiscardPacketDecoderFilter(ISession session) : base(session)
            {
            }

            public override void PacketReceived(ISessionFilterChain filterChain, IPacket packet)
            {
                if ((packet == null) || (filterChain.Session != base.session))
                {
                    base.PacketReceived(filterChain, packet);
                }
                else
                {
                    IBuffer content = packet.Content;
                    if (content != null)
                    {
                        try
                        {
                            base.Recognize(content, packet.EndPoint);
                        }
                        finally
                        {
                            content.Release();
                        }
                    }
                }
            }
        }
    }
}

⌨️ 快捷键说明

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