using System.Collections.Concurrent; using XGame.Framework.Memory; namespace XGame.Framework.Network { public static class SessionBufferPool { private const int MAX_BUFFER_COUNT = 8; public const int BufferSize = 0x1000; private static ConcurrentQueue _sendBuffers = new ConcurrentQueue(); static SessionBufferPool() { MemoryMonitor.Register(LowMemoryCallback); } private static void LowMemoryCallback() { _sendBuffers.Clear(); } public static void Recycle(byte[] buffer) { if (buffer != null && _sendBuffers.Count < MAX_BUFFER_COUNT) { _sendBuffers.Enqueue(buffer); } } //在心跳和ProtobufHelper中获取 将由TCPRemote回收 public static byte[] Acquire() { _sendBuffers.TryDequeue(out byte[] sendbuffer); return sendbuffer ?? new byte[BufferSize]; } public static void Dispose() { _sendBuffers.Clear(); } } }