TCPLink.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547
  1. // using System.Net.Sockets;
  2. // using NetCore.ContentParse;
  3. // using NetCore.NetServerCoreBasic;
  4. // using NetCore.Protocol;
  5. //
  6. // namespace GameServer.NetLink;
  7. //
  8. // public class TCPLink : INetContainer
  9. // {
  10. // public void Start()
  11. // {
  12. // }
  13. //
  14. // public void Init(IContentParse iContentParse, IProtocol iProtocol)
  15. // {
  16. // }
  17. //
  18. // public void AddGetData(object data)
  19. // {
  20. // }
  21. //
  22. //
  23. // private byte[] buffData = new byte[6553500];
  24. // private Socket _tcpClient;
  25. // private Thread thread;
  26. // public BinaryReader br;
  27. // public BinaryWriter bw;
  28. // public string url;
  29. // private bool isRun;
  30. // private long jg;
  31. // private Thread heartThread;
  32. // private Queue<SendBuffer> _sendBuffers = new Queue<SendBuffer>();
  33. // private int allCount;
  34. // private SocketAsyncEventArgs sae;
  35. //
  36. // //记录半包数据
  37. // private bool isBufferData;
  38. // private byte[] lastBuffData;
  39. // private int lastCount;
  40. //
  41. // private ushort lastXueLieHao;
  42. // //数据结束
  43. //
  44. // //尾包
  45. // private byte[] weiBaoBuffData;
  46. //
  47. //
  48. // //
  49. // public class RequestBuffer
  50. // {
  51. // // public HttpListenerContext httpListenerContext;
  52. // public ushort xlh;
  53. // public SimulationCombat combatRequest;
  54. // }
  55. //
  56. // public enum SendBufferType
  57. // {
  58. // Data,
  59. // Connect,
  60. // }
  61. //
  62. // public class SendBuffer
  63. // {
  64. // public RequestBuffer RequestBuffer;
  65. // public SimulationCombatResponse SimulationFightTogetherResponse;
  66. // public SendBufferType SendBufferType;
  67. //
  68. // /// <summary>
  69. // /// 版本
  70. // /// </summary>
  71. // public ushort ver;
  72. // }
  73. //
  74. // public Queue<RequestBuffer> requestBuffer = new Queue<RequestBuffer>();
  75. //
  76. // public void Init(string ip, int port)
  77. // {
  78. // this.url = ip;
  79. // LogTool.Log("开启协议");
  80. // _tcpClient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  81. // _tcpClient.Connect(ip, port);
  82. //
  83. // isRun = true;
  84. // LogTool.Log("启动成功" + url);
  85. // heartThread = new Thread(HrartUpdate);
  86. // heartThread.Start();
  87. // thread = new Thread(TheUpdate);
  88. // thread.Start();
  89. // }
  90. //
  91. // private void HrartUpdate()
  92. // {
  93. // while (isRun)
  94. // {
  95. // if (_tcpClient != null)
  96. // {
  97. // try
  98. // {
  99. // SendBuffer sbs = null;
  100. // lock (_sendBuffers)
  101. // {
  102. // if (!_sendBuffers.TryDequeue(out sbs))
  103. // {
  104. // }
  105. // }
  106. //
  107. // if (sbs != null)
  108. // {
  109. // byte[] data = GetSendData(sbs);
  110. // _tcpClient.Send(data);
  111. // }
  112. //
  113. // long currTime = DateTime.Now.Ticks / 10000;
  114. // double kk = currTime - jg;
  115. // if (kk > 5 * 1000)
  116. // {
  117. // jg = currTime;
  118. // byte[] cdByte = IntToByte(1);
  119. // byte[] mdata = new byte[5];
  120. // mdata[0] = cdByte[0];
  121. // mdata[1] = cdByte[1];
  122. // mdata[2] = cdByte[2];
  123. // mdata[3] = cdByte[3];
  124. // mdata[4] = 2;
  125. // _tcpClient.Send(mdata);
  126. // }
  127. // }
  128. // catch (Exception e)
  129. // {
  130. // Console.WriteLine(e);
  131. // // isRun = false;
  132. // Dispose();
  133. // }
  134. // }
  135. //
  136. //
  137. // Thread.Sleep(1);
  138. // }
  139. // }
  140. //
  141. // private byte[] ShortToByte(short number)
  142. // {
  143. // byte[] numberBytes = BitConverter.GetBytes(number).Reverse().ToArray();
  144. // return numberBytes;
  145. // }
  146. //
  147. // private byte[] UShortToByte(ushort number)
  148. // {
  149. // byte[] numberBytes = BitConverter.GetBytes(number).Reverse().ToArray();
  150. // return numberBytes;
  151. // }
  152. //
  153. // private byte[] IntToByte(int number)
  154. // {
  155. // byte[] numberBytes = BitConverter.GetBytes(number).Reverse().ToArray();
  156. // return numberBytes;
  157. // }
  158. //
  159. // private short ByteToShort(byte[] numberBytes, bool isReverse = false)
  160. // {
  161. // if (isReverse)
  162. // {
  163. // short converted = BitConverter.ToInt16(numberBytes.Reverse().ToArray(), 0);
  164. // return converted;
  165. // }
  166. // else
  167. // {
  168. // short converted = BitConverter.ToInt16(numberBytes, 0);
  169. // return converted;
  170. // }
  171. // }
  172. //
  173. // private ushort ByteToUshort(byte[] numberBytes, bool isReverse = false)
  174. // {
  175. // if (isReverse)
  176. // {
  177. // ushort converted = BitConverter.ToUInt16(numberBytes.Reverse().ToArray(), 0);
  178. // return converted;
  179. // }
  180. // else
  181. // {
  182. // ushort converted = BitConverter.ToUInt16(numberBytes, 0);
  183. // return converted;
  184. // }
  185. // }
  186. //
  187. // private int ByteToInt(byte[] numberBytes, bool isReverse = false)
  188. // {
  189. // if (isReverse)
  190. // {
  191. // int converted = BitConverter.ToInt32(numberBytes.Reverse().ToArray(), 0);
  192. // return converted;
  193. // }
  194. // else
  195. // {
  196. // int converted = BitConverter.ToInt32(numberBytes, 0);
  197. // return converted;
  198. // }
  199. // }
  200. //
  201. //
  202. // private void TheUpdate()
  203. // {
  204. // while (isRun)
  205. // {
  206. // // if (sae == null)
  207. // // {
  208. // // SendAe();
  209. // // }
  210. //
  211. // try
  212. // {
  213. // // SocketAsyncEventArgs sae=new SocketAsyncEventArgs();
  214. // // sae.SetBuffer(buffData);
  215. //
  216. // int count = _tcpClient.Receive(buffData);
  217. //
  218. // // if (count <= 4)
  219. // // {
  220. // // Thread.Sleep(1);
  221. // // continue;
  222. // // }
  223. // // LogTool.Log("数据来了" + count);
  224. // // byte[] currBuferr=new byte[count];
  225. // // Array.Copy(buffData, 0, currBuferr, 0,count);
  226. // // currBuferr = currBuferr.Reverse().ToArray();
  227. // try
  228. // {
  229. // int currCount = 0;
  230. // int startIndex = 0;
  231. //
  232. // // bool isEnd = false;
  233. // while (currCount < count)
  234. // {
  235. // // isEnd = true;
  236. // byte[] data = null;
  237. // ushort xueLieHao = 1;
  238. // int cdShort = 0;
  239. // // if (isWeiBao&&lastBuffData!=null)
  240. // // {
  241. // // LogTool.Log("修复尾包"+lastBuffData.Length+"__"+count);
  242. // // isWeiBao = false;
  243. // // int maxCount = 6553500;
  244. // // if (count + lastBuffData.Length > maxCount)
  245. // // {
  246. // // maxCount = count + lastBuffData.Length;
  247. // // }
  248. // // byte[] newBuffData = new byte[6553500];
  249. // // Array.Copy(lastBuffData, 0, newBuffData, 0, lastBuffData.Length);
  250. // // Array.Copy(buffData, 0, newBuffData, lastBuffData.Length, count);
  251. // // count += lastBuffData.Length;
  252. // // buffData = newBuffData;
  253. // // lastBuffData = null;
  254. // // }
  255. // if (!isBufferData)
  256. // {
  257. // if (weiBaoBuffData != null)
  258. // {
  259. // int maxCount = 6553500;
  260. // if (count + weiBaoBuffData.Length > maxCount)
  261. // {
  262. // maxCount = count + weiBaoBuffData.Length;
  263. // }
  264. //
  265. // // LogTool.Log("修复包太短__" + count + "___" + weiBaoBuffData.Length);
  266. // byte[] newBuff = new byte[maxCount];
  267. // Array.Copy(weiBaoBuffData, 0, newBuff, 0, weiBaoBuffData.Length);
  268. // Array.Copy(buffData, 0, newBuff, weiBaoBuffData.Length, count);
  269. //
  270. // buffData = newBuff;
  271. // count += weiBaoBuffData.Length;
  272. // // LogTool.Log("修复包后太短__" + count);
  273. // if (count < 6)
  274. // {
  275. // weiBaoBuffData = new byte[count];
  276. // // LogTool.Log("包太短222__" + count);
  277. // Array.Copy(buffData, currCount, weiBaoBuffData, 0, count);
  278. // break;
  279. // }
  280. // else
  281. // {
  282. // weiBaoBuffData = null;
  283. // }
  284. // }
  285. // else
  286. // {
  287. // if (count - currCount < 6)
  288. // {
  289. // weiBaoBuffData = new byte[count];
  290. // // LogTool.Log("包太短__" + count);
  291. // Array.Copy(buffData, currCount, weiBaoBuffData, 0, count);
  292. // break;
  293. // }
  294. // }
  295. // }
  296. //
  297. // if (!isBufferData)
  298. // {
  299. // byte[] cdByte = new byte[2];
  300. // cdByte[0] = buffData[currCount];
  301. // currCount++;
  302. // cdByte[1] = buffData[currCount];
  303. // currCount++;
  304. // xueLieHao = ByteToUshort(cdByte, true);
  305. //
  306. // byte[] dataBuffLanl = new byte[4];
  307. // dataBuffLanl[0] = buffData[currCount];
  308. // currCount++;
  309. // dataBuffLanl[1] = buffData[currCount];
  310. // currCount++;
  311. // dataBuffLanl[2] = buffData[currCount];
  312. // currCount++;
  313. // dataBuffLanl[3] = buffData[currCount];
  314. // currCount++;
  315. // cdShort = (ByteToInt(dataBuffLanl, true));
  316. // // LogTool.Log("数据到来" + cdShort + "__" + count + "_" + xueLieHao);
  317. // if ((currCount + cdShort) > count) //处理半包情况
  318. // {
  319. // lastBuffData = new byte[count - currCount];
  320. // lastCount = cdShort;
  321. // lastXueLieHao = xueLieHao;
  322. // Array.Copy(buffData, currCount, lastBuffData, 0, lastBuffData.Length);
  323. // // LogTool.Log("数据只有一半" + count + "__" + currCount + "___" + cdShort);
  324. // isBufferData = true;
  325. // break;
  326. // }
  327. // else
  328. // {
  329. // // LogTool.Log(currCount + "_收到长度:" + cdShort + "_Max" + count);
  330. // data = new byte[cdShort];
  331. // Array.Copy(buffData, currCount, data, 0, data.Length);
  332. // }
  333. // }
  334. // else if (lastCount - lastBuffData.Length > count)
  335. // {
  336. // // LogTool.Log("数据只有一半的一半" + count + "__" + lastCount + "___" + lastBuffData.Length);
  337. // byte[] newLastBuffData = new byte[lastBuffData.Length + count];
  338. // Array.Copy(lastBuffData, 0, newLastBuffData, 0, lastBuffData.Length);
  339. // Array.Copy(buffData, 0, newLastBuffData, lastBuffData.Length, count);
  340. // lastBuffData = newLastBuffData;
  341. // break;
  342. // }
  343. //
  344. // else if (lastBuffData != null) //处理半包情况
  345. // {
  346. // isBufferData = false;
  347. // // LogTool.Log("修复半包" + lastCount + "__" + count + "___" +
  348. // // (lastCount - lastBuffData.Length));
  349. // xueLieHao = lastXueLieHao;
  350. // data = new byte[lastCount];
  351. // cdShort = lastCount - lastBuffData.Length;
  352. // Array.Copy(lastBuffData, 0, data, 0, lastBuffData.Length);
  353. // Array.Copy(buffData, currCount, data, lastBuffData.Length, cdShort);
  354. // }
  355. //
  356. // SimulationCombat cr = SimulationCombat.Parser.ParseFrom(data);
  357. // RequestBuffer buffer = new RequestBuffer();
  358. // buffer.combatRequest = cr;
  359. // buffer.xlh = xueLieHao;
  360. // // allCount++;
  361. // // LogTool.Log("shoudao "+allCount);
  362. // // buffer.httpListenerContext = httpListenerContext;
  363. // LogTool.Log("收到中转服请求" + cr.CombatType + "___" + xueLieHao);
  364. // AddBuffer(buffer);
  365. // currCount += cdShort;
  366. // startIndex = currCount;
  367. // }
  368. //
  369. // // if (isEnd&&currCount < count&&!isBufferData)//尾包
  370. // // {
  371. // // LogTool.Log("尾巴处理"+currCount+"___"+count);
  372. // // isWeiBao = true;
  373. // // lastBuffData = new byte[count - currCount];
  374. // // Array.Copy(buffData, currCount, lastBuffData, 0, lastBuffData.Length);
  375. // //
  376. // // }
  377. // }
  378. // catch (Exception e)
  379. // {
  380. // // RequestBuffer buffer = new RequestBuffer();
  381. // // buffer.combatRequest = cr;
  382. // LogTool.Log(e);
  383. //
  384. // Send(null, null);
  385. // // LogTool.Log("");
  386. // }
  387. // }
  388. // catch (Exception e)
  389. // {
  390. // LogTool.Log(e);
  391. // break;
  392. // }
  393. // }
  394. //
  395. // LogTool.Log("服务器执行完毕");
  396. // }
  397. //
  398. //
  399. // public void AddBuffer(RequestBuffer buffer)
  400. // {
  401. // lock (requestBuffer)
  402. // {
  403. // requestBuffer.Enqueue(buffer);
  404. // }
  405. // }
  406. //
  407. // public RequestBuffer GetRequestBuffer()
  408. // {
  409. // lock (requestBuffer)
  410. // {
  411. // if (requestBuffer.TryDequeue(out RequestBuffer buffer))
  412. // {
  413. // return buffer;
  414. // }
  415. //
  416. // return null;
  417. // }
  418. // }
  419. //
  420. // private byte[] GetSendData(SendBuffer sendBuffer)
  421. // {
  422. // byte[] mdata = null;
  423. // if (sendBuffer != null)
  424. // {
  425. // if (sendBuffer.SimulationFightTogetherResponse != null)
  426. // {
  427. // byte[] data = null;
  428. // int size = sendBuffer.SimulationFightTogetherResponse.CalculateSize();
  429. // data = new byte[size];
  430. // CodedOutputStream cos = new CodedOutputStream(data);
  431. // sendBuffer.SimulationFightTogetherResponse.WriteTo(cos);
  432. // cos.CheckNoSpaceLeft();
  433. // LogTool.Log("回复消息" + sendBuffer.RequestBuffer.xlh + "__" + data.Length);
  434. // mdata = new byte[data.Length + 7];
  435. // byte[] zcd = IntToByte((mdata.Length - 4));
  436. //
  437. //
  438. // mdata[0] = zcd[0];
  439. // mdata[1] = zcd[1];
  440. // mdata[2] = zcd[2];
  441. // mdata[3] = zcd[3];
  442. //
  443. // // LogTool.Log( "mdata"+[0]+"_"+ mdata[1]+"_"+ mdata[2]+"_"+ mdata[3]);
  444. //
  445. // mdata[4] = 1;
  446. // byte[] cdByte = UShortToByte(sendBuffer.RequestBuffer.xlh);
  447. // mdata[5] = cdByte[0];
  448. // mdata[6] = cdByte[1];
  449. //
  450. // Array.Copy(data, 0, mdata, 7, data.Length);
  451. // }
  452. // else
  453. // {
  454. // if (sendBuffer.SendBufferType == SendBufferType.Connect)
  455. // {
  456. // byte[] cdByte = IntToByte(3);
  457. // mdata = new byte[7];
  458. // mdata[0] = cdByte[0];
  459. // mdata[1] = cdByte[1];
  460. // mdata[2] = cdByte[2];
  461. // mdata[3] = cdByte[3];
  462. // mdata[4] = 0;
  463. // cdByte = UShortToByte(sendBuffer.ver);
  464. // mdata[5] = cdByte[0];
  465. // mdata[6] = cdByte[1];
  466. // }
  467. // else
  468. // {
  469. // byte[] cdByte = IntToByte(3);
  470. // mdata = new byte[7];
  471. // mdata[0] = cdByte[0];
  472. // mdata[1] = cdByte[1];
  473. // mdata[2] = cdByte[2];
  474. // mdata[3] = cdByte[3];
  475. //
  476. // mdata[4] = 3;
  477. // cdByte = UShortToByte(sendBuffer.RequestBuffer.xlh);
  478. // mdata[5] = cdByte[0];
  479. // mdata[6] = cdByte[1];
  480. // }
  481. // }
  482. // }
  483. // else
  484. // {
  485. // LogTool.Log("回复消息" + 125);
  486. //
  487. // byte[] cdByte = IntToByte(1);
  488. // mdata = new byte[7];
  489. // mdata[0] = cdByte[0];
  490. // mdata[1] = cdByte[1];
  491. // mdata[2] = cdByte[2];
  492. // mdata[3] = cdByte[3];
  493. //
  494. // mdata[4] = 4;
  495. // }
  496. //
  497. //
  498. // return mdata;
  499. // }
  500. //
  501. // public void Send(RequestBuffer buffer, SimulationCombatResponse resp,
  502. // SendBufferType sendBufferType = SendBufferType.Data)
  503. // {
  504. // lock (_sendBuffers)
  505. // {
  506. // SendBuffer sendBuffer = new SendBuffer();
  507. // sendBuffer.RequestBuffer = buffer;
  508. // sendBuffer.SimulationFightTogetherResponse = resp;
  509. // sendBuffer.SendBufferType = sendBufferType;
  510. // _sendBuffers.Enqueue(sendBuffer);
  511. // }
  512. // }
  513. //
  514. // public void Send(SendBuffer sendBuffer)
  515. // {
  516. // lock (_sendBuffers)
  517. // {
  518. // // SendBuffer sendBuffer = new SendBuffer();
  519. // // sendBuffer.RequestBuffer = buffer;
  520. // // sendBuffer.SimulationFightTogetherResponse = resp;
  521. // // sendBuffer.SendBufferType = sendBufferType;
  522. // _sendBuffers.Enqueue(sendBuffer);
  523. // }
  524. // }
  525. //
  526. // public void Dispose()
  527. // {
  528. // isRun = false;
  529. // if (thread != null)
  530. // {
  531. // _tcpClient.Close();
  532. //
  533. // thread.Abort();
  534. // }
  535. //
  536. // if (heartThread != null)
  537. // {
  538. // heartThread.Abort();
  539. // }
  540. //
  541. // heartThread = null;
  542. // thread = null;
  543. // }
  544. // }
  545. //
  546. // }
  547. // }