Skip to content

Commit

Permalink
Bugfix polling sent request before previous result was received (#55)
Browse files Browse the repository at this point in the history
* Bugfix polling sent request before previous result was received

* removed unused method

* Closing properly when socket comms fails

* dictionary updated

* Avoiding comms messing up at socket level

* compile
  • Loading branch information
jdahlblom authored Dec 3, 2023
1 parent f7fdafd commit ad87a2e
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 9 deletions.
8 changes: 7 additions & 1 deletion src/client/DCSInsight/Communication/TCPClientHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ internal class TCPClientHandler : IDisposable, ICommandListener
private int _metaDataPollCounter;
public bool LogJSON { get; set; }
private string _currentMessage = "";
private volatile bool _responseReceived;

public TCPClientHandler(string host, string port)
{
Expand All @@ -48,6 +49,7 @@ public void Dispose()
private async void ClientThread()
{
ICEventHandler.SendConnectionStatus(_isRunning);
_responseReceived = true;
while (_isRunning)
{
try
Expand Down Expand Up @@ -81,12 +83,13 @@ private async void ClientThread()
Thread.Sleep(1000);
}

if (_asyncCommandsChannel.Reader.Count > 0)
if (_asyncCommandsChannel.Reader.Count > 0 && _responseReceived)
{
var cts = new CancellationTokenSource(100);
var dcsApi = await _asyncCommandsChannel.Reader.ReadAsync(cts.Token);
if (LogJSON) Logger.Info(JsonConvert.SerializeObject(dcsApi, Formatting.Indented));
_tcpClient.GetStream().Write(Encoding.ASCII.GetBytes(JsonConvert.SerializeObject(dcsApi) + "\n"));
_responseReceived = false;
}

if (_tcpClient.Available <= 0) continue;
Expand All @@ -101,6 +104,7 @@ private async void ClientThread()
catch (SocketException ex)
{
Logger.Error(ex);
break;
}
}

Expand All @@ -124,6 +128,7 @@ private void HandleMessage(string str)
var dcsApi = JsonConvert.DeserializeObject<DCSAPI>(_currentMessage + str);
_currentMessage = "";
ICEventHandler.SendData(dcsApi);
_responseReceived = true;
}
else
{
Expand All @@ -142,6 +147,7 @@ private void HandleAPIMessage(string str)
{
var dcsAPIList = JsonConvert.DeserializeObject<List<DCSAPI>>(str);
ICEventHandler.SendData(dcsAPIList);
_responseReceived = true;
_apiListReceived = true;
}
catch (Exception ex)
Expand Down
2 changes: 1 addition & 1 deletion src/client/DCSInsight/DCSInsight.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<UseWPF>true</UseWPF>
<AssemblyName>dcs-insight</AssemblyName>
<Version>1.0.0</Version>
<AssemblyVersion>1.8.2</AssemblyVersion>
<AssemblyVersion>1.8.3</AssemblyVersion>
<ApplicationIcon>Images\Magnifier_icon.ico</ApplicationIcon>
</PropertyGroup>
<ItemGroup>
Expand Down
1 change: 1 addition & 0 deletions src/client/DCSInsight/DCSInsight.sln.DotSettings
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=TCP/@EntryIndexedValue">TCP</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=UI/@EntryIndexedValue">UI</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/XamlNaming/Abbreviations/=API/@EntryIndexedValue">API</s:String>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Comms/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=consolas/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=DCSAPI/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=defs/@EntryIndexedValue">True</s:Boolean>
Expand Down
7 changes: 7 additions & 0 deletions src/client/DCSInsight/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,13 @@ public void ConnectionStatus(ConnectionEventArgs args)
try
{
_isConnected = args.IsConnected;
if (!_isConnected)
{
Dispatcher?.BeginInvoke((Action)(Disconnect));
Dispatcher?.BeginInvoke((Action)(SetFormState));
return;
}

Dispatcher?.BeginInvoke((Action)(() => SetConnectionStatus(args.IsConnected)));
Dispatcher?.BeginInvoke((Action)(SetFormState)); ;
}
Expand Down
4 changes: 2 additions & 2 deletions src/client/DCSInsight/UserControls/UserControlAPI.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,6 @@ protected override void BuildUI()
VerticalAlignment = VerticalAlignment.Center
};
ComboBoxPollTimes.DataContextChanged += ComboBoxPollTimes_OnDataContextChanged;
ComboBoxPollTimes.Items.Add(50);
ComboBoxPollTimes.Items.Add(100);
ComboBoxPollTimes.Items.Add(500);
ComboBoxPollTimes.Items.Add(1000);
Expand Down Expand Up @@ -206,7 +205,8 @@ public override void SetResult(DCSAPI dcsApi)
Dispatcher?.BeginInvoke((Action)(() => LabelResult.Content = $"Result ({dcsApi.ResultType})"));

var result = dcsApi.ErrorThrown ? dcsApi.ErrorMessage : (string.IsNullOrEmpty(dcsApi.Result) ? "nil" : dcsApi.Result);


AutoResetEventPolling.Set();
if (KeepResults)
{
Dispatcher?.BeginInvoke((Action)(() => TextBoxResult.Text = TextBoxResult.Text.Insert(0, "\n---\n")));
Expand Down
14 changes: 14 additions & 0 deletions src/client/DCSInsight/UserControls/UserControlAPIBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ public abstract partial class UserControlAPIBase : UserControl, IDisposable, IAs
protected CheckBox CheckBoxPolling;
protected Label LabelPollingInterval;
protected ComboBox ComboBoxPollTimes;
protected static readonly AutoResetEvent AutoResetEventPolling = new(false);

public int Id { get; protected set; }
protected abstract void BuildUI();
public abstract void SetResult(DCSAPI dcsApi);
Expand All @@ -50,15 +52,21 @@ protected UserControlAPIBase(DCSAPI dcsAPI, bool isConnected)

public void Dispose()
{
AutoResetEventPolling.Set();
AutoResetEventPolling.Set();
PollingTimer?.Dispose();
AutoResetEventPolling.Dispose();
GC.SuppressFinalize(this);
}

public async ValueTask DisposeAsync()
{
if (PollingTimer != null)
{
AutoResetEventPolling.Set();
AutoResetEventPolling.Set();
await PollingTimer.DisposeAsync();
AutoResetEventPolling.Dispose();
GC.SuppressFinalize(this);
}
}
Expand All @@ -68,6 +76,10 @@ public void SetConnectionStatus(bool connected)
try
{
IsConnected = connected;
if (!IsConnected)
{
PollingTimer.Change(Timeout.Infinite, 10000);
}
SetFormState();
}
catch (Exception ex)
Expand All @@ -93,6 +105,7 @@ protected void StartPolling(int milliseconds)
try
{
PollingTimer.Change(milliseconds, milliseconds);
AutoResetEventPolling.Set();
SetFormState();
}
catch (Exception ex)
Expand All @@ -118,6 +131,7 @@ protected void PollingTimerCallback(object state)
{
try
{
AutoResetEventPolling.WaitOne();
if (CanSend)
{
Dispatcher?.BeginInvoke((Action)(SendCommand));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,18 +233,15 @@ protected override void BuildUI()
}
}

private void TextBox_PreviewKeyDown(object sender, KeyEventArgs e)
{
throw new NotImplementedException();
}

public override void SetResult(DCSAPI dcsApi)
{
try
{
Dispatcher?.BeginInvoke((Action)(() => LabelResult.Content = $"Result ({dcsApi.ResultType})"));

var result = dcsApi.ErrorThrown ? dcsApi.ErrorMessage : (string.IsNullOrEmpty(dcsApi.Result) ? "nil" : dcsApi.Result);

AutoResetEventPolling.Set();

if (KeepResults)
{
Expand Down

0 comments on commit ad87a2e

Please sign in to comment.