3using System.Runtime.CompilerServices;
4using Unity.Collections.LowLevel.Unsafe;
18 ArraySegment<byte> buffer;
28 [MethodImpl(MethodImplOptions.AggressiveInlining)]
35 [MethodImpl(MethodImplOptions.AggressiveInlining)]
41 buffer =
new ArraySegment<byte>(bytes);
44 public NetworkReader(ArraySegment<byte> segment)
51 [MethodImpl(MethodImplOptions.AggressiveInlining)]
52 public void SetBuffer(
byte[] bytes)
54 buffer =
new ArraySegment<byte>(bytes);
58 [MethodImpl(MethodImplOptions.AggressiveInlining)]
59 public void SetBuffer(ArraySegment<byte> segment)
94 [MethodImpl(MethodImplOptions.AggressiveInlining)]
95 internal unsafe T ReadBlittable<T>()
100 if (!UnsafeUtility.IsBlittable(typeof(T)))
102 throw new ArgumentException($
"{typeof(T)} is not blittable!");
112 int size =
sizeof(T);
117 throw new EndOfStreamException($
"ReadBlittable<{typeof(T)}> out of range: {ToString()}");
122 fixed (
byte* ptr = &buffer.Array[buffer.Offset +
Position])
138 T* valueBuffer = stackalloc T[1];
139 UnsafeUtility.MemCpy(valueBuffer, ptr, size);
140 value = valueBuffer[0];
152 [MethodImpl(MethodImplOptions.AggressiveInlining)]
153 internal T? ReadBlittableNullable<T>()
154 where T : unmanaged =>
155 ReadByte() != 0 ? ReadBlittable<T>() : default(T?);
157 public
byte ReadByte() => ReadBlittable<
byte>();
164 if (count > bytes.Length)
166 throw new EndOfStreamException($
"ReadBytes can't read {count} + bytes because the passed byte[] only has length {bytes.Length}");
169 if (
Position + count > buffer.Count)
171 throw new EndOfStreamException($
"ReadBytesSegment can't read {count} bytes because it would read past the end of the stream. {ToString()}");
174 Array.Copy(buffer.Array, buffer.Offset +
Position, bytes, 0, count);
183 if (
Position + count > buffer.Count)
185 throw new EndOfStreamException($
"ReadBytesSegment can't read {count} bytes because it would read past the end of the stream. {ToString()}");
189 ArraySegment<byte> result =
new ArraySegment<byte>(buffer.Array, buffer.Offset +
Position, count);
194 [MethodImpl(MethodImplOptions.AggressiveInlining)]
195 public override string ToString() =>
196 $
"NetworkReader pos={Position} len={Length} buffer={BitConverter.ToString(buffer.Array, buffer.Offset, buffer.Count)}";
199 [MethodImpl(MethodImplOptions.AggressiveInlining)]
202 Func<NetworkReader, T> readerDelegate =
Reader<T>.read;
203 if (readerDelegate ==
null)
205 Debug.LogError($
"No reader found for {typeof(T)}. Use a type supported by Mirror or define a custom reader");
208 return readerDelegate(
this);
217 public static Func<NetworkReader, T> read;
Network Reader for most simple types like floats, ints, buffers, structs, etc. Use NetworkReaderPool....
int Length
Total number of bytes to read from buffer
byte[] ReadBytes(byte[] bytes, int count)
Read 'count' bytes into the bytes array
int Position
Next position to read from the buffer
T Read< T >()
Reads any data type that mirror supports. Uses weaver populated Reader(T).read
ArraySegment< byte > ReadBytesSegment(int count)
Read 'count' bytes allocation-free as ArraySegment that points to the internal array.
int Remaining
Remaining bytes that can be read, for convenience.
Helper class that weaver populates with all reader types.