Added close handlers to catch legit shutdowns from servers

This commit is contained in:
Heath Volmer
2018-09-10 20:23:57 -06:00
parent 1c9061cc28
commit 1df658b7eb

View File

@@ -86,9 +86,9 @@ namespace PepperDash.Essentials
CrestronConsole.AddNewConsoleCommand(PrintActionDictionaryPaths, "mobileshowactionpaths", CrestronConsole.AddNewConsoleCommand(PrintActionDictionaryPaths, "mobileshowactionpaths",
"Prints the paths in teh Action Dictionary", ConsoleAccessLevelEnum.AccessOperator); "Prints the paths in teh Action Dictionary", ConsoleAccessLevelEnum.AccessOperator);
CrestronConsole.AddNewConsoleCommand(s => ConnectStreamClient(), "mobileconnect", CrestronConsole.AddNewConsoleCommand(s => ConnectWebsocketClient(), "mobileconnect",
"Forces connect of websocket", ConsoleAccessLevelEnum.AccessOperator); "Forces connect of websocket", ConsoleAccessLevelEnum.AccessOperator);
CrestronConsole.AddNewConsoleCommand(s => DisconnectWebsocketClient(), "mobiledisco", CrestronConsole.AddNewConsoleCommand(s => CleanUpWebsocketClient(), "mobiledisco",
"Disconnects websocket", ConsoleAccessLevelEnum.AccessOperator); "Disconnects websocket", ConsoleAccessLevelEnum.AccessOperator);
CrestronEnvironment.ProgramStatusEventHandler += new ProgramStatusEventHandler(CrestronEnvironment_ProgramStatusEventHandler); CrestronEnvironment.ProgramStatusEventHandler += new ProgramStatusEventHandler(CrestronEnvironment_ProgramStatusEventHandler);
@@ -100,15 +100,15 @@ namespace PepperDash.Essentials
/// ///
/// </summary> /// </summary>
/// <param name="ethernetEventArgs"></param> /// <param name="ethernetEventArgs"></param>
void CrestronEnvironment_EthernetEventHandler(EthernetEventArgs ethernetEventArgs) void CrestronEnvironment_EthernetEventHandler(EthernetEventArgs args)
{ {
Debug.Console(1, this, Debug.ErrorLogLevel.Warning, "Ethernet status change, port {0}: {1}", Debug.Console(1, this, Debug.ErrorLogLevel.Warning, "Ethernet status change, port {0}: {1}",
ethernetEventArgs.EthernetAdapter, ethernetEventArgs.EthernetEventType); args.EthernetAdapter, args.EthernetEventType);
//if (ethernetEventArgs.EthernetEventType == eEthernetEventType.LinkDown) if (args.EthernetEventType == eEthernetEventType.LinkDown && WSClient != null && args.EthernetAdapter == WSClient.EthernetAdapter)
//{ {
// LinkUp = false; CleanUpWebsocketClient();
//} }
} }
/// <summary> /// <summary>
@@ -119,7 +119,7 @@ namespace PepperDash.Essentials
{ {
if (programEventType == eProgramStatusEventType.Stopping && WSClient.Connected) if (programEventType == eProgramStatusEventType.Stopping && WSClient.Connected)
{ {
DisconnectWebsocketClient(); CleanUpWebsocketClient();
} }
} }
@@ -299,7 +299,7 @@ namespace PepperDash.Essentials
/// <param name="url">URL of the server, including the port number, if not 80. Format: "serverUrlOrIp:port"</param> /// <param name="url">URL of the server, including the port number, if not 80. Format: "serverUrlOrIp:port"</param>
void RegisterSystemToServer() void RegisterSystemToServer()
{ {
ConnectStreamClient(); ConnectWebsocketClient();
return; return;
//var ready = RegisterLockEvent.Wait(20000); //var ready = RegisterLockEvent.Wait(20000);
@@ -401,14 +401,14 @@ namespace PepperDash.Essentials
/// Connects the Websocket Client /// Connects the Websocket Client
/// </summary> /// </summary>
/// <param name="o"></param> /// <param name="o"></param>
void ConnectStreamClient() void ConnectWebsocketClient()
{ {
Debug.Console(1, this, Debug.ErrorLogLevel.Notice, "Initializing Stream client to server."); Debug.Console(1, this, Debug.ErrorLogLevel.Notice, "Initializing Stream client to server.");
if (WSClient != null) if (WSClient != null)
{ {
Debug.Console(1, this, Debug.ErrorLogLevel.Notice, "Cleaning up previous socket"); Debug.Console(1, this, Debug.ErrorLogLevel.Notice, "Cleaning up previous socket");
DisconnectWebsocketClient(); CleanUpWebsocketClient();
} }
SystemUuid = ConfigReader.ConfigObject.SystemUuid; SystemUuid = ConfigReader.ConfigObject.SystemUuid;
@@ -508,15 +508,19 @@ namespace PepperDash.Essentials
/// Disconnects the SSE Client and stops the heartbeat timer /// Disconnects the SSE Client and stops the heartbeat timer
/// </summary> /// </summary>
/// <param name="command"></param> /// <param name="command"></param>
void DisconnectWebsocketClient() void CleanUpWebsocketClient()
{ {
Debug.Console(1, this, Debug.ErrorLogLevel.Notice, "Disconnecting websocket"); Debug.Console(1, this, Debug.ErrorLogLevel.Notice, "Disconnecting websocket");
if (WSClient != null) if (WSClient != null)
{ {
WSClient.Disconnect();
WSClient.SendCallBack = null; WSClient.SendCallBack = null;
WSClient.ReceiveCallBack = null; WSClient.ReceiveCallBack = null;
WSClient.ConnectionCallBack = null; WSClient.ConnectionCallBack = null;
WSClient.DisconnectCallBack = null;
if (WSClient.Connected)
{
WSClient.Disconnect();
}
WSClient = null; WSClient = null;
} }
} }
@@ -557,7 +561,7 @@ namespace PepperDash.Essentials
ServerHeartbeatCheckTimer.Stop(); ServerHeartbeatCheckTimer.Stop();
ServerHeartbeatCheckTimer = null; ServerHeartbeatCheckTimer = null;
} }
DisconnectWebsocketClient(); CleanUpWebsocketClient();
StartServerReconnectTimer(); StartServerReconnectTimer();
} }
@@ -568,14 +572,15 @@ namespace PepperDash.Essentials
/// <param name="repeatTime"></param> /// <param name="repeatTime"></param>
void ResetOrStartHearbeatTimer() void ResetOrStartHearbeatTimer()
{ {
if (ServerHeartbeatCheckTimer == null) if (ServerHeartbeatCheckTimer == null)
{ {
ServerHeartbeatCheckTimer = new CTimer(HeartbeatExpiredTimerCallback, null, ServerHeartbeatInterval, ServerHeartbeatInterval); ServerHeartbeatCheckTimer = new CTimer(HeartbeatExpiredTimerCallback, null, ServerHeartbeatInterval, ServerHeartbeatInterval);
Debug.Console(1, this, "Heartbeat Timer Started.");
Debug.Console(1, this, "Heartbeat Timer Started."); }
} else
{
ServerHeartbeatCheckTimer.Reset(ServerHeartbeatInterval, ServerHeartbeatInterval); ServerHeartbeatCheckTimer.Reset(ServerHeartbeatInterval, ServerHeartbeatInterval);
}
} }
/// <summary> /// <summary>
@@ -583,7 +588,7 @@ namespace PepperDash.Essentials
/// </summary> /// </summary>
void ReconnectStreamClient() void ReconnectStreamClient()
{ {
new CTimer(o => ConnectStreamClient(), 2000); new CTimer(o => ConnectWebsocketClient(), 2000);
} }
/// <summary> /// <summary>
@@ -668,10 +673,24 @@ namespace PepperDash.Essentials
int Websocket_ReceiveCallback(byte[] data, uint length, WebSocketClient.WEBSOCKET_PACKET_TYPES opcode, int Websocket_ReceiveCallback(byte[] data, uint length, WebSocketClient.WEBSOCKET_PACKET_TYPES opcode,
WebSocketClient.WEBSOCKET_RESULT_CODES err) WebSocketClient.WEBSOCKET_RESULT_CODES err)
{ {
var rx = System.Text.Encoding.UTF8.GetString(data, 0, (int)length); if (opcode == WebSocketClient.WEBSOCKET_PACKET_TYPES.LWS_WS_OPCODE_07__TEXT_FRAME)
if(rx.Length > 0) {
ParseStreamRx(rx); var rx = System.Text.Encoding.UTF8.GetString(data, 0, (int)length);
WSClient.ReceiveAsync(); if (rx.Length > 0)
ParseStreamRx(rx);
WSClient.ReceiveAsync();
}
else if (opcode == WebSocketClient.WEBSOCKET_PACKET_TYPES.LWS_WS_OPCODE_07__CLOSE)
{
Debug.Console(1, this, "Websocket disconnect received from remote");
CleanUpWebsocketClient();
}
else
{
Debug.Console(1, this, "websocket rx opcode/err {0}/{1}", opcode, err);
WSClient.ReceiveAsync();
}
return 0; return 0;
} }