@@ -18,12 +18,24 @@ permissions and limitations under the Licenses.
1818using System ;
1919using System . IO ;
2020using System . Reflection ;
21+ using System . Runtime . InteropServices ;
2122using System . Threading ;
2223using Flames . UI ;
2324namespace Flames . Cli
2425{
2526 public static class Program
2627 {
28+ public delegate bool ConsoleEventDelegate ( int eventType ) ;
29+ [ DllImport ( "kernel32.dll" , SetLastError = true ) ]
30+ public static extern bool SetConsoleCtrlHandler ( ConsoleEventDelegate callback , bool add ) ;
31+ public static ConsoleEventDelegate handler ;
32+ public static bool ConsoleEventCallback ( int eventType )
33+ {
34+ Write ( "&e-- Server shutdown requested --" ) ;
35+ Write ( "&eRestarting server..." ) ;
36+ Server . Stop ( true , Server . Config . DefaultShutdownMessage ) ;
37+ return false ;
38+ }
2739 [ STAThread ]
2840 public static void Main ( string [ ] args )
2941 {
@@ -98,6 +110,8 @@ public static void StartCLI()
98110 Server . Start ( ) ;
99111 Console . Title = Colors . Strip ( Server . Config . Name ) + " - " + Colors . Strip ( Server . SoftwareNameVersioned ) ;
100112 Console . CancelKeyPress += OnCancelKeyPress ;
113+ handler = new ConsoleEventDelegate ( ConsoleEventCallback ) ;
114+ SetConsoleCtrlHandler ( handler , true ) ;
101115 CheckNameVerification ( ) ;
102116 ConsoleLoop ( ) ;
103117 }
@@ -114,13 +128,13 @@ public static void OnCancelKeyPress(object sender, ConsoleCancelEventArgs e)
114128 case ConsoleSpecialKey . ControlBreak :
115129 // Cannot set e.Cancel for this one
116130 Write ( "&e-- Server shutdown (Ctrl+Break) --" ) ;
117- Thread stopThread = Server . Stop ( false , Server . Config . DefaultShutdownMessage ) ;
131+ Thread stopThread = Server . Stop ( true , Server . Config . DefaultShutdownMessage ) ;
118132 stopThread . Join ( ) ;
119133 break ;
120134 case ConsoleSpecialKey . ControlC :
121135 e . Cancel = true ;
122136 Write ( "&e-- Server shutdown (Ctrl+C) --" ) ;
123- Server . Stop ( false , Server . Config . DefaultShutdownMessage ) ;
137+ Server . Stop ( true , Server . Config . DefaultShutdownMessage ) ;
124138 break ;
125139 }
126140 }
@@ -321,4 +335,4 @@ public static ConsoleColor GetConsoleColor(char c)
321335 }
322336 }
323337 }
324- }
338+ }
0 commit comments