1 stable release

1.12.0 Oct 19, 2022

#82 in #mio

GPL-3.0 license

41KB
901 lines

General IO module.

Example usage for creating a network service and adding an IO handler:

extern crate ethcore_io;
use ethcore_io::*;
use std::sync::Arc;
use std::time::Duration;

struct MyHandler;

#[derive(Clone)]
struct MyMessage {
	data: u32
}

impl IoHandler<MyMessage> for MyHandler {
	fn initialize(&self, io: &IoContext<MyMessage>) {
   		io.register_timer(0, Duration::from_secs(1)).unwrap();
   	}

   	fn timeout(&self, _io: &IoContext<MyMessage>, timer: TimerToken) {
   		println!("Timeout {}", timer);
   	}

   	fn message(&self, _io: &IoContext<MyMessage>, message: &MyMessage) {
   		println!("Message {}", message.data);
   	}
}

fn main () {
	let mut service = IoService::<MyMessage>::start().expect("Error creating network service");
	service.register_handler(Arc::new(MyHandler)).unwrap();

	// Wait for quit condition
	// ...
	// Drop the service
}

Mio vs non-mio

This library has two modes: mio and not mio. The mio feature can be activated or deactivated when compiling or depending on the library.

Without mio, only timers and message-passing are available. With mio, you can also use low-level sockets provided by mio.

The non-mio mode exists because the mio library doesn't compile on platforms such as emscripten.

Dependencies

~7MB
~104K SLoC