From 2c5c4e39a4bedf42912dfee68884f09782eb9476 Mon Sep 17 00:00:00 2001 From: Joshua Gutenplan Date: Tue, 18 Jun 2019 16:54:29 -0700 Subject: [PATCH] updated client to add an event for the auto reconnect. Using this event with empty args to catch the client before the next attempt and change the port for a multi server environment. Also updated the server to not kill on a listen so that if the server stops listening due to max clients (does this automatically) it will be able to start listening on when it drops below max clients without disconnecting all connected clients. On server Also udpated the onConnectionChange to fire in its own thread so that the server can update its state and the state will be accurate in the event callbacks. See description in code. --- .../Comm/GenericSecureTcpIpClient_ForServer.cs | 4 ++++ .../Pepperdash Core/Comm/GenericSecureTcpIpServer.cs | 7 +++++-- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/Pepperdash Core/Pepperdash Core/Comm/GenericSecureTcpIpClient_ForServer.cs b/Pepperdash Core/Pepperdash Core/Comm/GenericSecureTcpIpClient_ForServer.cs index 334824a..c3882ca 100644 --- a/Pepperdash Core/Pepperdash Core/Comm/GenericSecureTcpIpClient_ForServer.cs +++ b/Pepperdash Core/Pepperdash Core/Comm/GenericSecureTcpIpClient_ForServer.cs @@ -33,6 +33,8 @@ namespace PepperDash.Core public event EventHandler TextReceived; + public event EventHandler AutoReconnectTriggered; + /// /// Event for Receiving text. Once subscribed to this event the receive callback will start a thread that dequeues the messages and invokes the event on a new thread. /// It is not recommended to use both the TextReceived event and the TextReceivedQueueInvoke event. @@ -559,6 +561,8 @@ namespace PepperDash.Core RetryTimer.Stop(); RetryTimer = null; } + if(AutoReconnectTriggered != null) + AutoReconnectTriggered(this, new EventArgs()); RetryTimer = new CTimer(o => Connect(), rndTime); } } diff --git a/Pepperdash Core/Pepperdash Core/Comm/GenericSecureTcpIpServer.cs b/Pepperdash Core/Pepperdash Core/Comm/GenericSecureTcpIpServer.cs index 3f21ac3..ea9c606 100644 --- a/Pepperdash Core/Pepperdash Core/Comm/GenericSecureTcpIpServer.cs +++ b/Pepperdash Core/Pepperdash Core/Comm/GenericSecureTcpIpServer.cs @@ -416,7 +416,7 @@ namespace PepperDash.Core } else { - KillServer(); + //KillServer(); Remove this to be able to reactivate listener if it stops itself due to max clients without disconnecting connected clients. SecureServer.PortNumber = Port; } ServerStopped = false; @@ -703,7 +703,10 @@ namespace PepperDash.Core { Debug.Console(0, this, Debug.ErrorLogLevel.Error, "Error in Socket Status Change Callback. Error: {0}", ex); } - onConnectionChange(clientIndex, server.GetServerSocketStatusForSpecificClient(clientIndex)); + //Use a thread for this event so that the server state updates to listening while this event is processed. Listening must be added to the server state + //after every client connection so that the server can check and see if it is at max clients. Due to this the event fires and server listening enum bit flag + //is not set. Putting in a thread allows the state to update before this event processes so that the subscribers to this event get accurate isListening in the event. + CrestronInvoke.BeginInvoke(o => onConnectionChange(clientIndex, server.GetServerSocketStatusForSpecificClient(clientIndex)), null); } #endregion