Skip to content

Commit

Permalink
add reconnect timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
Vaiz committed Oct 16, 2021
1 parent 6f0a37f commit 04164fb
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ protected override void SetVisibleCore(bool value)
{
if (value)
{
UpdateButtonState(HsHelper.Instance.UpdateHsState().RemoteConnectionCount == 0);
UpdateButtonState(!HsHelper.Instance.IsConnectedToServer);
updateTimer.Start();
}
else
Expand All @@ -112,7 +112,7 @@ protected override void OnClosing(CancelEventArgs e)

void UpdateButtonOnTimer(object sender, EventArgs e)
{
UpdateButtonState(HsHelper.Instance.UpdateHsState().RemoteConnectionCount == 0);
UpdateButtonState(!HsHelper.Instance.IsConnectedToServer);
}
void UpdateButtonState(bool disconnected)
{
Expand Down
4 changes: 2 additions & 2 deletions HsReconnectTool/HsReconnectTool/FloatReconnectButton.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ protected override void SetVisibleCore(bool value)
{
if (value)
{
UpdateButtonState(HsHelper.Instance.UpdateHsState().RemoteConnectionCount == 0);
UpdateButtonState(!HsHelper.Instance.IsConnectedToServer);
updateTimer.Start();
}
else
Expand All @@ -112,7 +112,7 @@ protected override void OnClosing(CancelEventArgs e)

void UpdateButtonOnTimer(object sender, EventArgs e)
{
UpdateButtonState(HsHelper.Instance.UpdateHsState().RemoteConnectionCount == 0);
UpdateButtonState(!HsHelper.Instance.IsConnectedToServer);
}
void UpdateButtonState(bool disconnected)
{
Expand Down
33 changes: 27 additions & 6 deletions HsReconnectTool/UtilLib/HsHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@ namespace UtilLib
{
public class HsHelper
{
private static readonly HsHelper singletonInst = new HsHelper();
static readonly HsHelper singletonInst = new HsHelper();
static readonly int DisconnectTimeoutMs = 4000;

Firewall firewall;
bool isForceDisconnected = false;

public static HsHelper Instance
{
Expand Down Expand Up @@ -55,15 +58,31 @@ public HsState UpdateHsState()

return state;
}
public bool IsConnectedToServer
{
get
{
if (isForceDisconnected)
return false;
return UpdateHsState().IsConnectedToServer;
}
}

void DisconnectViaFirewall()
{
isForceDisconnected = true;

firewall.EnableRule();
System.Threading.Thread.Sleep(4000);
System.Threading.Thread.Sleep(DisconnectTimeoutMs);
firewall.DisableRule();

isForceDisconnected = false;
}
void DisconnectViaTcpMessage(HsState state)
void DisconnectViaTcpMessage()
{
HsState state = UpdateHsState();
isForceDisconnected = true;

foreach (var c in state.Connections)
{
if (!Util.IsRemoteConnection(c))
Expand All @@ -74,19 +93,21 @@ void DisconnectViaTcpMessage(HsState state)
if (null != error)
MessageBox.Show(String.Format("Cannot close connection {0}\r\nError: {1}", c, error));
}

System.Threading.Thread.Sleep(DisconnectTimeoutMs);
isForceDisconnected = false;
}
public void CloseConnectionsToServer()
{
HsState state = UpdateHsState();
Console.WriteLine("Closing connections... HS running: {0}", state.IsRunning);
Console.WriteLine("Closing connections...");

if (firewall != null)
{
Task.Factory.StartNew(DisconnectViaFirewall);
}
else
{
DisconnectViaTcpMessage(state);
Task.Factory.StartNew(DisconnectViaTcpMessage);
}
}

Expand Down
9 changes: 8 additions & 1 deletion HsReconnectTool/UtilLib/HsState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,14 @@ public int ConnectionCount
return connections.Count;
}
}
public int RemoteConnectionCount
public bool IsConnectedToServer
{
get
{
return RemoteConnectionCount > 0;
}
}
int RemoteConnectionCount
{
get
{
Expand Down

0 comments on commit 04164fb

Please sign in to comment.