WatsonTcpServer.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics.CodeAnalysis;
  4. using System.Linq;
  5. using System.Runtime;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using WatsonTcp;
  9. namespace TcpEventBus
  10. {
  11. public class WatsonTcpServer : ISocket
  12. {
  13. public readonly static string SYNC = ("SyncMsg");
  14. public string IP { get; }
  15. public int Port { get; }
  16. Guid guid = Guid.Empty;
  17. public WatsonTcpServer(string ip,int port,int timeout = 5000)
  18. {
  19. IP = ip;
  20. Port = port;
  21. IsConnect = false;
  22. ReceiveTimeout = timeout;
  23. server = new WatsonTcp.WatsonTcpServer(ip,port);
  24. server.Events.MessageReceived += (sender, args) =>
  25. {
  26. if (args.Metadata != null && args.Metadata.TryGetValue(nameof(SYNC), out var val) && string.Equals(val.ToString(), SYNC)) return;
  27. OnData?.Invoke(args.Data);
  28. };
  29. server.Events.ClientConnected += (sender, args) =>
  30. {
  31. guid = args.Client.Guid;
  32. OnConnected?.Invoke();
  33. IsConnect = true;
  34. };
  35. server.Events.ClientDisconnected += (sender, args) =>
  36. {
  37. guid = Guid.Empty;
  38. OnDisconnected?.Invoke();
  39. IsConnect = false;
  40. };
  41. server.Settings.NoDelay = true;
  42. server.Settings.IdleClientTimeoutSeconds = 5;
  43. server.Keepalive.EnableTcpKeepAlives = true;
  44. server.Keepalive.TcpKeepAliveTime = 1;
  45. server.Keepalive.TcpKeepAliveInterval = 1;
  46. server.Keepalive.TcpKeepAliveRetryCount = 3;
  47. }
  48. private WatsonTcp.WatsonTcpServer server;
  49. public int ReceiveTimeout { get; private set; }
  50. [AllowNull]
  51. public Action OnConnected { get; set; }
  52. [AllowNull]
  53. public Action<ArraySegment<byte>> OnData { get; set; }
  54. [AllowNull]
  55. public Action OnDisconnected { get; set; }
  56. public bool IsConnect { get; private set; }
  57. public bool IsService => true;
  58. public void Disconnect()
  59. {
  60. server?.Stop();
  61. IsConnect = false;
  62. }
  63. public void Send(byte[] data)
  64. {
  65. if (server == null || guid == Guid.Empty) return;
  66. try
  67. {
  68. server.SendAsync(guid, data).Wait();
  69. }
  70. catch
  71. {
  72. }
  73. }
  74. public void Start()
  75. {
  76. if (server == null) return;
  77. if (server.IsListening) return;
  78. server.Start();
  79. }
  80. }
  81. public class WatsonTcpClient : ISocket
  82. {
  83. public string IP { get; }
  84. public int Port { get; }
  85. [AllowNull]
  86. private CancellationTokenSource tokenSource;
  87. public WatsonTcpClient(string ip, int port, int timeout = 5000)
  88. {
  89. IP = ip;
  90. Port = port;
  91. IsConnect = false;
  92. ReceiveTimeout = timeout;
  93. client = new WatsonTcp.WatsonTcpClient(ip, port);
  94. client.Events.MessageReceived += (sender, args) =>
  95. {
  96. OnData?.Invoke(args.Data);
  97. };
  98. client.Events.ServerConnected += (sender, args) =>
  99. {
  100. OnConnected?.Invoke();
  101. IsConnect = true;
  102. };
  103. client.Events.ServerDisconnected += (sender, args) =>
  104. {
  105. OnDisconnected?.Invoke();
  106. IsConnect = false;
  107. };
  108. client.Settings.NoDelay = true;
  109. client.Keepalive.EnableTcpKeepAlives = true;
  110. client.Keepalive.TcpKeepAliveTime = 1;
  111. client.Keepalive.TcpKeepAliveInterval = 1;
  112. client.Keepalive.TcpKeepAliveRetryCount = 3;
  113. v[nameof(WatsonTcpServer.SYNC)] = WatsonTcpServer.SYNC;
  114. }
  115. private WatsonTcp.WatsonTcpClient client;
  116. public int ReceiveTimeout { get; private set; }
  117. [AllowNull]
  118. public Action OnConnected { get; set; }
  119. [AllowNull]
  120. public Action<ArraySegment<byte>> OnData { get; set; }
  121. [AllowNull]
  122. public Action OnDisconnected { get; set; }
  123. public bool IsConnect { get; private set; }
  124. public bool IsService => true;
  125. Dictionary<string, object> v = new Dictionary<string, object>();
  126. public void Disconnect()
  127. {
  128. tokenSource?.Cancel();
  129. client?.Disconnect();
  130. IsConnect = false;
  131. }
  132. public void Send(byte[] data)
  133. {
  134. if (client == null || !client.Connected) return;
  135. client.SendAsync(data).Wait();
  136. }
  137. private void SendSync()
  138. {
  139. if (client == null || !client.Connected) return;
  140. client.SendAsync([], v).Wait();
  141. }
  142. public void Start()
  143. {
  144. if (client == null) return;
  145. if (client.Connected) return;
  146. client.Connect();
  147. tokenSource = new CancellationTokenSource();
  148. Task.Run(async () =>
  149. {
  150. while (!tokenSource.IsCancellationRequested)
  151. {
  152. SendSync();
  153. await Task.Delay(1000);
  154. }
  155. },tokenSource.Token);
  156. }
  157. }
  158. }