NetDataWriter.cs 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. #if DEBUG && !UNITY_WP_8_1 && !UNITY_WSA_8_1
  2. using System;
  3. using System.Text;
  4. namespace FlyingWormConsole3.LiteNetLib.Utils
  5. {
  6. public class NetDataWriter
  7. {
  8. protected byte[] _data;
  9. protected int _position;
  10. private int _maxLength;
  11. private readonly bool _autoResize;
  12. public NetDataWriter()
  13. {
  14. _maxLength = 64;
  15. _data = new byte[_maxLength];
  16. _autoResize = true;
  17. }
  18. public NetDataWriter(bool autoResize)
  19. {
  20. _maxLength = 64;
  21. _data = new byte[_maxLength];
  22. _autoResize = autoResize;
  23. }
  24. public NetDataWriter(bool autoResize, int initialSize)
  25. {
  26. _maxLength = initialSize;
  27. _data = new byte[_maxLength];
  28. _autoResize = autoResize;
  29. }
  30. public void ResizeIfNeed(int newSize)
  31. {
  32. if (_maxLength < newSize)
  33. {
  34. while (_maxLength < newSize)
  35. {
  36. _maxLength *= 2;
  37. }
  38. Array.Resize(ref _data, _maxLength);
  39. }
  40. }
  41. public void Reset(int size)
  42. {
  43. ResizeIfNeed(size);
  44. _position = 0;
  45. }
  46. public void Reset()
  47. {
  48. _position = 0;
  49. }
  50. public byte[] CopyData()
  51. {
  52. byte[] resultData = new byte[_position];
  53. Buffer.BlockCopy(_data, 0, resultData, 0, _position);
  54. return resultData;
  55. }
  56. public byte[] Data
  57. {
  58. get { return _data; }
  59. }
  60. public int Length
  61. {
  62. get { return _position; }
  63. }
  64. public void Put(float value)
  65. {
  66. if (_autoResize)
  67. ResizeIfNeed(_position + 4);
  68. FastBitConverter.GetBytes(_data, _position, value);
  69. _position += 4;
  70. }
  71. public void Put(double value)
  72. {
  73. if (_autoResize)
  74. ResizeIfNeed(_position + 8);
  75. FastBitConverter.GetBytes(_data, _position, value);
  76. _position += 8;
  77. }
  78. public void Put(long value)
  79. {
  80. if (_autoResize)
  81. ResizeIfNeed(_position + 8);
  82. FastBitConverter.GetBytes(_data, _position, value);
  83. _position += 8;
  84. }
  85. public void Put(ulong value)
  86. {
  87. if (_autoResize)
  88. ResizeIfNeed(_position + 8);
  89. FastBitConverter.GetBytes(_data, _position, value);
  90. _position += 8;
  91. }
  92. public void Put(int value)
  93. {
  94. if (_autoResize)
  95. ResizeIfNeed(_position + 4);
  96. FastBitConverter.GetBytes(_data, _position, value);
  97. _position += 4;
  98. }
  99. public void Put(uint value)
  100. {
  101. if (_autoResize)
  102. ResizeIfNeed(_position + 4);
  103. FastBitConverter.GetBytes(_data, _position, value);
  104. _position += 4;
  105. }
  106. public void Put(ushort value)
  107. {
  108. if (_autoResize)
  109. ResizeIfNeed(_position + 2);
  110. FastBitConverter.GetBytes(_data, _position, value);
  111. _position += 2;
  112. }
  113. public void Put(short value)
  114. {
  115. if (_autoResize)
  116. ResizeIfNeed(_position + 2);
  117. FastBitConverter.GetBytes(_data, _position, value);
  118. _position += 2;
  119. }
  120. public void Put(sbyte value)
  121. {
  122. if (_autoResize)
  123. ResizeIfNeed(_position + 1);
  124. _data[_position] = (byte)value;
  125. _position++;
  126. }
  127. public void Put(byte value)
  128. {
  129. if (_autoResize)
  130. ResizeIfNeed(_position + 1);
  131. _data[_position] = value;
  132. _position++;
  133. }
  134. public void Put(byte[] data, int offset, int length)
  135. {
  136. if (_autoResize)
  137. ResizeIfNeed(_position + length);
  138. Buffer.BlockCopy(data, offset, _data, _position, length);
  139. _position += length;
  140. }
  141. public void Put(byte[] data)
  142. {
  143. if (_autoResize)
  144. ResizeIfNeed(_position + data.Length);
  145. Buffer.BlockCopy(data, 0, _data, _position, data.Length);
  146. _position += data.Length;
  147. }
  148. public void PutBytesWithLength(byte[] data, int offset, int length)
  149. {
  150. if (_autoResize)
  151. ResizeIfNeed(_position + length);
  152. Put(length);
  153. Buffer.BlockCopy(data, offset, _data, _position, length);
  154. _position += length;
  155. }
  156. public void PutBytesWithLength(byte[] data)
  157. {
  158. if (_autoResize)
  159. ResizeIfNeed(_position + data.Length);
  160. Put(data.Length);
  161. Buffer.BlockCopy(data, 0, _data, _position, data.Length);
  162. _position += data.Length;
  163. }
  164. public void Put(bool value)
  165. {
  166. if (_autoResize)
  167. ResizeIfNeed(_position + 1);
  168. _data[_position] = (byte)(value ? 1 : 0);
  169. _position++;
  170. }
  171. public void PutArray(float[] value)
  172. {
  173. ushort len = value == null ? (ushort)0 : (ushort)value.Length;
  174. if (_autoResize)
  175. ResizeIfNeed(_position + len * 4 + 2);
  176. Put(len);
  177. for (int i = 0; i < len; i++)
  178. {
  179. Put(value[i]);
  180. }
  181. }
  182. public void PutArray(double[] value)
  183. {
  184. ushort len = value == null ? (ushort)0 : (ushort)value.Length;
  185. if (_autoResize)
  186. ResizeIfNeed(_position + len * 8 + 2);
  187. Put(len);
  188. for (int i = 0; i < len; i++)
  189. {
  190. Put(value[i]);
  191. }
  192. }
  193. public void PutArray(long[] value)
  194. {
  195. ushort len = value == null ? (ushort)0 : (ushort)value.Length;
  196. if (_autoResize)
  197. ResizeIfNeed(_position + len * 8 + 2);
  198. Put(len);
  199. for (int i = 0; i < len; i++)
  200. {
  201. Put(value[i]);
  202. }
  203. }
  204. public void PutArray(ulong[] value)
  205. {
  206. ushort len = value == null ? (ushort)0 : (ushort)value.Length;
  207. if (_autoResize)
  208. ResizeIfNeed(_position + len * 8 + 2);
  209. Put(len);
  210. for (int i = 0; i < len; i++)
  211. {
  212. Put(value[i]);
  213. }
  214. }
  215. public void PutArray(int[] value)
  216. {
  217. ushort len = value == null ? (ushort)0 : (ushort)value.Length;
  218. if (_autoResize)
  219. ResizeIfNeed(_position + len * 4 + 2);
  220. Put(len);
  221. for (int i = 0; i < len; i++)
  222. {
  223. Put(value[i]);
  224. }
  225. }
  226. public void PutArray(uint[] value)
  227. {
  228. ushort len = value == null ? (ushort)0 : (ushort)value.Length;
  229. if (_autoResize)
  230. ResizeIfNeed(_position + len * 4 + 2);
  231. Put(len);
  232. for (int i = 0; i < len; i++)
  233. {
  234. Put(value[i]);
  235. }
  236. }
  237. public void PutArray(ushort[] value)
  238. {
  239. ushort len = value == null ? (ushort)0 : (ushort)value.Length;
  240. if (_autoResize)
  241. ResizeIfNeed(_position + len * 2 + 2);
  242. Put(len);
  243. for (int i = 0; i < len; i++)
  244. {
  245. Put(value[i]);
  246. }
  247. }
  248. public void PutArray(short[] value)
  249. {
  250. ushort len = value == null ? (ushort)0 : (ushort)value.Length;
  251. if (_autoResize)
  252. ResizeIfNeed(_position + len * 2 + 2);
  253. Put(len);
  254. for (int i = 0; i < len; i++)
  255. {
  256. Put(value[i]);
  257. }
  258. }
  259. public void PutArray(bool[] value)
  260. {
  261. ushort len = value == null ? (ushort)0 : (ushort)value.Length;
  262. if (_autoResize)
  263. ResizeIfNeed(_position + len + 2);
  264. Put(len);
  265. for (int i = 0; i < len; i++)
  266. {
  267. Put(value[i]);
  268. }
  269. }
  270. public void PutArray(string[] value)
  271. {
  272. ushort len = value == null ? (ushort)0 : (ushort)value.Length;
  273. Put(len);
  274. for (int i = 0; i < value.Length; i++)
  275. {
  276. Put(value[i]);
  277. }
  278. }
  279. public void PutArray(string[] value, int maxLength)
  280. {
  281. ushort len = value == null ? (ushort)0 : (ushort)value.Length;
  282. Put(len);
  283. for (int i = 0; i < len; i++)
  284. {
  285. Put(value[i], maxLength);
  286. }
  287. }
  288. public void Put(NetEndPoint endPoint)
  289. {
  290. Put(endPoint.Host);
  291. Put(endPoint.Port);
  292. }
  293. public void Put(string value)
  294. {
  295. if (string.IsNullOrEmpty(value))
  296. {
  297. Put(0);
  298. return;
  299. }
  300. //put bytes count
  301. int bytesCount = Encoding.UTF8.GetByteCount(value);
  302. if (_autoResize)
  303. ResizeIfNeed(_position + bytesCount + 4);
  304. Put(bytesCount);
  305. //put string
  306. Encoding.UTF8.GetBytes(value, 0, value.Length, _data, _position);
  307. _position += bytesCount;
  308. }
  309. public void Put(string value, int maxLength)
  310. {
  311. if (string.IsNullOrEmpty(value))
  312. {
  313. Put(0);
  314. return;
  315. }
  316. int length = value.Length > maxLength ? maxLength : value.Length;
  317. //calculate max count
  318. int bytesCount = Encoding.UTF8.GetByteCount(value);
  319. if (_autoResize)
  320. ResizeIfNeed(_position + bytesCount + 4);
  321. //put bytes count
  322. Put(bytesCount);
  323. //put string
  324. Encoding.UTF8.GetBytes(value, 0, length, _data, _position);
  325. _position += bytesCount;
  326. }
  327. }
  328. }
  329. #endif