NetworkReader.cs 11 KB

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