openethereum/util/src/network/mod.rs

87 lines
3.1 KiB
Rust
Raw Normal View History

/// Network and general IO module.
///
/// Example usage for craeting a network service and adding an IO handler:
///
/// ```rust
/// extern crate ethcore_util as util;
2016-01-13 13:56:48 +01:00
/// use util::*;
///
/// struct MyHandler;
///
2016-01-13 13:56:48 +01:00
/// struct MyMessage {
/// data: u32
/// }
///
/// impl NetworkProtocolHandler<MyMessage> for MyHandler {
/// fn initialize(&mut self, io: &mut NetworkContext<MyMessage>) {
/// io.register_timer(1000);
/// }
///
2016-01-13 13:56:48 +01:00
/// fn read(&mut self, io: &mut NetworkContext<MyMessage>, peer: &PeerId, packet_id: u8, data: &[u8]) {
/// println!("Received {} ({} bytes) from {}", packet_id, data.len(), peer);
/// }
///
2016-01-13 13:56:48 +01:00
/// fn connected(&mut self, io: &mut NetworkContext<MyMessage>, peer: &PeerId) {
/// println!("Connected {}", peer);
/// }
///
2016-01-13 13:56:48 +01:00
/// fn disconnected(&mut self, io: &mut NetworkContext<MyMessage>, peer: &PeerId) {
/// println!("Disconnected {}", peer);
/// }
///
2016-01-13 13:56:48 +01:00
/// fn timeout(&mut self, io: &mut NetworkContext<MyMessage>, timer: TimerToken) {
/// println!("Timeout {}", timer);
/// }
///
2016-01-13 13:56:48 +01:00
/// fn message(&mut self, io: &mut NetworkContext<MyMessage>, message: &MyMessage) {
/// println!("Message {}", message.data);
/// }
/// }
///
/// fn main () {
2016-01-13 13:56:48 +01:00
/// let mut service = NetworkService::<MyMessage>::start().expect("Error creating network service");
/// service.register_protocol(Box::new(MyHandler), "myproto", &[1u8]);
///
/// // Wait for quit condition
/// // ...
/// // Drop the service
/// }
/// ```
2015-12-02 12:07:46 +01:00
mod host;
mod connection;
mod handshake;
mod session;
mod discovery;
2015-12-17 11:42:30 +01:00
mod service;
mod error;
mod node;
2015-12-17 11:42:30 +01:00
pub type PeerId = host::PeerId;
2015-12-22 22:23:43 +01:00
pub type PacketId = host::PacketId;
2016-01-13 13:56:48 +01:00
pub type NetworkContext<'s,'io, Message> = host::NetworkContext<'s, 'io, Message>;
pub type NetworkService<Message> = service::NetworkService<Message>;
pub type NetworkIoMessage<Message> = host::NetworkIoMessage<Message>;
2016-01-13 23:13:57 +01:00
pub use network::host::NetworkIoMessage::User as UserMessage;
pub type NetworkError = error::NetworkError;
use io::*;
2015-12-17 11:42:30 +01:00
2015-12-30 12:23:36 +01:00
/// Network IO protocol handler. This needs to be implemented for each new subprotocol.
/// All the handler function are called from within IO event loop.
/// `Message` is the type for message data.
pub trait NetworkProtocolHandler<Message>: Send where Message: Send {
2016-01-13 13:56:48 +01:00
/// Initialize the handler
fn initialize(&mut self, _io: &mut NetworkContext<Message>) {}
2015-12-30 12:23:36 +01:00
/// Called when new network packet received.
fn read(&mut self, io: &mut NetworkContext<Message>, peer: &PeerId, packet_id: u8, data: &[u8]);
2015-12-30 12:23:36 +01:00
/// Called when new peer is connected. Only called when peer supports the same protocol.
fn connected(&mut self, io: &mut NetworkContext<Message>, peer: &PeerId);
2015-12-30 12:23:36 +01:00
/// Called when a previously connected peer disconnects.
fn disconnected(&mut self, io: &mut NetworkContext<Message>, peer: &PeerId);
/// Timer function called after a timeout created with `NetworkContext::timeout`.
fn timeout(&mut self, _io: &mut NetworkContext<Message>, _timer: TimerToken) {}
/// Called when a broadcasted message is received. The message can only be sent from a different IO handler.
fn message(&mut self, _io: &mut NetworkContext<Message>, _message: &Message) {}
2015-12-02 12:07:46 +01:00
}
2015-12-17 11:42:30 +01:00