Mirror Networking
Mirror.Unbatcher Class Reference

Public Member Functions

bool AddBatch (ArraySegment< byte > batch)
 
bool GetNextMessage (out NetworkReader message, out double remoteTimeStamp)
 

Properties

int BatchesCount [get]
 

Detailed Description

Definition at line 13 of file Unbatcher.cs.

Member Function Documentation

◆ AddBatch()

bool Mirror.Unbatcher.AddBatch ( ArraySegment< byte >  batch)

Definition at line 43 of file Unbatcher.cs.

44 {
45 // IMPORTANT: ArraySegment is only valid until returning. we copy it!
46 //
47 // NOTE: it's not possible to create empty ArraySegments, so we
48 // don't need to check against that.
49
50 // make sure we have at least 8 bytes to read for tick timestamp
51 if (batch.Count < Batcher.HeaderSize)
52 return false;
53
54 // put into a (pooled) writer
55 // -> WriteBytes instead of WriteSegment because the latter
56 // would add a size header. we want to write directly.
57 // -> will be returned to pool when sending!
58 NetworkWriterPooled writer = NetworkWriterPool.Get();
59 writer.WriteBytes(batch.Array, batch.Offset, batch.Count);
60
61 // first batch? then point reader there
62 if (batches.Count == 0)
63 StartReadingBatch(writer);
64
65 // add batch
66 batches.Enqueue(writer);
67 //Debug.Log($"Adding Batch {BitConverter.ToString(batch.Array, batch.Offset, batch.Count)} => batches={batches.Count} reader={reader}");
68 return true;
69 }

◆ GetNextMessage()

bool Mirror.Unbatcher.GetNextMessage ( out NetworkReader  message,
out double  remoteTimeStamp 
)

Definition at line 73 of file Unbatcher.cs.

74 {
75 // getting messages would be easy via
76 // <<size, message, size, message, ...>>
77 // but to save A LOT of bandwidth, we use
78 // <<message, message, ...>
79 // in other words, we don't know where the current message ends
80 //
81 // BUT: it doesn't matter!
82 // -> we simply return the reader
83 // * if we have one yet
84 // * and if there's more to read
85 // -> the caller can then read one message from it
86 // -> when the end is reached, we retire the batch!
87 //
88 // for example:
89 // while (GetNextMessage(out message))
90 // ProcessMessage(message);
91 //
92 message = null;
93
94 // do nothing if we don't have any batches.
95 // otherwise the below queue.Dequeue() would throw an
96 // InvalidOperationException if operating on empty queue.
97 if (batches.Count == 0)
98 {
99 remoteTimeStamp = 0;
100 return false;
101 }
102
103 // was our reader pointed to anything yet?
104 if (reader.Length == 0)
105 {
106 remoteTimeStamp = 0;
107 return false;
108 }
109
110 // no more data to read?
111 if (reader.Remaining == 0)
112 {
113 // retire the batch
114 NetworkWriterPooled writer = batches.Dequeue();
115 NetworkWriterPool.Return(writer);
116
117 // do we have another batch?
118 if (batches.Count > 0)
119 {
120 // point reader to the next batch.
121 // we'll return the reader below.
122 NetworkWriterPooled next = batches.Peek();
123 StartReadingBatch(next);
124 }
125 // otherwise there's nothing more to read
126 else
127 {
128 remoteTimeStamp = 0;
129 return false;
130 }
131 }
132
133 // use the current batch's remote timestamp
134 // AFTER potentially moving to the next batch ABOVE!
135 remoteTimeStamp = readerRemoteTimeStamp;
136
137 // if we got here, then we have more data to read.
138 message = reader;
139 return true;
140 }
int Length
Total number of bytes to read from buffer
int Remaining
Remaining bytes that can be read, for convenience.

Property Documentation

◆ BatchesCount

int Mirror.Unbatcher.BatchesCount
get

Definition at line 19 of file Unbatcher.cs.