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 Array and stream based socket operations to connect to
11-- remote hosts, to receive connections from remote hosts, and to read and
12-- write streams and arrays of bytes to and from network sockets.
14-- For basic socket types and operations please consult the @Network.Socket@
15-- module of the <http://hackage.haskell.org/package/network network> package.
19-- To write a server, use the 'accept' unfold to start listening for
20-- connections from clients. 'accept' supplies a stream of connected sockets.
21-- We can map an effectful action on this socket stream to handle the
22-- connections. The action would typically use socket reading and writing
23-- operations to communicate with the remote host. We can read/write a stream
24-- of bytes or a stream of chunks of bytes ('Array').
26-- Following is a short example of a concurrent echo server. Please note that
27-- this example can be written even more succinctly by using higher level
28-- operations from "Streamly.Network.Inet.TCP" module.
31-- {-\# LANGUAGE FlexibleContexts #-}
33-- import Data.Function ((&))
34-- import Network.Socket
35-- import Streamly.Network.Socket (SockSpec(..))
37-- import qualified Streamly.Prelude as Stream
38-- import qualified Streamly.Network.Socket as Socket
42-- { sockFamily = AF_INET
44-- , sockProto = defaultProtocol
47-- addr = SockAddrInet 8090 (tupleToHostAddress (0,0,0,0))
53-- Stream.unfold Socket.accept (maxListenQueue, spec, addr) -- ParallelT IO Socket
54-- & Stream.mapM (Socket.forSocketM echo) -- ParallelT IO ()
55-- & Stream.fromParallel -- Stream IO ()
56-- & Stream.drain -- IO ()
59-- Stream.unfold Socket.readChunks sk -- Stream IO (Array Word8)
60-- & Stream.fold (Socket.writeChunks sk) -- IO ()
65-- Read IO requests to connected stream sockets are performed in chunks of
66-- 'Streamly.Internal.Data.Array.Unboxed.Type.defaultChunkSize'. Unless
67-- specified otherwise in the API, writes are collected into chunks of
68-- 'Streamly.Internal.Data.Array.Unboxed.Type.defaultChunkSize' before they are
69-- written to the socket. APIs are provided to control the chunking behavior.
71-- > import qualified Streamly.Network.Socket as Socket
75-- * "Streamly.Internal.Network.Socket"
76-- * <http://hackage.haskell.org/package/network network>
78-------------------------------------------------------------------------------
80-------------------------------------------------------------------------------
82-- A socket is a handle to a protocol endpoint.
84-- This module provides APIs to read and write streams and arrays from and to
85-- network sockets. Sockets may be connected or unconnected. Connected sockets
86-- can only send or recv data to/from the connected endpoint, therefore, APIs
87-- for connected sockets do not need to explicitly specify the remote endpoint.
88-- APIs for unconnected sockets need to explicitly specify the remote endpoint.
90-- By design, connected socket IO APIs are similar to
91-- "Streamly.Data.Array.Foreign" read write APIs. They are almost identical to the
92-- sequential streaming APIs in "Streamly.Internal.FileSystem.File".
94module Streamly.Network.Socket
96 -- * Socket Specification
99 -- * Accept Connections
132 , readChunksWithBufferOf
134 , writeChunksWithBufferOf
138import Control.Monad.IO.Class (MonadIO(..))
139import Data.Word (Word8)
140import Network.Socket (Socket, SockAddr)
141import Streamly.Internal.Data.Unfold.Type (Unfold(..))
142import Streamly.Internal.Data.Array.Unboxed.Type (Array(..))
143import Streamly.Internal.Data.Unboxed (Unbox)
145import Streamly.Internal.Network.Socket hiding (accept, read, readChunks)
146import Prelude hiding (read)
148{-# DEPRECATED accept "Please use 'acceptor' instead" #-}
150accept :: MonadIO m => Unfold m (Int, SockSpec, SockAddr) Socket
153{-# DEPRECATED readChunk "Please use 'getChunk' instead" #-}
154{-# INLINABLE readChunk #-}
155readChunk :: Int -> Socket -> IO (Array Word8)
158{-# DEPRECATED writeChunk "Please use 'putChunk' instead" #-}
159{-# INLINABLE writeChunk #-}
160writeChunk :: Unbox a => Socket -> Array a -> IO ()
163{-# DEPRECATED read "Please use 'reader' instead" #-}
165read :: MonadIO m => Unfold m Socket Word8
168{-# DEPRECATED readChunks "Please use 'chunkReader' instead" #-}
169{-# INLINE readChunks #-}
170readChunks :: MonadIO m => Unfold m Socket (Array Word8)
171readChunks = 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