Skip to content

Commit f6bd8cb

Browse files
committed
Increased timeouts & added persistent listener socket
1 parent 35e056f commit f6bd8cb

File tree

2 files changed

+58
-55
lines changed

2 files changed

+58
-55
lines changed

source/TS.NET.Engine/Threads/ScpiServer.cs

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,8 @@ protected override void OnError(SocketError error)
111111
string message)
112112
{
113113
const string InvalidParameters = "One or more invalid parameters";
114+
const int processingControlTimeoutMs = 2000;
115+
const int hardwareControlTimeoutMs = 500;
114116

115117
string? argument = null;
116118
string? subject = null;
@@ -596,7 +598,7 @@ protected override void OnError(SocketError error)
596598
case "STATE?":
597599
{
598600
processingControl.Request.Writer.Write(new ProcessingGetStateRequest());
599-
if (processingControl.Response.Reader.TryRead(out var response, 500))
601+
if (processingControl.Response.Reader.TryRead(out var response, processingControlTimeoutMs))
600602
{
601603
switch (response)
602604
{
@@ -613,7 +615,7 @@ protected override void OnError(SocketError error)
613615
case "MODE?":
614616
{
615617
processingControl.Request.Writer.Write(new ProcessingGetModeRequest());
616-
if (processingControl.Response.Reader.TryRead(out var response, 500))
618+
if (processingControl.Response.Reader.TryRead(out var response, processingControlTimeoutMs))
617619
{
618620
switch (response)
619621
{
@@ -646,7 +648,7 @@ protected override void OnError(SocketError error)
646648
case var _ when command.StartsWith("RES"):
647649
{
648650
processingControl.Request.Writer.Write(new ProcessingGetResolutionRequest());
649-
if (processingControl.Response.Reader.TryRead(out var response, 500))
651+
if (processingControl.Response.Reader.TryRead(out var response, processingControlTimeoutMs))
650652
{
651653
if (response is ProcessingGetResolutionResponse processingGetResolutionResponse)
652654
{
@@ -684,7 +686,7 @@ protected override void OnError(SocketError error)
684686
case var _ when command.StartsWith("SOU"):
685687
{
686688
processingControl.Request.Writer.Write(new ProcessingGetTriggerSourceRequest());
687-
if (processingControl.Response.Reader.TryRead(out var response, 500))
689+
if (processingControl.Response.Reader.TryRead(out var response, processingControlTimeoutMs))
688690
{
689691
switch (response)
690692
{
@@ -704,7 +706,7 @@ protected override void OnError(SocketError error)
704706
case var _ when command.StartsWith("TYPE"):
705707
{
706708
processingControl.Request.Writer.Write(new ProcessingGetTriggerTypeRequest());
707-
if (processingControl.Response.Reader.TryRead(out var response, 500))
709+
if (processingControl.Response.Reader.TryRead(out var response, processingControlTimeoutMs))
708710
{
709711
switch (response)
710712
{
@@ -724,7 +726,7 @@ protected override void OnError(SocketError error)
724726
case var _ when command.StartsWith("DEL"):
725727
{
726728
processingControl.Request.Writer.Write(new ProcessingGetTriggerDelayRequest());
727-
if (processingControl.Response.Reader.TryRead(out var response, 500))
729+
if (processingControl.Response.Reader.TryRead(out var response, processingControlTimeoutMs))
728730
{
729731
switch (response)
730732
{
@@ -744,7 +746,7 @@ protected override void OnError(SocketError error)
744746
case var _ when command.StartsWith("HOLD"):
745747
{
746748
processingControl.Request.Writer.Write(new ProcessingGetTriggerHoldoffRequest());
747-
if (processingControl.Response.Reader.TryRead(out var response, 500))
749+
if (processingControl.Response.Reader.TryRead(out var response, processingControlTimeoutMs))
748750
{
749751
switch (response)
750752
{
@@ -764,7 +766,7 @@ protected override void OnError(SocketError error)
764766
case var _ when command.StartsWith("INTER"):
765767
{
766768
processingControl.Request.Writer.Write(new ProcessingGetTriggerInterpolationRequest());
767-
if (processingControl.Response.Reader.TryRead(out var response, 500))
769+
if (processingControl.Response.Reader.TryRead(out var response, processingControlTimeoutMs))
768770
{
769771
switch (response)
770772
{
@@ -784,7 +786,7 @@ protected override void OnError(SocketError error)
784786
case var _ when command.StartsWith("EDGE:LEV"):
785787
{
786788
processingControl.Request.Writer.Write(new ProcessingGetEdgeTriggerLevelRequest());
787-
if (processingControl.Response.Reader.TryRead(out var response, 500))
789+
if (processingControl.Response.Reader.TryRead(out var response, processingControlTimeoutMs))
788790
{
789791
switch (response)
790792
{
@@ -804,7 +806,7 @@ protected override void OnError(SocketError error)
804806
case var _ when command.StartsWith("EDGE:DIR"):
805807
{
806808
processingControl.Request.Writer.Write(new ProcessingGetEdgeTriggerDirectionRequest());
807-
if (processingControl.Response.Reader.TryRead(out var response, 500))
809+
if (processingControl.Response.Reader.TryRead(out var response, processingControlTimeoutMs))
808810
{
809811
switch (response)
810812
{
@@ -836,7 +838,7 @@ protected override void OnError(SocketError error)
836838
case var _ when command.Equals("STATE?", StringComparison.OrdinalIgnoreCase):
837839
{
838840
hardwareControl.Request.Writer.Write(new HardwareGetEnabledRequest(channelIndex));
839-
if (hardwareControl.Response.Reader.TryRead(out var response, 500))
841+
if (hardwareControl.Response.Reader.TryRead(out var response, hardwareControlTimeoutMs))
840842
{
841843
if (response is HardwareGetEnabledResponse hardwareGetEnabledResponse)
842844
{
@@ -856,7 +858,7 @@ protected override void OnError(SocketError error)
856858
case var _ when command.StartsWith("BAND", StringComparison.OrdinalIgnoreCase):
857859
{
858860
hardwareControl.Request.Writer.Write(new HardwareGetBandwidthRequest(channelIndex));
859-
if (hardwareControl.Response.Reader.TryRead(out var response, 500))
861+
if (hardwareControl.Response.Reader.TryRead(out var response, hardwareControlTimeoutMs))
860862
{
861863
if (response is HardwareGetBandwidthResponse hardwareGetBandwidthResponse)
862864
{
@@ -887,7 +889,7 @@ protected override void OnError(SocketError error)
887889
case var _ when command.StartsWith("COUP", StringComparison.OrdinalIgnoreCase):
888890
{
889891
hardwareControl.Request.Writer.Write(new HardwareGetCouplingRequest(channelIndex));
890-
if (hardwareControl.Response.Reader.TryRead(out var response, 500))
892+
if (hardwareControl.Response.Reader.TryRead(out var response, hardwareControlTimeoutMs))
891893
{
892894
if (response is HardwareGetCouplingResponse hardwareGetCouplingResponse)
893895
{
@@ -913,7 +915,7 @@ protected override void OnError(SocketError error)
913915
case var _ when command.StartsWith("TERM", StringComparison.OrdinalIgnoreCase):
914916
{
915917
hardwareControl.Request.Writer.Write(new HardwareGetTerminationRequest(channelIndex));
916-
if (hardwareControl.Response.Reader.TryRead(out var response, 500))
918+
if (hardwareControl.Response.Reader.TryRead(out var response, hardwareControlTimeoutMs))
917919
{
918920
if (response is HardwareGetTerminationResponse hardwareGetTerminationResponse)
919921
{
@@ -940,7 +942,7 @@ protected override void OnError(SocketError error)
940942
case var _ when command.StartsWith("OFFS", StringComparison.OrdinalIgnoreCase):
941943
{
942944
hardwareControl.Request.Writer.Write(new HardwareGetVoltOffsetRequest(channelIndex));
943-
if (hardwareControl.Response.Reader.TryRead(out var response, 500))
945+
if (hardwareControl.Response.Reader.TryRead(out var response, hardwareControlTimeoutMs))
944946
{
945947
if (response is HardwareGetVoltOffsetResponse hardwareGetVoltOffsetResponse)
946948
return $"{hardwareGetVoltOffsetResponse.RequestedVoltOffset:0.######}\n";
@@ -955,7 +957,7 @@ protected override void OnError(SocketError error)
955957
case var _ when command.StartsWith("RANG", StringComparison.OrdinalIgnoreCase):
956958
{
957959
hardwareControl.Request.Writer.Write(new HardwareGetVoltFullScaleRequest(channelIndex));
958-
if (hardwareControl.Response.Reader.TryRead(out var response, 500))
960+
if (hardwareControl.Response.Reader.TryRead(out var response, hardwareControlTimeoutMs))
959961
{
960962
if (response is HardwareGetVoltFullScaleResponse hardwareGetVoltFullScaleResponse)
961963
{
@@ -982,7 +984,7 @@ protected override void OnError(SocketError error)
982984
string GetRates()
983985
{
984986
processingControl.Request.Writer.Write(new ProcessingGetRatesRequest());
985-
if (processingControl.Response.Reader.TryRead(out var response, 500))
987+
if (processingControl.Response.Reader.TryRead(out var response, processingControlTimeoutMs))
986988
{
987989
switch (response)
988990
{
@@ -1000,7 +1002,7 @@ string GetRates()
10001002
string GetRate()
10011003
{
10021004
processingControl.Request.Writer.Write(new ProcessingGetRateRequest());
1003-
if (processingControl.Response.Reader.TryRead(out var response, 500))
1005+
if (processingControl.Response.Reader.TryRead(out var response, processingControlTimeoutMs))
10041006
{
10051007
switch (response)
10061008
{
@@ -1038,7 +1040,7 @@ string GetDepths()
10381040
string GetDepth()
10391041
{
10401042
processingControl.Request.Writer.Write(new ProcessingGetDepthRequest());
1041-
if (processingControl.Response.Reader.TryRead(out var response, 500))
1043+
if (processingControl.Response.Reader.TryRead(out var response, processingControlTimeoutMs))
10421044
{
10431045
switch (response)
10441046
{

source/TS.NET.Engine/Waveform Buffer Readers/DataServer.cs

Lines changed: 37 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
using Microsoft.Extensions.Logging;
2-
using System.Diagnostics;
32
using System.Net;
43
using System.Net.Sockets;
54
using System.Runtime.InteropServices;
6-
using static System.Runtime.InteropServices.JavaScript.JSType;
75

86
namespace TS.NET.Engine;
97

@@ -56,45 +54,48 @@ public void Stop()
5654

5755
private void LoopListener(ILogger logger, CancellationToken cancelToken)
5856
{
59-
socketListener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
60-
socketListener.Bind(new IPEndPoint(address, port));
61-
socketListener.Listen(backlog: 1);
62-
logger.LogInformation($"Socket opened {socketListener.LocalEndPoint}");
63-
try
57+
while (!cancelToken.IsCancellationRequested)
6458
{
65-
while (true)
59+
socketListener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
60+
socketListener.Bind(new IPEndPoint(address, port));
61+
socketListener.Listen(backlog: 1);
62+
logger.LogInformation($"Socket opened {socketListener.LocalEndPoint}");
63+
try
6664
{
67-
cancelToken.ThrowIfCancellationRequested();
68-
var session = socketListener!.Accept();
69-
if (socketSession != null)
65+
while (true)
7066
{
71-
logger.LogInformation($"Dropping session {socketSession?.RemoteEndPoint} and accepting new session");
72-
try { socketSession?.Shutdown(SocketShutdown.Both); } catch { }
73-
try { socketSession?.Close(); } catch { }
74-
sessionCancelTokenSource?.Cancel();
67+
cancelToken.ThrowIfCancellationRequested();
68+
var session = socketListener!.Accept();
69+
if (socketSession != null)
70+
{
71+
logger.LogInformation($"Dropping session {socketSession?.RemoteEndPoint} and accepting new session");
72+
try { socketSession?.Shutdown(SocketShutdown.Both); } catch { }
73+
try { socketSession?.Close(); } catch { }
74+
sessionCancelTokenSource?.Cancel();
75+
}
76+
logger.LogInformation($"Session accepted {session.RemoteEndPoint}");
77+
sessionCancelTokenSource = new CancellationTokenSource();
78+
taskSession = Task.Factory.StartNew(() => LoopSession(logger, session, sessionCancelTokenSource.Token), TaskCreationOptions.LongRunning);
79+
socketSession = session;
7580
}
76-
logger.LogInformation($"Session accepted {session.RemoteEndPoint}");
77-
sessionCancelTokenSource = new CancellationTokenSource();
78-
taskSession = Task.Factory.StartNew(() => LoopSession(logger, session, sessionCancelTokenSource.Token), TaskCreationOptions.LongRunning);
79-
socketSession = session;
8081
}
81-
}
82-
catch (OperationCanceledException) { }
83-
catch (SocketException ex)
84-
{
85-
logger.LogDebug($"SocketException: {ex.SocketErrorCode}");
86-
}
87-
catch (Exception ex)
88-
{
89-
logger.LogDebug($"Exception: {ex.Message}");
90-
}
91-
finally
92-
{
93-
if (!cancelToken.IsCancellationRequested)
94-
logger.LogCritical($"Socket closed");
95-
else
96-
logger.LogDebug($"Socket closed");
97-
socketListener = null;
82+
catch (OperationCanceledException) { }
83+
catch (SocketException ex)
84+
{
85+
logger.LogDebug($"SocketException: {ex.SocketErrorCode}");
86+
}
87+
catch (Exception ex)
88+
{
89+
logger.LogDebug($"Exception: {ex.Message}");
90+
}
91+
finally
92+
{
93+
if (!cancelToken.IsCancellationRequested)
94+
logger.LogCritical($"Socket closed");
95+
else
96+
logger.LogDebug($"Socket closed");
97+
socketListener = null;
98+
}
9899
}
99100
}
100101

0 commit comments

Comments
 (0)