wizard-wipeout/src-server/Server/Communication/Receive.hs

77 lines
2.2 KiB
Haskell
Raw Normal View History

2024-11-03 03:40:46 +00:00
{-# LANGUAGE OverloadedStrings #-}
2024-05-01 21:52:49 +00:00
module Server.Communication.Receive where
import Control.Concurrent (threadDelay)
2024-05-01 21:52:49 +00:00
import qualified Control.Concurrent.STM as STM
import Control.Exception
import Control.Monad.IO.Class
import qualified Data.Aeson as A
import qualified Data.ByteString as B
import qualified Data.ByteString.Lazy.Char8 as B8
2024-10-31 18:19:13 +00:00
import Foreign.Marshal hiding (void)
2024-05-01 21:52:49 +00:00
import Network.Socket
import System.Random
2024-10-31 18:19:13 +00:00
-- internal imports
2024-05-01 21:52:49 +00:00
import Library.Types
import Server.Communication.Send
2024-11-04 05:53:58 +00:00
import Server.Log (logPrintIO)
2024-05-01 21:52:49 +00:00
import Server.Types
-- | receive a 'ClientMessage'
receiveMessage
2024-11-04 05:53:58 +00:00
:: LogLevel
-> STM.TMVar [ClientSocket]
-> STM.TMVar [ClientQueue]
2024-11-04 06:00:38 +00:00
-> STM.TMVar Socket
2024-05-01 21:52:49 +00:00
-> STM.TQueue ClientMessage
-> IO ()
2024-11-04 06:00:38 +00:00
receiveMessage curLevel socketList queueList sockContainer queue = do
2024-11-04 05:53:58 +00:00
randSleep <- randomRIO (1, 1000)
threadDelay randSleep
2024-05-01 21:52:49 +00:00
let maxBufferLength = 4096
2024-11-04 06:00:38 +00:00
sock <- STM.atomically $ STM.readTMVar sockContainer
2024-11-04 05:53:58 +00:00
logPrintIO curLevel Verbose "read socket container for receiving"
2024-11-03 15:21:03 +00:00
ptr <- mallocArray maxBufferLength
2024-11-04 05:53:58 +00:00
logPrintIO curLevel Verbose "receiving data"
2024-11-03 15:21:03 +00:00
eBufferLength <-
try $ recvBuf sock ptr maxBufferLength
2024-11-04 05:53:58 +00:00
logPrintIO curLevel Verbose $ "received raw buffer length of: " <> show eBufferLength
2024-11-03 15:21:03 +00:00
bufferLength <- case eBufferLength of
Left (e :: IOException) -> do
2024-11-04 05:53:58 +00:00
logPrintIO curLevel Warning ("Socket vanished, cleaning up after " <> show e)
dropClient curLevel socketList queueList sock
2024-11-03 15:21:03 +00:00
pure 0
Right len -> pure len
2024-11-04 05:53:58 +00:00
logPrintIO curLevel Verbose $ "received buffer of length: " <> show bufferLength
2024-11-03 15:21:03 +00:00
rawMsg <- B.pack <$> peekArray bufferLength ptr
free ptr
2024-11-04 05:53:58 +00:00
logPrintIO curLevel Verbose $ "received data: " <> show rawMsg
2024-11-03 15:21:03 +00:00
let msgs =
if B.length rawMsg < 1
then [] :: [B8.ByteString]
else map B8.tail $ init $ B8.split '>' $ B8.fromStrict rawMsg
2024-11-04 05:53:58 +00:00
logPrintIO curLevel Verbose $ "received messages: " <> show msgs
2024-11-04 06:02:52 +00:00
logPrintIO curLevel Verbose $ show msgs
2024-11-03 15:21:03 +00:00
mapM_
2024-11-03 03:40:46 +00:00
(\msg -> do
2024-11-03 15:21:03 +00:00
let mJsonMsg = A.decode' msg
maybe
2024-11-04 05:53:58 +00:00
(logPrintIO curLevel Warning $ "received garbled data: " <> B8.unpack (B8.fromStrict rawMsg))
2024-11-03 15:21:03 +00:00
(\jsonMsg -> do
STM.atomically $ STM.writeTQueue queue jsonMsg
)
mJsonMsg
2024-05-01 21:52:49 +00:00
)
2024-11-03 15:21:03 +00:00
msgs