📄 payloadmanager.cs
字号:
namespace NCindy.Protocol.RUDP
{
using NCindy.Protocol.RUDP.Packet;
using NCindy.Threading;
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
internal static class PayloadManager
{
private static int _currentAllocation = 0;
private static readonly ReaderWriterLockSlim[] _locks = new ReaderWriterLockSlim[0x9d];
private static readonly Queue<byte[]>[] _queues = new Queue<byte[]>[0x9d];
private static int _totalAllocation = 0;
private static readonly int MaxAllocation = 0x3e8000;
static PayloadManager()
{
foreach (int num in Enum.GetValues(typeof(RUDPPacketChannel)))
{
_queues[num] = new Queue<byte[]>();
_locks[num] = new ReaderWriterLockSlim();
}
}
public static byte[] Allocate(RUDPPacketChannel channel, int size)
{
byte[] buffer;
Queue<byte[]> queue = _queues[(int) channel];
ReaderWriterLockSlim slim = _locks[(int) channel];
slim.EnterWriteLock();
if (queue.get_Count() > 0)
{
buffer = queue.Dequeue();
_currentAllocation -= buffer.Length;
if (buffer.Length != size)
{
Array.Resize<byte>(ref buffer, size);
}
slim.ExitWriteLock();
return buffer;
}
slim.ExitWriteLock();
if (_totalAllocation < MaxAllocation)
{
_totalAllocation += size;
buffer = new byte[size];
GCHandle.Alloc(buffer, GCHandleType.Pinned);
GC.KeepAlive(buffer);
return buffer;
}
return new byte[size];
}
public static void Deallocate(RUDPPacketChannel channel, byte[] buffer)
{
if (_currentAllocation < MaxAllocation)
{
Queue<byte[]> queue = _queues[(int) channel];
ReaderWriterLockSlim slim = _locks[(int) channel];
slim.EnterWriteLock();
queue.Enqueue(buffer);
slim.ExitWriteLock();
_currentAllocation += buffer.Length;
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -