12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758 |
- using System.Collections.Concurrent;
- namespace XGame.Framework.Network
- {
- public class BytesWrapper
- {
- public byte[] buffer;
- public int offset;
- public int size;
- public BytesWrapper(byte[] buffer, int offset, int size)
- {
- this.buffer = buffer;
- this.offset = offset;
- this.size = size;
- }
- }
- public static class BytesWrapperPool
- {
- private const int MAX_BUFFER_COUNT = 8;
- private static ConcurrentQueue<BytesWrapper> bytesWrappers = new ConcurrentQueue<BytesWrapper>();
- //ProtobufHelper Recycle->Tpro2Sender /ProtobufSender
- //ZstdCompress Recycle->Tpro2Sender /----------------(tpro1发送不压缩)
- //ZstdDecompress Recycle->TPro2MsgResolver /TProMsgResolver
- //RC4Encrypt Recycle->TPro2Sender /----------------(tpro1发送不加密)
- //RC4Dencrypt Recycle->TPro2MsgResolver /----------------(tpro1接收不解密)
- //TProMsgResolver Recycle->TProMsgResolver
- //TPro2MsgResolver Recycle->TPro2MsgResolver
- //引用应为 Acquire 7次 Recycle 9次
- public static BytesWrapper Acquire(byte[] buffer, int offset, int size)
- {
- if (bytesWrappers.TryDequeue(out var bw))
- {
- bw.buffer = buffer;
- bw.offset = offset;
- bw.size = size;
- return bw;
- }
- else
- return new BytesWrapper(buffer, offset, size);
- }
- public static void Recycle(BytesWrapper bw)
- {
- if (bw != null && bytesWrappers.Count < MAX_BUFFER_COUNT)
- {
- bytesWrappers.Enqueue(bw);
- }
- }
- public static void Dispose()
- {
- while (bytesWrappers.TryDequeue(out _)) { }
- }
- }
- }
|