Data.Conduit.Network

conduit-extra-1.1.16: Batteries included conduit: adapters for common libraries.

Safe Haskell | None
Language | Haskell98

Data.Conduit.Network

Contents

- Basic utilities
- Simple TCP server/client interface.
  - Server
  - Client
  - Getters
  - Setters
- Types

Synopsis

- sourceSocket :: MonadIO m => Socket -> Producer m ByteString
- sinkSocket :: MonadIO m => Socket -> Consumer ByteString m ()
- data AppData :: *
- appSource :: (HasReadWrite ad, MonadIO m) => ad -> Producer m ByteString
- appSink :: (HasReadWrite ad, MonadIO m) => ad -> Consumer ByteString m ()
- appSockAddr :: AppData -> SockAddr
- appLocalAddr :: AppData -> Maybe SockAddr
- data ServerSettings :: *
- serverSettings :: Int -> HostPreference -> ServerSettings
- runTCPServer :: ServerSettings -> (AppData -> IO ()) -> IO a
- runTCPServerWithHandle :: ServerSettings -> ConnectionHandle -> IO a
- forkTCPServer :: MonadBaseControl IO m => ServerSettings -> (AppData -> m ()) -> m ThreadId
- runGeneralTCPServer :: MonadBaseControl IO m => ServerSettings -> (AppData -> m ()) -> m a
- data ClientSettings :: *
- clientSettings :: Int -> ByteString -> ClientSettings
- runTCPClient :: ClientSettings -> (AppData -> IO a) -> IO a
- runGeneralTCPClient :: MonadBaseControl IO m => ClientSettings -> (AppData -> m a) -> m a
- getPort :: HasPort a => a -> Int
- getHost :: ClientSettings -> ByteString
- getAfterBind :: HasAfterBind a => a -> Socket -> IO ()
- getNeedLocalAddr :: ServerSettings -> Bool
- setPort :: HasPort a => Int -> a -> a
- setHost :: ByteString -> ClientSettings -> ClientSettings
- setAfterBind :: HasAfterBind a => (Socket -> IO ()) -> a -> a
- setNeedLocalAddr :: Bool -> ServerSettings -> ServerSettings
- data HostPreference :: *

# Basic utilities

sourceSocket :: MonadIO m => Socket -> Producer m ByteString #

Stream data from the socket.

This function does not automatically close the socket.

Since 0.0.0

sinkSocket :: MonadIO m => Socket -> Consumer ByteString m () #

Stream data to the socket.

This function does not automatically close the socket.

Since 0.0.0

# Simple TCP server/client interface.

data AppData :: * #

The data passed to an `Application`.

Instances

HasReadWrite AppData |
Methods readLens :: Functor f => (IO ByteString -> f (IO ByteString)) -> AppData -> f AppData # writeLens :: Functor f => ((ByteString -> IO ()) -> f (ByteString -> IO ())) -> AppData -> f AppData #

appSource :: (HasReadWrite ad, MonadIO m) => ad -> Producer m ByteString #

appSink :: (HasReadWrite ad, MonadIO m) => ad -> Consumer ByteString m () #

appSockAddr :: AppData -> SockAddr #

appLocalAddr :: AppData -> Maybe SockAddr #

## Server

data ServerSettings :: * #

Settings for a TCP server. It takes a port to listen on, and an optional hostname to bind to.

Instances

HasPort ServerSettings |
Methods portLens :: Functor f => (Int -> f Int) -> ServerSettings -> f ServerSettings #
HasAfterBind ServerSettings |
Methods afterBindLens :: Functor f => ((Socket -> IO ()) -> f (Socket -> IO ())) -> ServerSettings -> f ServerSettings #
HasReadBufferSize ServerSettings | Since 0.1.13
Methods readBufferSizeLens :: Functor f => (Int -> f Int) -> ServerSettings -> f ServerSettings #

serverSettings :: Int -> HostPreference -> ServerSettings #

runTCPServer :: ServerSettings -> (AppData -> IO ()) -> IO a #

Run an `Application` with the given settings. This function will create a new listening socket, accept connections on it, and spawn a new thread for each connection.

runTCPServerWithHandle :: ServerSettings -> ConnectionHandle -> IO a #

