Mirror Networking
Mirror.NetworkWriter Class Reference

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

Inheritance diagram for Mirror.NetworkWriter:
Mirror.NetworkWriterPooled Mirror.PooledNetworkWriter

Public Member Functions

void Reset ()
 Reset both the position and length of the stream More...
 
byte[] ToArray ()
 Copies buffer until 'Position' to a new array. More...
 
ArraySegment< byte > ToArraySegment ()
 Returns allocation-free ArraySegment until 'Position'. More...
 
void WriteByte (byte value)
 
void WriteBytes (byte[] buffer, int offset, int count)
 
void Write< T > (T value)
 Writes any type that mirror supports. Uses weaver populated Writer(T).write. More...
 

Public Attributes

int Position
 Next position to write to the buffer More...
 

Static Public Attributes

const int MaxStringLength = 1024 * 32
 

Detailed Description

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

Definition at line 9 of file NetworkWriter.cs.

Member Function Documentation

◆ Reset()

void Mirror.NetworkWriter.Reset ( )

Reset both the position and length of the stream

Definition at line 25 of file NetworkWriter.cs.

26 {
27 Position = 0;
28 }
int Position
Next position to write to the buffer

◆ ToArray()

byte[] Mirror.NetworkWriter.ToArray ( )

Copies buffer until 'Position' to a new array.

Definition at line 45 of file NetworkWriter.cs.

46 {
47 byte[] data = new byte[Position];
48 Array.ConstrainedCopy(buffer, 0, data, 0, Position);
49 return data;
50 }

◆ ToArraySegment()

ArraySegment< byte > Mirror.NetworkWriter.ToArraySegment ( )

Returns allocation-free ArraySegment until 'Position'.

Definition at line 54 of file NetworkWriter.cs.

55 {
56 return new ArraySegment<byte>(buffer, 0, Position);
57 }

◆ Write< T >()

void Mirror.NetworkWriter.Write< T > ( value)

Writes any type that mirror supports. Uses weaver populated Writer(T).write.

Definition at line 184 of file NetworkWriter.cs.

185 {
186 Action<NetworkWriter, T> writeDelegate = Writer<T>.write;
187 if (writeDelegate == null)
188 {
189 Debug.LogError($"No writer found for {typeof(T)}. This happens either if you are missing a NetworkWriter extension for your custom type, or if weaving failed. Try to reimport a script to weave again.");
190 }
191 else
192 {
193 writeDelegate(this, value);
194 }
195 }

◆ WriteBytes()

void Mirror.NetworkWriter.WriteBytes ( byte[]  buffer,
int  offset,
int  count 
)

Definition at line 175 of file NetworkWriter.cs.

176 {
177 EnsureCapacity(Position + count);
178 Array.ConstrainedCopy(buffer, offset, this.buffer, Position, count);
179 Position += count;
180 }

Member Data Documentation

◆ MaxStringLength

const int Mirror.NetworkWriter.MaxStringLength = 1024 * 32
static

Definition at line 11 of file NetworkWriter.cs.

◆ Position

int Mirror.NetworkWriter.Position

Next position to write to the buffer

Definition at line 19 of file NetworkWriter.cs.