NetDataReader.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  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 NetDataReader
  7. {
  8. protected byte[] _data;
  9. protected int _position;
  10. protected int _dataSize;
  11. public byte[] Data
  12. {
  13. get { return _data; }
  14. }
  15. public int Position
  16. {
  17. get { return _position; }
  18. }
  19. public bool EndOfData
  20. {
  21. get { return _position == _dataSize; }
  22. }
  23. public int AvailableBytes
  24. {
  25. get { return _dataSize - _position; }
  26. }
  27. public void SetSource(NetDataWriter dataWriter)
  28. {
  29. _data = dataWriter.Data;
  30. _position = 0;
  31. _dataSize = dataWriter.Length;
  32. }
  33. public void SetSource(byte[] source)
  34. {
  35. _data = source;
  36. _position = 0;
  37. _dataSize = source.Length;
  38. }
  39. public void SetSource(byte[] source, int offset)
  40. {
  41. _data = source;
  42. _position = offset;
  43. _dataSize = source.Length;
  44. }
  45. public void SetSource(byte[] source, int offset, int dataSize)
  46. {
  47. _data = source;
  48. _position = offset;
  49. _dataSize = dataSize;
  50. }
  51. public NetDataReader()
  52. {
  53. }
  54. public NetDataReader(byte[] source)
  55. {
  56. SetSource(source);
  57. }
  58. public NetDataReader(byte[] source, int offset)
  59. {
  60. SetSource(source, offset);
  61. }
  62. public NetDataReader(byte[] source, int offset, int maxSize)
  63. {
  64. SetSource(source, offset, maxSize);
  65. }
  66. #region GetMethods
  67. public NetEndPoint GetNetEndPoint()
  68. {
  69. string host = GetString(1000);
  70. int port = GetInt();
  71. return new NetEndPoint(host, port);
  72. }
  73. public byte GetByte()
  74. {
  75. byte res = _data[_position];
  76. _position += 1;
  77. return res;
  78. }
  79. public sbyte GetSByte()
  80. {
  81. var b = (sbyte)_data[_position];
  82. _position++;
  83. return b;
  84. }
  85. public bool[] GetBoolArray()
  86. {
  87. ushort size = BitConverter.ToUInt16(_data, _position);
  88. _position += 2;
  89. var arr = new bool[size];
  90. for (int i = 0; i < size; i++)
  91. {
  92. arr[i] = GetBool();
  93. }
  94. return arr;
  95. }
  96. public ushort[] GetUShortArray()
  97. {
  98. ushort size = BitConverter.ToUInt16(_data, _position);
  99. _position += 2;
  100. var arr = new ushort[size];
  101. for (int i = 0; i < size; i++)
  102. {
  103. arr[i] = GetUShort();
  104. }
  105. return arr;
  106. }
  107. public short[] GetShortArray()
  108. {
  109. ushort size = BitConverter.ToUInt16(_data, _position);
  110. _position += 2;
  111. var arr = new short[size];
  112. for (int i = 0; i < size; i++)
  113. {
  114. arr[i] = GetShort();
  115. }
  116. return arr;
  117. }
  118. public long[] GetLongArray()
  119. {
  120. ushort size = BitConverter.ToUInt16(_data, _position);
  121. _position += 2;
  122. var arr = new long[size];
  123. for (int i = 0; i < size; i++)
  124. {
  125. arr[i] = GetLong();
  126. }
  127. return arr;
  128. }
  129. public ulong[] GetULongArray()
  130. {
  131. ushort size = BitConverter.ToUInt16(_data, _position);
  132. _position += 2;
  133. var arr = new ulong[size];
  134. for (int i = 0; i < size; i++)
  135. {
  136. arr[i] = GetULong();
  137. }
  138. return arr;
  139. }
  140. public int[] GetIntArray()
  141. {
  142. ushort size = BitConverter.ToUInt16(_data, _position);
  143. _position += 2;
  144. var arr = new int[size];
  145. for (int i = 0; i < size; i++)
  146. {
  147. arr[i] = GetInt();
  148. }
  149. return arr;
  150. }
  151. public uint[] GetUIntArray()
  152. {
  153. ushort size = BitConverter.ToUInt16(_data, _position);
  154. _position += 2;
  155. var arr = new uint[size];
  156. for (int i = 0; i < size; i++)
  157. {
  158. arr[i] = GetUInt();
  159. }
  160. return arr;
  161. }
  162. public float[] GetFloatArray()
  163. {
  164. ushort size = BitConverter.ToUInt16(_data, _position);
  165. _position += 2;
  166. var arr = new float[size];
  167. for (int i = 0; i < size; i++)
  168. {
  169. arr[i] = GetFloat();
  170. }
  171. return arr;
  172. }
  173. public double[] GetDoubleArray()
  174. {
  175. ushort size = BitConverter.ToUInt16(_data, _position);
  176. _position += 2;
  177. var arr = new double[size];
  178. for (int i = 0; i < size; i++)
  179. {
  180. arr[i] = GetDouble();
  181. }
  182. return arr;
  183. }
  184. public string[] GetStringArray(int maxLength)
  185. {
  186. ushort size = BitConverter.ToUInt16(_data, _position);
  187. _position += 2;
  188. var arr = new string[size];
  189. for (int i = 0; i < size; i++)
  190. {
  191. arr[i] = GetString(maxLength);
  192. }
  193. return arr;
  194. }
  195. public bool GetBool()
  196. {
  197. bool res = _data[_position] > 0;
  198. _position += 1;
  199. return res;
  200. }
  201. public ushort GetUShort()
  202. {
  203. ushort result = BitConverter.ToUInt16(_data, _position);
  204. _position += 2;
  205. return result;
  206. }
  207. public short GetShort()
  208. {
  209. short result = BitConverter.ToInt16(_data, _position);
  210. _position += 2;
  211. return result;
  212. }
  213. public long GetLong()
  214. {
  215. long result = BitConverter.ToInt64(_data, _position);
  216. _position += 8;
  217. return result;
  218. }
  219. public ulong GetULong()
  220. {
  221. ulong result = BitConverter.ToUInt64(_data, _position);
  222. _position += 8;
  223. return result;
  224. }
  225. public int GetInt()
  226. {
  227. int result = BitConverter.ToInt32(_data, _position);
  228. _position += 4;
  229. return result;
  230. }
  231. public uint GetUInt()
  232. {
  233. uint result = BitConverter.ToUInt32(_data, _position);
  234. _position += 4;
  235. return result;
  236. }
  237. public float GetFloat()
  238. {
  239. float result = BitConverter.ToSingle(_data, _position);
  240. _position += 4;
  241. return result;
  242. }
  243. public double GetDouble()
  244. {
  245. double result = BitConverter.ToDouble(_data, _position);
  246. _position += 8;
  247. return result;
  248. }
  249. public string GetString(int maxLength)
  250. {
  251. int bytesCount = GetInt();
  252. if (bytesCount <= 0 || bytesCount > maxLength*2)
  253. {
  254. return string.Empty;
  255. }
  256. int charCount = Encoding.UTF8.GetCharCount(_data, _position, bytesCount);
  257. if (charCount > maxLength)
  258. {
  259. return string.Empty;
  260. }
  261. string result = Encoding.UTF8.GetString(_data, _position, bytesCount);
  262. _position += bytesCount;
  263. return result;
  264. }
  265. public string GetString()
  266. {
  267. int bytesCount = GetInt();
  268. if (bytesCount <= 0)
  269. {
  270. return string.Empty;
  271. }
  272. string result = Encoding.UTF8.GetString(_data, _position, bytesCount);
  273. _position += bytesCount;
  274. return result;
  275. }
  276. public byte[] GetRemainingBytes()
  277. {
  278. byte[] outgoingData = new byte[AvailableBytes];
  279. Buffer.BlockCopy(_data, _position, outgoingData, 0, AvailableBytes);
  280. _position = _data.Length;
  281. return outgoingData;
  282. }
  283. public void GetRemainingBytes(byte[] destination)
  284. {
  285. Buffer.BlockCopy(_data, _position, destination, 0, AvailableBytes);
  286. _position = _data.Length;
  287. }
  288. public void GetBytes(byte[] destination, int lenght)
  289. {
  290. Buffer.BlockCopy(_data, _position, destination, 0, lenght);
  291. _position += lenght;
  292. }
  293. public byte[] GetBytesWithLength()
  294. {
  295. int length = GetInt();
  296. byte[] outgoingData = new byte[length];
  297. Buffer.BlockCopy(_data, _position, outgoingData, 0, length);
  298. _position += length;
  299. return outgoingData;
  300. }
  301. #endregion
  302. #region PeekMethods
  303. public byte PeekByte()
  304. {
  305. return _data[_position];
  306. }
  307. public sbyte PeekSByte()
  308. {
  309. return (sbyte)_data[_position];
  310. }
  311. public bool PeekBool()
  312. {
  313. return _data[_position] > 0;
  314. }
  315. public ushort PeekUShort()
  316. {
  317. return BitConverter.ToUInt16(_data, _position);
  318. }
  319. public short PeekShort()
  320. {
  321. return BitConverter.ToInt16(_data, _position);
  322. }
  323. public long PeekLong()
  324. {
  325. return BitConverter.ToInt64(_data, _position);
  326. }
  327. public ulong PeekULong()
  328. {
  329. return BitConverter.ToUInt64(_data, _position);
  330. }
  331. public int PeekInt()
  332. {
  333. return BitConverter.ToInt32(_data, _position);
  334. }
  335. public uint PeekUInt()
  336. {
  337. return BitConverter.ToUInt32(_data, _position);
  338. }
  339. public float PeekFloat()
  340. {
  341. return BitConverter.ToSingle(_data, _position);
  342. }
  343. public double PeekDouble()
  344. {
  345. return BitConverter.ToDouble(_data, _position);
  346. }
  347. public string PeekString(int maxLength)
  348. {
  349. int bytesCount = BitConverter.ToInt32(_data, _position);
  350. if (bytesCount <= 0 || bytesCount > maxLength * 2)
  351. {
  352. return string.Empty;
  353. }
  354. int charCount = Encoding.UTF8.GetCharCount(_data, _position + 4, bytesCount);
  355. if (charCount > maxLength)
  356. {
  357. return string.Empty;
  358. }
  359. string result = Encoding.UTF8.GetString(_data, _position + 4, bytesCount);
  360. return result;
  361. }
  362. public string PeekString()
  363. {
  364. int bytesCount = BitConverter.ToInt32(_data, _position);
  365. if (bytesCount <= 0)
  366. {
  367. return string.Empty;
  368. }
  369. string result = Encoding.UTF8.GetString(_data, _position + 4, bytesCount);
  370. return result;
  371. }
  372. #endregion
  373. public void Clear()
  374. {
  375. _position = 0;
  376. _dataSize = 0;
  377. _data = null;
  378. }
  379. }
  380. }
  381. #endif