@@ -1,13 +1,16 @@
11-- A concurrent TCP server that echoes everything that it receives.
22
3+import Control.Exception (finally)
4+import Control.Monad.IO.Class (liftIO)
35import Streamly
4−import Streamly.Internal.Network.Socket (useSocketM, readArrays)
56import Streamly.Network.Socket
6−import Streamly.Network.Server
7+import qualified Network.Socket as Net
8+import qualified Streamly.Network.Socket.Inet.TCP as TCP
79import qualified Streamly.Prelude as S
810
911main :: IO ()
1012main = S.drain
11− $ parallely $ S.mapM (flip useSocketM echo)
12− $ serially $ S.unfold listenOnPort 8090
13− where echo sk = S.fold (writeArrays sk) $ S.unfold readArrays sk
13+ $ parallely $ S.mapM (useWith echo)
14+ $ serially $ S.unfold TCP.listenOnPort 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)