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-- Socket.accept maxListenQueue spec addr
54-- & Stream.parMapM (Stream.eager True) (Socket.forSocketM echo)
55-- & Stream.fold Fold.drain
57-- Socket.readChunks 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
134 , readChunksWithBufferOf
136 , writeChunksWithBufferOf
140import Data.Word (Word8)
141import Network.Socket (Socket)
142import Streamly.Data.Array (Array)
143import Streamly.Data.MutByteArray (Unbox)
145import Streamly.Internal.Network.Socket
146import Prelude hiding (read)
148{-# DEPRECATED readChunk "Please use 'getChunk' instead" #-}
149{-# INLINABLE readChunk #-}
150readChunk :: Int -> Socket -> IO (Array Word8)
153{-# DEPRECATED writeChunk "Please use 'putChunk' instead" #-}
154{-# INLINABLE writeChunk #-}
155writeChunk :: Unbox a => Socket -> Array a -> IO ()
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