Mirror Networking
Mirror.Batcher Class Reference

Public Member Functions

 Batcher (int threshold)
 
void AddMessage (ArraySegment< byte > message, double timeStamp)
 
bool GetBatch (NetworkWriter writer)
 

Static Public Attributes

const int HeaderSize = sizeof(double)
 

Detailed Description

Definition at line 16 of file Batcher.cs.

Constructor & Destructor Documentation

◆ Batcher()

Mirror.Batcher.Batcher ( int  threshold)

Definition at line 46 of file Batcher.cs.

47 {
48 this.threshold = threshold;
49 }

Member Function Documentation

◆ AddMessage()

void Mirror.Batcher.AddMessage ( ArraySegment< byte >  message,
double  timeStamp 
)

Definition at line 54 of file Batcher.cs.

55 {
56 // when appending to a batch in progress, check final size.
57 // if it expands beyond threshold, then we should finalize it first.
58 // => less than or exactly threshold is fine.
59 // GetBatch() will finalize it.
60 // => see unit tests.
61 if (batch != null &&
62 batch.Position + message.Count > threshold)
63 {
64 batches.Enqueue(batch);
65 batch = null;
66 }
67
68 // initialize a new batch if necessary
69 if (batch == null)
70 {
71 // borrow from pool. we return it in GetBatch.
72 batch = NetworkWriterPool.Get();
73
74 // write timestamp first.
75 // -> double precision for accuracy over long periods of time
76 // -> batches are per-frame, it doesn't matter which message's
77 // timestamp we use.
78 batch.WriteDouble(timeStamp);
79 }
80
81 // add serialization to current batch. even if > threshold.
82 // -> we do allow > threshold sized messages as single batch
83 // -> WriteBytes instead of WriteSegment because the latter
84 // would add a size header. we want to write directly.
85 batch.WriteBytes(message.Array, message.Offset, message.Count);
86 }
int Position
Next position to write to the buffer

◆ GetBatch()

bool Mirror.Batcher.GetBatch ( NetworkWriter  writer)

Definition at line 106 of file Batcher.cs.

107 {
108 // get first batch from queue (if any)
109 if (batches.TryDequeue(out NetworkWriterPooled first))
110 {
111 CopyAndReturn(first, writer);
112 return true;
113 }
114
115 // if queue was empty, we can send the batch in progress.
116 if (batch != null)
117 {
118 CopyAndReturn(batch, writer);
119 batch = null;
120 return true;
121 }
122
123 // nothing was written
124 return false;
125 }

Member Data Documentation

◆ HeaderSize

const int Mirror.Batcher.HeaderSize = sizeof(double)
static

Definition at line 33 of file Batcher.cs.