Cleanly shut down on SIGTERM

This commit is contained in:
Drew DeVault 2015-04-26 18:39:37 -06:00
parent 79e584cd27
commit 4cbf9e714c
3 changed files with 18 additions and 4 deletions

View File

@ -30,6 +30,7 @@ namespace TrueCraft.API.Server
bool EnableClientLogging { get; set; }
void Start(IPEndPoint endPoint);
void Stop();
void RegisterPacketHandler(byte packetId, PacketHandler handler);
void AddWorld(IWorld world);
void AddLogProvider(ILogProvider provider);

View File

@ -60,6 +60,7 @@ namespace TrueCraft
private readonly PacketHandler[] PacketHandlers;
private IList<ILogProvider> LogProviders;
internal object ClientLock = new object();
private bool ShuttingDown = false;
public MultiplayerServer()
{
@ -96,6 +97,7 @@ namespace TrueCraft
public void Start(IPEndPoint endPoint)
{
ShuttingDown = false;
Listener = new TcpListener(endPoint);
Listener.Start();
Listener.BeginAcceptTcpClient(AcceptClient, null);
@ -104,6 +106,16 @@ namespace TrueCraft
EnvironmentWorker.Change(100, 1000 / 20);
}
public void Stop()
{
ShuttingDown = true;
Listener.Stop();
foreach (var w in Worlds)
w.Save();
foreach (var c in Clients)
DisconnectClient(c);
}
public void AddWorld(IWorld world)
{
Worlds.Add(world);
@ -235,6 +247,8 @@ namespace TrueCraft
private void DoEnvironment(object discarded)
{
if (ShuttingDown)
return;
Scheduler.Update();
foreach (var manager in EntityManagers)
{
@ -246,6 +260,8 @@ namespace TrueCraft
{
while (true)
{
if (ShuttingDown)
return;
bool idle = true;
for (int i = 0; i < Clients.Count && i >= 0; i++)
{

View File

@ -81,10 +81,7 @@ namespace TrueCraft
static void HandleCancelKeyPress(object sender, ConsoleCancelEventArgs e)
{
foreach (var w in Server.Worlds)
{
w.Save();
}
Server.Stop();
}
static void HandleChatMessageReceived(object sender, ChatMessageEventArgs e)