From b12e79804174880810c2aa9006e0d63574636b83 Mon Sep 17 00:00:00 2001 From: Alex Johnson Date: Wed, 24 Aug 2022 14:11:46 -0400 Subject: [PATCH] Reworks reconnect timer due to issues seen on 4 series where autoreconnect cycle breaks. --- .../Pepperdash Core/Comm/GenericSshClient.cs | 39 ++++++-------- .../Comm/GenericTcpIpClient.cs | 51 ++++++++++++------- 2 files changed, 48 insertions(+), 42 deletions(-) diff --git a/Pepperdash Core/Pepperdash Core/Comm/GenericSshClient.cs b/Pepperdash Core/Pepperdash Core/Comm/GenericSshClient.cs index a0093a7..5f4c5c8 100644 --- a/Pepperdash Core/Pepperdash Core/Comm/GenericSshClient.cs +++ b/Pepperdash Core/Pepperdash Core/Comm/GenericSshClient.cs @@ -151,6 +151,11 @@ namespace PepperDash.Core Username = username; Password = password; AutoReconnectIntervalMs = 5000; + + ReconnectTimer = new CTimer(o => + { + Connect(); + }, Timeout.Infinite); } /// @@ -162,6 +167,11 @@ namespace PepperDash.Core StreamDebugging = new CommunicationStreamDebugging(SPlusKey); CrestronEnvironment.ProgramStatusEventHandler += new ProgramStatusEventHandler(CrestronEnvironment_ProgramStatusEventHandler); AutoReconnectIntervalMs = 5000; + + ReconnectTimer = new CTimer(o => + { + Connect(); + }, Timeout.Infinite); } /// @@ -203,11 +213,7 @@ namespace PepperDash.Core Debug.Console(1, this, "attempting connect"); // Cancel reconnect if running. - if (ReconnectTimer != null) - { - ReconnectTimer.Stop(); - ReconnectTimer = null; - } + ReconnectTimer.Stop(); // Don't try to connect if already if (IsConnected) @@ -297,11 +303,7 @@ namespace PepperDash.Core { ConnectEnabled = false; // Stop trying reconnects, if we are - if (ReconnectTimer != null) - { - ReconnectTimer.Stop(); - ReconnectTimer = null; - } + ReconnectTimer.Stop(); KillClient(SocketStatus.SOCKET_STATUS_BROKEN_LOCALLY); } @@ -334,20 +336,9 @@ namespace PepperDash.Core if (AutoReconnect && ConnectEnabled) { Debug.Console(1, this, "Checking autoreconnect: {0}, {1}ms", AutoReconnect, AutoReconnectIntervalMs); - if (ReconnectTimer == null) - { - ReconnectTimer = new CTimer(o => - { - Connect(); - }, AutoReconnectIntervalMs); - Debug.Console(1, this, "Attempting connection in {0} seconds", - (float) (AutoReconnectIntervalMs/1000)); - } - else - { - Debug.Console(1, this, "{0} second reconnect cycle running", - (float) (AutoReconnectIntervalMs/1000)); - } + ReconnectTimer.Reset(AutoReconnectIntervalMs); + Debug.Console(1, this, "Attempting connection in {0} seconds", + (float) (AutoReconnectIntervalMs/1000)); } } diff --git a/Pepperdash Core/Pepperdash Core/Comm/GenericTcpIpClient.cs b/Pepperdash Core/Pepperdash Core/Comm/GenericTcpIpClient.cs index 480e4d1..5f09f36 100644 --- a/Pepperdash Core/Pepperdash Core/Comm/GenericTcpIpClient.cs +++ b/Pepperdash Core/Pepperdash Core/Comm/GenericTcpIpClient.cs @@ -185,6 +185,16 @@ namespace PepperDash.Core BufferSize = bufferSize; AutoReconnectIntervalMs = 5000; + RetryTimer = new CTimer(o => + { + if (Client == null) + { + return; + } + + Client.ConnectToServerAsync(ConnectToServerCallback); + }, Timeout.Infinite); + CrestronEnvironment.ProgramStatusEventHandler += new ProgramStatusEventHandler(CrestronEnvironment_ProgramStatusEventHandler); } @@ -199,6 +209,16 @@ namespace PepperDash.Core CrestronEnvironment.ProgramStatusEventHandler += new ProgramStatusEventHandler(CrestronEnvironment_ProgramStatusEventHandler); AutoReconnectIntervalMs = 5000; BufferSize = 2000; + + RetryTimer = new CTimer(o => + { + if (Client == null) + { + return; + } + + Client.ConnectToServerAsync(ConnectToServerCallback); + }, Timeout.Infinite); } /// @@ -211,6 +231,16 @@ namespace PepperDash.Core CrestronEnvironment.ProgramStatusEventHandler += new ProgramStatusEventHandler(CrestronEnvironment_ProgramStatusEventHandler); AutoReconnectIntervalMs = 5000; BufferSize = 2000; + + RetryTimer = new CTimer(o => + { + if (Client == null) + { + return; + } + + Client.ConnectToServerAsync(ConnectToServerCallback); + }, Timeout.Infinite); } /// @@ -287,11 +317,7 @@ namespace PepperDash.Core DisconnectCalledByUser = true; // Stop trying reconnects, if we are - if (RetryTimer != null) - { - RetryTimer.Stop(); - RetryTimer = null; - } + RetryTimer.Stop(); if (Client != null) { @@ -337,15 +363,7 @@ namespace PepperDash.Core Debug.Console(1, this, "Attempting reconnect, status={0}", Client.ClientStatus); if (!DisconnectCalledByUser) - RetryTimer = new CTimer(o => - { - if (Client == null) - { - return; - } - - Client.ConnectToServerAsync(ConnectToServerCallback); - }, AutoReconnectIntervalMs); + RetryTimer.Reset(AutoReconnectIntervalMs); } } @@ -381,11 +399,8 @@ namespace PepperDash.Core textHandler(this, new GenericCommMethodReceiveTextArgs(str)); - } - - + } } - client.ReceiveDataAsync(Receive); } }