2024-05-03 14:45:41 +02:00
|
|
|
using System.Buffers;
|
|
|
|
|
|
|
|
namespace TanksServer.Interactivity;
|
|
|
|
|
2024-11-12 18:27:04 +01:00
|
|
|
internal sealed class BufferPool : MemoryPool<byte>
|
2024-05-03 14:45:41 +02:00
|
|
|
{
|
|
|
|
private readonly MemoryPool<byte> _actualPool = Shared;
|
|
|
|
|
|
|
|
public override int MaxBufferSize => int.MaxValue;
|
|
|
|
|
2024-11-12 18:27:04 +01:00
|
|
|
protected override void Dispose(bool disposing) { }
|
2024-05-03 14:45:41 +02:00
|
|
|
|
|
|
|
public override IMemoryOwner<byte> Rent(int minBufferSize = -1)
|
|
|
|
{
|
|
|
|
ArgumentOutOfRangeException.ThrowIfLessThan(minBufferSize, 1);
|
|
|
|
return new BufferPoolMemoryOwner(_actualPool.Rent(minBufferSize), minBufferSize);
|
|
|
|
}
|
|
|
|
|
2024-11-12 18:27:04 +01:00
|
|
|
private sealed class BufferPoolMemoryOwner(IMemoryOwner<byte> actualOwner, int wantedSize) : IMemoryOwner<byte>
|
2024-05-03 14:45:41 +02:00
|
|
|
{
|
|
|
|
public Memory<byte> Memory { get; } = actualOwner.Memory[..wantedSize];
|
|
|
|
|
|
|
|
public void Dispose() => actualOwner.Dispose();
|
|
|
|
}
|
|
|
|
}
|