yuanshen/Yuanshen.Kcp/KcpSendReceiveBufferItemCache.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

73 lines
2.1 KiB
C#

#if NEED_LINKEDLIST_SHIM
using LinkedListOfBufferItem = KcpSharp.NetstandardShim.LinkedList<KcpSharp.KcpSendReceiveBufferItem>;
using LinkedListNodeOfBufferItem = KcpSharp.NetstandardShim.LinkedListNode<KcpSharp.KcpSendReceiveBufferItem>;
#else
using LinkedListNodeOfBufferItem = System.Collections.Generic.LinkedListNode<Yuanshen.Kcp.KcpSendReceiveBufferItem>;
using LinkedListOfBufferItem = System.Collections.Generic.LinkedList<Yuanshen.Kcp.KcpSendReceiveBufferItem>;
#endif
namespace Yuanshen.Kcp
{
internal struct KcpSendReceiveBufferItemCache
{
private LinkedListOfBufferItem _items;
private SpinLock _lock;
public static KcpSendReceiveBufferItemCache Create()
{
return new KcpSendReceiveBufferItemCache
{
_items = new LinkedListOfBufferItem(),
_lock = new SpinLock()
};
}
public LinkedListNodeOfBufferItem Allocate(in KcpSendReceiveBufferItem item)
{
bool lockAcquired = false;
try
{
_lock.Enter(ref lockAcquired);
LinkedListNodeOfBufferItem node = _items.First;
if (node is null)
{
node = new LinkedListNodeOfBufferItem(item);
}
else
{
_items.Remove(node);
node.ValueRef = item;
}
return node;
}
finally
{
if (lockAcquired)
{
_lock.Exit();
}
}
}
public void Return(LinkedListNodeOfBufferItem node)
{
bool lockAcquired = false;
try
{
_lock.Enter(ref lockAcquired);
node.ValueRef = default;
_items.AddLast(node);
}
finally
{
if (lockAcquired)
{
_lock.Exit();
}
}
}
}
}