Skip to content

Commit

Permalink
Refined connection timeout logic
Browse files Browse the repository at this point in the history
 - Fixed LastRead DateTime.Now to DateTime.UtcNow
 - Renamed LastRead to LastCommunicated
 - Doubled the timeout period to avoid sending carriage return to an Agent as long as possible.
  • Loading branch information
tbm0115 committed Sep 8, 2023
1 parent c453259 commit 95840c9
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 9 deletions.
2 changes: 1 addition & 1 deletion Mtconnect.TcpAdapter/TcpAdapter.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<RepositoryType>git</RepositoryType>
<PackageTags>Mtconnect;Adapter;TCP;TAMS;</PackageTags>
<PackageReleaseNotes>Updated to latest AdapterSDK. Support for reference Agent SHDR commands.</PackageReleaseNotes>
<Version>2.0.6.2-beta</Version>
<Version>2.0.6.3-beta</Version>
<IncludeSymbols>True</IncludeSymbols>
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
<DebugSymbols>true</DebugSymbols>
Expand Down
14 changes: 7 additions & 7 deletions Mtconnect.TcpAdapter/TcpConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public class TcpConnection : IDisposable
// Reference to a logging service.
public readonly ILogger _logger;

public DateTime? LastRead { get; private set; }
public DateTime? LastCommunicated { get; private set; }

/// <summary>
/// Constructs a new TCP connection
Expand Down Expand Up @@ -191,7 +191,7 @@ public bool Write(byte[] message)
try
{
_stream?.Write(message, 0, message.Length);
LastRead = DateTime.Now;
LastCommunicated = DateTime.UtcNow;
return true;
}
catch (Exception ex)
Expand Down Expand Up @@ -225,7 +225,7 @@ private async Task receive()
_logger?.LogDebug("Client: {clientId} is entering while loop (receive method).", this.ClientId);

var delay = TimeSpan.FromMilliseconds(500);
var timeout = TimeSpan.FromMilliseconds(Heartbeat * 2);
var timeout = TimeSpan.FromMilliseconds(Heartbeat * 4);

while (_client.Connected)
{
Expand All @@ -234,9 +234,9 @@ private async Task receive()
break;

// Check the last time communication occurred between the remote connection. If beyond the timeout, then test the connection with an empty message as a "PING".
if (LastRead == null)
LastRead = DateTime.UtcNow;
if ((DateTime.UtcNow - LastRead) >= timeout)
if (LastCommunicated == null)
LastCommunicated = DateTime.UtcNow;
if ((DateTime.UtcNow - LastCommunicated) >= timeout)
{
// Try to send a ping
if (!Write("\r"))
Expand Down Expand Up @@ -277,7 +277,7 @@ private async Task receive()
else
{
_logger?.LogDebug("Client {clientId} has a message ({byteSize} bytes)", ClientId, bytesRead);
LastRead = DateTime.UtcNow;
LastCommunicated = DateTime.UtcNow;
}

// See if we have a line
Expand Down
4 changes: 3 additions & 1 deletion Tests/ConnectionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public void TcpClientConnect()

DateTime startTime = DateTime.Now;
TimeSpan duration = TimeSpan.FromMilliseconds((int)(adapter.Heartbeat * 2.5));

Console.WriteLine("ENTERING TIME LOOP");
while (DateTime.Now - startTime < duration)
{
if (stream.DataAvailable)
Expand All @@ -52,7 +52,9 @@ public void TcpClientConnect()
Console.WriteLine($"Received: {input}");
}
}
Console.WriteLine($"EXIT TIME LOOP");
Assert.AreEqual(1, adapter.CurrentConnections);
Console.WriteLine($"DISCONNECTING");
client.Close();
}

Expand Down

0 comments on commit 95840c9

Please sign in to comment.