TCPClient.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. using System;
  2. using System.Net;
  3. using System.Net.Sockets;
  4. using System.Threading;
  5. using System.Threading.Tasks;
  6. using Fort23.UTool;
  7. using NetCore;
  8. using NetCore.ContentParse;
  9. using NetCore.Protocol;
  10. namespace NetClientCore.TCP
  11. {
  12. public class TCPClient<T, K> : IContentReceiver, IConnection
  13. where T : IContentParse where K : IProtocol
  14. {
  15. private Socket socket;
  16. public long ConnectionId { get; set; }
  17. private byte[] buffData = new byte[655300];
  18. public bool isConnected { get; set; }
  19. public IContentParse iContentParse
  20. {
  21. get { return _iContentParse; }
  22. }
  23. public IProtocol iProtocol
  24. {
  25. get { return _iProtocol; }
  26. }
  27. private IContentParse _iContentParse;
  28. public IProtocol _iProtocol;
  29. private Thread _udpClientThread;
  30. private Thread _heartbeat;
  31. private ILogicalParsing LogicalParsing;
  32. public async Task Connect(string ip, int port, ILogicalParsing LogicalParsing)
  33. {
  34. this.LogicalParsing = LogicalParsing;
  35. socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  36. await socket.ConnectAsync(ip, port);
  37. _iContentParse = Activator.CreateInstance<T>();
  38. _iProtocol = Activator.CreateInstance<K>();
  39. _udpClientThread = new Thread(TheUpdate);
  40. _udpClientThread.Start();
  41. _heartbeat = new Thread(HeartbeatUpdate);
  42. _heartbeat.Start();
  43. }
  44. private void HeartbeatUpdate()
  45. {
  46. while (socket != null)
  47. {
  48. SendHeartbeat();
  49. Thread.Sleep(10000);
  50. }
  51. }
  52. private void TheUpdate()
  53. {
  54. while (socket != null)
  55. {
  56. try
  57. {
  58. int count = socket.Receive(buffData);
  59. if (count <= 0)
  60. {
  61. Thread.Sleep(1);
  62. return;
  63. }
  64. byte[] data = new byte[count];
  65. Array.Copy(buffData, 0, data, 0, count);
  66. _iContentParse.ParseByte(data, _iProtocol, this);
  67. // LogTool.Log("数据来了" + count);
  68. }
  69. catch (Exception e)
  70. {
  71. LogTool.Error(e);
  72. isConnected = false;
  73. break;
  74. }
  75. }
  76. LogTool.Log("服务器执行完毕");
  77. }
  78. public void AddContent(byte[] data)
  79. {
  80. if (data == null)
  81. {
  82. return;
  83. }
  84. if (data.Length == 1 && data[0] == 1)
  85. {
  86. // 心跳
  87. return;
  88. }
  89. object serializeData = iProtocol.Deserialize(data);
  90. LogicalParsing.Logic(serializeData, this);
  91. }
  92. public void SendData(object sendData)
  93. {
  94. byte[] data = iProtocol.Serialize(sendData);
  95. byte[] parseData = _iContentParse.AssembleByte(data);
  96. socket.SendAsync(parseData, SocketFlags.None);
  97. }
  98. /// <summary>
  99. /// 发送心跳
  100. /// </summary>
  101. private void SendHeartbeat()
  102. {
  103. byte[] bytes = new byte[] { 1 };
  104. byte[] parseData = _iContentParse.AssembleByte(bytes);
  105. socket.SendAsync(parseData, SocketFlags.None);
  106. }
  107. public void Dispose()
  108. {
  109. socket?.Dispose();
  110. socket?.Close();
  111. socket = null;
  112. _iContentParse = null;
  113. _iProtocol = null;
  114. _udpClientThread = null;
  115. _heartbeat = null;
  116. LogicalParsing = null;
  117. }
  118. }
  119. }