Mirror Networking
Mirror.NetworkReader Class Reference

Network Reader for most simple types like floats, ints, buffers, structs, etc. Use NetworkReaderPool.GetReader() to avoid allocations. More...

Inheritance diagram for Mirror.NetworkReader:
Mirror.NetworkReaderPooled Mirror.PooledNetworkReader

Public Member Functions

 NetworkReader (byte[] bytes)
 
 NetworkReader (ArraySegment< byte > segment)
 
void SetBuffer (byte[] bytes)
 
void SetBuffer (ArraySegment< byte > segment)
 
byte ReadByte ()
 
byte[] ReadBytes (byte[] bytes, int count)
 Read 'count' bytes into the bytes array More...
 
ArraySegment< byte > ReadBytesSegment (int count)
 Read 'count' bytes allocation-free as ArraySegment that points to the internal array. More...
 
override string ToString ()
 
Read< T > ()
 Reads any data type that mirror supports. Uses weaver populated Reader(T).read More...
 

Public Attributes

int Position
 Next position to read from the buffer More...
 

Properties

int Length [get]
 Total number of bytes to read from buffer More...
 
int Remaining [get]
 Remaining bytes that can be read, for convenience. More...
 

Detailed Description

Network Reader for most simple types like floats, ints, buffers, structs, etc. Use NetworkReaderPool.GetReader() to avoid allocations.

Definition at line 13 of file NetworkReader.cs.

Constructor & Destructor Documentation

◆ NetworkReader() [1/2]

Mirror.NetworkReader.NetworkReader ( byte[]  bytes)

Definition at line 39 of file NetworkReader.cs.

40 {
41 buffer = new ArraySegment<byte>(bytes);
42 }

◆ NetworkReader() [2/2]

Mirror.NetworkReader.NetworkReader ( ArraySegment< byte >  segment)

Definition at line 44 of file NetworkReader.cs.

45 {
46 buffer = segment;
47 }

Member Function Documentation

◆ Read< T >()

T Mirror.NetworkReader.Read< T > ( )

Reads any data type that mirror supports. Uses weaver populated Reader(T).read

Definition at line 200 of file NetworkReader.cs.

201 {
202 Func<NetworkReader, T> readerDelegate = Reader<T>.read;
203 if (readerDelegate == null)
204 {
205 Debug.LogError($"No reader found for {typeof(T)}. Use a type supported by Mirror or define a custom reader");
206 return default;
207 }
208 return readerDelegate(this);
209 }

◆ ReadBytes()

byte[] Mirror.NetworkReader.ReadBytes ( byte[]  bytes,
int  count 
)

Read 'count' bytes into the bytes array

Definition at line 161 of file NetworkReader.cs.

162 {
163 // check if passed byte array is big enough
164 if (count > bytes.Length)
165 {
166 throw new EndOfStreamException($"ReadBytes can't read {count} + bytes because the passed byte[] only has length {bytes.Length}");
167 }
168 // check if within buffer limits
169 if (Position + count > buffer.Count)
170 {
171 throw new EndOfStreamException($"ReadBytesSegment can't read {count} bytes because it would read past the end of the stream. {ToString()}");
172 }
173
174 Array.Copy(buffer.Array, buffer.Offset + Position, bytes, 0, count);
175 Position += count;
176 return bytes;
177 }
int Position
Next position to read from the buffer

◆ ReadBytesSegment()

ArraySegment< byte > Mirror.NetworkReader.ReadBytesSegment ( int  count)

Read 'count' bytes allocation-free as ArraySegment that points to the internal array.

Definition at line 180 of file NetworkReader.cs.

181 {
182 // check if within buffer limits
183 if (Position + count > buffer.Count)
184 {
185 throw new EndOfStreamException($"ReadBytesSegment can't read {count} bytes because it would read past the end of the stream. {ToString()}");
186 }
187
188 // return the segment
189 ArraySegment<byte> result = new ArraySegment<byte>(buffer.Array, buffer.Offset + Position, count);
190 Position += count;
191 return result;
192 }

◆ SetBuffer() [1/2]

void Mirror.NetworkReader.SetBuffer ( ArraySegment< byte >  segment)

Definition at line 59 of file NetworkReader.cs.

60 {
61 buffer = segment;
62 Position = 0;
63 }

◆ SetBuffer() [2/2]

void Mirror.NetworkReader.SetBuffer ( byte[]  bytes)

Definition at line 52 of file NetworkReader.cs.

53 {
54 buffer = new ArraySegment<byte>(bytes);
55 Position = 0;
56 }

Member Data Documentation

◆ Position

int Mirror.NetworkReader.Position

Next position to read from the buffer

Definition at line 23 of file NetworkReader.cs.

Property Documentation

◆ Length

int Mirror.NetworkReader.Length
get

Total number of bytes to read from buffer

Definition at line 26 of file NetworkReader.cs.

27 {
28 [MethodImpl(MethodImplOptions.AggressiveInlining)]
29 get => buffer.Count;
30 }

◆ Remaining

int Mirror.NetworkReader.Remaining
get

Remaining bytes that can be read, for convenience.

Definition at line 33 of file NetworkReader.cs.

34 {
35 [MethodImpl(MethodImplOptions.AggressiveInlining)]
36 get => Length - Position;
37 }
int Length
Total number of bytes to read from buffer