gameServer/Actions.hs
changeset 5209 f7a610e2ef5f
parent 5184 bf7bba60ed93
child 5210 a5329e52a71b
--- a/gameServer/Actions.hs	Thu May 12 23:29:31 2011 +0200
+++ b/gameServer/Actions.hs	Sun May 15 18:10:01 2011 +0400
@@ -18,6 +18,8 @@
 import Control.Arrow
 import Control.Exception
 import OfficialServer.GameReplayStore
+import System.Process
+import Network.Socket
 -----------------------------
 import CoreTypes
 import Utils
@@ -57,7 +59,7 @@
     | DeleteClient ClientIndex
     | PingAll
     | StatsAction
-    | RestartServer Bool
+    | RestartServer
     | AddNick2Bans B.ByteString B.ByteString UTCTime
     | AddIP2Bans B.ByteString B.ByteString UTCTime
     | CheckBanned
@@ -153,6 +155,10 @@
 
     s <- get
     put $! s{removedClients = ci `Set.delete` removedClients s}
+    
+    sp <- gets (shutdownPending . serverInfo)
+    cls <- allClientsS
+    io $ when (sp && null cls) $ throwIO ShutdownException
 
 processAction (ModifyClient f) = do
     (Just ci) <- gets clientIndex
@@ -467,11 +473,15 @@
     where
           st irnc = (length $ allRooms irnc, length $ allClients irnc)
 
-processAction (RestartServer force) = do
-    if force then do
-        throw RestartException
-        else
-        processAction $ ModifyServerInfo (\s -> s{restartPending=True})
+processAction RestartServer = do
+    sock <- gets (fromJust . serverSocket . serverInfo)
+    io $ do
+        noticeM "Core" "Closing listening socket"
+        sClose sock
+        noticeM "Core" "Spawning new server"
+        _ <- createProcess (proc "./hedgewars-server" [])
+        return ()
+    processAction $ ModifyServerInfo (\s -> s{shutdownPending=True})
 
 processAction SaveReplay = do
     ri <- clientRoomA