middleman 0.1.0

A wrapper for a mio::TcpStream to send and receive serializable Structs (blocking or non blocking) of arbitrary size
Documentation

Middleman

Middleman aims to make sending structures over a network easy. It is built on top of mio, and is in the same spirit as the library wire.

 struct M: Message 
    ↑       ╷
    ┆       ┆

    Middleman (⌐■_)
       TCP
    ~~~~~~~~~
    ▲       ▼    
    ▲ bytes ▼    
    ▲       ▼
    ~~~~~~~~~    
       TCP
    Middleman (⌐■_)

    ┆       ┆
    ╵       ↓
 struct M: Message 

Function

This library aims to provide a number of structs that implement the Middleman trait. At present, the only one available is Threadless, which leans on mio's non-blocking reads to support blocking and non-blocking sends and receives without requiring additional threads.

Using the library

  1. Implement Message for any types T you want to send or receive. (This involves implementing the serde library's serialization traits. In most cases the serde_derive macros are just fine).
#[derive(Serialize, Deserialize)]
enum X {
	A,
	B(u32),
	C(Vec<u8>),
}
impl middleman::Message for X {}
  1. Acquire your mio::TcpStream object(s). See the documentation for mio for more information.
  2. (optional) alter socketoptions as desired for your socket (eg: NoDelay).
  3. Wrap your stream with some MiddleMan implmentor.
use middleman::{Middleman, Threadless};
let mut mm = Threadless::new(stream);

That's it. Messages from your peer can be received using recv() (blocking) or try_recv() (non-blocking).

use middleman::{TryRecvError};

// blocking receive
let x1 = mm.recv::<X>().unwrap();

// non-blocking receive
loop {
	do_work();
	match mm.try_recv::<X>() {
		Ok(msg) => {
			...
		},
		Err(TryRecvError::ReadNotReady) => (), // no problem
		Err(TryRecvError::Fatal(e)) => {
			// handle fatal error
		},
	}
}

Examples

See src/tests.rs for an example of a typical use case.