Fixed timeout signature; Added usage example
This commit is contained in:
parent
39f2dc9e2f
commit
73405cc74c
@ -1,3 +1,48 @@
|
||||
/// Network and general IO module.
|
||||
///
|
||||
/// Example usage for craeting a network service and adding an IO handler:
|
||||
///
|
||||
/// ```rust
|
||||
/// extern crate ethcore_util as util;
|
||||
/// use util::network::*;
|
||||
///
|
||||
/// struct MyHandler;
|
||||
///
|
||||
/// impl ProtocolHandler for MyHandler {
|
||||
/// fn initialize(&mut self, io: &mut HandlerIo) {
|
||||
/// io.register_timer(1000);
|
||||
/// }
|
||||
///
|
||||
/// fn read(&mut self, io: &mut HandlerIo, peer: &PeerId, packet_id: u8, data: &[u8]) {
|
||||
/// println!("Received {} ({} bytes) from {}", packet_id, data.len(), peer);
|
||||
/// }
|
||||
///
|
||||
/// fn connected(&mut self, io: &mut HandlerIo, peer: &PeerId) {
|
||||
/// println!("Connected {}", peer);
|
||||
/// }
|
||||
///
|
||||
/// fn disconnected(&mut self, io: &mut HandlerIo, peer: &PeerId) {
|
||||
/// println!("Disconnected {}", peer);
|
||||
/// }
|
||||
///
|
||||
/// fn timeout(&mut self, io: &mut HandlerIo, timer: TimerToken) {
|
||||
/// println!("Timeout {}", timer);
|
||||
/// }
|
||||
///
|
||||
/// fn message(&mut self, io: &mut HandlerIo, message: &Message) {
|
||||
/// println!("Message {}:{}", message.protocol, message.id);
|
||||
/// }
|
||||
/// }
|
||||
///
|
||||
/// fn main () {
|
||||
/// let mut service = NetworkService::start().expect("Error creating network service");
|
||||
/// service.register_protocol(Box::new(MyHandler), "myproto", &[1u8]);
|
||||
///
|
||||
/// // Wait for quit condition
|
||||
/// // ...
|
||||
/// // Drop the service
|
||||
/// }
|
||||
/// ```
|
||||
extern crate mio;
|
||||
mod host;
|
||||
mod connection;
|
||||
@ -90,7 +135,7 @@ pub trait ProtocolHandler: Send {
|
||||
/// Called when a previously connected peer disconnects.
|
||||
fn disconnected(&mut self, io: &mut HandlerIo, peer: &PeerId);
|
||||
/// Timer function called after a timeout created with `HandlerIo::timeout`.
|
||||
fn timeout(&mut self, io: &mut HandlerIo, timer: TimerToken) -> bool;
|
||||
fn timeout(&mut self, io: &mut HandlerIo, timer: TimerToken);
|
||||
/// Called when a broadcasted message is received. The message can only be sent from a different protocol handler.
|
||||
fn message(&mut self, io: &mut HandlerIo, message: &Message);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user