Inital Commit

This commit is contained in:
Ubuntu
2026-07-07 13:40:15 -04:00
parent 73701ff86e
commit b491e327dc
83 changed files with 8591 additions and 6895 deletions
+76 -103
View File
@@ -1,6 +1,7 @@
#region References
using System;
using System.Collections.Generic;
using System.Collections.Concurrent;
using System.Threading;
#endregion
namespace Server.Network
@@ -9,29 +10,20 @@ namespace Server.Network
{
public class Gram
{
private static readonly Stack<Gram> _pool = new Stack<Gram>();
private static readonly ConcurrentQueue<Gram> _pool = new ConcurrentQueue<Gram>();
public static Gram Acquire()
{
lock (_pool)
{
Gram gram;
public static Gram Acquire()
{
if (!_pool.TryDequeue(out Gram gram))
{
gram = new Gram();
}
if (_pool.Count > 0)
{
gram = _pool.Pop();
}
else
{
gram = new Gram();
}
gram._buffer = AcquireBuffer();
gram._length = 0;
gram._buffer = AcquireBuffer();
gram._length = 0;
return gram;
}
}
return gram;
}
private byte[] _buffer;
private int _length;
@@ -58,14 +50,12 @@ namespace Server.Network
return write;
}
public void Release()
{
lock (_pool)
{
_pool.Push(this);
ReleaseBuffer(_buffer);
}
}
public void Release()
{
_pool.Enqueue(this);
ReleaseBuffer(_buffer);
}
}
private static int m_CoalesceBufferSize = 512;
@@ -74,77 +64,60 @@ namespace Server.Network
public static int CoalesceBufferSize
{
get { return m_CoalesceBufferSize; }
set
{
if (m_CoalesceBufferSize == value)
{
return;
}
set
{
if (m_CoalesceBufferSize == value)
{
return;
}
BufferPool old = m_UnusedBuffers;
m_UnusedBuffers?.Free();
lock (old)
{
if (m_UnusedBuffers != null)
{
m_UnusedBuffers.Free();
}
m_CoalesceBufferSize = value;
m_UnusedBuffers = new BufferPool("Coalesced", 2048, m_CoalesceBufferSize);
}
}
m_CoalesceBufferSize = value;
m_UnusedBuffers = new BufferPool("Coalesced", 2048, m_CoalesceBufferSize);
}
}
public static byte[] AcquireBuffer()
{
lock (m_UnusedBuffers)
return m_UnusedBuffers.AcquireBuffer();
return m_UnusedBuffers.AcquireBuffer();
}
public static void ReleaseBuffer(byte[] buffer)
{
lock (m_UnusedBuffers)
if (buffer != null && buffer.Length == m_CoalesceBufferSize)
{
m_UnusedBuffers.ReleaseBuffer(buffer);
}
}
public static void ReleaseBuffer(byte[] buffer)
{
if (buffer != null && buffer.Length == m_CoalesceBufferSize)
{
m_UnusedBuffers.ReleaseBuffer(buffer);
}
}
private readonly Queue<Gram> _pending;
private readonly ConcurrentQueue<Gram> _pending = new ConcurrentQueue<Gram>();
private Gram _buffered;
private volatile Gram _buffered;
public bool IsFlushReady { get { return (_pending.Count == 0 && _buffered != null); } }
public bool IsEmpty { get { return (_pending.Count == 0 && _buffered == null); } }
public SendQueue()
{
_pending = new Queue<Gram>();
}
public Gram CheckFlushReady()
{
Gram gram = _buffered;
_pending.Enqueue(_buffered);
_buffered = null;
Gram gram = Interlocked.Exchange(ref _buffered, null);
_pending.Enqueue(gram);
return gram;
}
public Gram Dequeue()
{
Gram gram = null;
if (_pending.TryDequeue(out Gram gram))
{
gram?.Release();
gram = null;
if (_pending.Count > 0)
{
_pending.Dequeue().Release();
if (_pending.Count > 0)
{
gram = _pending.Peek();
}
}
if (_pending.TryPeek(out Gram peek))
gram = peek;
}
return gram;
}
@@ -177,41 +150,41 @@ namespace Server.Network
throw new ArgumentException("Offset and length do not point to a valid segment within the buffer.");
}
int existingBytes = (_pending.Count * m_CoalesceBufferSize) + (_buffered == null ? 0 : _buffered.Length);
int existingBytes = (_pending.Count * m_CoalesceBufferSize) + (_buffered?.Length ?? 0);
if ((existingBytes + length) > PendingCap)
{
throw new CapacityExceededException();
}
Gram gram = null;
Gram gram = null;
while (length > 0)
{
if (_buffered == null)
{
// nothing yet buffered
_buffered = Gram.Acquire();
}
while (length > 0)
{
if (_buffered == null)
{
// nothing yet buffered
_buffered = Gram.Acquire();
}
int bytesWritten = _buffered.Write(buffer, offset, length);
int bytesWritten = _buffered.Write(buffer, offset, length);
offset += bytesWritten;
length -= bytesWritten;
offset += bytesWritten;
length -= bytesWritten;
if (_buffered.IsFull)
{
if (_pending.Count == 0)
{
gram = _buffered;
}
if (_buffered.IsFull)
{
if (_pending.Count == 0)
{
gram = _buffered;
}
_pending.Enqueue(_buffered);
_buffered = null;
}
}
_pending.Enqueue(_buffered);
_buffered = null;
}
}
return gram;
return gram;
}
public void Clear()
@@ -222,9 +195,9 @@ namespace Server.Network
_buffered = null;
}
while (_pending.Count > 0)
while (_pending.TryDequeue(out Gram gram))
{
_pending.Dequeue().Release();
gram?.Release();
}
}
}
@@ -236,4 +209,4 @@ namespace Server.Network
: base("Too much data pending.")
{ }
}
}
}