NetworkReader.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. using System;
  2. using System.IO;
  3. using System.Runtime.CompilerServices;
  4. using System.Text;
  5. using Unity.Collections.LowLevel.Unsafe;
  6. using UnityEngine;
  7. namespace Mirror
  8. {
  9. /// <summary>Network Reader for most simple types like floats, ints, buffers, structs, etc. Use NetworkReaderPool.GetReader() to avoid allocations.</summary>
  10. // Note: This class is intended to be extremely pedantic,
  11. // and throw exceptions whenever stuff is going slightly wrong.
  12. // The exceptions will be handled in NetworkServer/NetworkClient.
  13. //
  14. // Note that NetworkWriter can be passed in constructor thanks to implicit
  15. // ArraySegment conversion:
  16. // NetworkReader reader = new NetworkReader(writer);
  17. public class NetworkReader
  18. {
  19. // internal buffer
  20. // byte[] pointer would work, but we use ArraySegment to also support
  21. // the ArraySegment constructor
  22. internal ArraySegment<byte> buffer;
  23. /// <summary>Next position to read from the buffer</summary>
  24. // 'int' is the best type for .Position. 'short' is too small if we send >32kb which would result in negative .Position
  25. // -> converting long to int is fine until 2GB of data (MAX_INT), so we don't have to worry about overflows here
  26. public int Position;
  27. /// <summary>Remaining bytes that can be read, for convenience.</summary>
  28. public int Remaining => buffer.Count - Position;
  29. /// <summary>Total buffer capacity, independent of reader position.</summary>
  30. public int Capacity => buffer.Count;
  31. [Obsolete("NetworkReader.Length was renamed to Capacity")] // 2022-09-25
  32. public int Length => Capacity;
  33. // cache encoding for ReadString instead of creating it with each time
  34. // 1000 readers before: 1MB GC, 30ms
  35. // 1000 readers after: 0.8MB GC, 18ms
  36. // member(!) to avoid static state.
  37. //
  38. // throwOnInvalidBytes is true.
  39. // if false, it would silently ignore the invalid bytes but continue
  40. // with the valid ones, creating strings like "a�������".
  41. // instead, we want to catch it manually and return String.Empty.
  42. // this is safer. see test: ReadString_InvalidUTF8().
  43. internal readonly UTF8Encoding encoding = new UTF8Encoding(false, true);
  44. public NetworkReader(ArraySegment<byte> segment)
  45. {
  46. buffer = segment;
  47. }
  48. #if !UNITY_2021_3_OR_NEWER
  49. // Unity 2019 doesn't have the implicit byte[] to segment conversion yet
  50. public NetworkReader(byte[] bytes)
  51. {
  52. buffer = new ArraySegment<byte>(bytes, 0, bytes.Length);
  53. }
  54. #endif
  55. // sometimes it's useful to point a reader on another buffer instead of
  56. // allocating a new reader (e.g. NetworkReaderPool)
  57. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  58. public void SetBuffer(ArraySegment<byte> segment)
  59. {
  60. buffer = segment;
  61. Position = 0;
  62. }
  63. #if !UNITY_2021_3_OR_NEWER
  64. // Unity 2019 doesn't have the implicit byte[] to segment conversion yet
  65. public void SetBuffer(byte[] bytes)
  66. {
  67. buffer = new ArraySegment<byte>(bytes, 0, bytes.Length);
  68. Position = 0;
  69. }
  70. #endif
  71. // ReadBlittable<T> from DOTSNET
  72. // this is extremely fast, but only works for blittable types.
  73. // => private to make sure nobody accidentally uses it for non-blittable
  74. //
  75. // Benchmark: see NetworkWriter.WriteBlittable!
  76. //
  77. // Note:
  78. // ReadBlittable assumes same endianness for server & client.
  79. // All Unity 2018+ platforms are little endian.
  80. //
  81. // This is not safe to expose to random structs.
  82. // * StructLayout.Sequential is the default, which is safe.
  83. // if the struct contains a reference type, it is converted to Auto.
  84. // but since all structs here are unmanaged blittable, it's safe.
  85. // see also: https://docs.microsoft.com/en-us/dotnet/api/system.runtime.interopservices.layoutkind?view=netframework-4.8#system-runtime-interopservices-layoutkind-sequential
  86. // * StructLayout.Pack depends on CPU word size.
  87. // this may be different 4 or 8 on some ARM systems, etc.
  88. // this is not safe, and would cause bytes/shorts etc. to be padded.
  89. // see also: https://docs.microsoft.com/en-us/dotnet/api/system.runtime.interopservices.structlayoutattribute.pack?view=net-6.0
  90. // * If we force pack all to '1', they would have no padding which is
  91. // great for bandwidth. but on some android systems, CPU can't read
  92. // unaligned memory.
  93. // see also: https://github.com/vis2k/Mirror/issues/3044
  94. // * The only option would be to force explicit layout with multiples
  95. // of word size. but this requires lots of weaver checking and is
  96. // still questionable (IL2CPP etc.).
  97. //
  98. // Note: inlining ReadBlittable is enough. don't inline ReadInt etc.
  99. // we don't want ReadBlittable to be copied in place everywhere.
  100. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  101. internal unsafe T ReadBlittable<T>()
  102. where T : unmanaged
  103. {
  104. // check if blittable for safety
  105. #if UNITY_EDITOR
  106. if (!UnsafeUtility.IsBlittable(typeof(T)))
  107. {
  108. throw new ArgumentException($"{typeof(T)} is not blittable!");
  109. }
  110. #endif
  111. // calculate size
  112. // sizeof(T) gets the managed size at compile time.
  113. // Marshal.SizeOf<T> gets the unmanaged size at runtime (slow).
  114. // => our 1mio writes benchmark is 6x slower with Marshal.SizeOf<T>
  115. // => for blittable types, sizeof(T) is even recommended:
  116. // https://docs.microsoft.com/en-us/dotnet/standard/native-interop/best-practices
  117. int size = sizeof(T);
  118. // ensure remaining
  119. if (Remaining < size)
  120. {
  121. throw new EndOfStreamException($"ReadBlittable<{typeof(T)}> not enough data in buffer to read {size} bytes: {ToString()}");
  122. }
  123. // read blittable
  124. T value;
  125. fixed (byte* ptr = &buffer.Array[buffer.Offset + Position])
  126. {
  127. #if UNITY_ANDROID
  128. // on some android systems, reading *(T*)ptr throws a NRE if
  129. // the ptr isn't aligned (i.e. if Position is 1,2,3,5, etc.).
  130. // here we have to use memcpy.
  131. //
  132. // => we can't get a pointer of a struct in C# without
  133. // marshalling allocations
  134. // => instead, we stack allocate an array of type T and use that
  135. // => stackalloc avoids GC and is very fast. it only works for
  136. // value types, but all blittable types are anyway.
  137. //
  138. // this way, we can still support blittable reads on android.
  139. // see also: https://github.com/vis2k/Mirror/issues/3044
  140. // (solution discovered by AIIO, FakeByte, mischa)
  141. T* valueBuffer = stackalloc T[1];
  142. UnsafeUtility.MemCpy(valueBuffer, ptr, size);
  143. value = valueBuffer[0];
  144. #else
  145. // cast buffer to a T* pointer and then read from it.
  146. value = *(T*)ptr;
  147. #endif
  148. }
  149. Position += size;
  150. return value;
  151. }
  152. // blittable'?' template for code reuse
  153. // note: bool isn't blittable. need to read as byte.
  154. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  155. internal T? ReadBlittableNullable<T>()
  156. where T : unmanaged =>
  157. ReadByte() != 0 ? ReadBlittable<T>() : default(T?);
  158. public byte ReadByte() => ReadBlittable<byte>();
  159. /// <summary>Read 'count' bytes into the bytes array</summary>
  160. // NOTE: returns byte[] because all reader functions return something.
  161. public byte[] ReadBytes(byte[] bytes, int count)
  162. {
  163. // user may call ReadBytes(ReadInt()). ensure positive count.
  164. if (count < 0) throw new ArgumentOutOfRangeException("ReadBytes requires count >= 0");
  165. // check if passed byte array is big enough
  166. if (count > bytes.Length)
  167. {
  168. throw new EndOfStreamException($"ReadBytes can't read {count} + bytes because the passed byte[] only has length {bytes.Length}");
  169. }
  170. // ensure remaining
  171. if (Remaining < count)
  172. {
  173. throw new EndOfStreamException($"ReadBytesSegment can't read {count} bytes because it would read past the end of the stream. {ToString()}");
  174. }
  175. Array.Copy(buffer.Array, buffer.Offset + Position, bytes, 0, count);
  176. Position += count;
  177. return bytes;
  178. }
  179. /// <summary>Read 'count' bytes allocation-free as ArraySegment that points to the internal array.</summary>
  180. public ArraySegment<byte> ReadBytesSegment(int count)
  181. {
  182. // user may call ReadBytes(ReadInt()). ensure positive count.
  183. if (count < 0) throw new ArgumentOutOfRangeException("ReadBytesSegment requires count >= 0");
  184. // ensure remaining
  185. if (Remaining < count)
  186. {
  187. throw new EndOfStreamException($"ReadBytesSegment can't read {count} bytes because it would read past the end of the stream. {ToString()}");
  188. }
  189. // return the segment
  190. ArraySegment<byte> result = new ArraySegment<byte>(buffer.Array, buffer.Offset + Position, count);
  191. Position += count;
  192. return result;
  193. }
  194. /// <summary>Reads any data type that mirror supports. Uses weaver populated Reader(T).read</summary>
  195. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  196. public T Read<T>()
  197. {
  198. Func<NetworkReader, T> readerDelegate = Reader<T>.read;
  199. if (readerDelegate == null)
  200. {
  201. Debug.LogError($"No reader found for {typeof(T)}. Use a type supported by Mirror or define a custom reader extension for {typeof(T)}.");
  202. return default;
  203. }
  204. return readerDelegate(this);
  205. }
  206. // print the full buffer with position / capacity.
  207. public override string ToString() =>
  208. $"[{buffer.ToHexString()} @ {Position}/{Capacity}]";
  209. }
  210. /// <summary>Helper class that weaver populates with all reader types.</summary>
  211. // Note that c# creates a different static variable for each type
  212. // -> Weaver.ReaderWriterProcessor.InitializeReaderAndWriters() populates it
  213. public static class Reader<T>
  214. {
  215. public static Func<NetworkReader, T> read;
  216. }
  217. }