yuanshen/Yuanshen.Kcp/KcpSendReceiveQueueItemCache.cs
wisedev fc2ac1050f First working version of the server
KCP gameserver with packet dispatch, scene streaming, combat, gacha and the
shop, plus a small HTTP server for the login flow and the wish history page.

Game data under assets is pulled out of the client instead of being typed by
hand. The excel tables come from the packed bin container and the spawn points
from the compiled scene scripts.
2026-09-04 02:10:50 +03:00

84 lines
2.2 KiB
C#

#if NEED_LINKEDLIST_SHIM
using LinkedListOfQueueItem = KcpSharp.NetstandardShim.LinkedList<(KcpSharp.KcpBuffer Data, byte Fragment)>;
using LinkedListNodeOfQueueItem = KcpSharp.NetstandardShim.LinkedListNode<(KcpSharp.KcpBuffer Data, byte Fragment)>;
#else
using LinkedListNodeOfQueueItem = System.Collections.Generic.LinkedListNode<(Yuanshen.Kcp.KcpBuffer Data, byte Fragment)>;
using LinkedListOfQueueItem = System.Collections.Generic.LinkedList<(Yuanshen.Kcp.KcpBuffer Data, byte Fragment)>;
#endif
namespace Yuanshen.Kcp
{
internal sealed class KcpSendReceiveQueueItemCache
{
private LinkedListOfQueueItem _list = new();
private SpinLock _lock;
public LinkedListNodeOfQueueItem Rent(in KcpBuffer buffer, byte fragment)
{
bool lockTaken = false;
try
{
_lock.Enter(ref lockTaken);
LinkedListNodeOfQueueItem node = _list.First;
if (node is null)
{
node = new LinkedListNodeOfQueueItem((buffer, fragment));
}
else
{
node.ValueRef = (buffer, fragment);
_list.RemoveFirst();
}
return node;
}
finally
{
if (lockTaken)
{
_lock.Exit();
}
}
}
public void Return(LinkedListNodeOfQueueItem node)
{
node.ValueRef = default;
bool lockTaken = false;
try
{
_lock.Enter(ref lockTaken);
_list.AddLast(node);
}
finally
{
if (lockTaken)
{
_lock.Exit();
}
}
}
public void Clear()
{
bool lockTaken = false;
try
{
_lock.Enter(ref lockTaken);
_list.Clear();
}
finally
{
if (lockTaken)
{
_lock.Exit();
}
}
}
}
}