From de8e811c25b5772d135aceb222e42851ef6c2dea Mon Sep 17 00:00:00 2001 From: Eric Fontana Date: Tue, 19 Aug 2014 07:00:33 -0400 Subject: [PATCH] Better error handling --- TimberWinR/Inputs/TcpInputListener.cs | 37 +++++++++++++++------------ 1 file changed, 21 insertions(+), 16 deletions(-) diff --git a/TimberWinR/Inputs/TcpInputListener.cs b/TimberWinR/Inputs/TcpInputListener.cs index d885115..fa2cb60 100644 --- a/TimberWinR/Inputs/TcpInputListener.cs +++ b/TimberWinR/Inputs/TcpInputListener.cs @@ -63,25 +63,30 @@ namespace TimberWinR.Inputs } private void HandleNewClient(object client) - { + { var tcpClient = (TcpClient)client; - NetworkStream clientStream = tcpClient.GetStream(); - var stream = new StreamReader(clientStream); - - string line; - while ((line = stream.ReadLine()) != null) - { - try + NetworkStream clientStream = null; + do + { + clientStream = tcpClient.GetStream(); + var stream = new StreamReader(clientStream); + string line; + while ((line = stream.ReadLine()) != null) { - JObject json = JObject.Parse(line); - ProcessJson(json); + try + { + JObject json = JObject.Parse(line); + ProcessJson(json); + } + catch (Exception ex) + { + LogManager.GetCurrentClassLogger().Error(ex); + } + if (CancelToken.IsCancellationRequested) + break; } - catch (Exception) - { - } - if (CancelToken.IsCancellationRequested) - break; - } + } while (!CancelToken.IsCancellationRequested); + clientStream.Close(); tcpClient.Close(); Finished();