diff --git a/Makefile b/Makefile index 4055de9..08be133 100644 --- a/Makefile +++ b/Makefile @@ -22,6 +22,7 @@ base: prelude network: prelude make -C libs/network IDRIS2=../../idris2 + make -C libs/network test IDRIS2=../../idris2 libs : prelude base network diff --git a/libs/network/Echo.idr b/libs/network/Echo.idr index 4eeb883..c9a4965 100644 --- a/libs/network/Echo.idr +++ b/libs/network/Echo.idr @@ -9,70 +9,49 @@ import Network.Socket.Raw runServer : IO (Either String (Port, ThreadID)) runServer = do - osock <- socket AF_INET Stream 0 - case osock of - Left fail => pure (Left $ "Failed to open socket: " ++ show fail) - Right sock => do - res <- bind sock (Just (Hostname "localhost")) 0 - if res /= 0 - then pure (Left $ "Failed to bind socket with error: " ++ show res) - else do - port <- getSockPort sock - res <- listen sock - if res /= 0 - then pure (Left $ "Failed to listen on socket with error: " ++ show res) - else do - forked <- fork (serve port sock) - pure $ Right (port, forked) + Right sock <- socket AF_INET Stream 0 + | Left fail => pure (Left $ "Failed to open socket: " ++ show fail) + res <- bind sock (Just (Hostname "localhost")) 0 + if res /= 0 + then pure (Left $ "Failed to bind socket with error: " ++ show res) + else do + port <- getSockPort sock + res <- listen sock + if res /= 0 + then pure (Left $ "Failed to listen on socket with error: " ++ show res) + else do + forked <- fork (serve port sock) + pure $ Right (port, forked) where serve : Port -> Socket -> IO () serve port sock = do - res <- accept sock - case res of - Left err => do - putStrLn ("Failed to accept on socket with error: " ++ show err) - Right (s, _) => do - received <- recv s 1024 - case received of - Left err => do - putStrLn ("Failed to accept on socket with error: " ++ show err) - Right (str, _) => do - putStrLn ("Received: " ++ str) - sent <- send s ("echo: " ++ str) - case sent of - Left err => do - putStrLn ("Server failed to send data with error: " ++ show err) - Right n => - putStrLn ("Server sent " ++ show n ++ " bytes") + Right (s, _) <- accept sock + | Left err => putStrLn ("Failed to accept on socket with error: " ++ show err) + Right (str, _) <- recv s 1024 + | Left err => putStrLn ("Failed to accept on socket with error: " ++ show err) + putStrLn ("Received: " ++ str) + Right n <- send s ("echo: " ++ str) + | Left err => putStrLn ("Server failed to send data with error: " ++ show err) + putStrLn ("Server sent " ++ show n ++ " bytes") runClient : Port -> IO () runClient serverPort = do - osock <- socket AF_INET Stream 0 - case osock of - Left fail => putStrLn ("Failed to open socket: " ++ show fail) - Right sock => do - res <- connect sock (Hostname "localhost") serverPort - if res /= 0 - then putStrLn ("Failed to connect client to port " ++ show serverPort ++ ": " ++ show res) - else do - sent <- send sock ("hello world!") - case sent of - Left err => do - putStrLn ("Client failed to send data with error: " ++ show err) - Right n => do - putStrLn ("Client sent " ++ show n ++ " bytes") - received <- recv sock 1024 - case received of - Left err => do - putStrLn ("Client failed to receive on socket with error: " ++ show err) - Right (str, _) => do - putStrLn ("Received: " ++ str) + Right sock <- socket AF_INET Stream 0 + | Left fail => putStrLn ("Failed to open socket: " ++ show fail) + res <- connect sock (Hostname "localhost") serverPort + if res /= 0 + then putStrLn ("Failed to connect client to port " ++ show serverPort ++ ": " ++ show res) + else do + Right n <- send sock ("hello world!") + | Left err => putStrLn ("Client failed to send data with error: " ++ show err) + putStrLn ("Client sent " ++ show n ++ " bytes") + Right (str, _) <- recv sock 1024 + | Left err => putStrLn ("Client failed to receive on socket with error: " ++ show err) + putStrLn ("Received: " ++ str) main : IO () main = do - server <- runServer - case server of - Left err => putStrLn $ "[server] " ++ err - Right (serverPort, tid) => do - runClient serverPort + Right (serverPort, tid) <- runServer + | Left err => putStrLn $ "[server] " ++ err + runClient serverPort diff --git a/libs/network/Makefile b/libs/network/Makefile index ccd7cf4..80770b3 100644 --- a/libs/network/Makefile +++ b/libs/network/Makefile @@ -48,8 +48,8 @@ $(DYLIBTARGET) : $(OBJS) $(CC) -o $(DYLIBTARGET) $(LIBFLAGS) -shared $(OBJS) install: - install $(DYLIBTARGET) $(HDRS) $(TARGET)/idris2/network ${IDRIS2} --install network.ipkg + install $(DYLIBTARGET) $(HDRS) $(TARGET)/idris2/network clean : rm -rf $(OBJS) $(LIBTARGET) $(DYLIBTARGET) build