TCPServerConnection.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. #if COMBAT_SERVER
  2. using System;
  3. using System.Net;
  4. using System.Net.Sockets;
  5. using Com.Fort23.Protocol.Protobuf;
  6. using Fort23.UTool;
  7. namespace Core.KCPTool
  8. {
  9. public class TCPServerConnection : IServerConnection
  10. {
  11. public bool isConnected { get; set; }
  12. public int connectionID { get; set; }
  13. public long playerId { get; set; }
  14. public IPEndPoint EndPoint { get; set; }
  15. private byte[] buffData = new byte[6553];
  16. private Socket socket;
  17. //记录半包数据
  18. private bool isBufferData;
  19. private byte[] lastBuffData;
  20. private int lastCount;
  21. private short lastSendType;
  22. //数据结束
  23. //尾包
  24. private byte[] weiBaoBuffData;
  25. private Thread _udpClientThread;
  26. private TCPServer _tcpServer;
  27. public TCPServerConnection(TCPServer tcpServer, Socket socket)
  28. {
  29. this.socket = socket;
  30. socket.NoDelay = true;
  31. _tcpServer = tcpServer;
  32. isConnected = true;
  33. _udpClientThread = new Thread(TheUpdate);
  34. _udpClientThread.Start();
  35. }
  36. private void TheUpdate()
  37. {
  38. while (socket != null)
  39. {
  40. try
  41. {
  42. int count = socket.Receive(buffData);
  43. if (count <= 0)
  44. {
  45. Thread.Sleep(1);
  46. return;
  47. }
  48. // LogTool.Log("数据来了" + count);
  49. try
  50. {
  51. int currCount = 0;
  52. int startIndex = 0;
  53. // bool isEnd = false;
  54. while (currCount < count)
  55. {
  56. // isEnd = true;
  57. byte[] data = null;
  58. short sendType = 0;
  59. int cdShort = 0;
  60. if (!isBufferData)
  61. {
  62. if (weiBaoBuffData != null)
  63. {
  64. int maxCount = 6553500;
  65. if (count + weiBaoBuffData.Length > maxCount)
  66. {
  67. maxCount = count + weiBaoBuffData.Length;
  68. }
  69. // LogTool.Log("修复包太短__" + count + "___" + weiBaoBuffData.Length);
  70. byte[] newBuff = new byte[maxCount];
  71. Array.Copy(weiBaoBuffData, 0, newBuff, 0, weiBaoBuffData.Length);
  72. Array.Copy(buffData, 0, newBuff, weiBaoBuffData.Length, count);
  73. buffData = newBuff;
  74. count += weiBaoBuffData.Length;
  75. // LogTool.Log("修复包后太短__" + count);
  76. if (count < 6)
  77. {
  78. weiBaoBuffData = new byte[count];
  79. // LogTool.Log("包太短222__" + count);
  80. Array.Copy(buffData, currCount, weiBaoBuffData, 0, count);
  81. break;
  82. }
  83. else
  84. {
  85. weiBaoBuffData = null;
  86. }
  87. }
  88. else
  89. {
  90. if (count - currCount < 6)
  91. {
  92. weiBaoBuffData = new byte[count];
  93. // LogTool.Log("包太短__" + count);
  94. Array.Copy(buffData, currCount, weiBaoBuffData, 0, count);
  95. break;
  96. }
  97. }
  98. }
  99. if (!isBufferData)
  100. {
  101. sendType = buffData[currCount];
  102. currCount++;
  103. byte[] dataBuffLanl = new byte[4];
  104. dataBuffLanl[0] = buffData[currCount];
  105. currCount++;
  106. dataBuffLanl[1] = buffData[currCount];
  107. currCount++;
  108. dataBuffLanl[2] = buffData[currCount];
  109. currCount++;
  110. dataBuffLanl[3] = buffData[currCount];
  111. currCount++;
  112. cdShort = (SocketTool.ByteToInt(dataBuffLanl));
  113. // LogTool.Log("数据到来" + cdShort + "__" + count + "_" + sendType);
  114. if ((currCount + cdShort) > count) //处理半包情况
  115. {
  116. lastBuffData = new byte[count - currCount];
  117. lastCount = cdShort;
  118. lastSendType = sendType;
  119. Array.Copy(buffData, currCount, lastBuffData, 0, lastBuffData.Length);
  120. // LogTool.Log("数据只有一半" + count + "__" + currCount + "___" + cdShort);
  121. isBufferData = true;
  122. break;
  123. }
  124. else
  125. {
  126. // LogTool.Log(currCount + "_收到长度:" + cdShort + "_Max" + count);
  127. data = new byte[cdShort];
  128. Array.Copy(buffData, currCount, data, 0, data.Length);
  129. }
  130. }
  131. else if (lastCount - lastBuffData.Length > count)
  132. {
  133. // LogTool.Log("数据只有一半的一半" + count + "__" + lastCount + "___" + lastBuffData.Length);
  134. byte[] newLastBuffData = new byte[lastBuffData.Length + count];
  135. Array.Copy(lastBuffData, 0, newLastBuffData, 0, lastBuffData.Length);
  136. Array.Copy(buffData, 0, newLastBuffData, lastBuffData.Length, count);
  137. lastBuffData = newLastBuffData;
  138. break;
  139. }
  140. else if (lastBuffData != null) //处理半包情况
  141. {
  142. isBufferData = false;
  143. // LogTool.Log("修复半包" + lastCount + "__" + count + "___" +
  144. // (lastCount - lastBuffData.Length));
  145. sendType = lastSendType;
  146. data = new byte[lastCount];
  147. cdShort = lastCount - lastBuffData.Length;
  148. Array.Copy(lastBuffData, 0, data, 0, lastBuffData.Length);
  149. Array.Copy(buffData, currCount, data, lastBuffData.Length, cdShort);
  150. }
  151. SendDataType sendDataType = (SendDataType)sendType;
  152. switch (sendDataType)
  153. {
  154. case SendDataType.ShakeHands:
  155. // int gameFrame = 0;
  156. // if (data.Length <= 8)
  157. // {
  158. // long playerId = SocketTool.ByteToLong(data);
  159. // this.playerId = playerId;
  160. // }
  161. // else
  162. {
  163. byte[] pdata = new byte[8];
  164. Array.Copy(data, 0, pdata, 0, pdata.Length);
  165. long playerId = SocketTool.ByteToLong(data);
  166. this.playerId = playerId;
  167. // byte[] fdata = new byte[4];
  168. // Array.Copy(data, 8, fdata, 0, fdata.Length);
  169. // gameFrame = SocketTool.ByteToInt(fdata);
  170. }
  171. _tcpServer.AddServerConnection(playerId, 0, this);
  172. break;
  173. case SendDataType.Data:
  174. CombatSynchronizeRequest combatSynchronizeRequest =
  175. CombatSynchronizeRequest.Parser.ParseFrom(data);
  176. if (combatSynchronizeRequest == null)
  177. {
  178. LogTool.Error("错误的实例化战斗" + data.Length);
  179. }
  180. else
  181. {
  182. // LogTool.Error("收到客户端消息" + combatSynchronizeRequest.CombatSynchronizeType);
  183. _tcpServer.AddCombatSynchronizeRequest(combatSynchronizeRequest);
  184. }
  185. break;
  186. case SendDataType.GetGameFrameByte:
  187. int gameFrame = 0;
  188. byte[] fdata = new byte[4];
  189. Array.Copy(data, 8, fdata, 0, fdata.Length);
  190. gameFrame = SocketTool.ByteToInt(fdata);
  191. _tcpServer.SendGameFrame(gameFrame, this);
  192. break;
  193. }
  194. currCount += cdShort;
  195. startIndex = currCount;
  196. }
  197. }
  198. catch (Exception e)
  199. {
  200. LogTool.Exception(e);
  201. // LogTool.Log("");
  202. }
  203. }
  204. catch (Exception e)
  205. {
  206. LogTool.Log(e);
  207. isConnected = false;
  208. break;
  209. }
  210. }
  211. LogTool.Log("服务器执行完毕");
  212. }
  213. public int SendData(byte[] data, int dataSize)
  214. {
  215. if (socket == null)
  216. {
  217. return -1;
  218. }
  219. try
  220. {
  221. socket.SendAsync(data);
  222. }
  223. catch (Exception e)
  224. {
  225. LogTool.Exception(e);
  226. isConnected = false;
  227. }
  228. return 0;
  229. }
  230. public void Update()
  231. {
  232. }
  233. public void ReceiveAsync()
  234. {
  235. }
  236. public int Input(byte[] data)
  237. {
  238. return 0;
  239. }
  240. public void Dispose()
  241. {
  242. socket?.Close();
  243. socket?.Dispose();
  244. socket = null;
  245. _tcpServer = null;
  246. _udpClientThread = null;
  247. }
  248. }
  249. }
  250. #endif