TCPServerConnection.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. using System.Net.Sockets;
  2. using NetCore;
  3. using NetCore.ContentParse;
  4. using NetCore.Protocol;
  5. namespace NetServer.NetLink.TCP;
  6. public class TCPServerConnection<T, K> : IContentReceiver, IConnection
  7. where T : IContentParse where K : IProtocol
  8. {
  9. // public long index;
  10. public bool isConnected { get; set; }
  11. private byte[] buffData = new byte[6553];
  12. private Socket socket;
  13. //记录半包数据
  14. private bool isBufferData;
  15. private byte[] lastBuffData;
  16. private int lastCount;
  17. private short lastSendType;
  18. //数据结束
  19. //尾包
  20. byte[] weiBaoBuffData;
  21. private Thread _udpClientThread;
  22. private TCPServer<T, K> _tcpServer;
  23. public long ConnectionId { get; set; }
  24. public IContentParse iContentParse
  25. {
  26. get { return _iContentParse; }
  27. }
  28. public IProtocol iProtocol
  29. {
  30. get { return _iProtocol; }
  31. }
  32. private IContentParse _iContentParse;
  33. public IProtocol _iProtocol;
  34. public long HeartbeatLasetTime;
  35. public TCPServerConnection(TCPServer<T, K> tcpServer, Socket socket)
  36. {
  37. this.socket = socket;
  38. socket.NoDelay = true;
  39. _tcpServer = tcpServer;
  40. isConnected = true;
  41. _iContentParse = Activator.CreateInstance<T>();
  42. _iProtocol = Activator.CreateInstance<K>();
  43. _udpClientThread = new Thread(TheUpdate);
  44. _udpClientThread.Start();
  45. HeartbeatLasetTime = DateTime.Now.Ticks;
  46. }
  47. private void TheUpdate()
  48. {
  49. while (socket != null)
  50. {
  51. try
  52. {
  53. int count = socket.Receive(buffData);
  54. if (count <= 0)
  55. {
  56. Thread.Sleep(1);
  57. return;
  58. }
  59. byte[] data = new byte[count];
  60. Array.Copy(buffData, 0, data, 0, count);
  61. _iContentParse.ParseByte(data, _iProtocol, this);
  62. // LogTool.Log("数据来了" + count);
  63. }
  64. catch (Exception e)
  65. {
  66. Console.WriteLine(e);
  67. isConnected = false;
  68. break;
  69. }
  70. }
  71. Console.WriteLine("服务器执行完毕");
  72. _tcpServer.RemoveConnection(this);
  73. }
  74. public void AddContent(byte[] data)
  75. {
  76. if (data == null)
  77. {
  78. return;
  79. }
  80. if (data.Length == 1 && data[0] == 1)
  81. {
  82. // 心跳
  83. HeartbeatLasetTime = DateTime.Now.Ticks;
  84. return;
  85. }
  86. object serializeData = iProtocol.Deserialize(data);
  87. _tcpServer.LogicalParsing.Logic(serializeData, this);
  88. }
  89. public void SendData(object sendData)
  90. {
  91. byte[] data = iProtocol.Serialize(sendData);
  92. if (data == null)
  93. {
  94. return;
  95. }
  96. byte[] parseData = _iContentParse.AssembleByte(data);
  97. socket.SendAsync(parseData, SocketFlags.None);
  98. }
  99. public void Dispose()
  100. {
  101. socket?.Close();
  102. socket?.Dispose();
  103. socket = null;
  104. _tcpServer = null;
  105. _udpClientThread = null;
  106. lastBuffData = null;
  107. weiBaoBuffData = null;
  108. buffData = null;
  109. }
  110. }