diff --git a/TimberWinR.ServiceHost/default.json b/TimberWinR.ServiceHost/default.json index 1b7eab4..043d20a 100644 --- a/TimberWinR.ServiceHost/default.json +++ b/TimberWinR.ServiceHost/default.json @@ -26,17 +26,17 @@ "drop": "true" } } - ] - }, - "Outputs": { - "Redis": [ - { - "_comment": "Change the host to your Redis instance", - "port": 6379, - "host": [ - "logaggregator.vistaprint.svc" - ] - } - ] + ], + "Outputs": { + "Redis": [ + { + "_comment": "Change the host to your Redis instance", + "port": 6379, + "host": [ + "logaggregator.vistaprint.svc" + ] + } + ] + } } } diff --git a/TimberWinR/Inputs/TcpInputListener.cs b/TimberWinR/Inputs/TcpInputListener.cs index 3734059..b4d9870 100644 --- a/TimberWinR/Inputs/TcpInputListener.cs +++ b/TimberWinR/Inputs/TcpInputListener.cs @@ -1,11 +1,9 @@ using System; -using System.Collections.Generic; using System.IO; -using System.Linq; -using System.Text; using System.Threading; using System.Net; using System.Net.Sockets; +using Newtonsoft.Json; using Newtonsoft.Json.Linq; using NLog; @@ -36,7 +34,7 @@ namespace TimberWinR.Inputs : base(cancelToken, "Win32-Tcp") { _port = port; - + LogManager.GetCurrentClassLogger().Info("Tcp Input(v4/v6) on Port {0} Ready", _port); @@ -67,7 +65,7 @@ namespace TimberWinR.Inputs listener.Start(); - + while (!CancelToken.IsCancellationRequested) { try @@ -92,27 +90,22 @@ namespace TimberWinR.Inputs private void HandleNewClient(object client) { var tcpClient = (TcpClient)client; - NetworkStream clientStream = null; try { - clientStream = tcpClient.GetStream(); - var stream = new StreamReader(clientStream); - string line; - while ((line = stream.ReadLine()) != null) + NetworkStream clientStream = tcpClient.GetStream(); + using (var stream = new StreamReader(clientStream)) { - try + //assume a continuous stream of JSON objects + using (var reader = new JsonTextReader(stream) { SupportMultipleContent = true }) { - JObject json = JObject.Parse(line); - ProcessJson(json); - _receivedMessages++; + while (reader.Read()) + { + if (CancelToken.IsCancellationRequested) break; + JObject json = JObject.Load(reader); + ProcessJson(json); + } } - catch (Exception ex) - { - LogManager.GetCurrentClassLogger().Error(ex); - } - if (CancelToken.IsCancellationRequested) - break; } } catch (Exception ex) @@ -120,9 +113,6 @@ namespace TimberWinR.Inputs LogManager.GetCurrentClassLogger().Error(ex); } - if (clientStream != null) - clientStream.Close(); - tcpClient.Close(); Finished(); }