Copyright | (c) Dong Han 2018 |
---|---|
License | BSD |
Maintainer | winterland1989@gmail.com |
Stability | experimental |
Portability | non-portable |
Safe Haskell | None |
Language | Haskell2010 |
This module provides an API for creating TCP servers and clients.
Synopsis
- data TCPClientConfig = TCPClientConfig {}
- data UVStream
- defaultTCPClientConfig :: TCPClientConfig
- initTCPClient :: HasCallStack => TCPClientConfig -> Resource UVStream
- getTCPSockName :: HasCallStack => UVStream -> IO SocketAddr
- data TCPServerConfig = TCPServerConfig {}
- defaultTCPServerConfig :: TCPServerConfig
- startTCPServer :: HasCallStack => TCPServerConfig -> (UVStream -> IO ()) -> IO ()
- getTCPPeerName :: HasCallStack => UVStream -> IO SocketAddr
- helloWorld :: UVStream -> IO ()
- echo :: UVStream -> IO ()
- startServerLoop :: HasCallStack => Int -> (UVManager -> Resource UVStream) -> (Ptr UVHandle -> IO ()) -> (FD -> (UVStream -> IO ()) -> IO ()) -> (UVStream -> IO ()) -> IO ()
- setTCPNoDelay :: HasCallStack => UVStream -> Bool -> IO ()
- setTCPKeepAlive :: HasCallStack => UVStream -> CUInt -> IO ()
- initTCPStream :: UVManager -> Resource UVStream
TCP Client
data TCPClientConfig Source #
A TCP client configuration
TCPClientConfig | |
|
Instances
A haskell data type wrap an uv_stream_t
inside
UVStream
DO NOT provide thread safety! Use UVStream
concurrently in multiple
threads will lead to undefined behavior.
defaultTCPClientConfig :: TCPClientConfig Source #
Default config, connect to localhost:8888
.
initTCPClient :: HasCallStack => TCPClientConfig -> Resource UVStream Source #
init a TCP client Resource
, which open a new connect when used.
getTCPSockName :: HasCallStack => UVStream -> IO SocketAddr Source #
Get the current address to which the handle is bound.
TCP Server
data TCPServerConfig Source #
A TCP server configuration
TCPServerConfig | |
|
Instances
defaultTCPServerConfig :: TCPServerConfig Source #
A default hello world server on 0.0.0.0:8888
Test it with main = startTCPServer defaultTCPServerConfig helloWorldWorker
or
main = startTCPServer defaultTCPServerConfig echoWorker
, now try nc -v 127.0.0.1 8888
:: HasCallStack | |
=> TCPServerConfig | |
-> (UVStream -> IO ()) | worker which will get an accepted TCP stream and run in a seperated haskell thread, will be closed upon exception or worker finishes. |
-> IO () |
Start a TCP server
Fork new worker threads upon a new connection.
getTCPPeerName :: HasCallStack => UVStream -> IO SocketAddr Source #
Get the address of the peer connected to the handle.
For test
Internal helper
:: HasCallStack | |
=> Int | backLog |
-> (UVManager -> Resource UVStream) | uv_stream_t initializer |
-> (Ptr UVHandle -> IO ()) | bind function |
-> (FD -> (UVStream -> IO ()) -> IO ()) | thread spawner |
-> (UVStream -> IO ()) | worker |
-> IO () |
Start a server loop with different kind of uv_stream
s, such as tcp or pipe.
setTCPNoDelay :: HasCallStack => UVStream -> Bool -> IO () Source #
Enable or disable TCP_NODELAY
, which enable or disable Nagle’s algorithm.
setTCPKeepAlive :: HasCallStack => UVStream -> CUInt -> IO () Source #
Enable / disable TCP keep-alive. delay is the initial delay in seconds, ignored when enable is zero.
After delay has been reached, 10 successive probes, each spaced 1 second from the previous one,
will still happen. If the connection is still lost at the end of this procedure,
then the connection is closed, pending io thread will throw TimeExpired
exception.