on 44e3cb3 help sign in
streamly/examples/EchoServer.hs
16 lines 580 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 8090
15 where echo sk = S.fold (writeArrays sk) $ S.unfold readArraysOf (32768, sk)
16 useWith f sk = finally (liftIO (Net.close sk)) (f sk)