fix communication

This commit is contained in:
nek0 2023-12-11 07:07:09 +01:00
parent c68bb2a369
commit 46f41519fc
3 changed files with 46 additions and 40 deletions

View file

@ -1,14 +1,18 @@
{-# LANGUAGE LambdaCase #-} {-# LANGUAGE LambdaCase #-}
module Client.Communication where module Client.Communication where
import Control.Monad (void)
import qualified Data.Aeson as A import qualified Data.Aeson as A
import qualified Data.ByteString as B import qualified Data.ByteString as B
import qualified Data.ByteString.Lazy.Char8 as B8 import qualified Data.ByteString.Lazy.Char8 as B8
import Network.Socket import qualified Data.Vector.Storable as VS
import System.IO import Foreign hiding (void)
import Network.Socket
-- internal imports -- internal imports
@ -21,8 +25,6 @@ connectSocket path = do
sock <- socket AF_UNIX Stream defaultProtocol sock <- socket AF_UNIX Stream defaultProtocol
setSocketOption sock KeepAlive 1 setSocketOption sock KeepAlive 1
connect sock (SockAddrUnix path) connect sock (SockAddrUnix path)
-- handle <- socketToHandle sock ReadWriteMode
-- hSetBuffering handle (BlockBuffering Nothing)
pure sock pure sock
-- | Sends a specified message through given socket to the server -- | Sends a specified message through given socket to the server
@ -31,29 +33,24 @@ sendMessage
-> Socket -> Socket
-> IO () -> IO ()
sendMessage msg sock = do sendMessage msg sock = do
handle <- socketToHandle sock WriteMode
hSetBuffering handle (BlockBuffering Nothing)
let msgJson = A.encode msg let msgJson = A.encode msg
B.hPutStr handle $ B.toStrict msgJson msgVector = VS.fromList $ B.unpack $ B.toStrict msgJson
hFlush handle VS.unsafeWith
-- hClose handle msgVector
(\ptr -> void $ sendBuf sock ptr (VS.length msgVector))
receiveMessage receiveMessage
:: Socket :: Socket
-> IO ServerMessage -> IO ServerMessage
receiveMessage sock = do receiveMessage sock = do
handle <- socketToHandle sock ReadMode let maxBufferLength = 4096
hSetBuffering handle LineBuffering ptr <- mallocArray maxBufferLength
handleOpen <- hIsOpen handle bufferLength <- recvBuf sock ptr maxBufferLength
if handleOpen msg <- B.pack <$> peekArray bufferLength ptr
then do let mJsonMsg = A.decode' $ B8.fromStrict msg
message <- hGetContents handle jsonMsg <- maybe
let mJsonMessage = A.decode' $ B8.pack message :: Maybe ServerMessage (error $ "unexpected message from Server: " <> show msg)
errMsg = error $ "unexpected message from Server: " <> message pure
-- hClose handle mJsonMsg
maybe free ptr
errMsg pure jsonMsg
pure
mJsonMessage
else
error "Handle is closed"

View file

@ -14,9 +14,14 @@ import Control.Monad.RWS.Strict
import qualified Data.Aeson as A import qualified Data.Aeson as A
import qualified Data.ByteString as B import qualified Data.ByteString as B
import qualified Data.ByteString.Lazy.Char8 as B8
import Data.UUID.V4 import Data.UUID.V4
import qualified Data.Vector.Storable as VS
import Foreign hiding (void)
import Network.Socket as Net import Network.Socket as Net
import System.IO import System.IO
@ -82,12 +87,7 @@ processRequests = do
acceptConnection mainSocket socketList = do acceptConnection mainSocket socketList = do
putStrLn "accepting new connections…" putStrLn "accepting new connections…"
(clientSock, _) <- accept mainSocket (clientSock, _) <- accept mainSocket
-- clientHandle <- socketToHandle clientSock ReadWriteMode
-- hSetBuffering clientHandle (BlockBuffering Nothing)
putStrLn "accepted new connection" putStrLn "accepted new connection"
-- uuid <- nextRandom
-- putStrLn $ "accepting client with uuid " <> show uuid
-- sendMessage (AcceptClient uuid) clientHandle
liftIO $ STM.atomically $ do liftIO $ STM.atomically $ do
list <- STM.takeTMVar socketList list <- STM.takeTMVar socketList
STM.putTMVar socketList ((clientSock, Nothing) : list) STM.putTMVar socketList ((clientSock, Nothing) : list)
@ -99,12 +99,11 @@ sendMessage
-> Socket -> Socket
-> IO () -> IO ()
sendMessage msg sock = do sendMessage msg sock = do
handle <- socketToHandle sock WriteMode
hSetBuffering handle (BlockBuffering Nothing)
let msgJson = A.encode msg let msgJson = A.encode msg
B.hPutStr handle $ B.toStrict msgJson msgVector = VS.fromList $ B.unpack $ B.toStrict msgJson
hFlush handle VS.unsafeWith
-- hClose handle msgVector
(\ptr -> void $ sendBuf sock ptr (VS.length msgVector))
-- | process incoming messages from clients -- | process incoming messages from clients
processMessages :: Game () processMessages :: Game ()
@ -117,12 +116,20 @@ processMessages = do
) )
clients clients
-- | receive a 'ClientMessage'
receiveMessage receiveMessage
:: Socket :: Socket
-> IO () -> IO ()
receiveMessage clientSocket = do receiveMessage sock = do
clientHandle <- socketToHandle clientSocket ReadMode let maxBufferLength = 4096
hSetBuffering clientHandle (BlockBuffering Nothing) ptr <- mallocArray maxBufferLength
message <- hGetContents clientHandle bufferLength <- recvBuf sock ptr maxBufferLength
putStr message when (bufferLength > 0) $ do
-- hClose connectionHandle msg <- B.pack <$> peekArray bufferLength ptr
let mJsonMsg = A.decode' $ B8.fromStrict msg :: Maybe ClientMessages
jsonMsg <- maybe
(error $ "unexpected message from Server: " <> show msg)
pure
mJsonMsg
free ptr
print jsonMsg

View file

@ -44,6 +44,7 @@ executable wizard-wipeout-client
, network , network
, optparse-applicative , optparse-applicative
, uuid , uuid
, vector
, vty , vty
, wizard-wipeout , wizard-wipeout
hs-source-dirs: src-client hs-source-dirs: src-client
@ -74,6 +75,7 @@ executable wizard-wipeout-server
, stm , stm
, unix , unix
, uuid , uuid
, vector
, wizard-wipeout , wizard-wipeout
, yaml , yaml
hs-source-dirs: src-server hs-source-dirs: src-server