diff --git a/src/Portalum.Zvt/Portalum.Zvt.csproj b/src/Portalum.Zvt/Portalum.Zvt.csproj index de384ac..9ce5a64 100644 --- a/src/Portalum.Zvt/Portalum.Zvt.csproj +++ b/src/Portalum.Zvt/Portalum.Zvt.csproj @@ -17,14 +17,14 @@ https://github.com/Portalum/Portalum.Zvt - netstandard2.0;netstandard2.1 + netstandard2.0;netstandard2.1;net6 - 2.9.1 + 2.10.0 - + diff --git a/src/Portalum.Zvt/TcpNetworkDeviceCommunication.cs b/src/Portalum.Zvt/TcpNetworkDeviceCommunication.cs index f9b4485..a85a314 100644 --- a/src/Portalum.Zvt/TcpNetworkDeviceCommunication.cs +++ b/src/Portalum.Zvt/TcpNetworkDeviceCommunication.cs @@ -1,5 +1,5 @@ using Microsoft.Extensions.Logging; -using SuperSimpleTcp; +using Nager.TcpClient; using System; using System.Threading; using System.Threading.Tasks; @@ -12,7 +12,8 @@ namespace Portalum.Zvt public class TcpNetworkDeviceCommunication : IDeviceCommunication, IDisposable { private readonly string _ipAddress; - private readonly SimpleTcpClient _simpleTcpClient; + private readonly int _port; + private readonly TcpClient _tcpClient; private readonly ILogger _logger; /// @@ -38,22 +39,27 @@ public TcpNetworkDeviceCommunication( ILogger logger = default) { this._ipAddress = ipAddress; - - this._simpleTcpClient = new SimpleTcpClient(ipAddress, port); - this._simpleTcpClient.Events.DataReceived += this.Receive; - this._simpleTcpClient.Events.Connected += this.Connected; - this._simpleTcpClient.Events.Disconnected += this.Disconnected; + this._port = port; if (enableKeepAlive) { - this._simpleTcpClient.Keepalive = new SimpleTcpKeepaliveSettings + var keepAliveConfig = new TcpClientKeepAliveConfig { - EnableTcpKeepAlives = true, - TcpKeepAliveTime = 2, - TcpKeepAliveInterval = 2, - TcpKeepAliveRetryCount = 1 + KeepAliveTime = 2, + KeepAliveInterval = 2, + KeepAliveRetryCount = 1 }; + + this._tcpClient = new TcpClient(keepAliveConfig: keepAliveConfig); + } + else + { + this._tcpClient = new TcpClient(); } + + this._tcpClient.DataReceived += this.Receive; + this._tcpClient.Connected += this.Connected; + this._tcpClient.Disconnected += this.Disconnected; this._logger = logger; } @@ -70,28 +76,28 @@ protected virtual void Dispose(bool disposing) // Check to see if Dispose has already been called. if (disposing) { - if (this._simpleTcpClient == null) + if (this._tcpClient == null) { return; } - this._simpleTcpClient.Events.DataReceived -= this.Receive; - this._simpleTcpClient.Events.Connected -= this.Connected; - this._simpleTcpClient.Events.Disconnected -= this.Disconnected; + this._tcpClient.DataReceived -= this.Receive; + this._tcpClient.Connected -= this.Connected; + this._tcpClient.Disconnected -= this.Disconnected; - if (this._simpleTcpClient.IsConnected) + if (this._tcpClient.IsConnected) { - this._simpleTcpClient.Disconnect(); + this._tcpClient.Disconnect(); } - this._simpleTcpClient.Dispose(); + this._tcpClient.Dispose(); } } /// public bool IsConnected { - get { return this._simpleTcpClient.IsConnected; } + get { return this._tcpClient.IsConnected; } } /// @@ -101,20 +107,26 @@ public string ConnectionIdentifier } /// - public Task ConnectAsync(CancellationToken cancellationToken = default) + public async Task ConnectAsync(CancellationToken cancellationToken = default) { try { this._logger?.LogInformation($"{nameof(ConnectAsync)}"); - this._simpleTcpClient.Connect(); - return Task.FromResult(true); + +#if NET6_0_OR_GREATER + await this._tcpClient.ConnectAsync(this._ipAddress, this._port, cancellationToken); +#else + await this._tcpClient.ConnectAsync(this._ipAddress, this._port); +#endif + + return true; } catch (Exception exception) { this._logger?.LogError($"{nameof(ConnectAsync)} - {exception}"); } - return Task.FromResult(false); + return false; } /// @@ -123,7 +135,7 @@ public Task DisconnectAsync(CancellationToken cancellationToken = default) try { this._logger?.LogInformation($"{nameof(DisconnectAsync)}"); - this._simpleTcpClient.Disconnect(); + this._tcpClient.Disconnect(); return Task.FromResult(true); } catch (Exception exception) @@ -134,33 +146,33 @@ public Task DisconnectAsync(CancellationToken cancellationToken = default) return Task.FromResult(false); } - private void Connected(object sender, ConnectionEventArgs e) + private void Connected() { - this._logger?.LogInformation($"{nameof(Connected)} {e.IpPort}"); + this._logger?.LogInformation($"{nameof(Connected)}"); this.ConnectionStateChanged?.Invoke(ConnectionState.Connected); } - private void Disconnected(object sender, ConnectionEventArgs e) + private void Disconnected() { - this._logger?.LogInformation($"{nameof(Disconnected)} {e.IpPort} {e.Reason}"); + this._logger?.LogInformation($"{nameof(Disconnected)}"); this.ConnectionStateChanged?.Invoke(ConnectionState.Disconnected); } + private void Receive(byte[] data) + { + this._logger?.LogDebug($"{nameof(Receive)} - {BitConverter.ToString(data)}"); + this.DataReceived?.Invoke(data); + } + /// public async Task SendAsync(byte[] data) { this.DataSent?.Invoke(data); this._logger?.LogDebug($"{nameof(SendAsync)} - {BitConverter.ToString(data)}"); - await this._simpleTcpClient.SendAsync(data); - } - - private void Receive(object sender, DataReceivedEventArgs e) - { - this._logger?.LogDebug($"{nameof(Receive)} - {BitConverter.ToString(e.Data)}"); - this.DataReceived?.Invoke(e.Data); + await this._tcpClient.SendAsync(data); } } }