NetPkg.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. 
  2. using System;
  3. using System.Net;
  4. using UnityEngine;
  5. using System.Diagnostics;
  6. using System.IO;
  7. public class NetPkgHead
  8. {
  9. private UInt16 mPkgLen; //包总的大小 = 包头 + 包体
  10. private UInt16 mCmdId; //消息ID
  11. private UInt32 mSvrPkgSeq; //发送的序列号
  12. private UInt16 mEncryptFlag = 0; //加密标志位:0-不加密,1-RSA,2-AES
  13. public UInt16 PkgLen
  14. {
  15. get { return mPkgLen; }
  16. set { mPkgLen = value; }
  17. }
  18. public UInt16 CmdId
  19. {
  20. get { return mCmdId; }
  21. set { mCmdId = value; }
  22. }
  23. public UInt32 SvrPkgSeq
  24. {
  25. get { return mSvrPkgSeq; }
  26. set { mSvrPkgSeq = value; }
  27. }
  28. public UInt16 EncryptFlag
  29. {
  30. get { return mEncryptFlag; }
  31. set { mEncryptFlag = value; }
  32. }
  33. public static UInt16 HeadLength
  34. {
  35. get { return sizeof(UInt16) * 3 + sizeof(UInt32); }
  36. }
  37. public NetPkgHead()
  38. {
  39. }
  40. public NetPkgHead(int cmdId)
  41. {
  42. this.mCmdId = (UInt16)cmdId;
  43. }
  44. public CommError.Type pack(ref byte[] buffer, int size, ref int usedSize)
  45. {
  46. if (null == buffer || 0 == buffer.GetLength(0) || (size > buffer.GetLength(0)))
  47. {
  48. return CommError.Type.COMM_ERR_INVALID_BUFFER_PARAMETER;
  49. }
  50. CommWriteBuf destBuf = new CommWriteBuf(ref buffer, size, usedSize);
  51. CommError.Type ret = pack(ref destBuf);
  52. if (ret == CommError.Type.COMM_NO_ERROR)
  53. {
  54. buffer = destBuf.getBeginPtr();
  55. usedSize = destBuf.getUsedSize();
  56. }
  57. return ret;
  58. }
  59. public CommError.Type pack(ref CommWriteBuf destBuf)
  60. {
  61. CommError.Type ret = CommError.Type.COMM_NO_ERROR;
  62. /* pack member: this.pkg_len */
  63. {
  64. ret = destBuf.writeUInt16(this.mPkgLen);
  65. if (CommError.Type.COMM_NO_ERROR != ret)
  66. {
  67. return ret;
  68. }
  69. }
  70. /* pack member: this.cmd_id */
  71. {
  72. ret = destBuf.writeUInt16(this.mCmdId);
  73. if (CommError.Type.COMM_NO_ERROR != ret)
  74. {
  75. return ret;
  76. }
  77. }
  78. /* pack member: this.mSvrPkgSeq */
  79. {
  80. ret = destBuf.writeUInt32(this.mSvrPkgSeq);
  81. if (CommError.Type.COMM_NO_ERROR != ret)
  82. {
  83. return ret;
  84. }
  85. }
  86. /* pack member: this.mEncryptFlag */
  87. {
  88. ret = destBuf.writeUInt16(this.mEncryptFlag);
  89. if (CommError.Type.COMM_NO_ERROR != ret)
  90. {
  91. return ret;
  92. }
  93. }
  94. return ret;
  95. }
  96. public CommError.Type unpack(ref byte[] buffer, int size, ref int usedSize)
  97. {
  98. if (null == buffer || 0 == buffer.GetLength(0) || size > buffer.GetLength(0))
  99. {
  100. return CommError.Type.COMM_ERR_INVALID_BUFFER_PARAMETER;
  101. }
  102. CommReadBuf srcBuf = new CommReadBuf(ref buffer, size, usedSize);
  103. CommError.Type ret = unpack(ref srcBuf);
  104. usedSize = srcBuf.getUsedSize();
  105. srcBuf = null;
  106. return ret;
  107. }
  108. public CommError.Type unpack(ref CommReadBuf srcBuf)
  109. {
  110. CommError.Type ret = CommError.Type.COMM_NO_ERROR;
  111. /* unpack member: this.pkg_len */
  112. {
  113. ret = srcBuf.readUInt16(ref this.mPkgLen);
  114. if (CommError.Type.COMM_NO_ERROR != ret)
  115. {
  116. return ret;
  117. }
  118. }
  119. /* unpack member: this.cmd_id */
  120. {
  121. ret = srcBuf.readUInt16(ref this.mCmdId);
  122. if (CommError.Type.COMM_NO_ERROR != ret)
  123. {
  124. return ret;
  125. }
  126. }
  127. /* unpack member: this.mSvrPkgSeq */
  128. {
  129. ret = srcBuf.readUInt32(ref this.mSvrPkgSeq);
  130. if (CommError.Type.COMM_NO_ERROR != ret)
  131. {
  132. return ret;
  133. }
  134. }
  135. /* unpack member: this.mEncryptFlag */
  136. {
  137. ret = srcBuf.readUInt16(ref this.mEncryptFlag);
  138. if (CommError.Type.COMM_NO_ERROR != ret)
  139. {
  140. return ret;
  141. }
  142. }
  143. return ret;
  144. }
  145. }
  146. public class NetPkgBody
  147. {
  148. //包体内容
  149. private byte[] mMsgData = null;
  150. public byte[] MsgData
  151. {
  152. get { return mMsgData; }
  153. }
  154. public NetPkgBody()
  155. {
  156. this.mMsgData = null;
  157. }
  158. public void CopyBuffData(byte[] buf)
  159. {
  160. if (buf == null) return;
  161. CopyBuffData(buf, (uint)buf.Length);
  162. }
  163. public void CopyBuffData(byte[] buf,uint len)
  164. {
  165. if (buf == null) return;
  166. if (mMsgData == null)
  167. {
  168. mMsgData = new byte[len];
  169. }
  170. else if (mMsgData.Length < len)
  171. {
  172. mMsgData = new byte[len];
  173. }
  174. Array.Clear(mMsgData, 0, mMsgData.Length);
  175. Array.Copy(buf, mMsgData, len);
  176. }
  177. public void InitMessage(int packLen)
  178. {
  179. mMsgData = null;
  180. mMsgData = new byte[packLen];
  181. }
  182. public CommError.Type pack(ref byte[] buffer, int size, ref int usedSize)
  183. {
  184. if (null == buffer || 0 == buffer.GetLength(0) || (size > buffer.GetLength(0)))
  185. {
  186. return CommError.Type.COMM_ERR_INVALID_BUFFER_PARAMETER;
  187. }
  188. int nLen = 0;
  189. if (mMsgData != null)
  190. {
  191. nLen = (int)mMsgData.Length;
  192. }
  193. CommWriteBuf destBuf = new CommWriteBuf(ref buffer, size, usedSize);
  194. CommError.Type ret = pack(ref destBuf, mMsgData, nLen);
  195. if (ret == CommError.Type.COMM_NO_ERROR)
  196. {
  197. buffer = destBuf.getBeginPtr();
  198. usedSize = destBuf.getUsedSize();
  199. }
  200. return ret;
  201. }
  202. public CommError.Type pack(ref CommWriteBuf destBuf, byte[] data, int len)
  203. {
  204. CommError.Type ret = CommError.Type.COMM_NO_ERROR;
  205. if(data == null || len == 0)
  206. {
  207. return ret;
  208. }
  209. /*pack member mMsg data*/
  210. ret = destBuf.writeByteArrar(data, len);
  211. if (CommError.Type.COMM_NO_ERROR != ret)
  212. {
  213. return ret;
  214. }
  215. return ret;
  216. }
  217. public CommError.Type unpack(ref byte[] buffer, int size, ref int usedSize)
  218. {
  219. if (null == buffer || 0 == buffer.GetLength(0) || size > buffer.GetLength(0))
  220. {
  221. return CommError.Type.COMM_ERR_INVALID_BUFFER_PARAMETER;
  222. }
  223. if (null == this.mMsgData)
  224. {
  225. return CommError.Type.COMM_ERR_USE_HAVE_NOT_INIT_VARIABLE_ARRAY;
  226. }
  227. CommReadBuf srcBuf = new CommReadBuf(ref buffer, size, usedSize);
  228. CommError.Type ret = unpack(ref srcBuf);
  229. usedSize = srcBuf.getUsedSize();
  230. srcBuf = null;
  231. return ret;
  232. }
  233. public CommError.Type unpack(ref CommReadBuf srcBuf)
  234. {
  235. CommError.Type ret = CommError.Type.COMM_NO_ERROR;
  236. /* unpack member: this.mMsgData */
  237. {
  238. ret = srcBuf.readByteArray(ref this.mMsgData, mMsgData.Length);
  239. if (CommError.Type.COMM_NO_ERROR != ret)
  240. {
  241. return ret;
  242. }
  243. }
  244. return ret;
  245. }
  246. }
  247. public class NetPkg
  248. {
  249. /// <summary>
  250. /// 消息包头
  251. /// </summary>
  252. public NetPkgHead Head = null;
  253. public NetPkgBody Body = null;
  254. private bool bConfirm = false;
  255. public bool Confirm
  256. {
  257. get { return bConfirm; }
  258. set { bConfirm = value; }
  259. }
  260. /// <summary>
  261. /// 创建网络发送消息包
  262. /// </summary>
  263. /// <param name="cmdId"></param>
  264. /// <param name="msg"></param>
  265. /// <returns></returns>
  266. public static NetPkg CreateNetReqPkg(UInt16 cmdId, byte[] buf)
  267. {
  268. NetPkg reqPkg = new NetPkg(cmdId, buf);
  269. return reqPkg;
  270. }
  271. /// <summary>
  272. /// 创建接收消息包
  273. /// </summary>
  274. /// <returns></returns>
  275. public static NetPkg CreateNetRevPkg()
  276. {
  277. NetPkg reqPkg = new NetPkg(0, null);
  278. return reqPkg;
  279. }
  280. protected NetPkg(UInt16 cmdId, byte[] msg)
  281. {
  282. if (Head == null && cmdId > 0)
  283. Head = new NetPkgHead(cmdId);
  284. if (Body == null)
  285. Body = new NetPkgBody();
  286. Body.CopyBuffData(msg);
  287. }
  288. /// <summary>
  289. /// 序列号
  290. /// </summary>
  291. /// <param name="buffer"></param>
  292. /// <param name="size"></param>
  293. /// <param name="usedSize"></param>
  294. /// <returns></returns>
  295. public CommError.Type pack(ref byte[] buffer, int size, ref int usedSize)
  296. {
  297. CommError.Type ret = CommError.Type.COMM_NO_ERROR;
  298. if (Head == null) Head = new NetPkgHead();
  299. int start = usedSize;
  300. ret = Head.pack(ref buffer, size, ref usedSize);
  301. if (ret != CommError.Type.COMM_NO_ERROR) return ret;
  302. if (null == Body) return CommError.Type.COMM_ERR_ARGUMENT_NULL_EXCEPTION;
  303. ret = Body.pack(ref buffer, size, ref usedSize);
  304. if (ret != CommError.Type.COMM_NO_ERROR) return ret;
  305. int pack_len = usedSize - start - NetPkgHead.HeadLength;
  306. Head.PkgLen = (UInt16)pack_len;
  307. Head.pack(ref buffer, size, ref start);
  308. return ret;
  309. }
  310. /// <summary>
  311. ///
  312. /// </summary>
  313. /// <param name="buffer"></param>
  314. /// <param name="size"></param>
  315. /// <param name="usedSize"></param>
  316. /// <returns></returns>
  317. public CommError.Type unpack(ref byte[] buffer, int size, ref int usedSize)
  318. {
  319. CommError.Type ret = CommError.Type.COMM_NO_ERROR;
  320. if (Head == null) Head = new NetPkgHead();
  321. ret = Head.unpack(ref buffer, size, ref usedSize);
  322. if (ret != CommError.Type.COMM_NO_ERROR) return ret;
  323. if (size < Head.PkgLen) return CommError.Type.COMM_ERR_SHORT_BUF_FOR_READ;
  324. Body.InitMessage((int)Head.PkgLen);
  325. ret = Body.unpack(ref buffer, size, ref usedSize);
  326. if (ret != CommError.Type.COMM_NO_ERROR) return ret;
  327. if(Head.EncryptFlag == NetworkMgr.c_RSA_Encrypt)
  328. {
  329. byte[] decryptBuf = SecurityLayer.Instance.RSADecrypt(Body.MsgData);
  330. Body.CopyBuffData(decryptBuf);
  331. }
  332. else if(Head.EncryptFlag == NetworkMgr.c_AES_Encrypt)
  333. {
  334. byte[] decryptBuf = SecurityLayer.Instance.AESDecrypt(Body.MsgData);
  335. Body.CopyBuffData(decryptBuf);
  336. }
  337. return ret;
  338. }
  339. }