Skip to content

Commit

Permalink
Fix for socket timeout issue
Browse files Browse the repository at this point in the history
  • Loading branch information
davewalker5 committed Aug 27, 2023
1 parent 1464795 commit 11e1ee3
Show file tree
Hide file tree
Showing 10 changed files with 52 additions and 20 deletions.
4 changes: 2 additions & 2 deletions src/BaseStationReader.Data/BaseStationReader.Data.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<PackageId>BaseStationReader.Data</PackageId>
<PackageVersion>1.13.0.0</PackageVersion>
<PackageVersion>1.14.0.0</PackageVersion>
<Authors>Dave Walker</Authors>
<Copyright>Copyright (c) Dave Walker 2023</Copyright>
<Owners>Dave Walker</Owners>
Expand All @@ -17,7 +17,7 @@
<PackageProjectUrl>https://github.com/davewalker5/ADS-B-BaseStationReader</PackageProjectUrl>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<PackageRequireLicenseAcceptance>false</PackageRequireLicenseAcceptance>
<ReleaseVersion>1.13.0.0</ReleaseVersion>
<ReleaseVersion>1.14.0.0</ReleaseVersion>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<PackageId>BaseStationReader.Entities</PackageId>
<PackageVersion>1.13.0.0</PackageVersion>
<PackageVersion>1.14.0.0</PackageVersion>
<Authors>Dave Walker</Authors>
<Copyright>Copyright (c) Dave Walker 2023</Copyright>
<Owners>Dave Walker</Owners>
Expand All @@ -17,7 +17,7 @@
<PackageProjectUrl>https://github.com/davewalker5/ADS-B-BaseStationReader</PackageProjectUrl>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<PackageRequireLicenseAcceptance>false</PackageRequireLicenseAcceptance>
<ReleaseVersion>1.13.0.0</ReleaseVersion>
<ReleaseVersion>1.14.0.0</ReleaseVersion>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ public class ApplicationSettings
{
public string Host { get; set; } = "";
public int Port { get; set; }
public int SocketReadTimeout { get; set; }
public int TimeToRecent { get; set; }
public int TimeToStale { get; set; }
public int TimeToRemoval { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ public enum CommandLineOptionType
{
Host,
Port,
SocketReadTimeout,
TimeToRecent,
TimeToStale,
TimeToRemoval,
Expand Down
4 changes: 2 additions & 2 deletions src/BaseStationReader.Logic/BaseStationReader.Logic.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<PackageId>BaseStationReader.Logic</PackageId>
<PackageVersion>1.13.0.0</PackageVersion>
<PackageVersion>1.14.0.0</PackageVersion>
<Authors>Dave Walker</Authors>
<Copyright>Copyright (c) Dave Walker 2023</Copyright>
<Owners>Dave Walker</Owners>
Expand All @@ -17,7 +17,7 @@
<PackageProjectUrl>https://github.com/davewalker5/ADS-B-BaseStationReader</PackageProjectUrl>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<PackageRequireLicenseAcceptance>false</PackageRequireLicenseAcceptance>
<ReleaseVersion>1.13.0.0</ReleaseVersion>
<ReleaseVersion>1.14.0.0</ReleaseVersion>
</PropertyGroup>

<ItemGroup>
Expand Down
40 changes: 32 additions & 8 deletions src/BaseStationReader.Logic/MessageReader.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Diagnostics.CodeAnalysis;
using System.Net.Sockets;
using System.Threading;
using BaseStationReader.Entities.Events;
using BaseStationReader.Entities.Interfaces;

Expand All @@ -10,13 +11,15 @@ public class MessageReader : IMessageReader
{
private readonly string _server;
private readonly int _port;
private readonly int _readTimeout;

public event EventHandler<MessageReadEventArgs>? MessageRead;

public MessageReader(string server, int port)
public MessageReader(string server, int port, int readTimeout)
{
_server = server;
_port = port;
_readTimeout = readTimeout;
}

/// <summary>
Expand All @@ -27,17 +30,38 @@ public MessageReader(string server, int port)
/// <returns></returns>
public async Task Start(CancellationToken token)
{
using (var client = new TcpClient(_server, _port))
// Continue until cancellation's requested
while (!token.IsCancellationRequested)
{
NetworkStream stream = client.GetStream();
using (var reader = new StreamReader(stream))
// Get a TCP client used to read the message stream
using (var client = new TcpClient(_server, _port))
{
while (!token.IsCancellationRequested)
// Get a network stream and set the timeout
NetworkStream stream = client.GetStream();
stream.ReadTimeout = _readTimeout;

// Create a stream reader and begin reading messages
using (var reader = new StreamReader(stream))
{
string? message = await reader.ReadLineAsync(token);
if (message != null)
// Wait for cancellation to be requested or for a read timeout
var timedOut = false;
while (!token.IsCancellationRequested && !timedOut)
{
MessageRead?.Invoke(this, new MessageReadEventArgs { Message = message });
try
{
// Read the next message and notify subscribers
string? message = await reader.ReadLineAsync(token);
if (!string.IsNullOrEmpty(message))
{
MessageRead?.Invoke(this, new MessageReadEventArgs { Message = message });
}
}
catch (IOException)
{
// The read has timed out - set the flag that will break out of the loop
// and cause the application to reconnect and try again
timedOut = true;
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<ReleaseVersion>1.14.0.0</ReleaseVersion>
<FileVersion>1.14.0.0</FileVersion>
<ProductVersion>1.14.0</ProductVersion>
<ReleaseVersion>1.15.0.0</ReleaseVersion>
<FileVersion>1.15.0.0</FileVersion>
<ProductVersion>1.15.0</ProductVersion>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
Expand Down
6 changes: 5 additions & 1 deletion src/BaseStationReader.Terminal/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ await AnsiConsole.Live(_table)
var parser = new CommandLineParser();
parser.Add(CommandLineOptionType.Host, false, "--host", "-h", "Host to connect to for data stream", 1, 1);
parser.Add(CommandLineOptionType.Port, false, "--port", "-p", "Port to connect to for data stream", 1, 1);
parser.Add(CommandLineOptionType.SocketReadTimeout, false, "--read-timeout", "-t", "Timeout (ms) for socket read operations", 1, 1);
parser.Add(CommandLineOptionType.TimeToRecent, false, "--recent", "-r", "Time (ms) to 'recent' staleness", 1, 1);
parser.Add(CommandLineOptionType.TimeToStale, false, "--stale", "-s", "Time (ms) to 'stale' staleness", 1, 1);
parser.Add(CommandLineOptionType.TimeToRemoval, false, "--remove", "-x", "Time (ms) removal of stale records", 1, 1);
Expand All @@ -98,6 +99,9 @@ await AnsiConsole.Live(_table)
values = parser.GetValues(CommandLineOptionType.Port);
if (values != null) settings!.Port = int.Parse(values[0]);

values = parser.GetValues(CommandLineOptionType.SocketReadTimeout);
if (values != null) settings!.SocketReadTimeout = int.Parse(values[0]);

values = parser.GetValues(CommandLineOptionType.TimeToRecent);
if (values != null) settings!.TimeToRecent = int.Parse(values[0]);

Expand Down Expand Up @@ -130,7 +134,7 @@ await AnsiConsole.Live(_table)
private static async Task ShowTrackingTable(LiveDisplayContext ctx)

Check warning on line 134 in src/BaseStationReader.Terminal/Program.cs

View workflow job for this annotation

GitHub Actions / build

Add a way to break out of this method's recursion.

Check warning on line 134 in src/BaseStationReader.Terminal/Program.cs

View workflow job for this annotation

GitHub Actions / build

Add a way to break out of this method's recursion.
{
// Set up the message reader and parser and the aircraft tracker
var reader = new MessageReader(_settings!.Host, _settings.Port);
var reader = new MessageReader(_settings!.Host, _settings.Port, _settings.SocketReadTimeout);
var parsers = new Dictionary<MessageType, IMessageParser>
{
{ MessageType.MSG, new MsgMessageParser() }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"profiles": {
"BaseStationReader.Terminal": {
"commandName": "Project"
"commandName": "Project",
"commandLineArgs": "-w false"
}
}
}
3 changes: 2 additions & 1 deletion src/BaseStationReader.Terminal/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@
"ApplicationSettings": {
"Host": "192.168.0.98",
"Port": 30003,
"SocketReadTimeout": 60000,
"TimeToRecent": 60000,
"TimeToStale": 120000,
"TimeToRemoval": 180000,
"LogFile": "C:\\MyApps\\AircraftTracker.log",
"EnableSqlWriter": true,
"EnableSqlWriter": true,
"WriterInterval": 30000,
"WriterBatchSize": 20000
},
Expand Down

0 comments on commit 11e1ee3

Please sign in to comment.