Skip to content

Commit

Permalink
Avoiding comms messing up at socket level
Browse files Browse the repository at this point in the history
  • Loading branch information
jdahlblom committed Dec 3, 2023
1 parent 830604c commit 8f41289
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 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 Down Expand Up @@ -125,6 +128,7 @@ private void HandleMessage(string str)
var dcsApi = JsonConvert.DeserializeObject<DCSAPI>(_currentMessage + str);
_currentMessage = "";
ICEventHandler.SendData(dcsApi);
_responseReceived = true;
}
else
{
Expand All @@ -143,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

0 comments on commit 8f41289

Please sign in to comment.