NetworkWriter.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. using System;
  2. using System.Runtime.CompilerServices;
  3. using System.Text;
  4. using Unity.Collections.LowLevel.Unsafe;
  5. using UnityEngine;
  6. namespace Mirror
  7. {
  8. /// <summary>Network Writer for most simple types like floats, ints, buffers, structs, etc. Use NetworkWriterPool.GetReader() to avoid allocations.</summary>
  9. public class NetworkWriter
  10. {
  11. public const int MaxStringLength = 1024 * 32;
  12. public const int DefaultCapacity = 1500;
  13. // create writer immediately with it's own buffer so no one can mess with it and so that we can resize it.
  14. // note: BinaryWriter allocates too much, so we only use a MemoryStream
  15. // => 1500 bytes by default because on average, most packets will be <= MTU
  16. internal byte[] buffer = new byte[DefaultCapacity];
  17. /// <summary>Next position to write to the buffer</summary>
  18. public int Position;
  19. /// <summary>Current capacity. Automatically resized if necessary.</summary>
  20. public int Capacity => buffer.Length;
  21. // cache encoding for WriteString instead of creating it each time.
  22. // 1000 readers before: 1MB GC, 30ms
  23. // 1000 readers after: 0.8MB GC, 18ms
  24. // not(!) static for thread safety.
  25. //
  26. // throwOnInvalidBytes is true.
  27. // writer should throw and user should fix if this ever happens.
  28. // unlike reader, which needs to expect it to happen from attackers.
  29. internal readonly UTF8Encoding encoding = new UTF8Encoding(false, true);
  30. /// <summary>Reset both the position and length of the stream</summary>
  31. // Leaves the capacity the same so that we can reuse this writer without
  32. // extra allocations
  33. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  34. public void Reset()
  35. {
  36. Position = 0;
  37. }
  38. // NOTE that our runtime resizing comes at no extra cost because:
  39. // 1. 'has space' checks are necessary even for fixed sized writers.
  40. // 2. all writers will eventually be large enough to stop resizing.
  41. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  42. internal void EnsureCapacity(int value)
  43. {
  44. if (buffer.Length < value)
  45. {
  46. int capacity = Math.Max(value, buffer.Length * 2);
  47. Array.Resize(ref buffer, capacity);
  48. }
  49. }
  50. /// <summary>Copies buffer until 'Position' to a new array.</summary>
  51. // Try to use ToArraySegment instead to avoid allocations!
  52. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  53. public byte[] ToArray()
  54. {
  55. byte[] data = new byte[Position];
  56. Array.ConstrainedCopy(buffer, 0, data, 0, Position);
  57. return data;
  58. }
  59. /// <summary>Returns allocation-free ArraySegment until 'Position'.</summary>
  60. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  61. public ArraySegment<byte> ToArraySegment() =>
  62. new ArraySegment<byte>(buffer, 0, Position);
  63. // implicit conversion for convenience
  64. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  65. public static implicit operator ArraySegment<byte>(NetworkWriter w) =>
  66. w.ToArraySegment();
  67. // WriteBlittable<T> from DOTSNET.
  68. // this is extremely fast, but only works for blittable types.
  69. //
  70. // Benchmark:
  71. // WriteQuaternion x 100k, Macbook Pro 2015 @ 2.2Ghz, Unity 2018 LTS (debug mode)
  72. //
  73. // | Median | Min | Max | Avg | Std | (ms)
  74. // before | 30.35 | 29.86 | 48.99 | 32.54 | 4.93 |
  75. // blittable* | 5.69 | 5.52 | 27.51 | 7.78 | 5.65 |
  76. //
  77. // * without IsBlittable check
  78. // => 4-6x faster!
  79. //
  80. // WriteQuaternion x 100k, Macbook Pro 2015 @ 2.2Ghz, Unity 2020.1 (release mode)
  81. //
  82. // | Median | Min | Max | Avg | Std | (ms)
  83. // before | 9.41 | 8.90 | 23.02 | 10.72 | 3.07 |
  84. // blittable* | 1.48 | 1.40 | 16.03 | 2.60 | 2.71 |
  85. //
  86. // * without IsBlittable check
  87. // => 6x faster!
  88. //
  89. // Note:
  90. // WriteBlittable assumes same endianness for server & client.
  91. // All Unity 2018+ platforms are little endian.
  92. // => run NetworkWriterTests.BlittableOnThisPlatform() to verify!
  93. //
  94. // This is not safe to expose to random structs.
  95. // * StructLayout.Sequential is the default, which is safe.
  96. // if the struct contains a reference type, it is converted to Auto.
  97. // but since all structs here are unmanaged blittable, it's safe.
  98. // see also: https://docs.microsoft.com/en-us/dotnet/api/system.runtime.interopservices.layoutkind?view=netframework-4.8#system-runtime-interopservices-layoutkind-sequential
  99. // * StructLayout.Pack depends on CPU word size.
  100. // this may be different 4 or 8 on some ARM systems, etc.
  101. // this is not safe, and would cause bytes/shorts etc. to be padded.
  102. // see also: https://docs.microsoft.com/en-us/dotnet/api/system.runtime.interopservices.structlayoutattribute.pack?view=net-6.0
  103. // * If we force pack all to '1', they would have no padding which is
  104. // great for bandwidth. but on some android systems, CPU can't read
  105. // unaligned memory.
  106. // see also: https://github.com/vis2k/Mirror/issues/3044
  107. // * The only option would be to force explicit layout with multiples
  108. // of word size. but this requires lots of weaver checking and is
  109. // still questionable (IL2CPP etc.).
  110. //
  111. // Note: inlining WriteBlittable is enough. don't inline WriteInt etc.
  112. // we don't want WriteBlittable to be copied in place everywhere.
  113. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  114. internal unsafe void WriteBlittable<T>(T value)
  115. where T : unmanaged
  116. {
  117. // check if blittable for safety
  118. #if UNITY_EDITOR
  119. if (!UnsafeUtility.IsBlittable(typeof(T)))
  120. {
  121. Debug.LogError($"{typeof(T)} is not blittable!");
  122. return;
  123. }
  124. #endif
  125. // calculate size
  126. // sizeof(T) gets the managed size at compile time.
  127. // Marshal.SizeOf<T> gets the unmanaged size at runtime (slow).
  128. // => our 1mio writes benchmark is 6x slower with Marshal.SizeOf<T>
  129. // => for blittable types, sizeof(T) is even recommended:
  130. // https://docs.microsoft.com/en-us/dotnet/standard/native-interop/best-practices
  131. int size = sizeof(T);
  132. // ensure capacity
  133. // NOTE that our runtime resizing comes at no extra cost because:
  134. // 1. 'has space' checks are necessary even for fixed sized writers.
  135. // 2. all writers will eventually be large enough to stop resizing.
  136. EnsureCapacity(Position + size);
  137. // write blittable
  138. fixed (byte* ptr = &buffer[Position])
  139. {
  140. #if UNITY_ANDROID
  141. // on some android systems, assigning *(T*)ptr throws a NRE if
  142. // the ptr isn't aligned (i.e. if Position is 1,2,3,5, etc.).
  143. // here we have to use memcpy.
  144. //
  145. // => we can't get a pointer of a struct in C# without
  146. // marshalling allocations
  147. // => instead, we stack allocate an array of type T and use that
  148. // => stackalloc avoids GC and is very fast. it only works for
  149. // value types, but all blittable types are anyway.
  150. //
  151. // this way, we can still support blittable reads on android.
  152. // see also: https://github.com/vis2k/Mirror/issues/3044
  153. // (solution discovered by AIIO, FakeByte, mischa)
  154. T* valueBuffer = stackalloc T[1]{value};
  155. UnsafeUtility.MemCpy(ptr, valueBuffer, size);
  156. #else
  157. // cast buffer to T* pointer, then assign value to the area
  158. *(T*)ptr = value;
  159. #endif
  160. }
  161. Position += size;
  162. }
  163. // blittable'?' template for code reuse
  164. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  165. internal void WriteBlittableNullable<T>(T? value)
  166. where T : unmanaged
  167. {
  168. // bool isn't blittable. write as byte.
  169. WriteByte((byte)(value.HasValue ? 0x01 : 0x00));
  170. // only write value if exists. saves bandwidth.
  171. if (value.HasValue)
  172. WriteBlittable(value.Value);
  173. }
  174. public void WriteByte(byte value) => WriteBlittable(value);
  175. // for byte arrays with consistent size, where the reader knows how many to read
  176. // (like a packet opcode that's always the same)
  177. public void WriteBytes(byte[] array, int offset, int count)
  178. {
  179. EnsureCapacity(Position + count);
  180. Array.ConstrainedCopy(array, offset, this.buffer, Position, count);
  181. Position += count;
  182. }
  183. // write an unsafe byte* array.
  184. // useful for bit tree compression, etc.
  185. public unsafe bool WriteBytes(byte* ptr, int offset, int size)
  186. {
  187. EnsureCapacity(Position + size);
  188. fixed (byte* destination = &buffer[Position])
  189. {
  190. // write 'size' bytes at position
  191. // 10 mio writes: 868ms
  192. // Array.Copy(value.Array, value.Offset, buffer, Position, value.Count);
  193. // 10 mio writes: 775ms
  194. // Buffer.BlockCopy(value.Array, value.Offset, buffer, Position, value.Count);
  195. // 10 mio writes: 637ms
  196. UnsafeUtility.MemCpy(destination, ptr + offset, size);
  197. }
  198. Position += size;
  199. return true;
  200. }
  201. /// <summary>Writes any type that mirror supports. Uses weaver populated Writer(T).write.</summary>
  202. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  203. public void Write<T>(T value)
  204. {
  205. Action<NetworkWriter, T> writeDelegate = Writer<T>.write;
  206. if (writeDelegate == null)
  207. {
  208. 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.");
  209. }
  210. else
  211. {
  212. writeDelegate(this, value);
  213. }
  214. }
  215. // print with buffer content for easier debugging.
  216. // [content, position / capacity].
  217. // showing "position / space" would be too confusing.
  218. public override string ToString() =>
  219. $"[{ToArraySegment().ToHexString()} @ {Position}/{Capacity}]";
  220. }
  221. /// <summary>Helper class that weaver populates with all writer types.</summary>
  222. // Note that c# creates a different static variable for each type
  223. // -> Weaver.ReaderWriterProcessor.InitializeReaderAndWriters() populates it
  224. public static class Writer<T>
  225. {
  226. public static Action<NetworkWriter, T> write;
  227. }
  228. }