ByteParse.cs 9.7 KB

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