forkTCPServer :: MonadBaseControl IO m => ServerSettings -> (AppData -> m ()) -> m ThreadId #

Fork a TCP Server

Will fork the runGeneralTCPServer function but will only return from this call when the server is bound to the port and accepting incoming connections. Will return the thread id of the server

Since 1.1.4

runGeneralTCPServer :: MonadBaseControl IO m => ServerSettings -> (AppData -> m ()) -> m a #

Run a general TCP server

Same as `runTCPServer`, except monad can be any instance of `MonadBaseControl` `IO`.

Note that any changes to the monadic state performed by individual client handlers will be discarded. If you have mutable state you want to share among multiple handlers, you need to use some kind of mutable variables.

Since 1.1.3

## Client

data ClientSettings :: * #

Settings for a TCP client, specifying how to connect to the server.

Instances

HasPort ClientSettings |
Methods portLens :: Functor f => (Int -> f Int) -> ClientSettings -> f ClientSettings #
HasReadBufferSize ClientSettings | Since 0.1.13
Methods readBufferSizeLens :: Functor f => (Int -> f Int) -> ClientSettings -> f ClientSettings #

clientSettings :: Int -> ByteString -> ClientSettings #

runTCPClient :: ClientSettings -> (AppData -> IO a) -> IO a #

Run an `Application` by connecting to the specified server.

runGeneralTCPClient :: MonadBaseControl IO m => ClientSettings -> (AppData -> m a) -> m a #

Run a general TCP client

Same as `runTCPClient`, except monad can be any instance of `MonadBaseControl` `IO`.

Since 1.1.3

## Getters

getPort :: HasPort a => a -> Int #

getHost :: ClientSettings -> ByteString #

getAfterBind :: HasAfterBind a => a -> Socket -> IO () #

getNeedLocalAddr :: ServerSettings -> Bool #

## Setters

setPort :: HasPort a => Int -> a -> a #

setHost :: ByteString -> ClientSettings -> ClientSettings #

setAfterBind :: HasAfterBind a => (Socket -> IO ()) -> a -> a #

setNeedLocalAddr :: Bool -> ServerSettings -> ServerSettings #

# Types

data HostPreference :: * #

Which host to bind.

Note: The `IsString` instance recognizes the following special values:

- `*` means `HostAny` - "any IPv4 or IPv6 hostname"
- `*4` means `HostIPv4` - "any IPv4 or IPv6 hostname, IPv4 preferred"
- `!4` means `HostIPv4Only` - "any IPv4 hostname"
- `*6` means `HostIPv6`@ - "any IPv4 or IPv6 hostname, IPv6 preferred"
- `!6` means `HostIPv6Only` - "any IPv6 hostname"

Note that the permissive `*` values allow binding to an IPv4 or an IPv6 hostname, which means you might be able to successfully bind to a port more times than you expect (eg once on the IPv4 localhost 127.0.0.1 and again on the IPv6 localhost 0:0:0:0:0:0:0:1).

Any other value is treated as a hostname. As an example, to bind to the IPv4 local host only, use "127.0.0.1".

Instances

Eq HostPreference |
Methods (==) :: HostPreference -> HostPreference -> Bool # (/=) :: HostPreference -> HostPreference -> Bool #
Ord HostPreference |
Methods compare :: HostPreference -> HostPreference -> Ordering # (<) :: HostPreference -> HostPreference -> Bool # (<=) :: HostPreference -> HostPreference -> Bool # (>) :: HostPreference -> HostPreference -> Bool # (>=) :: HostPreference -> HostPreference -> Bool # max :: HostPreference -> HostPreference -> HostPreference # min :: HostPreference -> HostPreference -> HostPreference #
Read HostPreference |
Methods readsPrec :: Int -> ReadS HostPreference # readList :: ReadS [HostPreference] # readPrec :: ReadPrec HostPreference # readListPrec :: ReadPrec [HostPreference] #
Show HostPreference |
Methods showsPrec :: Int -> HostPreference -> ShowS # show :: HostPreference -> String # showList :: [HostPreference] -> ShowS #
IsString HostPreference |
Methods fromString :: String -> HostPreference #