-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
10075e8
commit cf858ed
Showing
2 changed files
with
91 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
{-# LANGUAGE CPP #-} | ||
module Main (main) where | ||
|
||
import Data.Function ((&)) | ||
import Data.Word (Word8) | ||
import Network.Socket (PortNumber) | ||
#if ! MIN_VERSION_streamly(0,11,0) | ||
import Network.Socket (Socket) | ||
#endif | ||
import Streamly.Data.Stream.Prelude (Stream) | ||
|
||
import qualified Streamly.Console.Stdio as Stdio | ||
import qualified Streamly.Data.Array as Array | ||
import qualified Streamly.Data.Fold as Fold | ||
import qualified Streamly.Data.Stream.Prelude as Stream | ||
#if MIN_VERSION_streamly(0,11,0) | ||
import qualified Streamly.Internal.Network.Inet.TCP as TCP | ||
#else | ||
import qualified Streamly.Network.Inet.TCP as TCP | ||
#endif | ||
import qualified Streamly.Unicode.Stream as Unicode | ||
#if ! MIN_VERSION_streamly(0,11,0) | ||
import qualified Streamly.Internal.Network.Socket as Socket | ||
#endif | ||
|
||
|
||
remoteAddr :: (Word8,Word8,Word8,Word8) | ||
remoteAddr = (127, 0, 0, 1) | ||
|
||
remotePort :: PortNumber | ||
remotePort = 8091 | ||
|
||
#if MIN_VERSION_streamly(0,11,0) | ||
|
||
-- | ||
-- The following implementation requires the pipeChunks transformation function, | ||
-- which can make the code more concise and easier to follow. | ||
-- | ||
|
||
echo :: Stream IO () | ||
echo = | ||
Stream.unfold Stdio.reader () -- Stream IO Word8 | ||
& split (== 10) Array.write -- Stream IO (Array Word8) | ||
& Stream.trace print | ||
& TCP.pipeChunks remoteAddr remotePort -- Stream IO (Array Word8) | ||
& Stream.unfoldMany Array.reader -- Stream IO Word8 | ||
& Unicode.decodeLatin1 -- Stream IO Char | ||
& Stream.mapM putChar -- Stream IO () | ||
|
||
where | ||
|
||
split p f = Stream.foldMany (Fold.takeEndBy p f) | ||
|
||
main :: IO () | ||
main = Stream.fold Fold.drain echo | ||
|
||
#else | ||
|
||
-- | ||
-- This implementation does not use the pipeChunks transformation function. | ||
-- | ||
|
||
echo :: Socket -> Stream IO () | ||
echo sk = do | ||
Stream.unfold Stdio.reader () -- Stream IO Word8 | ||
& split (== 10) Array.write -- Stream IO (Array Word8) | ||
& Stream.concatMapM sendRecv -- Stream IO (Array Word8) | ||
& Stream.unfoldMany Array.reader -- Stream IO Word8 | ||
& Unicode.decodeLatin1 -- Stream IO Char | ||
& Stream.mapM putChar -- Stream IO () | ||
|
||
where | ||
|
||
split p f = Stream.foldMany (Fold.takeEndBy p f) | ||
sendRecv chunk = do | ||
Socket.putChunks sk (Stream.fromPure chunk) | ||
return $ Stream.take 1 $ Socket.readChunks sk | ||
|
||
main :: IO () | ||
main = do | ||
sk <- TCP.connect remoteAddr remotePort | ||
Stream.fold Fold.drain $ echo sk | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters