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

📄 ingoinglistrwlock.cs

📁 破解的飞信源代码
💻 CS
字号:
namespace NCindy.Protocol.RUDP.Packet
{
    using NCindy.Threading;
    using System;
    using System.Collections.Generic;

    internal sealed class IngoingListRWLock
    {
        private int _currentPacketId = -1;
        private LinkedList<RUDPIngoingPacket> _list = new LinkedList<RUDPIngoingPacket>();
        private ReaderWriterLockSlim _lock = new ReaderWriterLockSlim();

        internal void AddPacket(RUDPIngoingPacket packet)
        {
            if (packet.PacketId < 0)
            {
                this._lock.EnterWriteLock();
                this._list.AddLast(packet);
                this._lock.ExitWriteLock();
            }
            else
            {
                this._lock.EnterUpgradeableReadLock();
                try
                {
                    LinkedListNode<RUDPIngoingPacket> node = this._list.get_Last();
                    while ((node != null) && ((node.get_Value().PacketId < 0) || (packet.PacketId < node.get_Value().PacketId)))
                    {
                        node = node.get_Previous();
                    }
                    if (node == null)
                    {
                        this._lock.EnterWriteLock();
                        this._list.AddFirst(packet);
                        this._lock.ExitWriteLock();
                    }
                    else if (packet.PacketId != node.get_Value().PacketId)
                    {
                        this._lock.EnterWriteLock();
                        this._list.AddAfter(node, packet);
                        this._lock.ExitWriteLock();
                    }
                }
                finally
                {
                    this._lock.ExitUpgradeableReadLock();
                }
            }
        }

        internal void Clear()
        {
            this._lock.EnterWriteLock();
            this._list.Clear();
            this._currentPacketId = -1;
            this._lock.ExitWriteLock();
        }

        internal bool ContainsPacket(int packetId)
        {
            this._lock.EnterReadLock();
            LinkedListNode<RUDPIngoingPacket> node = this._list.get_First();
            while (node != null)
            {
                if (node.get_Value().PacketId != packetId)
                {
                    node = node.get_Next();
                }
                else
                {
                    this._lock.ExitReadLock();
                    return true;
                }
            }
            this._lock.ExitReadLock();
            return false;
        }

        internal RUDPIngoingPacket RemoveNextPacket()
        {
            this._lock.EnterUpgradeableReadLock();
            try
            {
                LinkedListNode<RUDPIngoingPacket> node = this._list.get_First();
                if (node == null)
                {
                    return null;
                }
                if ((node.get_Value().PacketId < 0) || (node.get_Value().PacketId == (this._currentPacketId + 1)))
                {
                    this._lock.EnterWriteLock();
                    this._currentPacketId++;
                    this._list.Remove(node);
                    this._lock.ExitWriteLock();
                    return node.get_Value();
                }
            }
            finally
            {
                this._lock.ExitUpgradeableReadLock();
            }
            return null;
        }

        internal int Count
        {
            get
            {
                return this._list.get_Count();
            }
        }

        internal int CurrentPacketId
        {
            get
            {
                return this._currentPacketId;
            }
            set
            {
                this._currentPacketId = value;
            }
        }

        internal int LastPacketId
        {
            get
            {
                int packetId;
                this._lock.EnterReadLock();
                try
                {
                    if (this._list.get_Count() < 1)
                    {
                        return -1;
                    }
                    packetId = this._list.get_Last().get_Value().PacketId;
                }
                finally
                {
                    this._lock.ExitReadLock();
                }
                return packetId;
            }
        }
    }
}

⌨️ 快捷键说明

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