Expand description
mtcp provides a “blocking” implementation of TcpListener and
TcpStream with proper timeout and cancellation support.
mtcp_rs::TcpListener and mtcp_rs::TcpStream
pretty much are drop-in replacements for
std::net::TcpListener,
std::net::TcpStream, but with an additional
timeout parameter in the “blocking” I/O functions – including but not
limited to the accept() function! Also, a
mtcp_rs::TcpCanceller can be used to abort “pending” I/O
operation immediately, e.g. from another thread or from the Ctrl+C (SIGINT)
handler, so that “cleanly” shutting down your server becomes a possibility.
The “blocking” I/O operations in mtcp are emulated via non-blocking
operations, using the mio library, in order to make timeouts
and cancellation support possible while also providing very high
performance. But, thanks to mtcp, you won’t have to bother with mio
events and the event polling mechanism at all. All platforms supported by
mio are supported by mtcp as well.
§Usage
First of all, a mtcp_rs::TcpManager instance for the
current thread must be obtained. Then a new
mtcp_rs::TcpListener instance can be bound to a local
socket. New incoming connections are returned in the form of
mtcp_rs::TcpConnection instances. Usually an
mtcp_rs::TcpConnection instance is converted into an
mtcp_rs::TcpStream for read/write access.
The function TcpManager::canceller() optionally provides a
new mtcp_rs::TcpCanceller instance that may be used to
cancel pending I/O operations. You can use, for example,
ctrlc to invoke
cancel() from your Ctrl+C (SIGINT) handler.
§Examples
Examples be can found in the examples sub-directory, or on
GitHub.
Structs§
- TcpCanceller
- A canceller that can be used to abort “pending” I/O operations
- TcpConnection
- A pending incoming TCP connection, usually used to initialize a new
mtcp_rs::TcpStream - TcpListener
- A TCP socket server, listening for connections, akin to
std::net::TcpListener - TcpManager
- A manager for “shared” resources, used by
mtcp_rs::TcpListenerandmtcp_rs::TcpStream - TcpStream
- A TCP stream between a local and a remote socket, akin to
std::net::TcpStream
Enums§
- TcpError
- The error type for mtcp I/O operations