Skip to content

Commit

Permalink
Replace TcpClient Library, add .NET 6 TargetFramework
Browse files Browse the repository at this point in the history
  • Loading branch information
tinohager committed Feb 23, 2022
1 parent 2b3ba77 commit 2ac72c0
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 39 deletions.
6 changes: 3 additions & 3 deletions src/Portalum.Zvt/Portalum.Zvt.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@

<RepositoryUrl>https://github.com/Portalum/Portalum.Zvt</RepositoryUrl>

<TargetFrameworks>netstandard2.0;netstandard2.1</TargetFrameworks>
<TargetFrameworks>netstandard2.0;netstandard2.1;net6</TargetFrameworks>

<Version>2.9.1</Version>
<Version>2.10.0</Version>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="6.0.0" />
<PackageReference Include="SuperSimpleTcp" Version="2.6.0.0" />
<PackageReference Include="Nager.TcpClient" Version="1.0.3" />
<PackageReference Include="System.IO.Ports" Version="6.0.0" />
<PackageReference Include="System.Text.Encoding.CodePages" Version="6.0.0" />
</ItemGroup>
Expand Down
84 changes: 48 additions & 36 deletions src/Portalum.Zvt/TcpNetworkDeviceCommunication.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using Microsoft.Extensions.Logging;
using SuperSimpleTcp;
using Nager.TcpClient;
using System;
using System.Threading;
using System.Threading.Tasks;
Expand All @@ -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<TcpNetworkDeviceCommunication> _logger;

/// <inheritdoc />
Expand All @@ -38,22 +39,27 @@ public TcpNetworkDeviceCommunication(
ILogger<TcpNetworkDeviceCommunication> 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;
}
Expand All @@ -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();
}
}

/// <inheritdoc />
public bool IsConnected
{
get { return this._simpleTcpClient.IsConnected; }
get { return this._tcpClient.IsConnected; }
}

/// <inheritdoc />
Expand All @@ -101,20 +107,26 @@ public string ConnectionIdentifier
}

/// <inheritdoc />
public Task<bool> ConnectAsync(CancellationToken cancellationToken = default)
public async Task<bool> 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;
}

/// <inheritdoc />
Expand All @@ -123,7 +135,7 @@ public Task<bool> DisconnectAsync(CancellationToken cancellationToken = default)
try
{
this._logger?.LogInformation($"{nameof(DisconnectAsync)}");
this._simpleTcpClient.Disconnect();
this._tcpClient.Disconnect();
return Task.FromResult(true);
}
catch (Exception exception)
Expand All @@ -134,33 +146,33 @@ public Task<bool> 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);
}

/// <inheritdoc />
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);
}
}
}

0 comments on commit 2ac72c0

Please sign in to comment.