12using System.Collections.Generic;
30 readonly
int threshold;
33 public const int HeaderSize =
sizeof(double);
41 Queue<NetworkWriterPooled> batches =
new Queue<NetworkWriterPooled>();
48 this.threshold = threshold;
54 public void AddMessage(ArraySegment<byte> message,
double timeStamp)
62 batch.
Position + message.Count > threshold)
64 batches.Enqueue(batch);
78 batch.WriteDouble(timeStamp);
85 batch.WriteBytes(message.Array, message.Offset, message.Count);
93 throw new ArgumentException($
"GetBatch needs a fresh writer!");
97 writer.WriteBytes(segment.Array, segment.Offset, segment.Count);
111 CopyAndReturn(first, writer);
118 CopyAndReturn(batch, writer);
Network Writer for most simple types like floats, ints, buffers, structs, etc. Use NetworkWriterPool....
ArraySegment< byte > ToArraySegment()
Returns allocation-free ArraySegment until 'Position'.
int Position
Next position to write to the buffer
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'