#region copyright //------------------------------------------------------------------------------ // 此代码版权声明为全文件覆盖,如有原作者特别声明,会在下方手动补充 // 此代码版权(除特别声明外的代码)归作者本人Diego所有 // 源代码使用协议遵循本仓库的开源协议及附加协议 // Gitee源代码仓库:https://gitee.com/diego2098/ThingsGateway // Github源代码仓库:https://github.com/kimdiego2098/ThingsGateway // 使用文档:https://diego2098.gitee.io/thingsgateway-docs/ // QQ群:605534569 //------------------------------------------------------------------------------ #endregion namespace ThingsGateway.Foundation; /// /// 服务设备 /// public abstract class ReadWriteDevicesTcpServerBase : ReadWriteDevicesClientBase { /// public ReadWriteDevicesTcpServerBase(TcpService tcpService) { TcpService = tcpService; TcpService.Connecting += Connecting; TcpService.Connected += Connected; TcpService.Received += Received; TcpService.Disconnecting += Disconnecting; TcpService.Disconnected += Disconnected; Logger = TcpService.Logger; } /// /// 服务管理对象 /// public TcpService TcpService { get; } /// public override Task> SendThenResponseAsync(byte[] data, WaitingOptions waitingOptions = null, CancellationToken token = default) { throw new NotImplementedException(); } /// public override OperResult SendThenResponse(byte[] data, WaitingOptions waitingOptions = null, CancellationToken token = default) { throw new NotImplementedException(); } /// public override void SetDataAdapter() { throw new NotImplementedException(); } /// public override Task ConnectAsync(CancellationToken token) { return Task.Run(() => TcpService.Start()); } /// public override void Connect(CancellationToken token) { TcpService.Start(); } /// public override void Disconnect() { TcpService.Stop(); } /// /// 设置适配器 /// /// 客户端 public abstract void SetDataAdapter(SocketClient client); /// public override string ToString() { return TcpService.ServerName; } /// protected override void Dispose(bool disposing) { TcpService.Connecting -= Connecting; TcpService.Connected -= Connected; TcpService.Disconnecting -= Disconnecting; TcpService.Disconnected -= Disconnected; TcpService.Stop(); TcpService.SafeDispose(); base.Dispose(disposing); } /// /// 接收解析 /// protected abstract void Received(SocketClient client, IRequestInfo requestInfo); private void Connected(SocketClient client, ConnectedEventArgs e) { Logger?.Debug(client.IP + ":" + client.Port + "连接成功"); } private void Connecting(SocketClient client, ConnectingEventArgs e) { Logger?.Debug(client.IP + ":" + client.Port + "正在连接"); SetDataAdapter(client); } private void Disconnected(ITcpClientBase client, DisconnectEventArgs e) { Logger?.Debug(client.IP + ":" + client.Port + "断开连接-" + e.Message); } private void Disconnecting(ITcpClientBase client, DisconnectEventArgs e) { Logger?.Debug(client.IP + ":" + client.Port + "正在主动断开连接-" + e.Message); } private void Received(SocketClient client, ByteBlock byteBlock, IRequestInfo requestInfo) { try { Received(client, requestInfo); } catch (Exception ex) { Logger.Exception(this, ex); } } }