From 7339587bd1a30f4ed22a081fdd199ccb1c0ef5b0 Mon Sep 17 00:00:00 2001 From: nek0 Date: Tue, 12 Dec 2023 11:21:25 +0100 Subject: [PATCH] fix socket file removal --- src-client/Client/Communication.hs | 6 ++++-- src-client/Client/Types.hs | 6 ++++-- src-server/Main.hs | 6 ++++-- src-server/Server/Communication.hs | 14 +++++++------- 4 files changed, 19 insertions(+), 13 deletions(-) diff --git a/src-client/Client/Communication.hs b/src-client/Client/Communication.hs index 82c8c99..1114ab9 100644 --- a/src-client/Client/Communication.hs +++ b/src-client/Client/Communication.hs @@ -1,6 +1,8 @@ {-# LANGUAGE LambdaCase #-} module Client.Communication where +import Control.Concurrent (threadDelay) + import qualified Control.Concurrent.STM as STM import Control.Monad (void) @@ -73,9 +75,9 @@ terminateGameOnSigint = do void $ liftIO $ installHandler keyboardSignal (CatchOnce $ do - void $ STM.atomically $ STM.swapTMVar serverState (clientState { clientStop = True }) + currentState <- STM.atomically $ STM.readTMVar clientState threadDelay (10 ^ 6) - sendMessage sock (ClientMessage clientId ClientQuit) + sendMessage (ClientMessage clientId ClientQuit) sock close sock -- Raise SIGINT again so it does not get blocked raiseSignal keyboardSignal diff --git a/src-client/Client/Types.hs b/src-client/Client/Types.hs index 3cec11c..50c7f15 100644 --- a/src-client/Client/Types.hs +++ b/src-client/Client/Types.hs @@ -1,5 +1,7 @@ module Client.Types where +import qualified Control.Concurrent.STM as STM + import Control.Monad.RWS import Data.UUID @@ -28,9 +30,9 @@ data ReaderContainer = ReaderContainer , rcClientUUID :: UUID } -newtype StateContainer = StateContainer +data StateContainer = StateContainer { scWizard :: Wizard - , scClientState :: ClientState + , scClientState :: STM.TMVar ClientState } type Game = RWST ReaderContainer String StateContainer IO diff --git a/src-server/Main.hs b/src-server/Main.hs index c48db85..f565498 100644 --- a/src-server/Main.hs +++ b/src-server/Main.hs @@ -26,6 +26,7 @@ import Server.Communication import Server.Game import Server.Map (generateArena) import Server.Types +import Server.Util main :: IO () main = do @@ -56,12 +57,13 @@ main = do initState = StateContainer (arenaSpawners arena) [] serverState now sockList messageQueue (finalState, finalWrite) <- execRWST (do - terminateGameOnSigint + terminateGameOnSigint setSocketPath runGame ) initRead initState - print finalWrite + removeIfExists setSocketPath + putStrLn "bye bye" where opts = info (options <**> helper) ( fullDesc diff --git a/src-server/Server/Communication.hs b/src-server/Server/Communication.hs index 9dffe22..7371117 100644 --- a/src-server/Server/Communication.hs +++ b/src-server/Server/Communication.hs @@ -60,24 +60,24 @@ bindSocket path = do -- | Function that installs a handler on SIGINT to terminate game terminateGameOnSigint - :: Game () -terminateGameOnSigint = do + :: FilePath + -> Game () +terminateGameOnSigint path = do sock <- asks rcMainSocket clients <- liftIO . STM.atomically . STM.readTMVar =<< gets scClientSockets serverState <- gets scServerState void $ liftIO $ installHandler - keyboardSignal + sigINT (CatchOnce $ do + putStrLn "SIGINT caught, terminating…" void $ STM.atomically $ STM.swapTMVar serverState (ServerState False True) disconnectClients clients - threadDelay (10 ^ 6) - (SockAddrUnix path) <- getSocketName sock close sock removeIfExists path -- Raise SIGINT again so it does not get blocked - raiseSignal keyboardSignal + raiseSignal sigINT ) - Nothing + (Just emptySignalSet) where disconnectClients = mapM_ (\(_, clientSocket) -> do