Copyright | (c) 2019 Composewell Technologies |
---|---|
License | BSD3 |
Maintainer | streamly@composewell.com |
Stability | experimental |
Portability | GHC |
Safe Haskell | Safe-Inferred |
Language | Haskell2010 |
Combinators to build Inet/TCP clients and servers.
Synopsis
- accept :: MonadIO m => PortNumber -> Stream m Socket
- acceptLocal :: MonadIO m => PortNumber -> Stream m Socket
- acceptOnAddr :: MonadIO m => (Word8, Word8, Word8, Word8) -> PortNumber -> Stream m Socket
- acceptOnAddrWith :: MonadIO m => [(SocketOption, Int)] -> (Word8, Word8, Word8, Word8) -> PortNumber -> Stream m Socket
- acceptor :: MonadIO m => Unfold m PortNumber Socket
- acceptorLocal :: MonadIO m => Unfold m PortNumber Socket
- acceptorWith :: MonadIO m => [(SocketOption, Int)] -> Unfold m PortNumber Socket
- acceptorOnAddr :: MonadIO m => Unfold m ((Word8, Word8, Word8, Word8), PortNumber) Socket
- acceptorOnAddrWith :: MonadIO m => [(SocketOption, Int)] -> Unfold m ((Word8, Word8, Word8, Word8), PortNumber) Socket
- connect :: (Word8, Word8, Word8, Word8) -> PortNumber -> IO Socket
- withConnectionM :: (MonadMask m, MonadIO m) => (Word8, Word8, Word8, Word8) -> PortNumber -> (Socket -> m ()) -> m ()
- usingConnection :: (MonadCatch m, MonadAsync m) => Unfold m Socket a -> Unfold m ((Word8, Word8, Word8, Word8), PortNumber) a
- reader :: (MonadCatch m, MonadAsync m) => Unfold m ((Word8, Word8, Word8, Word8), PortNumber) Word8
- withConnection :: (MonadCatch m, MonadAsync m) => (Word8, Word8, Word8, Word8) -> PortNumber -> (Socket -> Stream m a) -> Stream m a
- read :: (MonadCatch m, MonadAsync m) => (Word8, Word8, Word8, Word8) -> PortNumber -> Stream m Word8
- write :: (MonadIO m, MonadCatch m) => (Word8, Word8, Word8, Word8) -> PortNumber -> Fold m Word8 ()
- writeWithBufferOf :: (MonadIO m, MonadCatch m) => Int -> (Word8, Word8, Word8, Word8) -> PortNumber -> Fold m Word8 ()
- putBytes :: (MonadCatch m, MonadAsync m) => (Word8, Word8, Word8, Word8) -> PortNumber -> Stream m Word8 -> m ()
- putBytesWithBufferOf :: (MonadCatch m, MonadAsync m) => Int -> (Word8, Word8, Word8, Word8) -> PortNumber -> Stream m Word8 -> m ()
- writeChunks :: (MonadIO m, MonadCatch m) => (Word8, Word8, Word8, Word8) -> PortNumber -> Fold m (Array Word8) ()
- putChunks :: (MonadCatch m, MonadAsync m) => (Word8, Word8, Word8, Word8) -> PortNumber -> Stream m (Array Word8) -> m ()
- pipeBytes :: (MonadAsync m, MonadCatch m) => (Word8, Word8, Word8, Word8) -> PortNumber -> Stream m Word8 -> Stream m Word8
- acceptorOnPort :: MonadIO m => Unfold m PortNumber Socket
- acceptorOnPortLocal :: MonadIO m => Unfold m PortNumber Socket
Setup
To execute the code examples provided in this module in ghci, please run the following commands first.
>>>
:m
>>>
import qualified Streamly.Data.Unfold as Unfold
>>>
import qualified Streamly.Network.Inet.TCP as TCP
TCP Servers
Streams
accept :: MonadIO m => PortNumber -> Stream m Socket Source #
Start a TCP stream server that binds on the IPV4 address 0.0.0.0
and
listens for TCP connections from remote hosts on the specified server port.
The server generates a stream of connected sockets.
>>>
accept = TCP.acceptOnAddr (0,0,0,0)
Pre-release
acceptLocal :: MonadIO m => PortNumber -> Stream m Socket Source #
Like accept
but binds on the localhost IPv4 address 127.0.0.1
.
The server can only be accessed from the local host, it cannot be accessed
from other hosts on the network.
>>>
acceptLocal = TCP.acceptOnAddr (127,0,0,1)
Pre-release
acceptOnAddr :: MonadIO m => (Word8, Word8, Word8, Word8) -> PortNumber -> Stream m Socket Source #
Like accept
but binds on the specified IPv4 address.
>>>
acceptOnAddr = TCP.acceptOnAddrWith []
Pre-release
acceptOnAddrWith :: MonadIO m => [(SocketOption, Int)] -> (Word8, Word8, Word8, Word8) -> PortNumber -> Stream m Socket Source #
Like acceptOnAddr
but with the ability to specify a list of socket
options.
Pre-release
Unfolds
acceptor :: MonadIO m => Unfold m PortNumber Socket Source #
Like acceptorOnAddr
but binds on the IPv4 address 0.0.0.0
i.e. on all
IPv4 addresses/interfaces of the machine and listens for TCP connections on
the specified port.
>>>
acceptor = Unfold.first (0,0,0,0) TCP.acceptorOnAddr
acceptorLocal :: MonadIO m => Unfold m PortNumber Socket Source #
Like acceptor
but binds on the localhost IPv4 address 127.0.0.1
. The
server can only be accessed from the local host, it cannot be accessed from
other hosts on the network.
>>>
acceptorLocal = Unfold.first (127,0,0,1) TCP.acceptorOnAddr
acceptorWith :: MonadIO m => [(SocketOption, Int)] -> Unfold m PortNumber Socket Source #
acceptorOnAddr :: MonadIO m => Unfold m ((Word8, Word8, Word8, Word8), PortNumber) Socket Source #
Unfold a tuple (ipAddr, port)
into a stream of connected TCP sockets.
ipAddr
is the local IP address and port
is the local port on which
connections are accepted.
acceptorOnAddrWith :: MonadIO m => [(SocketOption, Int)] -> Unfold m ((Word8, Word8, Word8, Word8), PortNumber) Socket Source #
TCP clients
IP Address based operations.
connect :: (Word8, Word8, Word8, Word8) -> PortNumber -> IO Socket Source #
Connect to the specified IP address and port number. Returns a connected socket or throws an exception.
withConnectionM :: (MonadMask m, MonadIO m) => (Word8, Word8, Word8, Word8) -> PortNumber -> (Socket -> m ()) -> m () Source #
Connect to a remote host using IP address and port and run the supplied
action on the resulting socket. withConnectionM
makes sure that the
socket is closed on normal termination or in case of an exception. If
closing the socket raises an exception, then this exception will be raised
by withConnectionM
.
Pre-release
Unfolds
usingConnection :: (MonadCatch m, MonadAsync m) => Unfold m Socket a -> Unfold m ((Word8, Word8, Word8, Word8), PortNumber) a Source #
Transform an Unfold
from a Socket
to an unfold from a remote IP
address and port. The resulting unfold opens a socket, uses it using the
supplied unfold and then makes sure that the socket is closed on normal
termination or in case of an exception. If closing the socket raises an
exception, then this exception will be raised by usingConnection
.
Pre-release
reader :: (MonadCatch m, MonadAsync m) => Unfold m ((Word8, Word8, Word8, Word8), PortNumber) Word8 Source #
Read a stream from the supplied IPv4 host address and port number.
Streams
withConnection :: (MonadCatch m, MonadAsync m) => (Word8, Word8, Word8, Word8) -> PortNumber -> (Socket -> Stream m a) -> Stream m a Source #
opens a connection to the specified IPv4
host address and port and passes the resulting socket handle to the
computation withConnection
addr port actact
. The handle will be closed on exit from withConnection
,
whether by normal termination or by raising an exception. If closing the
handle raises an exception, then this exception will be raised by
withConnection
rather than any exception raised by act
.
Pre-release
Source
read :: (MonadCatch m, MonadAsync m) => (Word8, Word8, Word8, Word8) -> PortNumber -> Stream m Word8 Source #
Read a stream from the supplied IPv4 host address and port number.
Pre-release
Sink
write :: (MonadIO m, MonadCatch m) => (Word8, Word8, Word8, Word8) -> PortNumber -> Fold m Word8 () Source #
Write a stream to the supplied IPv4 host address and port number.
writeWithBufferOf :: (MonadIO m, MonadCatch m) => Int -> (Word8, Word8, Word8, Word8) -> PortNumber -> Fold m Word8 () Source #
Like write
but provides control over the write buffer. Output will
be written to the IO device as soon as we collect the specified number of
input elements.
putBytes :: (MonadCatch m, MonadAsync m) => (Word8, Word8, Word8, Word8) -> PortNumber -> Stream m Word8 -> m () Source #
Write a stream to the supplied IPv4 host address and port number.
Pre-release
putBytesWithBufferOf :: (MonadCatch m, MonadAsync m) => Int -> (Word8, Word8, Word8, Word8) -> PortNumber -> Stream m Word8 -> m () Source #
Like write
but provides control over the write buffer. Output will
be written to the IO device as soon as we collect the specified number of
input elements.
Pre-release
writeChunks :: (MonadIO m, MonadCatch m) => (Word8, Word8, Word8, Word8) -> PortNumber -> Fold m (Array Word8) () Source #
Write a stream of arrays to the supplied IPv4 host address and port number.
putChunks :: (MonadCatch m, MonadAsync m) => (Word8, Word8, Word8, Word8) -> PortNumber -> Stream m (Array Word8) -> m () Source #
Write a stream of arrays to the supplied IPv4 host address and port number.
Pre-release
Transformation
pipeBytes :: (MonadAsync m, MonadCatch m) => (Word8, Word8, Word8, Word8) -> PortNumber -> Stream m Word8 -> Stream m Word8 Source #
Send an input stream to a remote host and produce the output stream from the host. The server host just acts as a transformation function on the input stream. Both sending and receiving happen asynchronously.
Pre-release
Deprecated
acceptorOnPort :: MonadIO m => Unfold m PortNumber Socket Source #
Deprecated: Use "acceptor" instead.
acceptorOnPortLocal :: MonadIO m => Unfold m PortNumber Socket Source #
Deprecated: Use "acceptorLocal" instead.