123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172 |
- 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<ArraySegment<byte>> 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<ArraySegment<byte>> OnData { get; set; }
- [AllowNull]
- public Action OnDisconnected { get; set; }
- public bool IsConnect { get; private set; }
- public bool IsService => true;
- Dictionary<string, object> v = new Dictionary<string, object>();
- 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);
- }
- }
- }
|