Mirror Networking
NetworkWriterPool.cs
1// API consistent with Microsoft's ObjectPool<T>.
2using System;
3using System.Runtime.CompilerServices;
4
5namespace Mirror
6{
8 public static class NetworkWriterPool
9 {
10 // reuse Pool<T>
11 // we still wrap it in NetworkWriterPool.Get/Recycle so we can reset the
12 // position before reusing.
13 // this is also more consistent with NetworkReaderPool where we need to
14 // assign the internal buffer before reusing.
16 () => new NetworkWriterPooled(),
17 // initial capacity to avoid allocations in the first few frames
18 // 1000 * 1200 bytes = around 1 MB.
19 1000
20 );
21
22 // DEPRECATED 2022-03-10
23 [Obsolete("GetWriter() was renamed to Get()")]
24 public static NetworkWriterPooled GetWriter() => Get();
25
27 [MethodImpl(MethodImplOptions.AggressiveInlining)]
28 public static NetworkWriterPooled Get()
29 {
30 // grab from pool & reset position
31 NetworkWriterPooled writer = Pool.Get();
32 writer.Reset();
33 return writer;
34 }
35
36 // DEPRECATED 2022-03-10
37 [Obsolete("Recycle() was renamed to Return()")]
38 public static void Recycle(NetworkWriterPooled writer) => Return(writer);
39
41 [MethodImpl(MethodImplOptions.AggressiveInlining)]
42 public static void Return(NetworkWriterPooled writer)
43 {
44 Pool.Return(writer);
45 }
46 }
47}
void Reset()
Reset both the position and length of the stream
Pool of NetworkWriters to avoid allocations.
static void Return(NetworkWriterPooled writer)
Return a writer to the pool.
static NetworkWriterPooled Get()
Get a writer from the pool. Creates new one if pool is empty.
Pooled NetworkWriter, automatically returned to pool when using 'using'