Added API.OnStop(). Added ConsoleCancelEvent
This commit is contained in:
@ -5,6 +5,7 @@ namespace CoopServer
|
||||
{
|
||||
class Program
|
||||
{
|
||||
public static bool ReadyToStop = false;
|
||||
static void Main(string[] args)
|
||||
{
|
||||
try
|
||||
@ -16,6 +17,15 @@ namespace CoopServer
|
||||
File.WriteAllText("log.txt", string.Empty);
|
||||
}
|
||||
|
||||
Console.CancelKeyPress += delegate(object sender, ConsoleCancelEventArgs e)
|
||||
{
|
||||
if (e.SpecialKey == ConsoleSpecialKey.ControlC)
|
||||
{
|
||||
e.Cancel = true;
|
||||
ReadyToStop = true;
|
||||
}
|
||||
};
|
||||
|
||||
_ = new Server();
|
||||
}
|
||||
catch (Exception e)
|
||||
|
@ -97,7 +97,7 @@ namespace CoopServer
|
||||
|
||||
bool responseError = false;
|
||||
|
||||
while (!responseError)
|
||||
while (!Program.ReadyToStop && !responseError)
|
||||
{
|
||||
string msg =
|
||||
"{ " +
|
||||
@ -193,7 +193,7 @@ namespace CoopServer
|
||||
{
|
||||
Logging.Info("Listening for clients");
|
||||
|
||||
while (true)
|
||||
while (!Program.ReadyToStop)
|
||||
{
|
||||
NetIncomingMessage message;
|
||||
|
||||
@ -432,6 +432,14 @@ namespace CoopServer
|
||||
// 16 milliseconds to sleep to reduce CPU usage
|
||||
Thread.Sleep(1000 / 60);
|
||||
}
|
||||
|
||||
Logging.Warning("Server is shutting down!");
|
||||
// Waiting for resource...
|
||||
while (MainResource != null && !MainResource.ReadyToStop)
|
||||
{
|
||||
// 16 milliseconds to sleep to reduce CPU usage
|
||||
Thread.Sleep(1000 / 60);
|
||||
}
|
||||
}
|
||||
|
||||
#region -- PLAYER --
|
||||
|
@ -17,8 +17,9 @@ namespace CoopServer
|
||||
|
||||
internal class Resource
|
||||
{
|
||||
public bool ReadyToStop = false;
|
||||
|
||||
private static Thread _mainThread;
|
||||
private static bool _hasToStop = false;
|
||||
private static Queue _actionQueue;
|
||||
private static ServerScript _script;
|
||||
|
||||
@ -40,7 +41,7 @@ namespace CoopServer
|
||||
|
||||
private void ThreadLoop()
|
||||
{
|
||||
while (!_hasToStop)
|
||||
while (!Program.ReadyToStop)
|
||||
{
|
||||
Queue localQueue;
|
||||
lock (_actionQueue.SyncRoot)
|
||||
@ -57,6 +58,9 @@ namespace CoopServer
|
||||
// 16 milliseconds to sleep to reduce CPU usage
|
||||
Thread.Sleep(1000 / 60);
|
||||
}
|
||||
|
||||
_script.API.InvokeStop();
|
||||
ReadyToStop = true;
|
||||
}
|
||||
|
||||
public bool InvokeModPacketReceived(long from, long target, string mod, byte customID, byte[] bytes)
|
||||
@ -137,6 +141,7 @@ namespace CoopServer
|
||||
|
||||
#region EVENTS
|
||||
public event EmptyEvent OnStart;
|
||||
public event EmptyEvent OnStop;
|
||||
public event ChatEvent OnChatMessage;
|
||||
public event PlayerEvent OnPlayerHandshake;
|
||||
public event PlayerEvent OnPlayerConnected;
|
||||
@ -151,6 +156,11 @@ namespace CoopServer
|
||||
OnStart?.Invoke();
|
||||
}
|
||||
|
||||
internal void InvokeStop()
|
||||
{
|
||||
OnStop?.Invoke();
|
||||
}
|
||||
|
||||
internal void InvokePlayerHandshake(Client client)
|
||||
{
|
||||
OnPlayerHandshake?.Invoke(client);
|
||||
|
Reference in New Issue
Block a user