Tweaked shutdown code for async Udp Listener
This commit is contained in:
@@ -337,11 +337,11 @@ namespace TimberWinR.TestGenerator
|
||||
|
||||
private static JObject ShutdownTimberWinR()
|
||||
{
|
||||
_timberWinR.Shutdown();
|
||||
|
||||
// Cancel any/all other threads
|
||||
_cancellationTokenSource.Cancel();
|
||||
|
||||
_timberWinR.Shutdown();
|
||||
|
||||
var json = Diagnostics.DiagnosticsOutput();
|
||||
|
||||
LogManager.GetCurrentClassLogger()
|
||||
|
||||
@@ -15,7 +15,7 @@ namespace TimberWinR.Inputs
|
||||
private IPEndPoint _udpEndpointV4;
|
||||
private readonly BlockingCollection<byte[]> _unprocessedRawData;
|
||||
private readonly Thread _rawDataProcessingThread;
|
||||
|
||||
public bool Stop { get; set; }
|
||||
private readonly int _port;
|
||||
private long _receivedMessages;
|
||||
private long _parseErrors;
|
||||
@@ -29,7 +29,7 @@ namespace TimberWinR.Inputs
|
||||
new JObject(new JProperty("port", _port),
|
||||
new JProperty("receive_errors", _receiveErrors),
|
||||
new JProperty("parse_errors", _parseErrors),
|
||||
new JProperty("received_messages", _receivedMessages),
|
||||
new JProperty("messages", _receivedMessages),
|
||||
new JProperty("parsed_messages", _parsedMessages),
|
||||
new JProperty("unprocessed_messages", _unprocessedRawData.Count))));
|
||||
|
||||
@@ -52,11 +52,15 @@ namespace TimberWinR.Inputs
|
||||
|
||||
public override void Shutdown()
|
||||
{
|
||||
Stop = true;
|
||||
|
||||
LogManager.GetCurrentClassLogger().Info("Shutting Down {0}", InputType);
|
||||
|
||||
// close UDP listeners, which will end the listener threads
|
||||
_udpListenerV4.Close();
|
||||
|
||||
Finished();
|
||||
|
||||
base.Shutdown();
|
||||
}
|
||||
|
||||
@@ -76,6 +80,7 @@ namespace TimberWinR.Inputs
|
||||
|
||||
private void StartReceiving()
|
||||
{
|
||||
if (!CancelToken.IsCancellationRequested)
|
||||
_udpListenerV4.BeginReceive(DataReceived, null);
|
||||
}
|
||||
|
||||
@@ -115,12 +120,17 @@ namespace TimberWinR.Inputs
|
||||
|
||||
private void ProcessDataLoop()
|
||||
{
|
||||
while (!_unprocessedRawData.IsCompleted)
|
||||
while (!Stop && !_unprocessedRawData.IsCompleted)
|
||||
{
|
||||
try
|
||||
{
|
||||
ProcessData(_unprocessedRawData.Take());
|
||||
}
|
||||
catch(OperationCanceledException)
|
||||
{
|
||||
// we are shutting down.
|
||||
break;
|
||||
}
|
||||
catch (InvalidOperationException)
|
||||
{
|
||||
// when the collection is marked as completed
|
||||
@@ -132,8 +142,6 @@ namespace TimberWinR.Inputs
|
||||
Thread.Sleep(100);
|
||||
}
|
||||
}
|
||||
|
||||
Finished();
|
||||
}
|
||||
|
||||
private void ProcessData(byte[] bytes)
|
||||
|
||||
Reference in New Issue
Block a user