Cleanly shut down on SIGTERM
This commit is contained in:
parent
79e584cd27
commit
4cbf9e714c
@ -30,6 +30,7 @@ namespace TrueCraft.API.Server
|
|||||||
bool EnableClientLogging { get; set; }
|
bool EnableClientLogging { get; set; }
|
||||||
|
|
||||||
void Start(IPEndPoint endPoint);
|
void Start(IPEndPoint endPoint);
|
||||||
|
void Stop();
|
||||||
void RegisterPacketHandler(byte packetId, PacketHandler handler);
|
void RegisterPacketHandler(byte packetId, PacketHandler handler);
|
||||||
void AddWorld(IWorld world);
|
void AddWorld(IWorld world);
|
||||||
void AddLogProvider(ILogProvider provider);
|
void AddLogProvider(ILogProvider provider);
|
||||||
|
@ -60,6 +60,7 @@ namespace TrueCraft
|
|||||||
private readonly PacketHandler[] PacketHandlers;
|
private readonly PacketHandler[] PacketHandlers;
|
||||||
private IList<ILogProvider> LogProviders;
|
private IList<ILogProvider> LogProviders;
|
||||||
internal object ClientLock = new object();
|
internal object ClientLock = new object();
|
||||||
|
private bool ShuttingDown = false;
|
||||||
|
|
||||||
public MultiplayerServer()
|
public MultiplayerServer()
|
||||||
{
|
{
|
||||||
@ -96,6 +97,7 @@ namespace TrueCraft
|
|||||||
|
|
||||||
public void Start(IPEndPoint endPoint)
|
public void Start(IPEndPoint endPoint)
|
||||||
{
|
{
|
||||||
|
ShuttingDown = false;
|
||||||
Listener = new TcpListener(endPoint);
|
Listener = new TcpListener(endPoint);
|
||||||
Listener.Start();
|
Listener.Start();
|
||||||
Listener.BeginAcceptTcpClient(AcceptClient, null);
|
Listener.BeginAcceptTcpClient(AcceptClient, null);
|
||||||
@ -104,6 +106,16 @@ namespace TrueCraft
|
|||||||
EnvironmentWorker.Change(100, 1000 / 20);
|
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)
|
public void AddWorld(IWorld world)
|
||||||
{
|
{
|
||||||
Worlds.Add(world);
|
Worlds.Add(world);
|
||||||
@ -235,6 +247,8 @@ namespace TrueCraft
|
|||||||
|
|
||||||
private void DoEnvironment(object discarded)
|
private void DoEnvironment(object discarded)
|
||||||
{
|
{
|
||||||
|
if (ShuttingDown)
|
||||||
|
return;
|
||||||
Scheduler.Update();
|
Scheduler.Update();
|
||||||
foreach (var manager in EntityManagers)
|
foreach (var manager in EntityManagers)
|
||||||
{
|
{
|
||||||
@ -246,6 +260,8 @@ namespace TrueCraft
|
|||||||
{
|
{
|
||||||
while (true)
|
while (true)
|
||||||
{
|
{
|
||||||
|
if (ShuttingDown)
|
||||||
|
return;
|
||||||
bool idle = true;
|
bool idle = true;
|
||||||
for (int i = 0; i < Clients.Count && i >= 0; i++)
|
for (int i = 0; i < Clients.Count && i >= 0; i++)
|
||||||
{
|
{
|
||||||
|
@ -81,10 +81,7 @@ namespace TrueCraft
|
|||||||
|
|
||||||
static void HandleCancelKeyPress(object sender, ConsoleCancelEventArgs e)
|
static void HandleCancelKeyPress(object sender, ConsoleCancelEventArgs e)
|
||||||
{
|
{
|
||||||
foreach (var w in Server.Worlds)
|
Server.Stop();
|
||||||
{
|
|
||||||
w.Save();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void HandleChatMessageReceived(object sender, ChatMessageEventArgs e)
|
static void HandleChatMessageReceived(object sender, ChatMessageEventArgs e)
|
||||||
|
Reference in New Issue
Block a user