2-- Module : Streamly.Network.Socket
3-- Copyright : (c) 2018 Composewell Technologies
5-- License : BSD-3-Clause
6-- Maintainer : streamly@composewell.com
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.
13-- For basic socket types and operations please consult the @Network.Socket@
14-- module of the <http://hackage.haskell.org/package/network network> package.
18-- To write a server, use the 'acceptor' unfold to start listening for
19-- connections from clients. 'acceptor' generates a stream of connected
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
22-- operations to communicate with the remote host. We can read/write a stream
23-- of bytes or a stream of chunks of bytes ('Array').
25-- Following is a short example of a concurrent echo server. Please note that
26-- this example can be written even more succinctly by using higher level
27-- operations from "Streamly.Network.Inet.TCP" module.
29-- >>> :set -XFlexibleContexts
31-- >>> import Data.Function ((&))
32-- >>> import Network.Socket
33-- >>> import Streamly.Network.Socket (SockSpec(..))
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
38-- >>> import qualified Streamly.Network.Socket as Socket
44-- { sockFamily = AF_INET
46-- , sockProto = defaultProtocol
49-- addr = SockAddrInet 8090 (tupleToHostAddress (0,0,0,0))
53-- Stream.unfold Socket.acceptor (maxListenQueue, spec, addr)
54-- & Stream.parMapM (Stream.eager True) (Socket.forSocketM echo)
55-- & Stream.fold Fold.drain
57-- Stream.unfold Socket.chunkReader sk -- Stream IO (Array Word8)
58-- & Stream.fold (Socket.writeChunks sk) -- IO ()
63-- Read IO requests to connected stream sockets are performed in chunks of
64-- 'Streamly.Internal.Data.Array.Type.defaultChunkSize'. Unless
65-- specified otherwise in the API, writes are collected into chunks of
66-- 'Streamly.Internal.System.IO.defaultChunkSize' before they are written to
69-- >>> import qualified Streamly.Network.Socket as Socket
73-- * "Streamly.Internal.Network.Socket"
74-- * <http://hackage.haskell.org/package/network network>
76-------------------------------------------------------------------------------
78-------------------------------------------------------------------------------
80-- A socket is a handle to a protocol endpoint.
82-- This module provides APIs to read and write streams and arrays from and to
83-- network sockets. Sockets may be connected or unconnected. Connected sockets
84-- can only send or recv data to/from the connected endpoint, therefore, APIs
85-- for connected sockets do not need to explicitly specify the remote endpoint.
86-- APIs for unconnected sockets need to explicitly specify the remote endpoint.
88-- By design, connected socket IO APIs are similar to
89-- "Streamly.Data.Array.Foreign" read write APIs. They are almost identical to the
90-- sequential streaming APIs in "Streamly.Internal.FileSystem.File".
92module Streamly.Network.Socket
94 -- * Socket Specification
97 -- * Accept Connections
130 , readChunksWithBufferOf
132 , writeChunksWithBufferOf
136import Control.Monad.IO.Class (MonadIO(..))
137import Data.Word (Word8)
138import Network.Socket (Socket, SockAddr)
139import Streamly.Internal.Data.Unfold (Unfold(..))
140import Streamly.Internal.Data.Array (Array(..))
141import Streamly.Internal.Data.Unbox (Unbox)
143import Streamly.Internal.Network.Socket hiding (accept, read, readChunks)
144import Prelude hiding (read)
146{-# DEPRECATED accept "Please use 'acceptor' instead" #-}
148accept :: MonadIO m => Unfold m (Int, SockSpec, SockAddr) Socket
151{-# DEPRECATED readChunk "Please use 'getChunk' instead" #-}
152{-# INLINABLE readChunk #-}
153readChunk :: Int -> Socket -> IO (Array Word8)
156{-# DEPRECATED writeChunk "Please use 'putChunk' instead" #-}
157{-# INLINABLE writeChunk #-}
158writeChunk :: Unbox a => Socket -> Array a -> IO ()
161{-# DEPRECATED read "Please use 'reader' instead" #-}
163read :: MonadIO m => Unfold m Socket Word8
166{-# DEPRECATED readChunks "Please use 'chunkReader' instead" #-}
167{-# INLINE readChunks #-}
168readChunks :: MonadIO m => Unfold m Socket (Array Word8)
169readChunks = chunkReader
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list