mirror of
https://github.com/PepperDash/Essentials.git
synced 2026-07-02 10:38:16 +00:00
fix: add port forward timeout handling in DebugSessionRequestHandler
This commit is contained in:
parent
782bb6c057
commit
3974455337
2 changed files with 73 additions and 1 deletions
|
|
@ -72,6 +72,20 @@ namespace PepperDash.Core
|
|||
/// </summary>
|
||||
public bool IsRunning { get => _httpsServer?.IsListening ?? false; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets a value indicating whether there are active WebSocket connections.
|
||||
/// </summary>
|
||||
public bool HasActiveConnections
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_httpsServer == null || !_httpsServer.IsListening) return false;
|
||||
var service = _httpsServer.WebSocketServices[_path];
|
||||
if (service == null) return false;
|
||||
return service.Sessions.Count > 0;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private readonly ITextFormatter _textFormatter;
|
||||
|
||||
|
|
@ -218,6 +232,8 @@ namespace PepperDash.Core
|
|||
Debug.LogInformation("Starting Websocket Server on port: {0}", port);
|
||||
|
||||
|
||||
|
||||
|
||||
Start(port, CertPath, _certificatePassword);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -18,6 +18,9 @@ namespace PepperDash.Essentials.Core.Web.RequestHandlers
|
|||
/// </summary>
|
||||
public class DebugSessionRequestHandler : WebApiBaseRequestHandler
|
||||
{
|
||||
private CTimer _portForwardTimeoutTimer;
|
||||
private readonly object _timerLock = new object();
|
||||
|
||||
/// <summary>
|
||||
/// Constructor
|
||||
/// </summary>
|
||||
|
|
@ -81,6 +84,7 @@ namespace PepperDash.Essentials.Core.Web.RequestHandlers
|
|||
else
|
||||
{
|
||||
Debug.LogMessage(LogEventLevel.Information, "Port {0} forwarded to CS LAN for debug websocket", port);
|
||||
StartPortForwardTimeout(port, csIp);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -126,6 +130,8 @@ namespace PepperDash.Essentials.Core.Web.RequestHandlers
|
|||
/// <param name="context"></param>
|
||||
protected override void HandlePost(HttpCwsContext context)
|
||||
{
|
||||
CancelPortForwardTimeout();
|
||||
|
||||
var port = Debug.WebsocketSink.Port;
|
||||
|
||||
Debug.WebsocketSink.StopServer();
|
||||
|
|
@ -174,5 +180,55 @@ namespace PepperDash.Essentials.Core.Web.RequestHandlers
|
|||
Debug.LogMessage(LogEventLevel.Information, "Websocket Debug Session Stopped");
|
||||
}
|
||||
|
||||
private void StartPortForwardTimeout(int port, string csIp)
|
||||
{
|
||||
lock (_timerLock)
|
||||
{
|
||||
_portForwardTimeoutTimer?.Dispose();
|
||||
_portForwardTimeoutTimer = new CTimer(_ =>
|
||||
{
|
||||
if (Debug.WebsocketSink.HasActiveConnections)
|
||||
{
|
||||
Debug.LogMessage(LogEventLevel.Debug, "Debug websocket has active connections; keeping port forward");
|
||||
return;
|
||||
}
|
||||
|
||||
Debug.LogMessage(LogEventLevel.Information, "No debug websocket connection within 30 seconds; removing port forward for port {0}", port);
|
||||
|
||||
try
|
||||
{
|
||||
var result = CrestronEthernetHelper.RemovePortForwarding(
|
||||
(ushort)port, (ushort)port, csIp,
|
||||
CrestronEthernetHelper.ePortMapTransport.TCP);
|
||||
|
||||
if (result != CrestronEthernetHelper.PortForwardingUserPatRetCodes.NoErr)
|
||||
{
|
||||
Debug.LogMessage(LogEventLevel.Warning, "Error removing port forwarding on timeout: {0}", result);
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.LogMessage(LogEventLevel.Information, "Port forwarding for port {0} removed due to timeout", port);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Debug.LogMessage(LogEventLevel.Warning, "Error removing port forwarding on timeout: {0}", ex.Message);
|
||||
}
|
||||
}, 30000);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Cancels the port forward timeout timer if a session is being explicitly stopped.
|
||||
/// </summary>
|
||||
private void CancelPortForwardTimeout()
|
||||
{
|
||||
lock (_timerLock)
|
||||
{
|
||||
_portForwardTimeoutTimer?.Dispose();
|
||||
_portForwardTimeoutTimer = null;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue