on 5054813 help sign in
streamly/examples/EchoServer.hs
19 lines 606 B history raw
1-- A concurrent TCP server that echoes everything that it receives.
3import Control.Exception (finally)
4import Control.Monad.IO.Class (liftIO)
5import Streamly
6import Streamly.Network.Socket
7import qualified Network.Socket as Net
8import qualified Streamly.Network.Inet.TCP as TCP
9import qualified Streamly.Prelude as S
11main :: IO ()
12main = S.drain
13 $ parallely $ S.mapM (useWith echo)
14 $ serially $ S.unfold TCP.acceptOnPort 8091
15 where
16 echo sk =
17 S.fold (writeChunks sk)
18 $ S.unfold readChunksWithBufferOf (32768, sk)
19 useWith f sk = finally (f sk) (liftIO (Net.close sk))