notify server of quitting client

This commit is contained in:
nek0 2023-12-12 09:47:50 +01:00
parent 13a4022547
commit eda823bb17
5 changed files with 30 additions and 6 deletions

View file

@ -2,4 +2,4 @@ setSocketPath : "/tmp/wizard.sock"
setMapRows : 20
setMapColumns : 20
setSpawnerProbability : 0.01
setFPS : 30
setFPS : 5

View file

@ -5,6 +5,8 @@ import qualified Control.Concurrent.STM as STM
import Control.Monad (void)
import Control.Monad.RWS
import qualified Data.Aeson as A
import qualified Data.ByteString as B
@ -18,9 +20,12 @@ import Foreign hiding (void)
import Network.Socket
import System.Posix.Signals
-- internal imports
import Library.Types
import Client.Types
connectSocket
:: FilePath
@ -57,3 +62,22 @@ receiveMessage sock queue = do
(putStrLn $ "received garbled data: " <> B8.unpack (B8.fromStrict msg))
(STM.atomically . STM.writeTQueue queue)
mJsonMsg
-- | Function that installs a handler on SIGINT to terminate game
terminateGameOnSigint
:: Game ()
terminateGameOnSigint = do
sock <- asks rcSocket
clientId <- asks rcClientUUID
clientState <- gets scClientState
void $ liftIO $ installHandler
keyboardSignal
(CatchOnce $ do
void $ STM.atomically $ STM.swapTMVar serverState (clientState { clientStop = True })
threadDelay (10 ^ 6)
sendMessage sock (ClientMessage clientId ClientQuit)
close sock
-- Raise SIGINT again so it does not get blocked
raiseSignal keyboardSignal
)
Nothing

View file

@ -24,12 +24,13 @@ options = Options
)
data ReaderContainer = ReaderContainer
{ rcSocketHandle :: Socket
, rcClientUUID :: UUID
{ rcSocket :: Socket
, rcClientUUID :: UUID
}
newtype StateContainer = StateContainer
{ scWizard :: Wizard
{ scWizard :: Wizard
, scClientState :: ClientState
}
type Game = RWST ReaderContainer String StateContainer IO

View file

@ -5,8 +5,6 @@ import Control.Concurrent (threadDelay)
import qualified Control.Concurrent.STM as STM
import Control.Monad.Loops (whileM_)
import Options.Applicative
-- internal imports

View file

@ -46,6 +46,7 @@ executable wizard-wipeout-client
, network
, optparse-applicative
, stm
, unix
, uuid
, vector
, vty