From 8dd03d25586158d70057751d7c83396568c58ce9 Mon Sep 17 00:00:00 2001 From: nek0 Date: Sun, 3 Nov 2024 16:21:03 +0100 Subject: [PATCH] fixed sommunication issues --- src-client/Client/Communication.hs | 5 +- src-server/Server/Communication/Handler.hs | 3 ++ src-server/Server/Communication/Receive.hs | 58 +++++++++++----------- 3 files changed, 35 insertions(+), 31 deletions(-) diff --git a/src-client/Client/Communication.hs b/src-client/Client/Communication.hs index 6e01d20..fbfc374 100644 --- a/src-client/Client/Communication.hs +++ b/src-client/Client/Communication.hs @@ -1,5 +1,4 @@ -{-# LANGUAGE LambdaCase #-} -{-# LANGUAGE RecordWildCards #-} +{-# LANGUAGE OverloadedStrings #-} {-# LANGUAGE BangPatterns #-} module Client.Communication where @@ -48,7 +47,7 @@ sendMessage -> IO () sendMessage msg sock = do putStrLn $ "sending message: " <> show msg - let msgJson = A.encode msg + let msgJson = ('<' `B8.cons` A.encode msg) `B8.snoc` '>' msgList = B.unpack $ B.toStrict msgJson putStrLn $ "sending raw message buffer: " <> show msgList ptr <- newArray msgList diff --git a/src-server/Server/Communication/Handler.hs b/src-server/Server/Communication/Handler.hs index e403ae3..34eb91b 100644 --- a/src-server/Server/Communication/Handler.hs +++ b/src-server/Server/Communication/Handler.hs @@ -63,6 +63,9 @@ handleMessage stateContainer readerContainer msg = do case payload of ClientQuit -> do putStrLn $ "client has quit the game: " <> show clientId + currentPlayers <- STM.atomically $ STM.readTMVar (scPlayers stateContainer) + let newPlayers = filter (\p -> playerId p /= clientId) currentPlayers + void $ STM.atomically $ STM.swapTMVar (scPlayers stateContainer) newPlayers let client = find (\a -> csUUID a == Just clientId) socks dropClient socketList queueList (csSocket $ fromJust client) ClientRequestWizard -> do diff --git a/src-server/Server/Communication/Receive.hs b/src-server/Server/Communication/Receive.hs index 4187668..db0f33f 100644 --- a/src-server/Server/Communication/Receive.hs +++ b/src-server/Server/Communication/Receive.hs @@ -38,33 +38,35 @@ receiveMessage sockContainer queue = do sock <- STM.atomically $ STM.readTMVar sockContainer let maxBufferLength = 4096 putStrLn "read socket container for receiving" - mMsg <- do - ptr <- mallocArray maxBufferLength - putStrLn "receiving data" - eBufferLength <- - try $ recvBuf sock ptr maxBufferLength - putStrLn $ "received raw buffer length of: " <> show eBufferLength - free ptr - bufferLength <- case eBufferLength of - Left (e :: IOException) -> do - -- putStrLn ("Socket vanished, cleaning up after " <> show e) - -- dropClient clientList sock - pure 0 - Right len -> pure len - putStrLn $ "received buffer of length: " <> show bufferLength - msg <- B.pack <$> peekArray bufferLength ptr - putStrLn $ "received data: " <> show msg - if bufferLength > 0 && msg /= "" - then do - putStrLn $ "received message: " <> show msg - pure (A.decode' $ B8.fromStrict msg :: Maybe ClientMessage) - else - pure Nothing - maybe - (pure ()) + ptr <- mallocArray maxBufferLength + putStrLn "receiving data" + eBufferLength <- + try $ recvBuf sock ptr maxBufferLength + putStrLn $ "received raw buffer length of: " <> show eBufferLength + bufferLength <- case eBufferLength of + Left (e :: IOException) -> do + -- putStrLn ("Socket vanished, cleaning up after " <> show e) + -- dropClient clientList sock + pure 0 + Right len -> pure len + putStrLn $ "received buffer of length: " <> show bufferLength + rawMsg <- B.pack <$> peekArray bufferLength ptr + free ptr + putStrLn $ "received data: " <> show rawMsg + let msgs = + if B.length rawMsg < 1 + then [] :: [B8.ByteString] + else map B8.tail $ init $ B8.split '>' $ B8.fromStrict rawMsg + putStrLn $ "received messages: " <> show msgs + print msgs + mapM_ (\msg -> do - print msg - liftIO $ STM.atomically $ STM.writeTQueue queue msg - -- when (msg == IdRequest) (threadDelay $ 10 ^ 3) + let mJsonMsg = A.decode' msg + maybe + (putStrLn $ "received garbled data: " <> B8.unpack (B8.fromStrict rawMsg)) + (\jsonMsg -> do + STM.atomically $ STM.writeTQueue queue jsonMsg + ) + mJsonMsg ) - mMsg + msgs