{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# OPTIONS_GHC -Wall #-}
module Box.TCP.Example where
import Box
import Box.Socket.Types
import Box.TCP
import Control.Concurrent.Async
import Data.ByteString
import Data.Profunctor
import Data.Text
import Data.Text.Encoding
senderExample :: [ByteString] -> IO [ByteString]
senderExample :: [ByteString] -> IO [ByteString]
senderExample [ByteString]
ts = do
(Committer IO ByteString
c, IO [ByteString]
r) <- forall a. IO (Committer IO a, IO [a])
refCommitter
Async ()
a <- forall a. IO a -> IO (Async a)
async (TCPConfig -> PostSend -> Box IO ByteString ByteString -> IO ()
serverBox TCPConfig
defaultTCPConfig (Double -> PostSend
CloseAfter Double
0.2) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) c e. Committer m c -> Emitter m e -> Box m c e
Box forall a. Monoid a => a
mempty forall {k} a (m :: k -> *) (r :: k).
(a -> m r) -> Codensity m a -> m r
<$|> forall a. [a] -> CoEmitter IO a
qList [ByteString]
ts)
Double -> IO ()
sleep Double
0.2
TCPConfig -> PostSend -> Box IO ByteString ByteString -> IO ()
clientBox TCPConfig
defaultTCPConfig (Double -> PostSend
CloseAfter Double
0.5) (forall (m :: * -> *) c e. Committer m c -> Emitter m e -> Box m c e
Box Committer IO ByteString
c forall a. Monoid a => a
mempty)
Double -> IO ()
sleep Double
0.6
forall a. Async a -> IO ()
cancel Async ()
a
IO [ByteString]
r
senderLinesExample :: [Text] -> IO [Text]
senderLinesExample :: [Text] -> IO [Text]
senderLinesExample [Text]
ts = do
(Committer IO Text
c, IO [Text]
r) <- forall a. IO (Committer IO a, IO [a])
refCommitter
Async ()
a <- forall a. IO a -> IO (Async a)
async (TCPConfig -> PostSend -> Box IO ByteString ByteString -> IO ()
serverBox TCPConfig
defaultTCPConfig (Double -> PostSend
CloseAfter Double
0.2) forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Box IO Text Text -> Box IO ByteString ByteString
fromLineBox Text
"\n" forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) c e. Committer m c -> Emitter m e -> Box m c e
Box forall a. Monoid a => a
mempty forall {k} a (m :: k -> *) (r :: k).
(a -> m r) -> Codensity m a -> m r
<$|> forall a. [a] -> CoEmitter IO a
qList [Text]
ts)
Double -> IO ()
sleep Double
0.2
TCPConfig -> PostSend -> Box IO ByteString ByteString -> IO ()
clientBox TCPConfig
defaultTCPConfig (Double -> PostSend
CloseAfter Double
0.5) (Text -> Box IO Text Text -> Box IO ByteString ByteString
fromLineBox Text
"\n" forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) c e. Committer m c -> Emitter m e -> Box m c e
Box Committer IO Text
c forall a. Monoid a => a
mempty)
Double -> IO ()
sleep Double
0.6
forall a. Async a -> IO ()
cancel Async ()
a
IO [Text]
r
echoExample :: [ByteString] -> IO [ByteString]
echoExample :: [ByteString] -> IO [ByteString]
echoExample [ByteString]
ts = do
(Committer IO ByteString
c, IO [ByteString]
r) <- forall a. IO (Committer IO a, IO [a])
refCommitter
Async ()
a <-
forall a. IO a -> IO (Async a)
async
(TCPConfig -> (ByteString -> Maybe ByteString) -> IO ()
responseServer TCPConfig
defaultTCPConfig (forall (f :: * -> *) a. Applicative f => a -> f a
pure forall b c a. (b -> c) -> (a -> b) -> a -> c
. (ByteString
"echo: " <>)))
Double -> IO ()
sleep Double
0.1
TCPConfig -> PostSend -> Box IO ByteString ByteString -> IO ()
clientBox TCPConfig
defaultTCPConfig (Double -> PostSend
CloseAfter Double
0.2) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) c e. Committer m c -> Emitter m e -> Box m c e
Box Committer IO ByteString
c forall {k} a (m :: k -> *) (r :: k).
(a -> m r) -> Codensity m a -> m r
<$|> forall a. [a] -> CoEmitter IO a
qList [ByteString]
ts
Double -> IO ()
sleep Double
0.1
forall a. Async a -> IO ()
cancel Async ()
a
IO [ByteString]
r
clientIO :: IO ()
clientIO :: IO ()
clientIO =
TCPConfig -> PostSend -> Box IO ByteString ByteString -> IO ()
clientBox TCPConfig
defaultTCPConfig (Double -> PostSend
CloseAfter Double
0) (forall (p :: * -> * -> *) a b c d.
Profunctor p =>
(a -> b) -> (c -> d) -> p b c -> p a d
dimap ByteString -> Text
decodeUtf8 Text -> ByteString
encodeUtf8 (Text -> Box IO Text Text
stdBox Text
"q"))
serverIO :: IO ()
serverIO :: IO ()
serverIO = TCPConfig -> PostSend -> Box IO ByteString ByteString -> IO ()
serverBox TCPConfig
defaultTCPConfig (Double -> PostSend
CloseAfter Double
0) (forall (p :: * -> * -> *) a b c d.
Profunctor p =>
(a -> b) -> (c -> d) -> p b c -> p a d
dimap ByteString -> Text
decodeUtf8 Text -> ByteString
encodeUtf8 (Text -> Box IO Text Text
stdBox Text
"q"))