More cleanup around remote access port switching

This commit is contained in:
Cyberboss
2017-11-23 15:40:13 -05:00
parent dabecf7270
commit d83c20f614
3 changed files with 61 additions and 13 deletions
+1 -2
View File
@@ -390,8 +390,7 @@ namespace TGS.CommandLine
}
Interface.Dispose(); //close the channels to prevent holding the service open
OutputProc("Change will be applied after service restart");
return ExitCode.Normal;
}
}
+9 -2
View File
@@ -332,8 +332,15 @@ namespace TGS.Server
/// </summary>
ReferencePush = 7700,
/// <summary>
/// Error: When restarting the root service host and an unrecoverable error was encountered
/// Info: When the root ServiceHost has been restarted
/// Error: When restarting the root ServiceHost and an unrecoverable error was encountered
/// </summary>
RootHostRestartFailure = 7800,
RootHostRestart = 7800,
/// <summary>
/// Info: When an operation to change the remote access port begins
/// Warning: When a port change operation fails and a revert is attempted
/// Error: When the config could not be saved when attempting to change the remote access port
/// </summary>
RemoteAccessPortChange = 7900,
}
}
+51 -9
View File
@@ -104,22 +104,37 @@ namespace TGS.Server
OnlineAllHosts();
}
void SafeCloseServiceHost(ServiceHost host)
{
try
{
host.Close();
}
catch
{
host.Abort();
}
}
/// <summary>
/// Attempts to reload <see cref="serviceHost"/>
/// </summary>
void RestartHost()
/// <returns><see langword="true"/> on success, <see langword="false"/> otherwise</returns>
bool RestartHost()
{
lock (this)
try
{
serviceHost.Close();
Config.Save(DefaultConfigDirectory);
SafeCloseServiceHost(serviceHost);
SetupService();
serviceHost.Open();
Logger.WriteInfo("Root ServiceHost restarted", EventID.RootHostRestart, LoggingID);
return true;
}
catch (Exception e)
{
Logger.WriteError(String.Format("An unrecoverable error occurred while attempting to restart the root ServiceHost! Error: {0}", e.ToString()), EventID.RootHostRestartFailure, LoggingID);
Logger.WriteError(String.Format("An unrecoverable error occurred while attempting to restart the root ServiceHost! Error: {0}", e.ToString()), EventID.RootHostRestart, LoggingID);
return false;
}
}
@@ -368,7 +383,7 @@ namespace TGS.Server
{
var host = I.Value;
var instance = (Instance)host.SingletonInstance;
host.Close();
SafeCloseServiceHost(host);
instance.Dispose();
UnlockLoggingID(instance.LoggingID);
}
@@ -377,7 +392,7 @@ namespace TGS.Server
{
Logger.WriteError(e.ToString(), EventID.ServiceShutdownFail, LoggingID);
}
serviceHost.Close();
SafeCloseServiceHost(serviceHost);
Config.Save(DefaultConfigDirectory);
}
}
@@ -403,8 +418,35 @@ namespace TGS.Server
{
if (port == 0)
return "Cannot bind to port 0";
Config.RemoteAccessPort = port;
Task.Factory.StartNew(() => RestartHost());
ushort orig_port;
lock (this)
{
orig_port = Config.RemoteAccessPort;
if (port == orig_port)
return null;
try
{
//save first just in case
Config.Save(DefaultConfigDirectory);
}
catch (Exception e)
{
Logger.WriteError(e.ToString(), EventID.RemoteAccessPortChange, LoggingID);
return String.Format("An error occurred: {0}", e.ToString());
}
Logger.WriteInfo(String.Format("Changing remote access port from {0} to {1}", orig_port, port), EventID.RemoteAccessPortChange, LoggingID);
Config.RemoteAccessPort = port;
}
Task.Factory.StartNew(() =>
{
lock (this)
if (!RestartHost())
{
Logger.WriteWarning(String.Format("Port change to {0} failed, Reverting to port {1}!", port, orig_port), EventID.RemoteAccessPortChange, LoggingID);
Config.RemoteAccessPort = orig_port;
RestartHost();
}
});
return null;
}
@@ -583,7 +625,7 @@ namespace TGS.Server
var host = hosts[Name];
hosts.Remove(Name);
var inst = (Instance)host.SingletonInstance;
host.Close();
SafeCloseServiceHost(host);
path = inst.ServerDirectory();
inst.Offline();
inst.Dispose();