ByteParse.cs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. using NetCore.NetServerCoreBasic;
  2. using NetCore.Protocol;
  3. namespace NetCore.ContentParse;
  4. public class ByteParse : IContentParse
  5. {
  6. // private byte[] buffData = new byte[655350];
  7. //记录半包数据
  8. private bool isBufferData;
  9. private byte[] lastBuffData;
  10. private int lastCount;
  11. private ushort lastXueLieHao;
  12. //数据结束
  13. //尾包
  14. private byte[] weiBaoBuffData;
  15. private byte[] ShortToByte(short number)
  16. {
  17. byte[] numberBytes = BitConverter.GetBytes(number).Reverse().ToArray();
  18. return numberBytes;
  19. }
  20. private byte[] UShortToByte(ushort number)
  21. {
  22. byte[] numberBytes = BitConverter.GetBytes(number).Reverse().ToArray();
  23. return numberBytes;
  24. }
  25. private byte[] IntToByte(int number,bool isReverse = false)
  26. {
  27. if (isReverse)
  28. {
  29. byte[] numberBytes = BitConverter.GetBytes(number).Reverse().ToArray();
  30. return numberBytes;
  31. }
  32. else
  33. {
  34. byte[] numberBytes = BitConverter.GetBytes(number);
  35. return numberBytes;
  36. }
  37. }
  38. private short ByteToShort(byte[] numberBytes, bool isReverse = false)
  39. {
  40. if (isReverse)
  41. {
  42. short converted = BitConverter.ToInt16(numberBytes.Reverse().ToArray(), 0);
  43. return converted;
  44. }
  45. else
  46. {
  47. short converted = BitConverter.ToInt16(numberBytes, 0);
  48. return converted;
  49. }
  50. }
  51. private ushort ByteToUshort(byte[] numberBytes, bool isReverse = false)
  52. {
  53. if (isReverse)
  54. {
  55. ushort converted = BitConverter.ToUInt16(numberBytes.Reverse().ToArray(), 0);
  56. return converted;
  57. }
  58. else
  59. {
  60. ushort converted = BitConverter.ToUInt16(numberBytes, 0);
  61. return converted;
  62. }
  63. }
  64. private int ByteToInt(byte[] numberBytes, bool isReverse = false)
  65. {
  66. if (isReverse)
  67. {
  68. int converted = BitConverter.ToInt32(numberBytes.Reverse().ToArray(), 0);
  69. return converted;
  70. }
  71. else
  72. {
  73. int converted = BitConverter.ToInt32(numberBytes, 0);
  74. return converted;
  75. }
  76. }
  77. public byte[] AssembleByte(byte[] content)
  78. {
  79. byte[] mdata = null;
  80. // LogTool.Log("回复消息" + buffer.Length);
  81. mdata = new byte[content.Length + 4];
  82. byte[] zcd = IntToByte(content.Length);
  83. mdata[0] = zcd[0];
  84. mdata[1] = zcd[1];
  85. mdata[2] = zcd[2];
  86. mdata[3] = zcd[3];
  87. Array.Copy(content, 0, mdata, 4, content.Length);
  88. return mdata;
  89. }
  90. public void ParseByte(byte[] buffData, IProtocol iProtocol, IContentReceiver contentReceiver)
  91. {
  92. int count = buffData.Length;
  93. try
  94. {
  95. int currCount = 0;
  96. int startIndex = 0;
  97. // bool isEnd = false;
  98. while (currCount < count)
  99. {
  100. // isEnd = true;
  101. byte[] data = null;
  102. ushort xueLieHao = 1;
  103. int cdShort = 0;
  104. if (!isBufferData)
  105. {
  106. if (weiBaoBuffData != null)
  107. {
  108. int maxCount = 6553500;
  109. if (count + weiBaoBuffData.Length > maxCount)
  110. {
  111. maxCount = count + weiBaoBuffData.Length;
  112. }
  113. // LogTool.Log("修复包太短__" + count + "___" + weiBaoBuffData.Length);
  114. byte[] newBuff = new byte[maxCount];
  115. Array.Copy(weiBaoBuffData, 0, newBuff, 0, weiBaoBuffData.Length);
  116. Array.Copy(buffData, 0, newBuff, weiBaoBuffData.Length, count);
  117. buffData = newBuff;
  118. count += weiBaoBuffData.Length;
  119. // LogTool.Log("修复包后太短__" + count);
  120. if (count < 6)
  121. {
  122. weiBaoBuffData = new byte[count];
  123. // LogTool.Log("包太短222__" + count);
  124. Array.Copy(buffData, currCount, weiBaoBuffData, 0, count);
  125. break;
  126. }
  127. else
  128. {
  129. weiBaoBuffData = null;
  130. }
  131. }
  132. else
  133. {
  134. if (count - currCount < 6)
  135. {
  136. weiBaoBuffData = new byte[count];
  137. // LogTool.Log("包太短__" + count);
  138. Array.Copy(buffData, currCount, weiBaoBuffData, 0, count);
  139. break;
  140. }
  141. }
  142. }
  143. if (!isBufferData)
  144. {
  145. byte[] dataBuffLanl = new byte[4];
  146. dataBuffLanl[0] = buffData[currCount];
  147. currCount++;
  148. dataBuffLanl[1] = buffData[currCount];
  149. currCount++;
  150. dataBuffLanl[2] = buffData[currCount];
  151. currCount++;
  152. dataBuffLanl[3] = buffData[currCount];
  153. currCount++;
  154. cdShort = (ByteToInt(dataBuffLanl, false));
  155. // LogTool.Log("数据到来" + cdShort + "__" + count + "_" + xueLieHao);
  156. if ((currCount + cdShort) > count) //处理半包情况
  157. {
  158. lastBuffData = new byte[count - currCount];
  159. lastCount = cdShort;
  160. lastXueLieHao = xueLieHao;
  161. Array.Copy(buffData, currCount, lastBuffData, 0, lastBuffData.Length);
  162. // LogTool.Log("数据只有一半" + count + "__" + currCount + "___" + cdShort);
  163. isBufferData = true;
  164. break;
  165. }
  166. else
  167. {
  168. // LogTool.Log(currCount + "_收到长度:" + cdShort + "_Max" + count);
  169. data = new byte[cdShort];
  170. Array.Copy(buffData, currCount, data, 0, data.Length);
  171. }
  172. }
  173. else if (lastCount - lastBuffData.Length > count)
  174. {
  175. // LogTool.Log("数据只有一半的一半" + count + "__" + lastCount + "___" + lastBuffData.Length);
  176. byte[] newLastBuffData = new byte[lastBuffData.Length + count];
  177. Array.Copy(lastBuffData, 0, newLastBuffData, 0, lastBuffData.Length);
  178. Array.Copy(buffData, 0, newLastBuffData, lastBuffData.Length, count);
  179. lastBuffData = newLastBuffData;
  180. break;
  181. }
  182. else if (lastBuffData != null) //处理半包情况
  183. {
  184. isBufferData = false;
  185. // LogTool.Log("修复半包" + lastCount + "__" + count + "___" +
  186. // (lastCount - lastBuffData.Length));
  187. xueLieHao = lastXueLieHao;
  188. data = new byte[lastCount];
  189. cdShort = lastCount - lastBuffData.Length;
  190. Array.Copy(lastBuffData, 0, data, 0, lastBuffData.Length);
  191. Array.Copy(buffData, currCount, data, lastBuffData.Length, cdShort);
  192. }
  193. // SimulationCombat cr = SimulationCombat.Parser.ParseFrom(data);
  194. // RequestBuffer buffer = new RequestBuffer();
  195. // buffer.combatRequest = cr;
  196. // buffer.xlh = xueLieHao;
  197. // // allCount++;
  198. // // LogTool.Log("shoudao "+allCount);
  199. // // buffer.httpListenerContext = httpListenerContext;
  200. // LogTool.Log("收到中转服请求" + cr.CombatType + "___" + xueLieHao);
  201. // AddBuffer(buffer);
  202. currCount += cdShort;
  203. startIndex = currCount;
  204. object serializeData = iProtocol.Deserialize(data);
  205. contentReceiver.AddContent(serializeData);
  206. }
  207. // if (isEnd&&currCount < count&&!isBufferData)//尾包
  208. // {
  209. // LogTool.Log("尾巴处理"+currCount+"___"+count);
  210. // isWeiBao = true;
  211. // lastBuffData = new byte[count - currCount];
  212. // Array.Copy(buffData, currCount, lastBuffData, 0, lastBuffData.Length);
  213. //
  214. // }
  215. }
  216. catch (Exception e)
  217. {
  218. Console.WriteLine(e);
  219. // RequestBuffer buffer = new RequestBuffer();
  220. // buffer.combatRequest = cr;
  221. // LogTool.Log(e);
  222. //
  223. // Send(null, null);
  224. // LogTool.Log("");
  225. }
  226. }
  227. }