Files

213 lines
5.2 KiB
C#
Raw Permalink Normal View History

2026-06-19 00:00:16 -04:00
#region References
using System;
2026-07-07 13:40:15 -04:00
using System.Collections.Concurrent;
using System.Threading;
2026-06-19 00:00:16 -04:00
#endregion
namespace Server.Network
{
public class SendQueue
{
public class Gram
{
2026-07-07 13:40:15 -04:00
private static readonly ConcurrentQueue<Gram> _pool = new ConcurrentQueue<Gram>();
2026-06-19 00:00:16 -04:00
2026-07-07 13:40:15 -04:00
public static Gram Acquire()
{
if (!_pool.TryDequeue(out Gram gram))
{
gram = new Gram();
}
gram._buffer = AcquireBuffer();
gram._length = 0;
return gram;
}
2026-06-19 00:00:16 -04:00
private byte[] _buffer;
private int _length;
public byte[] Buffer { get { return _buffer; } }
public int Length { get { return _length; } }
public int Available { get { return (_buffer.Length - _length); } }
public bool IsFull { get { return (_length == _buffer.Length); } }
private Gram()
{ }
public int Write(byte[] buffer, int offset, int length)
{
int write = Math.Min(length, Available);
System.Buffer.BlockCopy(buffer, offset, _buffer, _length, write);
_length += write;
return write;
}
2026-07-07 13:40:15 -04:00
public void Release()
{
_pool.Enqueue(this);
ReleaseBuffer(_buffer);
}
2026-06-19 00:00:16 -04:00
}
private static int m_CoalesceBufferSize = 512;
private static BufferPool m_UnusedBuffers = new BufferPool("Coalesced", 2048, m_CoalesceBufferSize);
public static int CoalesceBufferSize
{
get { return m_CoalesceBufferSize; }
2026-07-07 13:40:15 -04:00
set
{
if (m_CoalesceBufferSize == value)
{
return;
}
m_UnusedBuffers?.Free();
m_CoalesceBufferSize = value;
m_UnusedBuffers = new BufferPool("Coalesced", 2048, m_CoalesceBufferSize);
}
2026-06-19 00:00:16 -04:00
}
public static byte[] AcquireBuffer()
{
2026-07-07 13:40:15 -04:00
return m_UnusedBuffers.AcquireBuffer();
2026-06-19 00:00:16 -04:00
}
2026-07-07 13:40:15 -04:00
public static void ReleaseBuffer(byte[] buffer)
{
if (buffer != null && buffer.Length == m_CoalesceBufferSize)
{
m_UnusedBuffers.ReleaseBuffer(buffer);
}
}
2026-06-19 00:00:16 -04:00
2026-07-07 13:40:15 -04:00
private readonly ConcurrentQueue<Gram> _pending = new ConcurrentQueue<Gram>();
2026-06-19 00:00:16 -04:00
2026-07-07 13:40:15 -04:00
private volatile Gram _buffered;
2026-06-19 00:00:16 -04:00
public bool IsFlushReady { get { return (_pending.Count == 0 && _buffered != null); } }
public bool IsEmpty { get { return (_pending.Count == 0 && _buffered == null); } }
public Gram CheckFlushReady()
{
2026-07-07 13:40:15 -04:00
Gram gram = Interlocked.Exchange(ref _buffered, null);
_pending.Enqueue(gram);
2026-06-19 00:00:16 -04:00
return gram;
}
public Gram Dequeue()
{
2026-07-07 13:40:15 -04:00
if (_pending.TryDequeue(out Gram gram))
{
gram?.Release();
gram = null;
2026-06-19 00:00:16 -04:00
2026-07-07 13:40:15 -04:00
if (_pending.TryPeek(out Gram peek))
gram = peek;
}
2026-06-19 00:00:16 -04:00
return gram;
}
private const int PendingCap = 0x200000;
public Gram Enqueue(byte[] buffer, int length)
{
return Enqueue(buffer, 0, length);
}
public Gram Enqueue(byte[] buffer, int offset, int length)
{
if (buffer == null)
{
throw new ArgumentNullException("buffer");
}
else if (!(offset >= 0 && offset < buffer.Length))
{
throw new ArgumentOutOfRangeException(
"offset", offset, "Offset must be greater than or equal to zero and less than the size of the buffer.");
}
else if (length < 0 || length > buffer.Length)
{
throw new ArgumentOutOfRangeException(
"length", length, "Length cannot be less than zero or greater than the size of the buffer.");
}
else if ((buffer.Length - offset) < length)
{
throw new ArgumentException("Offset and length do not point to a valid segment within the buffer.");
}
2026-07-07 13:40:15 -04:00
int existingBytes = (_pending.Count * m_CoalesceBufferSize) + (_buffered?.Length ?? 0);
2026-06-19 00:00:16 -04:00
if ((existingBytes + length) > PendingCap)
{
throw new CapacityExceededException();
}
2026-07-07 13:40:15 -04:00
Gram gram = null;
2026-06-19 00:00:16 -04:00
2026-07-07 13:40:15 -04:00
while (length > 0)
{
if (_buffered == null)
{
// nothing yet buffered
_buffered = Gram.Acquire();
}
2026-06-19 00:00:16 -04:00
2026-07-07 13:40:15 -04:00
int bytesWritten = _buffered.Write(buffer, offset, length);
offset += bytesWritten;
length -= bytesWritten;
if (_buffered.IsFull)
{
if (_pending.Count == 0)
{
gram = _buffered;
}
_pending.Enqueue(_buffered);
_buffered = null;
}
}
return gram;
2026-06-19 00:00:16 -04:00
}
public void Clear()
{
if (_buffered != null)
{
_buffered.Release();
_buffered = null;
}
2026-07-07 13:40:15 -04:00
while (_pending.TryDequeue(out Gram gram))
2026-06-19 00:00:16 -04:00
{
2026-07-07 13:40:15 -04:00
gram?.Release();
2026-06-19 00:00:16 -04:00
}
}
}
[Serializable]
public sealed class CapacityExceededException : Exception
{
public CapacityExceededException()
: base("Too much data pending.")
{ }
}
2026-07-07 13:40:15 -04:00
}