Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions src/FluentModbus/Client/ModbusTcpClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,19 @@ private void Initialize(TcpClient tcpClient, IPEndPoint? remoteEndpoint, ModbusE
var isInternal = remoteEndpoint is not null;
_tcpClient = (tcpClient, isInternal);

if (remoteEndpoint is not null && !tcpClient.ConnectAsync(remoteEndpoint.Address, remoteEndpoint.Port).Wait(ConnectTimeout))
throw new Exception(ErrorMessage.ModbusClient_TcpConnectTimeout);
if (remoteEndpoint is not null)
{
var connectTask = tcpClient.ConnectAsync(remoteEndpoint.Address, remoteEndpoint.Port);
if (!connectTask.Wait(ConnectTimeout))
{
// Ensure the task exception is observed to avoid triggering UnobservedTaskException handlers (which can crash the process or flood logs).
// Attaching a continuation that accesses task.Exception guarantees the exception is observed.
connectTask.ContinueWith(t => t.Exception, TaskContinuationOptions.OnlyOnFaulted | TaskContinuationOptions.ExecuteSynchronously);
// Close the TcpClient to cancel the connection attempt.
tcpClient.Close();
throw new Exception(ErrorMessage.ModbusClient_TcpConnectTimeout);
}
}

// Why no method signature with NetworkStream only and then set the timeouts
// in the Connect method like for the RTU client?
Expand Down