1234567891011121314151617181920212223242526272829303132333435363738394041 |
- 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<byte[]> _sendBuffers = new ConcurrentQueue<byte[]>();
- 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();
- }
- }
- }
|