TCPServer.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. using System.Collections.Concurrent;
  2. using System.Net;
  3. using System.Net.Sockets;
  4. using NetCore;
  5. using NetCore.ContentParse;
  6. using NetCore.NetServerCoreBasic;
  7. using NetCore.Protocol;
  8. namespace NetServer.NetLink.TCP;
  9. public class TCPServer<T, K> : IServer where T : IContentParse where K : IProtocol
  10. {
  11. // private byte[] buffData = new byte[6553];
  12. private Socket socket;
  13. public ILogicalParsing LogicalParsing
  14. {
  15. get { return _logicalParsing; }
  16. }
  17. private ILogicalParsing _logicalParsing;
  18. //记录半包数据
  19. private bool isBufferData;
  20. private byte[] lastBuffData;
  21. private int lastCount;
  22. private short lastXueLieHao;
  23. //数据结束
  24. //尾包
  25. private byte[] weiBaoBuffData;
  26. private long index;
  27. private ConcurrentDictionary<long, TCPServerConnection<T, K>> allTcpServerConnections =
  28. new ConcurrentDictionary<long, TCPServerConnection<T, K>>();
  29. private Thread _ConnectionUpdateThread;
  30. public TCPServer(int port, ILogicalParsing logicalParsing)
  31. {
  32. _logicalParsing = logicalParsing;
  33. socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  34. socket.Bind(new IPEndPoint(IPAddress.Any, port));
  35. socket.Listen();
  36. AcceptAsync();
  37. _ConnectionUpdateThread = new Thread(ConnectionUpdate);
  38. _ConnectionUpdateThread.Start();
  39. }
  40. private void ConnectionUpdate()
  41. {
  42. while (socket != null)
  43. {
  44. long currTime = DateTime.Now.Ticks;
  45. foreach (var VARIABLE in allTcpServerConnections)
  46. {
  47. long lasetHearTime = VARIABLE.Value.HeartbeatLasetTime;
  48. if (currTime - lasetHearTime > 1200000000) //超时
  49. {
  50. RemoveConnection(VARIABLE.Value);
  51. }
  52. }
  53. Thread.Sleep(1000);
  54. }
  55. }
  56. private async Task AcceptAsync()
  57. {
  58. try
  59. {
  60. if (socket == null)
  61. {
  62. return;
  63. }
  64. Socket newSocket = await socket.AcceptAsync();
  65. AcceptAsync();
  66. int code = newSocket.RemoteEndPoint.GetHashCode();
  67. TCPServerConnection<T, K> tcpServerConnection = new TCPServerConnection<T, K>(this, newSocket);
  68. long currIndex = Interlocked.Add(ref index, 1);
  69. // long currIndex = index;
  70. tcpServerConnection.ConnectionId = currIndex;
  71. bool isAdd = allTcpServerConnections.TryAdd(currIndex, tcpServerConnection);
  72. if (!isAdd)
  73. {
  74. Console.Error.WriteLine("添加失败");
  75. newSocket.Close();
  76. newSocket.Dispose();
  77. }
  78. else
  79. {
  80. _logicalParsing.AddConnection(tcpServerConnection);
  81. }
  82. }
  83. catch (Exception e)
  84. {
  85. Console.WriteLine(e);
  86. }
  87. }
  88. public void RemoveConnection(TCPServerConnection<T, K> tcpServerConnection)
  89. {
  90. bool isOk = allTcpServerConnections.TryRemove(tcpServerConnection.ConnectionId,
  91. out TCPServerConnection<T, K> value);
  92. if (!isOk)
  93. {
  94. Console.Error.WriteLine("移除失败");
  95. }
  96. else
  97. {
  98. tcpServerConnection.Dispose();
  99. _logicalParsing.RemoveConnection(tcpServerConnection);
  100. }
  101. }
  102. }