NetBytes.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. using UnityEngine;
  2. using System.Collections;
  3. using System;
  4. using System.Text;
  5. namespace Engine.Net
  6. {
  7. /// <summary>网络字节流对象</summary>
  8. public class NetBytes
  9. {
  10. public const int BYTE_LEN = 1;
  11. public const int SHORT16_LEN = 2;
  12. public const int INT32_LEN = 4;
  13. public const int FLOAT_LEN = 4;
  14. public const int LONG_LEN = 8;
  15. public const int DOUBLE_LEN = 8;
  16. private byte[] data;
  17. private int byteIndex = 0;
  18. public NetBytes(byte[] bytes = null)
  19. {
  20. data = (bytes == null) ? new byte[0] : bytes;
  21. }
  22. /// <summary>byte[] 数据原</summary>
  23. public byte[] Data
  24. {
  25. get { return data; }
  26. set { data = value; }
  27. }
  28. /// <summary>可读字节数,修改读取位置可修改此值</summary>
  29. public int ByteLength
  30. {
  31. get { return data.Length - byteIndex; }
  32. }
  33. /// <summary>总字节数据</summary>
  34. public int Length
  35. {
  36. get { return data.Length; }
  37. }
  38. /// <summary>当前读取位置</summary>
  39. public int Position
  40. {
  41. get { return byteIndex; }
  42. set { byteIndex = value; }
  43. }
  44. public byte ReadByte()
  45. {
  46. return this.ReadBytes(1)[0];
  47. }
  48. public bool ReadBool()
  49. {
  50. byte bt = this.ReadByte();
  51. return (bt == (byte)1) ? true : false;
  52. }
  53. public short ReadShort()
  54. {
  55. return BitConverter.ToInt16(this.ReadBytes(SHORT16_LEN), 0);
  56. }
  57. public int ReadInt()
  58. {
  59. return BitConverter.ToInt32(this.ReadBytes(INT32_LEN), 0);
  60. }
  61. public long ReadLong()
  62. {
  63. return BitConverter.ToInt64(this.ReadBytes(LONG_LEN), 0);
  64. }
  65. public float ReadFloat()
  66. {
  67. return BitConverter.ToSingle(this.ReadBytes(FLOAT_LEN), 0);
  68. }
  69. public double ReadDouble()
  70. {
  71. return BitConverter.ToDouble(this.ReadBytes(DOUBLE_LEN), 0);
  72. }
  73. public string ReadString()
  74. {
  75. return Encoding.UTF8.GetString(this.ReadBytes(this.ReadShort(), false));
  76. }
  77. public char[] ReadChars()
  78. {
  79. string x =Encoding.UTF8.GetString(this.ReadBytes(this.ReadShort(), false));
  80. return x.ToCharArray();
  81. }
  82. public byte[] ReadByteArray()
  83. {
  84. return this.ReadBytes(this.ReadShort(), false);
  85. }
  86. public Vector3 ReadVector3()
  87. {
  88. Vector3 v = Vector3.zero;
  89. v.x = BitConverter.ToSingle(this.ReadBytes(FLOAT_LEN), 0);
  90. v.y = BitConverter.ToSingle(this.ReadBytes(FLOAT_LEN), 0);
  91. v.z = BitConverter.ToSingle(this.ReadBytes(FLOAT_LEN), 0);
  92. return v;
  93. }
  94. public NetBytes ReadUnit()
  95. {
  96. int leng = this.ReadShort();
  97. return new NetBytes(this.ReadBytes(leng, false));
  98. }
  99. public byte[] ReadBytes(int leng, bool isReverse = true)
  100. {
  101. if (leng > this.ByteLength)
  102. {
  103. throw new Exception("超出字节流长度");
  104. }
  105. //这里重复开辟了内存,会产生内存垃圾,可能会有性能问题
  106. byte[] byets = new byte[leng];
  107. for (int i = 0; i < leng; i++)
  108. {
  109. byets[i] = data[byteIndex];
  110. byteIndex++;
  111. }
  112. if (isReverse)
  113. {
  114. //这里和真实后端通信的时候记得要反序
  115. //Array.Reverse(byets);
  116. }
  117. return byets;
  118. }
  119. public void WriteByte(byte by)
  120. {
  121. byte[] bytes = new byte[BYTE_LEN];
  122. bytes[0] = by;
  123. this.WriteBytes(bytes);
  124. }
  125. public void WriteBool(bool flag)
  126. {
  127. byte b = (byte)0;
  128. if (flag) b = (byte)1;
  129. this.WriteByte(b);
  130. }
  131. public void WriteShort(short value)
  132. {
  133. byte[] bytes = BitConverter.GetBytes(value);
  134. this.WriteBytes(bytes);
  135. }
  136. public void WriteInt(int value)
  137. {
  138. byte[] bytes = BitConverter.GetBytes(value);
  139. this.WriteBytes(bytes);
  140. }
  141. public void WriteLong(int value)
  142. {
  143. byte[] bytes = BitConverter.GetBytes(value);
  144. this.WriteBytes(bytes);
  145. }
  146. public void WriteDouble(double value)
  147. {
  148. byte[] bytes = BitConverter.GetBytes(value);
  149. this.WriteBytes(bytes);
  150. }
  151. public void WriteFloat(float value)
  152. {
  153. byte[] bytes = BitConverter.GetBytes(value);
  154. this.WriteBytes(bytes);
  155. }
  156. public void WriteString(string str, short len = -1)
  157. {
  158. if (len == (short)-1)
  159. {
  160. len = (short)Encoding.UTF8.GetByteCount(str);
  161. }
  162. this.WriteShort(len);
  163. byte[] bytes = new byte[len];
  164. if (str == null)
  165. {
  166. CDebug.Log("string is null");
  167. }
  168. Encoding.UTF8.GetBytes(str, 0, str.Length, bytes, 0);
  169. this.WriteBytes(bytes, false);
  170. }
  171. public void WriteChars(char[] str)
  172. {
  173. short len = 12;
  174. this.WriteShort(len);
  175. byte[] bytes = new byte[len];
  176. Encoding.UTF8.GetBytes(str, 0, str.Length, bytes, 0);
  177. this.WriteBytes(bytes, false);
  178. }
  179. public void WriteByteArray(byte[] bs)
  180. {
  181. short len = (short)bs.Length;
  182. this.WriteShort(len);
  183. byte[] bytes = new byte[len];
  184. Array.Copy(bs, bytes, len);
  185. // Encoding.UTF8.GetBytes(bs, 0, bs.Length, bytes, 0);
  186. this.WriteBytes(bytes, false);
  187. }
  188. public void WriteVector3(Vector3 v)
  189. {
  190. this.WriteFloat(v.x);
  191. this.WriteFloat(v.y);
  192. this.WriteFloat(v.z);
  193. }
  194. public void WriteUnit(byte[] bytes)
  195. {
  196. this.WriteShort((short)bytes.Length);
  197. this.WriteBytes(bytes, false);
  198. }
  199. public void WriteBytes(byte[] bytes, bool isReverse = true)
  200. {
  201. if (isReverse)
  202. {
  203. //这里和真实后端通信的时候记得要反序
  204. //Array.Reverse(bytes);
  205. }
  206. byteIndex = data.Length;
  207. int leng = bytes.Length;
  208. //这里重复开辟了内存,会产生内存垃圾,可能会有性能问题
  209. byte[] newData = new byte[byteIndex + leng];
  210. data.CopyTo(newData, 0);
  211. Array.Copy(bytes, 0, newData, byteIndex, leng);
  212. data = newData;
  213. byteIndex = data.Length;
  214. }
  215. public void Dispose()
  216. {
  217. data = null;
  218. }
  219. }
  220. }