finally fixed communication
This commit is contained in:
parent
67b09aaa06
commit
07189c7e76
4 changed files with 28 additions and 22 deletions
|
@ -60,8 +60,7 @@ terminateGameOnSigint = do
|
||||||
sigINT
|
sigINT
|
||||||
(CatchOnce $ do
|
(CatchOnce $ do
|
||||||
putStrLn "SIGINT caught, terminating…"
|
putStrLn "SIGINT caught, terminating…"
|
||||||
queues <- liftIO $ STM.atomically $ STM.readTMVar queueList
|
disconnectClients clientList queueList
|
||||||
disconnectClients clientList queues
|
|
||||||
threadDelay (10 ^ 6)
|
threadDelay (10 ^ 6)
|
||||||
close sock
|
close sock
|
||||||
st <- STM.atomically $ STM.readTMVar serverState
|
st <- STM.atomically $ STM.readTMVar serverState
|
||||||
|
@ -74,17 +73,18 @@ terminateGameOnSigint = do
|
||||||
-- | Disconnect all connected clients gracefully by announcing the server quitting
|
-- | Disconnect all connected clients gracefully by announcing the server quitting
|
||||||
disconnectClients
|
disconnectClients
|
||||||
:: STM.TMVar [ClientSocket]
|
:: STM.TMVar [ClientSocket]
|
||||||
-> [ClientQueue]
|
-> STM.TMVar [ClientQueue]
|
||||||
-> IO ()
|
-> IO ()
|
||||||
disconnectClients clientSockets queues = do
|
disconnectClients clientSockets queueList = do
|
||||||
putStrLn "server shutting down. Notifying all clients…"
|
putStrLn "server shutting down. Notifying all clients…"
|
||||||
|
queues <- STM.atomically $ STM.readTMVar queueList
|
||||||
mapM_
|
mapM_
|
||||||
(\queue -> do
|
(\queue -> do
|
||||||
maybe
|
maybe
|
||||||
(pure ())
|
(pure ())
|
||||||
(\uuid -> do
|
(\uuid -> do
|
||||||
putStrLn $ "notifying client: " <> show uuid
|
putStrLn $ "notifying client: " <> show uuid
|
||||||
queueMessage ServerQuit uuid queues
|
queueMessage ServerQuit uuid queueList
|
||||||
sock <- do
|
sock <- do
|
||||||
socketList <- STM.atomically $ STM.readTMVar clientSockets
|
socketList <- STM.atomically $ STM.readTMVar clientSockets
|
||||||
pure $ fromJust $ find (\s -> csUUID s == Just uuid) socketList
|
pure $ fromJust $ find (\s -> csUUID s == Just uuid) socketList
|
||||||
|
@ -114,10 +114,14 @@ processRequests = do
|
||||||
Right (clientSock, _) -> do
|
Right (clientSock, _) -> do
|
||||||
clientQueue <- STM.newTQueueIO
|
clientQueue <- STM.newTQueueIO
|
||||||
sockContainer <- STM.newTMVarIO clientSock
|
sockContainer <- STM.newTMVarIO clientSock
|
||||||
receiverThreadId <- liftIO $ forkIO $ forever $ do
|
receiverThreadId <- liftIO $ do
|
||||||
receiveMessage sockContainer serverQueue
|
t <- forkIO $ forever $ receiveMessage sockContainer serverQueue
|
||||||
senderThreadId <- liftIO $ forkIO $ forever $ do
|
putStrLn "enabled listener thread"
|
||||||
sendMessageQueue sockContainer clientQueue
|
pure t
|
||||||
|
senderThreadId <- liftIO $ do
|
||||||
|
t <- forkIO $ forever $ sendMessageQueue sockContainer clientQueue
|
||||||
|
putStrLn "enabled sender thread"
|
||||||
|
pure t
|
||||||
print clientSock
|
print clientSock
|
||||||
liftIO $ STM.atomically $ do
|
liftIO $ STM.atomically $ do
|
||||||
slist <- STM.takeTMVar socketList
|
slist <- STM.takeTMVar socketList
|
||||||
|
|
|
@ -53,7 +53,7 @@ handleMessage stateContainer readerContainer msg = do
|
||||||
void $ STM.swapTMVar socketList newSocks
|
void $ STM.swapTMVar socketList newSocks
|
||||||
void $ STM.swapTMVar queueList newQueues
|
void $ STM.swapTMVar queueList newQueues
|
||||||
putStrLn $ "Accepted Client with UUID: " <> show clientId
|
putStrLn $ "Accepted Client with UUID: " <> show clientId
|
||||||
queueMessage (AcceptClient clientId) clientId newQueues
|
queueMessage (AcceptClient clientId) clientId queueList
|
||||||
else
|
else
|
||||||
putStrLn $ "Unable to assign ID to Socket and queue: " <> show clientId
|
putStrLn $ "Unable to assign ID to Socket and queue: " <> show clientId
|
||||||
ClientMessage clientId payload ->
|
ClientMessage clientId payload ->
|
||||||
|
@ -73,7 +73,7 @@ handleMessage stateContainer readerContainer msg = do
|
||||||
currentPlayers <- STM.readTMVar (scPlayers stateContainer)
|
currentPlayers <- STM.readTMVar (scPlayers stateContainer)
|
||||||
void $ STM.swapTMVar (scPlayers stateContainer) $
|
void $ STM.swapTMVar (scPlayers stateContainer) $
|
||||||
Player clientId freshWizard False (now, uuid) : currentPlayers
|
Player clientId freshWizard False (now, uuid) : currentPlayers
|
||||||
queueMessage (ProvideInitialWizard freshWizard) clientId queues
|
queueMessage (ProvideInitialWizard freshWizard) clientId queueList
|
||||||
ClientReady -> do
|
ClientReady -> do
|
||||||
now <- getCurrentTime
|
now <- getCurrentTime
|
||||||
STM.atomically $ do
|
STM.atomically $ do
|
||||||
|
|
|
@ -33,11 +33,11 @@ receiveMessage
|
||||||
-> STM.TQueue ClientMessage
|
-> STM.TQueue ClientMessage
|
||||||
-> IO ()
|
-> IO ()
|
||||||
receiveMessage sockContainer queue = do
|
receiveMessage sockContainer queue = do
|
||||||
randSleep <- randomRIO (1, 1000)
|
-- randSleep <- randomRIO (1, 1000)
|
||||||
threadDelay randSleep
|
-- threadDelay randSleep
|
||||||
sock <- STM.atomically $ STM.takeTMVar sockContainer
|
sock <- STM.atomically $ STM.readTMVar sockContainer
|
||||||
let maxBufferLength = 4096
|
let maxBufferLength = 4096
|
||||||
putStrLn "took socket container for receiving"
|
putStrLn "read socket container for receiving"
|
||||||
mMsg <- do
|
mMsg <- do
|
||||||
ptr <- mallocArray maxBufferLength
|
ptr <- mallocArray maxBufferLength
|
||||||
putStrLn "receiving data"
|
putStrLn "receiving data"
|
||||||
|
|
|
@ -28,7 +28,7 @@ import qualified Data.Vector.Storable as VS
|
||||||
|
|
||||||
import Data.List
|
import Data.List
|
||||||
|
|
||||||
import Data.UUID
|
import Data.UUID hiding (null)
|
||||||
import Data.UUID.V4
|
import Data.UUID.V4
|
||||||
|
|
||||||
import Linear
|
import Linear
|
||||||
|
@ -47,9 +47,10 @@ import Server.Types
|
||||||
queueMessage
|
queueMessage
|
||||||
:: ServerMessage
|
:: ServerMessage
|
||||||
-> UUID
|
-> UUID
|
||||||
-> [ClientQueue]
|
-> STM.TMVar [ClientQueue]
|
||||||
-> IO ()
|
-> IO ()
|
||||||
queueMessage msg uuid queueList = do
|
queueMessage msg uuid queueListContainer = do
|
||||||
|
queueList <- STM.atomically $ STM.readTMVar queueListContainer
|
||||||
putStrLn $ "queueing message \"" <> show msg <> "\" for client " <> show uuid <> " …"
|
putStrLn $ "queueing message \"" <> show msg <> "\" for client " <> show uuid <> " …"
|
||||||
let mQueue = cqQueue <$> find (\client -> cqUUID client == Just uuid) queueList
|
let mQueue = cqQueue <$> find (\client -> cqUUID client == Just uuid) queueList
|
||||||
maybe
|
maybe
|
||||||
|
@ -65,9 +66,10 @@ sendMessageQueue
|
||||||
-> STM.TQueue ServerMessage
|
-> STM.TQueue ServerMessage
|
||||||
-> IO ()
|
-> IO ()
|
||||||
sendMessageQueue sockContainer queue = do
|
sendMessageQueue sockContainer queue = do
|
||||||
randTime <- randomRIO (1, 1000)
|
-- randTime <- randomRIO (1, 1000)
|
||||||
threadDelay randTime
|
-- threadDelay randTime
|
||||||
msgs <- STM.atomically $ STM.flushTQueue queue
|
msgs <- STM.atomically $ STM.flushTQueue queue
|
||||||
|
unless (null msgs) $ putStrLn $ "messages in queue: " <> show msgs
|
||||||
mapM_
|
mapM_
|
||||||
(\msg -> do
|
(\msg -> do
|
||||||
sock <- STM.atomically $ STM.readTMVar sockContainer
|
sock <- STM.atomically $ STM.readTMVar sockContainer
|
||||||
|
@ -113,7 +115,7 @@ sendPings = do
|
||||||
random <- liftIO nextRandom
|
random <- liftIO nextRandom
|
||||||
let newPong = (now, random)
|
let newPong = (now, random)
|
||||||
liftIO $ do
|
liftIO $ do
|
||||||
queues <- STM.atomically $ STM.readTMVar (scClientQueues stateContainer)
|
let queues = scClientQueues stateContainer
|
||||||
queueMessage
|
queueMessage
|
||||||
( Ping random
|
( Ping random
|
||||||
)
|
)
|
||||||
|
@ -193,5 +195,5 @@ sendUpdate stateContainer tileMap player = do
|
||||||
let msg = TickUpdate slice wizard
|
let msg = TickUpdate slice wizard
|
||||||
-- print slice
|
-- print slice
|
||||||
liftIO $ do
|
liftIO $ do
|
||||||
queues <- STM.atomically $ STM.readTMVar $ scClientQueues stateContainer
|
let queues = scClientQueues stateContainer
|
||||||
queueMessage msg playerId queues
|
queueMessage msg playerId queues
|
||||||
|
|
Loading…
Reference in a new issue