on master help sign in
Add code example in the TCP module
a96d164Harendra Kumar
src/Streamly/Network/Inet/TCP.hs+28
@@ -11,6 +11,34 @@
11--
11--
12-- >>> import qualified Streamly.Network.Inet.TCP as TCP
12-- >>> import qualified Streamly.Network.Inet.TCP as TCP
13--
13--
14+-- = Examples
15+--
16+-- Following is a short example of a concurrent echo server.
17+--
18+-- >>> import Control.Monad.Catch (finally)
19+-- >>> import Data.Function ((&))
20+-- >>> import Network.Socket (Socket)
21+-- >>>
22+-- >>> import qualified Network.Socket as Net
23+-- >>> import qualified Streamly.Data.Fold as Fold
24+-- >>> import qualified Streamly.Data.Stream.Prelude as Stream
25+-- >>> import qualified Streamly.Network.Inet.TCP as TCP
26+-- >>> import qualified Streamly.Network.Socket as Socket
27+-- >>>
28+-- >>> :{
29+-- main :: IO ()
30+-- main =
31+-- TCP.accept 8091 -- Stream IO Socket
32+-- & Stream.parMapM id (handleExceptions echo) -- Stream IO ()
33+-- & Stream.fold Fold.drain -- IO ()
34+-- where
35+-- echo :: Socket -> IO ()
36+-- echo sk =
37+-- Socket.readChunksWith 32768 sk -- Stream IO (Array Word8)
38+-- & Stream.fold (Socket.writeChunks sk) -- IO ()
39+-- handleExceptions :: (Socket -> IO ()) -> Socket -> IO ()
40+-- handleExceptions f sk = finally (f sk) (Net.close sk)
41+-- :}
14
42
15module Streamly.Network.Inet.TCP
43module Streamly.Network.Inet.TCP
16 (
44 (
src/Streamly/Network/Socket.hs+3−4
@@ -10,13 +10,13 @@
10-- This module provides socket based streaming APIs to to receive connections
10-- This module provides socket based streaming APIs to to receive connections
11-- from remote hosts, and to read and write from and to network sockets.
11-- from remote hosts, and to read and write from and to network sockets.
12--
12--
13−-- For basic socket types and operations please consult the @Network.Socket@
13+-- For basic socket types and non-streaming operations please consult the @Network.Socket@
14-- module of the <http://hackage.haskell.org/package/network network> package.
14-- module of the <http://hackage.haskell.org/package/network network> package.
15--
15--
16-- = Examples
16-- = Examples
17--
17--
18−-- To write a server, use the 'acceptor' unfold to start listening for
18+-- To write a server, use the 'accept' stream to start listening for
19−-- connections from clients. 'acceptor' generates a stream of connected
19+-- connections from clients. 'accept' generates a stream of connected
20-- sockets. We can map an effectful action on this socket stream to handle the
20-- sockets. We can map an effectful action on this socket stream to handle the
21-- connections. The action would typically use socket reading and writing
21-- connections. The action would typically use socket reading and writing
22-- operations to communicate with the remote host. We can read/write a stream
22-- operations to communicate with the remote host. We can read/write a stream
@@ -33,7 +33,6 @@
33-- >>> import Streamly.Network.Socket (SockSpec(..))
33-- >>> import Streamly.Network.Socket (SockSpec(..))
34-- >>>
34-- >>>
35-- >>> import qualified Streamly.Data.Fold as Fold
35-- >>> import qualified Streamly.Data.Fold as Fold
36−-- >>> import qualified Streamly.Data.Stream as Stream
37-- >>> import qualified Streamly.Data.Stream.Prelude as Stream
36-- >>> import qualified Streamly.Data.Stream.Prelude as Stream
38-- >>> import qualified Streamly.Network.Socket as Socket
37-- >>> import qualified Streamly.Network.Socket as Socket
39-- >>>
38-- >>>