1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
|
{-# LANGUAGE CPP #-}
module Data.Streaming.Network.Internal
( ServerSettings (..)
, ClientSettings (..)
, HostPreference (..)
, Message (..)
, AppData (..)
, ServerSettingsUnix (..)
, ClientSettingsUnix (..)
, AppDataUnix (..)
) where
import Data.String (IsString (..))
import Data.ByteString (ByteString)
import Network.Socket (Socket, SockAddr, Family)
-- | Settings for a TCP server. It takes a port to listen on, and an optional
-- hostname to bind to.
data ServerSettings = ServerSettings
{ serverPort :: !Int
, serverHost :: !HostPreference
, serverSocket :: !(Maybe Socket) -- ^ listening socket
, serverAfterBind :: !(Socket -> IO ())
, serverNeedLocalAddr :: !Bool
, serverReadBufferSize :: !Int
}
-- | Settings for a TCP client, specifying how to connect to the server.
data ClientSettings = ClientSettings
{ clientPort :: !Int
, clientHost :: !ByteString
, clientAddrFamily :: !Family
, clientReadBufferSize :: !Int
}
-- | 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\".
data HostPreference =
HostAny
| HostIPv4
| HostIPv4Only
| HostIPv6
| HostIPv6Only
| Host String
deriving (Eq, Ord, Show, Read)
instance IsString HostPreference where
fromString "*" = HostAny
fromString "*4" = HostIPv4
fromString "!4" = HostIPv4Only
fromString "*6" = HostIPv6
fromString "!6" = HostIPv6Only
fromString s = Host s
-- | Settings for a Unix domain sockets server.
data ServerSettingsUnix = ServerSettingsUnix
{ serverPath :: !FilePath
, serverAfterBindUnix :: !(Socket -> IO ())
, serverReadBufferSizeUnix :: !Int
}
-- | Settings for a Unix domain sockets client.
data ClientSettingsUnix = ClientSettingsUnix
{ clientPath :: !FilePath
, clientReadBufferSizeUnix :: !Int
}
-- | The data passed to a Unix domain sockets @Application@.
data AppDataUnix = AppDataUnix
{ appReadUnix :: !(IO ByteString)
, appWriteUnix :: !(ByteString -> IO ())
}
-- | Representation of a single UDP message
data Message = Message { msgData :: {-# UNPACK #-} !ByteString
, msgSender :: !SockAddr
}
-- | The data passed to an @Application@.
data AppData = AppData
{ appRead' :: !(IO ByteString)
, appWrite' :: !(ByteString -> IO ())
, appSockAddr' :: !SockAddr
, appLocalAddr' :: !(Maybe SockAddr)
, appCloseConnection' :: !(IO ())
, appRawSocket' :: Maybe Socket
}
|