From acf9514fc50b28ddff5a7ac8e215446a37a2dbdf Mon Sep 17 00:00:00 2001 From: Neil Dorin Date: Fri, 15 Nov 2019 12:57:31 -0700 Subject: [PATCH 01/11] Adds a flag to the debug context class to save whether or not an application should start after the next boot. --- Pepperdash Core/Pepperdash Core/Logging/Debug.cs | 12 ++++++++++++ .../Pepperdash Core/Logging/DebugMemory.cs | 9 +++++++++ 2 files changed, 21 insertions(+) diff --git a/Pepperdash Core/Pepperdash Core/Logging/Debug.cs b/Pepperdash Core/Pepperdash Core/Logging/Debug.cs index 14da95e..083baa2 100644 --- a/Pepperdash Core/Pepperdash Core/Logging/Debug.cs +++ b/Pepperdash Core/Pepperdash Core/Logging/Debug.cs @@ -237,6 +237,18 @@ namespace PepperDash.Core } } + /// + /// Sets the flag to prevent application starting on next boot + /// + /// + public static void SetDoNotStartOnNextBoot(bool state) + { + Contexts.DoNotStartOnNextBoot = state; + CrestronConsole.PrintLine("[Application {0}], Do Not Start on Next Boot set to {1}", + InitialParametersClass.ApplicationNumber, Contexts.DoNotStartOnNextBoot); + SaveMemoryOnTimeout(); + } + /// /// /// diff --git a/Pepperdash Core/Pepperdash Core/Logging/DebugMemory.cs b/Pepperdash Core/Pepperdash Core/Logging/DebugMemory.cs index 82ad51f..d75e3b9 100644 --- a/Pepperdash Core/Pepperdash Core/Logging/DebugMemory.cs +++ b/Pepperdash Core/Pepperdash Core/Logging/DebugMemory.cs @@ -10,6 +10,12 @@ namespace PepperDash.Core.DebugThings { public class DebugContextCollection { + /// + /// Property to tell the program not to intitialize when it boots, if desired + /// + [JsonProperty("doNotStartOnNextBoot")] + public bool DoNotStartOnNextBoot { get; set; } + [JsonProperty("items")] Dictionary Items; @@ -47,6 +53,9 @@ namespace PepperDash.Core.DebugThings public class DebugContextItem { + /// + /// The level of debug messages to print + /// [JsonProperty("level")] public int Level { get; set; } From 7fdbf7c3a13db84cd1239463a81b511aefece64f Mon Sep 17 00:00:00 2001 From: Neil Dorin Date: Fri, 15 Nov 2019 13:06:37 -0700 Subject: [PATCH 02/11] Exposes DoNotStartOnNextBoot property on the Debug class properly. --- Pepperdash Core/Pepperdash Core/Logging/Debug.cs | 15 +++++++++++---- .../Pepperdash Core/Logging/DebugMemory.cs | 13 +++++++------ 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/Pepperdash Core/Pepperdash Core/Logging/Debug.cs b/Pepperdash Core/Pepperdash Core/Logging/Debug.cs index 083baa2..ab5a3b5 100644 --- a/Pepperdash Core/Pepperdash Core/Logging/Debug.cs +++ b/Pepperdash Core/Pepperdash Core/Logging/Debug.cs @@ -28,6 +28,8 @@ namespace PepperDash.Core public static int Level { get; private set; } + public static bool DoNotStartOnNextBoot { get; private set; } + static DebugContextCollection Contexts; static int SaveTimeoutMs = 30000; @@ -81,7 +83,10 @@ namespace PepperDash.Core CrestronEnvironment.ProgramStatusEventHandler += CrestronEnvironment_ProgramStatusEventHandler; LoadMemory(); - Level = Contexts.GetOrCreateItem("DEFAULT").Level; + + var context = Contexts.GetOrCreateItem("DEFAULT"); + Level = context.Level; + DoNotStartOnNextBoot = context.DoNotStartOnNextBoot; try { @@ -243,10 +248,12 @@ namespace PepperDash.Core /// public static void SetDoNotStartOnNextBoot(bool state) { - Contexts.DoNotStartOnNextBoot = state; - CrestronConsole.PrintLine("[Application {0}], Do Not Start on Next Boot set to {1}", - InitialParametersClass.ApplicationNumber, Contexts.DoNotStartOnNextBoot); + DoNotStartOnNextBoot = state; + Contexts.GetOrCreateItem("DEFAULT").DoNotStartOnNextBoot = state; SaveMemoryOnTimeout(); + + CrestronConsole.PrintLine("[Application {0}], Do Not Start on Next Boot set to {1}", + InitialParametersClass.ApplicationNumber, DoNotStartOnNextBoot); } /// diff --git a/Pepperdash Core/Pepperdash Core/Logging/DebugMemory.cs b/Pepperdash Core/Pepperdash Core/Logging/DebugMemory.cs index d75e3b9..2312160 100644 --- a/Pepperdash Core/Pepperdash Core/Logging/DebugMemory.cs +++ b/Pepperdash Core/Pepperdash Core/Logging/DebugMemory.cs @@ -10,12 +10,6 @@ namespace PepperDash.Core.DebugThings { public class DebugContextCollection { - /// - /// Property to tell the program not to intitialize when it boots, if desired - /// - [JsonProperty("doNotStartOnNextBoot")] - public bool DoNotStartOnNextBoot { get; set; } - [JsonProperty("items")] Dictionary Items; @@ -59,6 +53,13 @@ namespace PepperDash.Core.DebugThings [JsonProperty("level")] public int Level { get; set; } + /// + /// Property to tell the program not to intitialize when it boots, if desired + /// + [JsonProperty("doNotStartOnNextBoot")] + public bool DoNotStartOnNextBoot { get; set; } + + public DebugContextItem(DebugContextCollection parent) { From b7c7513a183624880028501611fc624ff8316337 Mon Sep 17 00:00:00 2001 From: Neil Dorin Date: Fri, 15 Nov 2019 16:46:25 -0700 Subject: [PATCH 03/11] Renamed property to DoNotLoadOnNextBoot for more clarity --- Pepperdash Core/Pepperdash Core/Logging/Debug.cs | 10 +++++----- Pepperdash Core/Pepperdash Core/Logging/DebugMemory.cs | 4 ++-- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/Pepperdash Core/Pepperdash Core/Logging/Debug.cs b/Pepperdash Core/Pepperdash Core/Logging/Debug.cs index ab5a3b5..737cf79 100644 --- a/Pepperdash Core/Pepperdash Core/Logging/Debug.cs +++ b/Pepperdash Core/Pepperdash Core/Logging/Debug.cs @@ -28,7 +28,7 @@ namespace PepperDash.Core public static int Level { get; private set; } - public static bool DoNotStartOnNextBoot { get; private set; } + public static bool DoNotLoadOnNextBoot { get; private set; } static DebugContextCollection Contexts; @@ -86,7 +86,7 @@ namespace PepperDash.Core var context = Contexts.GetOrCreateItem("DEFAULT"); Level = context.Level; - DoNotStartOnNextBoot = context.DoNotStartOnNextBoot; + DoNotLoadOnNextBoot = context.DoNotLoadOnNextBoot; try { @@ -248,12 +248,12 @@ namespace PepperDash.Core /// public static void SetDoNotStartOnNextBoot(bool state) { - DoNotStartOnNextBoot = state; - Contexts.GetOrCreateItem("DEFAULT").DoNotStartOnNextBoot = state; + DoNotLoadOnNextBoot = state; + Contexts.GetOrCreateItem("DEFAULT").DoNotLoadOnNextBoot = state; SaveMemoryOnTimeout(); CrestronConsole.PrintLine("[Application {0}], Do Not Start on Next Boot set to {1}", - InitialParametersClass.ApplicationNumber, DoNotStartOnNextBoot); + InitialParametersClass.ApplicationNumber, DoNotLoadOnNextBoot); } /// diff --git a/Pepperdash Core/Pepperdash Core/Logging/DebugMemory.cs b/Pepperdash Core/Pepperdash Core/Logging/DebugMemory.cs index 2312160..6dcb65f 100644 --- a/Pepperdash Core/Pepperdash Core/Logging/DebugMemory.cs +++ b/Pepperdash Core/Pepperdash Core/Logging/DebugMemory.cs @@ -56,8 +56,8 @@ namespace PepperDash.Core.DebugThings /// /// Property to tell the program not to intitialize when it boots, if desired /// - [JsonProperty("doNotStartOnNextBoot")] - public bool DoNotStartOnNextBoot { get; set; } + [JsonProperty("doNotLoadOnNextBoot")] + public bool DoNotLoadOnNextBoot { get; set; } public DebugContextItem(DebugContextCollection parent) From d442929453de4571d6ffde5b1e25595565742330 Mon Sep 17 00:00:00 2001 From: Neil Dorin Date: Fri, 15 Nov 2019 17:04:36 -0700 Subject: [PATCH 04/11] Fixed m ehtod name to SetDoNotLoadOnNextBoot --- Pepperdash Core/Pepperdash Core/Logging/Debug.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Pepperdash Core/Pepperdash Core/Logging/Debug.cs b/Pepperdash Core/Pepperdash Core/Logging/Debug.cs index 737cf79..9fa9e9f 100644 --- a/Pepperdash Core/Pepperdash Core/Logging/Debug.cs +++ b/Pepperdash Core/Pepperdash Core/Logging/Debug.cs @@ -246,7 +246,7 @@ namespace PepperDash.Core /// Sets the flag to prevent application starting on next boot /// /// - public static void SetDoNotStartOnNextBoot(bool state) + public static void SetDoNotLoadOnNextBoot(bool state) { DoNotLoadOnNextBoot = state; Contexts.GetOrCreateItem("DEFAULT").DoNotLoadOnNextBoot = state; From 374ebc8d850123a11956b34f945bb3f2f2fb9a49 Mon Sep 17 00:00:00 2001 From: Neil Dorin Date: Fri, 15 Nov 2019 18:02:56 -0700 Subject: [PATCH 05/11] Adds console command to get/set DoNotLoadOnNextBoot --- .../Pepperdash Core/Logging/Debug.cs | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/Pepperdash Core/Pepperdash Core/Logging/Debug.cs b/Pepperdash Core/Pepperdash Core/Logging/Debug.cs index 9fa9e9f..fca2b1c 100644 --- a/Pepperdash Core/Pepperdash Core/Logging/Debug.cs +++ b/Pepperdash Core/Pepperdash Core/Logging/Debug.cs @@ -67,6 +67,9 @@ namespace PepperDash.Core if (CrestronEnvironment.RuntimeEnvironment == eRuntimeEnvironment.SimplSharpPro) { // Add command to console + CrestronConsole.AddNewConsoleCommand(SetDoNotLoadOnNextBootFromConsole, "donotloadonnextboot", + "donotloadonnextboot:P [true/false]: Sets whether the application should load config on next boot", ConsoleAccessLevelEnum.AccessOperator); + CrestronConsole.AddNewConsoleCommand(SetDebugFromConsole, "appdebug", "appdebug:P [0-2]: Sets the application's console debug message level", ConsoleAccessLevelEnum.AccessOperator); @@ -145,6 +148,32 @@ namespace PepperDash.Core } } + /// + /// Callback for console command + /// + /// + public static void SetDoNotLoadOnNextBootFromConsole(string stateString) + { + try + { + if (string.IsNullOrEmpty(stateString.Trim())) + { + CrestronConsole.PrintLine("DoNotLoadOnNextBoot = {0}", DoNotLoadOnNextBoot); + return; + } + + SetDoNotLoadOnNextBoot(Boolean.Parse(stateString)); + } + catch + { + CrestronConsole.PrintLine("Usage: donotloadonnextboot:P [true/false]"); + } + } + + /// + /// Callback for console command + /// + /// public static void SetDebugFilterFromConsole(string items) { var str = items.Trim(); From 4f2c059e0cada71bcb085aa9f76412a6dc6edf3c Mon Sep 17 00:00:00 2001 From: Neil Dorin Date: Fri, 15 Nov 2019 21:30:43 -0700 Subject: [PATCH 06/11] Reduces length of help tip for new console command --- Pepperdash Core/Pepperdash Core/Logging/Debug.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Pepperdash Core/Pepperdash Core/Logging/Debug.cs b/Pepperdash Core/Pepperdash Core/Logging/Debug.cs index fca2b1c..5e83679 100644 --- a/Pepperdash Core/Pepperdash Core/Logging/Debug.cs +++ b/Pepperdash Core/Pepperdash Core/Logging/Debug.cs @@ -68,7 +68,7 @@ namespace PepperDash.Core { // Add command to console CrestronConsole.AddNewConsoleCommand(SetDoNotLoadOnNextBootFromConsole, "donotloadonnextboot", - "donotloadonnextboot:P [true/false]: Sets whether the application should load config on next boot", ConsoleAccessLevelEnum.AccessOperator); + "donotloadonnextboot:P [true/false]: Should the application load on next boot", ConsoleAccessLevelEnum.AccessOperator); CrestronConsole.AddNewConsoleCommand(SetDebugFromConsole, "appdebug", "appdebug:P [0-2]: Sets the application's console debug message level", From b701802326fa35a0795bbc806bf575e053d25f5e Mon Sep 17 00:00:00 2001 From: Neil Dorin Date: Thu, 12 Dec 2019 10:33:45 -0700 Subject: [PATCH 07/11] Adds console statement to clarify when DoNotStartOnNextBoot is set from console command --- Pepperdash Core/Pepperdash Core/Logging/Debug.cs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Pepperdash Core/Pepperdash Core/Logging/Debug.cs b/Pepperdash Core/Pepperdash Core/Logging/Debug.cs index 5e83679..14c6b37 100644 --- a/Pepperdash Core/Pepperdash Core/Logging/Debug.cs +++ b/Pepperdash Core/Pepperdash Core/Logging/Debug.cs @@ -91,6 +91,8 @@ namespace PepperDash.Core Level = context.Level; DoNotLoadOnNextBoot = context.DoNotLoadOnNextBoot; + CrestronConsole.PrintLine(string.Format("Program {0} will not load config after next boot. Use console command go:{0} to load the config manually", InitialParametersClass.ApplicationNumber)); + try { if (InitialParametersClass.NumberOfRemovableDrives > 0) From f9ae8ca38f6a656836c9798ef94193498d7e5043 Mon Sep 17 00:00:00 2001 From: Neil Dorin Date: Thu, 12 Dec 2019 14:14:56 -0700 Subject: [PATCH 08/11] Adds XML summary comments --- .../Comm/GenericTcpIpClient.cs | 145 +++++++++--------- 1 file changed, 76 insertions(+), 69 deletions(-) diff --git a/Pepperdash Core/Pepperdash Core/Comm/GenericTcpIpClient.cs b/Pepperdash Core/Pepperdash Core/Comm/GenericTcpIpClient.cs index b8b715f..3fb5e0a 100644 --- a/Pepperdash Core/Pepperdash Core/Comm/GenericTcpIpClient.cs +++ b/Pepperdash Core/Pepperdash Core/Comm/GenericTcpIpClient.cs @@ -11,15 +11,18 @@ using Newtonsoft.Json.Linq; namespace PepperDash.Core { + /// + /// A class to handle basic TCP/IP communications with a server + /// public class GenericTcpIpClient : Device, ISocketStatus, IAutoReconnect { /// - /// + /// Fires when data is received from the server and returns it as a Byte array /// public event EventHandler BytesReceived; /// - /// + /// Fires when data is received from the server and returns it as text /// public event EventHandler TextReceived; @@ -72,12 +75,12 @@ namespace PepperDash.Core public int BufferSize { get; set; } /// - /// + /// The actual client class /// public TCPClient Client { get; private set; } /// - /// + /// True if connected to the server /// public bool IsConnected { @@ -93,7 +96,7 @@ namespace PepperDash.Core } /// - /// + /// Status of the socket /// public SocketStatus ClientStatus { @@ -115,23 +118,23 @@ namespace PepperDash.Core } /// - /// + /// Status of the socket /// public string ClientStatusText { get { return ClientStatus.ToString(); } } [Obsolete] /// - /// + /// Ushort representation of client status /// public ushort UClientStatus { get { return (ushort)ClientStatus; } } /// - /// + /// Connection failure reason /// public string ConnectionFailure { get { return ClientStatus.ToString(); } } /// - /// + /// If true, enables AutoConnect /// public bool AutoReconnect { get; set; } @@ -164,7 +167,7 @@ namespace PepperDash.Core CTimer RetryTimer; /// - /// + /// Constructor /// /// /// @@ -180,26 +183,10 @@ namespace PepperDash.Core AutoReconnectIntervalMs = 5000; CrestronEnvironment.ProgramStatusEventHandler += new ProgramStatusEventHandler(CrestronEnvironment_ProgramStatusEventHandler); - - //if (string.IsNullOrEmpty(address)) - //{ - // Debug.Console(1, Debug.ErrorLogLevel.Warning, "GenericTcpIpClient '{0}': No address set", key); - // return; - //} - //if (port < 1 || port > 65535) - //{ - // { - // Debug.Console(1, Debug.ErrorLogLevel.Warning, "GenericTcpIpClient '{0}': Invalid port", key); - // return; - // } - //} - - //Client = new TCPClient(address, port, bufferSize); - //Client.SocketStatusChange += Client_SocketStatusChange; } /// - /// + /// Constructor /// /// public GenericTcpIpClient(string key) @@ -241,18 +228,23 @@ namespace PepperDash.Core } } - //public override bool CustomActivate() - //{ - // return true; - //} - + /// + /// + /// + /// public override bool Deactivate() { - if(Client != null) - Client.SocketStatusChange -= this.Client_SocketStatusChange; + if (Client != null) + { + Client.SocketStatusChange -= this.Client_SocketStatusChange; + DisconnectClient(); + } return true; } + /// + /// Attempts to connect to the server + /// public void Connect() { if (IsConnected) @@ -274,6 +266,7 @@ namespace PepperDash.Core if (Client == null) { Client = new TCPClient(Hostname, Port, BufferSize); + Client.SocketStatusChange -= Client_SocketStatusChange; Client.SocketStatusChange += Client_SocketStatusChange; } DisconnectCalledByUser = false; @@ -281,23 +274,37 @@ namespace PepperDash.Core Client.ConnectToServerAsync(ConnectToServerCallback); // (null); } + /// + /// Attempts to disconnect the client + /// public void Disconnect() { - DisconnectCalledByUser = true; - DisconnectClient(); + if (Client != null) + { + DisconnectCalledByUser = true; + DisconnectClient(); + Client = null; + Debug.Console(1, this, "Disconnected"); + } } + /// + /// Does the actual disconnect business + /// public void DisconnectClient() { if (Client != null) { Debug.Console(1, this, "Disconnecting client"); - //Client.SocketStatusChange -= Client_SocketStatusChange; if(IsConnected) Client.DisconnectFromServer(); } } + /// + /// Callback method for connection attempt + /// + /// void ConnectToServerCallback(TCPClient c) { Debug.Console(1, this, "Server connection result: {0}", c.ClientStatus); @@ -305,6 +312,9 @@ namespace PepperDash.Core WaitAndTryReconnect(); } + /// + /// Disconnects, waits and attemtps to connect again + /// void WaitAndTryReconnect() { DisconnectClient(); @@ -314,6 +324,11 @@ namespace PepperDash.Core RetryTimer = new CTimer(o => { Client.ConnectToServerAsync(ConnectToServerCallback); }, AutoReconnectIntervalMs); } + /// + /// Recieves incoming data + /// + /// + /// void Receive(TCPClient client, int numBytes) { if (numBytes > 0) @@ -358,6 +373,10 @@ namespace PepperDash.Core SendText(unescapedText); } + /// + /// Sends Bytes to the server + /// + /// public void SendBytes(byte[] bytes) { //if (Debug.Level == 2) @@ -366,7 +385,11 @@ namespace PepperDash.Core Client.SendData(bytes, bytes.Length); } - + /// + /// Socket Status Change Handler + /// + /// + /// void Client_SocketStatusChange(TCPClient client, SocketStatus clientSocketStatus) { Debug.Console(1, this, "Socket status change {0} ({1})", clientSocketStatus, ClientStatusText); @@ -393,15 +416,30 @@ namespace PepperDash.Core } } + /// + /// Configuration properties for TCP/SSH Connections + /// public class TcpSshPropertiesConfig { + /// + /// Address to connect to + /// [JsonProperty(Required = Required.Always)] public string Address { get; set; } + /// + /// Port to connect to + /// [JsonProperty(Required = Required.Always)] public int Port { get; set; } + /// + /// Username credential + /// public string Username { get; set; } + /// + /// Passord credential + /// public string Password { get; set; } /// @@ -430,35 +468,4 @@ namespace PepperDash.Core } - //public class TcpIpConfig - //{ - // [JsonProperty(Required = Required.Always)] - // public string Address { get; set; } - - // [JsonProperty(Required = Required.Always)] - // public int Port { get; set; } - - // /// - // /// Defaults to 32768 - // /// - // public int BufferSize { get; set; } - - // /// - // /// Defaults to true - // /// - // public bool AutoReconnect { get; set; } - - // /// - // /// Defaults to 5000ms - // /// - // public int AutoReconnectIntervalMs { get; set; } - - // public TcpIpConfig() - // { - // BufferSize = 32768; - // AutoReconnect = true; - // AutoReconnectIntervalMs = 5000; - // } - //} - } \ No newline at end of file From aebd577dd6f744f292b64f811adf4b9019db3bdb Mon Sep 17 00:00:00 2001 From: Neil Dorin Date: Thu, 12 Dec 2019 15:07:40 -0700 Subject: [PATCH 09/11] Adds null check in WaitAndTryReconnect() --- .../Pepperdash Core/Comm/GenericTcpIpClient.cs | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/Pepperdash Core/Pepperdash Core/Comm/GenericTcpIpClient.cs b/Pepperdash Core/Pepperdash Core/Comm/GenericTcpIpClient.cs index 3fb5e0a..53349df 100644 --- a/Pepperdash Core/Pepperdash Core/Comm/GenericTcpIpClient.cs +++ b/Pepperdash Core/Pepperdash Core/Comm/GenericTcpIpClient.cs @@ -317,11 +317,16 @@ namespace PepperDash.Core /// void WaitAndTryReconnect() { + if (Client != null) + { + Debug.Console(1, "Attempting reconnect, status={0}", Client.ClientStatus); + } + DisconnectClient(); - Debug.Console(1, "Attempting reconnect, status={0}", Client.ClientStatus); - if(!DisconnectCalledByUser) + if (!DisconnectCalledByUser) RetryTimer = new CTimer(o => { Client.ConnectToServerAsync(ConnectToServerCallback); }, AutoReconnectIntervalMs); + } /// From f3b70a8edb0a0749ebd1011f4297ec53c5f395fa Mon Sep 17 00:00:00 2001 From: Neil Dorin Date: Thu, 12 Dec 2019 15:14:55 -0700 Subject: [PATCH 10/11] Adds better null check to WaitAndTryReconnect() --- .../Pepperdash Core/Comm/GenericTcpIpClient.cs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Pepperdash Core/Pepperdash Core/Comm/GenericTcpIpClient.cs b/Pepperdash Core/Pepperdash Core/Comm/GenericTcpIpClient.cs index 53349df..c043da3 100644 --- a/Pepperdash Core/Pepperdash Core/Comm/GenericTcpIpClient.cs +++ b/Pepperdash Core/Pepperdash Core/Comm/GenericTcpIpClient.cs @@ -317,16 +317,16 @@ namespace PepperDash.Core /// void WaitAndTryReconnect() { + DisconnectClient(); + if (Client != null) { Debug.Console(1, "Attempting reconnect, status={0}", Client.ClientStatus); + + if (!DisconnectCalledByUser) + RetryTimer = new CTimer(o => { Client.ConnectToServerAsync(ConnectToServerCallback); }, AutoReconnectIntervalMs); } - DisconnectClient(); - - if (!DisconnectCalledByUser) - RetryTimer = new CTimer(o => { Client.ConnectToServerAsync(ConnectToServerCallback); }, AutoReconnectIntervalMs); - } /// From b96ac89195505cdb5ffe82860001e239fcffccbb Mon Sep 17 00:00:00 2001 From: Neil Dorin Date: Thu, 12 Dec 2019 15:32:58 -0700 Subject: [PATCH 11/11] adds a null check to Receive() --- .../Comm/GenericTcpIpClient.cs | 32 +++++++++++-------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/Pepperdash Core/Pepperdash Core/Comm/GenericTcpIpClient.cs b/Pepperdash Core/Pepperdash Core/Comm/GenericTcpIpClient.cs index c043da3..c1384f9 100644 --- a/Pepperdash Core/Pepperdash Core/Comm/GenericTcpIpClient.cs +++ b/Pepperdash Core/Pepperdash Core/Comm/GenericTcpIpClient.cs @@ -336,20 +336,24 @@ namespace PepperDash.Core /// void Receive(TCPClient client, int numBytes) { - if (numBytes > 0) - { - var bytes = client.IncomingDataBuffer.Take(numBytes).ToArray(); - var bytesHandler = BytesReceived; - if (bytesHandler != null) - bytesHandler(this, new GenericCommMethodReceiveBytesArgs(bytes)); - var textHandler = TextReceived; - if (textHandler != null) - { - var str = Encoding.GetEncoding(28591).GetString(bytes, 0, bytes.Length); - textHandler(this, new GenericCommMethodReceiveTextArgs(str)); - } - } - Client.ReceiveDataAsync(Receive); + if (client != null) + { + if (numBytes > 0) + { + var bytes = client.IncomingDataBuffer.Take(numBytes).ToArray(); + var bytesHandler = BytesReceived; + if (bytesHandler != null) + bytesHandler(this, new GenericCommMethodReceiveBytesArgs(bytes)); + var textHandler = TextReceived; + if (textHandler != null) + { + var str = Encoding.GetEncoding(28591).GetString(bytes, 0, bytes.Length); + textHandler(this, new GenericCommMethodReceiveTextArgs(str)); + } + } + + client.ReceiveDataAsync(Receive); + } } ///