-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPacketPool.cs
More file actions
38 lines (34 loc) · 1015 Bytes
/
Copy pathPacketPool.cs
File metadata and controls
38 lines (34 loc) · 1015 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
using System;
using System.Collections.Concurrent;
namespace RakNet;
/// <summary>
/// Object pool for Packet instances, mirroring Go's sync.Pool. Reduces GC
/// pressure by reusing Packet objects that encapsulate content buffers.
/// Thread-safe.
/// </summary>
internal static class PacketPool
{
private static readonly ConcurrentBag<Packet> _pool = new();
public static Packet Get()
{
if (_pool.TryTake(out var pk))
return pk;
return new Packet { Reliability = Reliability.ReliableOrdered };
}
/// <summary>
/// Returns a packet to the pool. The packet's content is cleared to avoid
/// retaining references to large buffers.
/// </summary>
public static void Put(Packet pk)
{
pk.Content = Array.Empty<byte>();
pk.Split = false;
pk.SplitCount = 0;
pk.SplitIndex = 0;
pk.SplitId = 0;
pk.MessageIndex = 0;
pk.SequenceIndex = 0;
pk.OrderIndex = 0;
_pool.Add(pk);
}
}