Crate mtcp_rs

source ·
Expand description

mtcp provides a “blocking” implementation of TcpListener and TcpStream with proper timeout and cancellation support.

mtcp::TcpListener and mtcp: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::TcpCanceller can be used to abort any “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::TcpManager instance for the current thread must be obtained. Then a new mtcp::TcpListener instance can be bound to a local socket. New incoming connections are returned in the form of mtcp::TcpConnection instances. Usually each mtcp::TcpConnection instance is converted into an mtcp::TcpStream for read/write access.

The function TcpManager::canceller() optionally provides a new mtcp::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 src/examples directory, or on GitHub.

Structs

A canceller that can be used to abort “pending” I/O operations
A pending incoming TCP connection, usually used to initialize a new mtcp::TcpStream
A TCP socket server, listening for connections, akin to std::net::TcpListener
A manager for “shared” resources, used by mtcp::TcpListener and mtcp::TcpStream
A TCP stream between a local and a remote socket, akin to std::net::TcpStream

Enums

The error type for mtcp I/O operations