Mirror Networking
NetworkReaderPool.cs
1// API consistent with Microsoft's ObjectPool<T>.
2using System;
3using System.Runtime.CompilerServices;
4
5namespace Mirror
6{
8 public static class NetworkReaderPool
9 {
10 // reuse Pool<T>
11 // we still wrap it in NetworkReaderPool.Get/Recyle so we can reset the
12 // position and array before reusing.
14 // byte[] will be assigned in GetReader
15 () => new NetworkReaderPooled(new byte[]{}),
16 // initial capacity to avoid allocations in the first few frames
17 1000
18 );
19
20 // DEPRECATED 2022-03-10
21 [Obsolete("GetReader() was renamed to Get()")]
22 public static NetworkReaderPooled GetReader(byte[] bytes) => Get(bytes);
23
25 [MethodImpl(MethodImplOptions.AggressiveInlining)]
26 public static NetworkReaderPooled Get(byte[] bytes)
27 {
28 // grab from pool & set buffer
29 NetworkReaderPooled reader = Pool.Get();
30 reader.SetBuffer(bytes);
31 return reader;
32 }
33
34 // DEPRECATED 2022-03-10
35 [Obsolete("GetReader() was renamed to Get()")]
36 public static NetworkReaderPooled GetReader(ArraySegment<byte> segment) => Get(segment);
37
39 [MethodImpl(MethodImplOptions.AggressiveInlining)]
40 public static NetworkReaderPooled Get(ArraySegment<byte> segment)
41 {
42 // grab from pool & set buffer
43 NetworkReaderPooled reader = Pool.Get();
44 reader.SetBuffer(segment);
45 return reader;
46 }
47
48 // DEPRECATED 2022-03-10
49 [Obsolete("Recycle() was renamed to Return()")]
50 public static void Recycle(NetworkReaderPooled reader) => Return(reader);
51
53 [MethodImpl(MethodImplOptions.AggressiveInlining)]
54 public static void Return(NetworkReaderPooled reader)
55 {
56 Pool.Return(reader);
57 }
58 }
59}
Pool of NetworkReaders to avoid allocations.
static NetworkReaderPooled Get(byte[] bytes)
Get the next reader in the pool. If pool is empty, creates a new Reader
static void Return(NetworkReaderPooled reader)
Returns a reader to the pool.
static NetworkReaderPooled Get(ArraySegment< byte > segment)
Get the next reader in the pool. If pool is empty, creates a new Reader
Pooled NetworkReader, automatically returned to pool when using 'using'