some little optimizations

This commit is contained in:
nek0 2023-12-12 03:31:57 +01:00
parent 9691a0412a
commit 087ae2d072
2 changed files with 20 additions and 14 deletions

View file

@ -22,7 +22,6 @@ import qualified Data.Matrix as M
import Data.Maybe import Data.Maybe
import Data.UUID
import Data.UUID.V4 import Data.UUID.V4
import qualified Data.Vector.Storable as VS import qualified Data.Vector.Storable as VS
@ -115,24 +114,30 @@ sendMessage msg sock = do
msgVector msgVector
(\ptr -> void $ sendBuf sock ptr (VS.length msgVector)) (\ptr -> void $ sendBuf sock ptr (VS.length msgVector))
-- | process incoming messages from clients -- | receive incoming messages from clients
processMessages :: Game () receiveMessages :: Game ()
processMessages = do receiveMessages = do
clientsVar <- gets scClientSockets clientsVar <- gets scClientSockets
clients <- liftIO $ STM.atomically $ STM.readTMVar clientsVar clients <- liftIO $ STM.atomically $ STM.readTMVar clientsVar
queue <- gets scMessageQueue queue <- gets scMessageQueue
serverState <- get void $ liftIO $ forkIO $ mapM_
readerContainer <- ask (\(_, clientSocket) -> do
mapM_
(\(uuid, clientSocket) -> void $ liftIO $ forkIO $ do
receiveMessage clientSocket queue receiveMessage clientSocket queue
msgs <- liftIO $ STM.atomically $ STM.flushTQueue queue
mapM_
(handleMessage serverState readerContainer)
msgs
) )
clients clients
-- | handle received messages
handleMessages :: Game ()
handleMessages = do
queue <- gets scMessageQueue
serverState <- get
readerContainer <- ask
msgs <- liftIO $ STM.atomically $ STM.flushTQueue queue
void $ liftIO $ do
mapM_
(handleMessage serverState readerContainer)
msgs
-- | receive a 'ClientMessage' -- | receive a 'ClientMessage'
receiveMessage receiveMessage
:: Socket :: Socket

View file

@ -1,7 +1,7 @@
{-# LANGUAGE OverloadedStrings #-} {-# LANGUAGE OverloadedStrings #-}
module Server.Game where module Server.Game where
import Control.Concurrent (threadDelay, killThread) import Control.Concurrent (threadDelay)
import qualified Control.Concurrent.STM as STM import qualified Control.Concurrent.STM as STM
@ -34,7 +34,8 @@ runGame = do
now <- liftIO getCurrentTime now <- liftIO getCurrentTime
let delta = realToFrac $ diffUTCTime now before let delta = realToFrac $ diffUTCTime now before
liftIO $ B8.hPutStr stdout $ "tick: " <> fromString (show delta) <> " \r" liftIO $ B8.hPutStr stdout $ "tick: " <> fromString (show delta) <> " \r"
processMessages receiveMessages
handleMessages
updateSpawners delta updateSpawners delta
updateWizards delta updateWizards delta
modify' modify'