Safe Haskell | Safe-Inferred |
---|---|
Language | Haskell2010 |
Utility functions for TCP sockets
Synopsis
- data ControlHeader
- encodeControlHeader :: ControlHeader -> Word32
- decodeControlHeader :: Word32 -> Maybe ControlHeader
- data ConnectionRequestResponse
- encodeConnectionRequestResponse :: ConnectionRequestResponse -> Word32
- decodeConnectionRequestResponse :: Word32 -> Maybe ConnectionRequestResponse
- forkServer :: HostName -> ServiceName -> Int -> Bool -> (SomeException -> IO ()) -> (SomeException -> IO ()) -> (IO () -> (Socket, SockAddr) -> IO ()) -> IO (ServiceName, ThreadId)
- recvWithLength :: Word32 -> Socket -> IO [ByteString]
- recvExact :: Socket -> Word32 -> IO [ByteString]
- recvWord32 :: Socket -> IO Word32
- encodeWord32 :: Word32 -> ByteString
- tryCloseSocket :: Socket -> IO ()
- tryShutdownSocketBoth :: Socket -> IO ()
- resolveSockAddr :: SockAddr -> IO (Maybe (HostName, HostName, ServiceName))
- type EndPointId = Word32
- encodeEndPointAddress :: HostName -> ServiceName -> EndPointId -> EndPointAddress
- decodeEndPointAddress :: EndPointAddress -> Maybe (HostName, ServiceName, EndPointId)
- randomEndPointAddress :: IO EndPointAddress
- type ProtocolVersion = Word32
- currentProtocolVersion :: ProtocolVersion
Documentation
data ControlHeader Source #
Control headers
CreatedNewConnection | Tell the remote endpoint that we created a new connection |
CloseConnection | Tell the remote endpoint we will no longer be using a connection |
CloseSocket | Request to close the connection (see module description) |
CloseEndPoint | Sent by an endpoint when it is closed. |
ProbeSocket | Message sent to probe a socket |
ProbeSocketAck | Acknowledgement of the ProbeSocket message |
Instances
Show ControlHeader Source # | |
Defined in Network.Transport.TCP.Internal showsPrec :: Int -> ControlHeader -> ShowS # show :: ControlHeader -> String # showList :: [ControlHeader] -> ShowS # |
data ConnectionRequestResponse Source #
Response sent by B to A when A tries to connect
ConnectionRequestUnsupportedVersion | B does not support the protocol version requested by A. |
ConnectionRequestAccepted | B accepts the connection |
ConnectionRequestInvalid | A requested an invalid endpoint |
ConnectionRequestCrossed | As request crossed with a request from B (see protocols) |
ConnectionRequestHostMismatch | A gave an incorrect host (did not match the host that B observed). |
Instances
Show ConnectionRequestResponse Source # | |
Defined in Network.Transport.TCP.Internal showsPrec :: Int -> ConnectionRequestResponse -> ShowS # show :: ConnectionRequestResponse -> String # showList :: [ConnectionRequestResponse] -> ShowS # |
:: HostName | Host |
-> ServiceName | Port |
-> Int | Backlog (maximum number of queued connections) |
-> Bool | Set ReuseAddr option? |
-> (SomeException -> IO ()) | Error handler. Called with an exception raised when accepting a connection. |
-> (SomeException -> IO ()) | Termination handler. Called when the error handler throws an exception. |
-> (IO () -> (Socket, SockAddr) -> IO ()) | Request handler. Gets an action which completes when the socket is closed. |
-> IO (ServiceName, ThreadId) |
Start a server at the specified address.
This sets up a server socket for the specified host and port. Exceptions thrown during setup are not caught.
Once the socket is created we spawn a new thread which repeatedly accepts
incoming connections and executes the given request handler in another
thread. If any exception occurs the accepting thread terminates and calls
the terminationHandler. Threads spawned for previous accepted connections
are not killed.
This exception may occur because of a call to accept
, or because the
thread was explicitly killed.
The request handler is not responsible for closing the socket. It will be closed once that handler returns. Take care to ensure that the socket is not used after the handler returns, or you will get undefined behavior (the file descriptor may be re-used).
The return value includes the port was bound to. This is not always the same port as that given in the argument. For example, binding to port 0 actually binds to a random port, selected by the OS.
recvWithLength :: Word32 -> Socket -> IO [ByteString] Source #
Read a length and then a payload of that length, subject to a limit
on the length.
If the length (first Word32
received) is greater than the limit then
an exception is thrown.
:: Socket | Socket to read from |
-> Word32 | Number of bytes to read |
-> IO [ByteString] | Data read |
Read an exact number of bytes from a socket
Throws an I/O exception if the socket closes before the specified number of bytes could be read
encodeWord32 :: Word32 -> ByteString #
Serialize 32-bit to network byte order
tryCloseSocket :: Socket -> IO () Source #
Close a socket, ignoring I/O exceptions.
tryShutdownSocketBoth :: Socket -> IO () Source #
Shutdown socket sends and receives, ignoring I/O exceptions.
resolveSockAddr :: SockAddr -> IO (Maybe (HostName, HostName, ServiceName)) Source #
Get the numeric host, resolved host (via getNameInfo), and port from a
SockAddr. The numeric host is first, then resolved host (which may be the
same as the numeric host).
Will only give Just
for IPv4 addresses.
type EndPointId = Word32 Source #
Local identifier for an endpoint within this transport
encodeEndPointAddress :: HostName -> ServiceName -> EndPointId -> EndPointAddress Source #
Encode end point address
decodeEndPointAddress :: EndPointAddress -> Maybe (HostName, ServiceName, EndPointId) Source #
Decode end point address
randomEndPointAddress :: IO EndPointAddress Source #
Generate an EndPointAddress which does not encode a hostportendpointid. Such addresses are used for unreachable endpoints, and for ephemeral addresses when such endpoints establish new heavyweight connections.
type ProtocolVersion = Word32 Source #
Identifies the version of the network-transport-tcp protocol. It's the first piece of data sent when a new heavyweight connection is established.