NetworkWriter.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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. // the limit of ushort is so we can write string size prefix as only 2 bytes.
  12. // -1 so we can still encode 'null' into it too.
  13. public const ushort MaxStringLength = ushort.MaxValue - 1;
  14. // create writer immediately with it's own buffer so no one can mess with it and so that we can resize it.
  15. // note: BinaryWriter allocates too much, so we only use a MemoryStream
  16. // => 1500 bytes by default because on average, most packets will be <= MTU
  17. public const int DefaultCapacity = 1500;
  18. internal byte[] buffer = new byte[DefaultCapacity];
  19. /// <summary>Next position to write to the buffer</summary>
  20. public int Position;
  21. /// <summary>Current capacity. Automatically resized if necessary.</summary>
  22. public int Capacity => buffer.Length;
  23. // cache encoding for WriteString instead of creating it each time.
  24. // 1000 readers before: 1MB GC, 30ms
  25. // 1000 readers after: 0.8MB GC, 18ms
  26. // not(!) static for thread safety.
  27. //
  28. // throwOnInvalidBytes is true.
  29. // writer should throw and user should fix if this ever happens.
  30. // unlike reader, which needs to expect it to happen from attackers.
  31. internal readonly UTF8Encoding encoding = new UTF8Encoding(false, true);
  32. /// <summary>Reset both the position and length of the stream</summary>
  33. // Leaves the capacity the same so that we can reuse this writer without
  34. // extra allocations
  35. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  36. public void Reset()
  37. {
  38. Position = 0;
  39. }
  40. // NOTE that our runtime resizing comes at no extra cost because:
  41. // 1. 'has space' checks are necessary even for fixed sized writers.
  42. // 2. all writers will eventually be large enough to stop resizing.
  43. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  44. internal void EnsureCapacity(int value)
  45. {
  46. if (buffer.Length < value)
  47. {
  48. int capacity = Math.Max(value, buffer.Length * 2);
  49. Array.Resize(ref buffer, capacity);
  50. }
  51. }
  52. /// <summary>Copies buffer until 'Position' to a new array.</summary>
  53. // Try to use ToArraySegment instead to avoid allocations!
  54. public byte[] ToArray()
  55. {
  56. byte[] data = new byte[Position];
  57. Array.ConstrainedCopy(buffer, 0, data, 0, Position);
  58. return data;
  59. }
  60. /// <summary>Returns allocation-free ArraySegment until 'Position'.</summary>
  61. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  62. public ArraySegment<byte> ToArraySegment() =>
  63. new ArraySegment<byte>(buffer, 0, Position);
  64. // implicit conversion for convenience
  65. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  66. public static implicit operator ArraySegment<byte>(NetworkWriter w) =>
  67. w.ToArraySegment();
  68. // WriteBlittable<T> from DOTSNET.
  69. // this is extremely fast, but only works for blittable types.
  70. //
  71. // Benchmark:
  72. // WriteQuaternion x 100k, Macbook Pro 2015 @ 2.2Ghz, Unity 2018 LTS (debug mode)
  73. //
  74. // | Median | Min | Max | Avg | Std | (ms)
  75. // before | 30.35 | 29.86 | 48.99 | 32.54 | 4.93 |
  76. // blittable* | 5.69 | 5.52 | 27.51 | 7.78 | 5.65 |
  77. //
  78. // * without IsBlittable check
  79. // => 4-6x faster!
  80. //
  81. // WriteQuaternion x 100k, Macbook Pro 2015 @ 2.2Ghz, Unity 2020.1 (release mode)
  82. //
  83. // | Median | Min | Max | Avg | Std | (ms)
  84. // before | 9.41 | 8.90 | 23.02 | 10.72 | 3.07 |
  85. // blittable* | 1.48 | 1.40 | 16.03 | 2.60 | 2.71 |
  86. //
  87. // * without IsBlittable check
  88. // => 6x faster!
  89. //
  90. // Note:
  91. // WriteBlittable assumes same endianness for server & client.
  92. // All Unity 2018+ platforms are little endian.
  93. // => run NetworkWriterTests.BlittableOnThisPlatform() to verify!
  94. //
  95. // This is not safe to expose to random structs.
  96. // * StructLayout.Sequential is the default, which is safe.
  97. // if the struct contains a reference type, it is converted to Auto.
  98. // but since all structs here are unmanaged blittable, it's safe.
  99. // see also: https://docs.microsoft.com/en-us/dotnet/api/system.runtime.interopservices.layoutkind?view=netframework-4.8#system-runtime-interopservices-layoutkind-sequential
  100. // * StructLayout.Pack depends on CPU word size.
  101. // this may be different 4 or 8 on some ARM systems, etc.
  102. // this is not safe, and would cause bytes/shorts etc. to be padded.
  103. // see also: https://docs.microsoft.com/en-us/dotnet/api/system.runtime.interopservices.structlayoutattribute.pack?view=net-6.0
  104. // * If we force pack all to '1', they would have no padding which is
  105. // great for bandwidth. but on some android systems, CPU can't read
  106. // unaligned memory.
  107. // see also: https://github.com/vis2k/Mirror/issues/3044
  108. // * The only option would be to force explicit layout with multiples
  109. // of word size. but this requires lots of weaver checking and is
  110. // still questionable (IL2CPP etc.).
  111. //
  112. // Note: inlining WriteBlittable is enough. don't inline WriteInt etc.
  113. // we don't want WriteBlittable to be copied in place everywhere.
  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. public void Write<T>(T value)
  203. {
  204. Action<NetworkWriter, T> writeDelegate = Writer<T>.write;
  205. if (writeDelegate == null)
  206. {
  207. 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.");
  208. }
  209. else
  210. {
  211. writeDelegate(this, value);
  212. }
  213. }
  214. // print with buffer content for easier debugging.
  215. // [content, position / capacity].
  216. // showing "position / space" would be too confusing.
  217. public override string ToString() =>
  218. $"[{ToArraySegment().ToHexString()} @ {Position}/{Capacity}]";
  219. }
  220. /// <summary>Helper class that weaver populates with all writer types.</summary>
  221. // Note that c# creates a different static variable for each type
  222. // -> Weaver.ReaderWriterProcessor.InitializeReaderAndWriters() populates it
  223. public static class Writer<T>
  224. {
  225. public static Action<NetworkWriter, T> write;
  226. }
  227. }