notify server of quitting client
This commit is contained in:
parent
13a4022547
commit
eda823bb17
5 changed files with 30 additions and 6 deletions
|
@ -2,4 +2,4 @@ setSocketPath : "/tmp/wizard.sock"
|
||||||
setMapRows : 20
|
setMapRows : 20
|
||||||
setMapColumns : 20
|
setMapColumns : 20
|
||||||
setSpawnerProbability : 0.01
|
setSpawnerProbability : 0.01
|
||||||
setFPS : 30
|
setFPS : 5
|
||||||
|
|
|
@ -5,6 +5,8 @@ import qualified Control.Concurrent.STM as STM
|
||||||
|
|
||||||
import Control.Monad (void)
|
import Control.Monad (void)
|
||||||
|
|
||||||
|
import Control.Monad.RWS
|
||||||
|
|
||||||
import qualified Data.Aeson as A
|
import qualified Data.Aeson as A
|
||||||
|
|
||||||
import qualified Data.ByteString as B
|
import qualified Data.ByteString as B
|
||||||
|
@ -18,9 +20,12 @@ import Foreign hiding (void)
|
||||||
|
|
||||||
import Network.Socket
|
import Network.Socket
|
||||||
|
|
||||||
|
import System.Posix.Signals
|
||||||
|
|
||||||
-- internal imports
|
-- internal imports
|
||||||
|
|
||||||
import Library.Types
|
import Library.Types
|
||||||
|
import Client.Types
|
||||||
|
|
||||||
connectSocket
|
connectSocket
|
||||||
:: FilePath
|
:: FilePath
|
||||||
|
@ -57,3 +62,22 @@ receiveMessage sock queue = do
|
||||||
(putStrLn $ "received garbled data: " <> B8.unpack (B8.fromStrict msg))
|
(putStrLn $ "received garbled data: " <> B8.unpack (B8.fromStrict msg))
|
||||||
(STM.atomically . STM.writeTQueue queue)
|
(STM.atomically . STM.writeTQueue queue)
|
||||||
mJsonMsg
|
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
|
||||||
|
|
|
@ -24,12 +24,13 @@ options = Options
|
||||||
)
|
)
|
||||||
|
|
||||||
data ReaderContainer = ReaderContainer
|
data ReaderContainer = ReaderContainer
|
||||||
{ rcSocketHandle :: Socket
|
{ rcSocket :: Socket
|
||||||
, rcClientUUID :: UUID
|
, rcClientUUID :: UUID
|
||||||
}
|
}
|
||||||
|
|
||||||
newtype StateContainer = StateContainer
|
newtype StateContainer = StateContainer
|
||||||
{ scWizard :: Wizard
|
{ scWizard :: Wizard
|
||||||
|
, scClientState :: ClientState
|
||||||
}
|
}
|
||||||
|
|
||||||
type Game = RWST ReaderContainer String StateContainer IO
|
type Game = RWST ReaderContainer String StateContainer IO
|
||||||
|
|
|
@ -5,8 +5,6 @@ import Control.Concurrent (threadDelay)
|
||||||
|
|
||||||
import qualified Control.Concurrent.STM as STM
|
import qualified Control.Concurrent.STM as STM
|
||||||
|
|
||||||
import Control.Monad.Loops (whileM_)
|
|
||||||
|
|
||||||
import Options.Applicative
|
import Options.Applicative
|
||||||
|
|
||||||
-- internal imports
|
-- internal imports
|
||||||
|
|
|
@ -46,6 +46,7 @@ executable wizard-wipeout-client
|
||||||
, network
|
, network
|
||||||
, optparse-applicative
|
, optparse-applicative
|
||||||
, stm
|
, stm
|
||||||
|
, unix
|
||||||
, uuid
|
, uuid
|
||||||
, vector
|
, vector
|
||||||
, vty
|
, vty
|
||||||
|
|
Loading…
Reference in a new issue