using System; using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; using System.Linq; using System.Runtime; using System.Text; using System.Threading.Tasks; using WatsonTcp; namespace TcpEventBus { public class WatsonTcpServer : ISocket { public readonly static string SYNC = ("SyncMsg"); public string IP { get; } public int Port { get; } Guid guid = Guid.Empty; public WatsonTcpServer(string ip,int port,int timeout = 5000) { IP = ip; Port = port; IsConnect = false; ReceiveTimeout = timeout; server = new WatsonTcp.WatsonTcpServer(ip,port); server.Events.MessageReceived += (sender, args) => { if (args.Metadata != null && args.Metadata.TryGetValue(nameof(SYNC), out var val) && string.Equals(val.ToString(), SYNC)) return; OnData?.Invoke(args.Data); }; server.Events.ClientConnected += (sender, args) => { guid = args.Client.Guid; OnConnected?.Invoke(); IsConnect = true; }; server.Events.ClientDisconnected += (sender, args) => { guid = Guid.Empty; OnDisconnected?.Invoke(); IsConnect = false; }; server.Settings.NoDelay = true; server.Settings.IdleClientTimeoutSeconds = 5; server.Keepalive.EnableTcpKeepAlives = true; server.Keepalive.TcpKeepAliveTime = 1; server.Keepalive.TcpKeepAliveInterval = 1; server.Keepalive.TcpKeepAliveRetryCount = 3; } private WatsonTcp.WatsonTcpServer server; public int ReceiveTimeout { get; private set; } [AllowNull] public Action OnConnected { get; set; } [AllowNull] public Action> OnData { get; set; } [AllowNull] public Action OnDisconnected { get; set; } public bool IsConnect { get; private set; } public bool IsService => true; public void Disconnect() { server?.Stop(); IsConnect = false; } public void Send(byte[] data) { if (server == null || guid == Guid.Empty) return; try { server.SendAsync(guid, data).Wait(); } catch { } } public void Start() { if (server == null) return; if (server.IsListening) return; server.Start(); } } public class WatsonTcpClient : ISocket { public string IP { get; } public int Port { get; } [AllowNull] private CancellationTokenSource tokenSource; public WatsonTcpClient(string ip, int port, int timeout = 5000) { IP = ip; Port = port; IsConnect = false; ReceiveTimeout = timeout; client = new WatsonTcp.WatsonTcpClient(ip, port); client.Events.MessageReceived += (sender, args) => { OnData?.Invoke(args.Data); }; client.Events.ServerConnected += (sender, args) => { OnConnected?.Invoke(); IsConnect = true; }; client.Events.ServerDisconnected += (sender, args) => { OnDisconnected?.Invoke(); IsConnect = false; }; client.Settings.NoDelay = true; client.Keepalive.EnableTcpKeepAlives = true; client.Keepalive.TcpKeepAliveTime = 1; client.Keepalive.TcpKeepAliveInterval = 1; client.Keepalive.TcpKeepAliveRetryCount = 3; v[nameof(WatsonTcpServer.SYNC)] = WatsonTcpServer.SYNC; } private WatsonTcp.WatsonTcpClient client; public int ReceiveTimeout { get; private set; } [AllowNull] public Action OnConnected { get; set; } [AllowNull] public Action> OnData { get; set; } [AllowNull] public Action OnDisconnected { get; set; } public bool IsConnect { get; private set; } public bool IsService => true; Dictionary v = new Dictionary(); public void Disconnect() { tokenSource?.Cancel(); client?.Disconnect(); IsConnect = false; } public void Send(byte[] data) { if (client == null || !client.Connected) return; client.SendAsync(data).Wait(); } private void SendSync() { if (client == null || !client.Connected) return; client.SendAsync([], v).Wait(); } public void Start() { if (client == null) return; if (client.Connected) return; client.Connect(); tokenSource = new CancellationTokenSource(); Task.Run(async () => { while (!tokenSource.IsCancellationRequested) { SendSync(); await Task.Delay(1000); } },tokenSource.Token); } } }