2016-01-04 23:58:59 +01:00
|
|
|
/// 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::*;
|
2016-01-04 23:58:59 +01:00
|
|
|
///
|
|
|
|
/// 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>) {
|
2016-01-04 23:58:59 +01:00
|
|
|
/// 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]) {
|
2016-01-04 23:58:59 +01:00
|
|
|
/// 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) {
|
2016-01-04 23:58:59 +01:00
|
|
|
/// println!("Connected {}", peer);
|
|
|
|
/// }
|
|
|
|
///
|
2016-01-13 13:56:48 +01:00
|
|
|
/// fn disconnected(&mut self, io: &mut NetworkContext<MyMessage>, peer: &PeerId) {
|
2016-01-04 23:58:59 +01:00
|
|
|
/// println!("Disconnected {}", peer);
|
|
|
|
/// }
|
|
|
|
///
|
2016-01-13 13:56:48 +01:00
|
|
|
/// fn timeout(&mut self, io: &mut NetworkContext<MyMessage>, timer: TimerToken) {
|
2016-01-04 23:58:59 +01:00
|
|
|
/// 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);
|
2016-01-04 23:58:59 +01:00
|
|
|
/// }
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// fn main () {
|
2016-01-13 13:56:48 +01:00
|
|
|
/// let mut service = NetworkService::<MyMessage>::start().expect("Error creating network service");
|
2016-01-04 23:58:59 +01:00
|
|
|
/// 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;
|
2015-12-03 15:11:40 +01:00
|
|
|
mod discovery;
|
2015-12-17 11:42:30 +01:00
|
|
|
mod service;
|
2016-01-13 11:31:37 +01:00
|
|
|
mod error;
|
|
|
|
mod node;
|
2015-12-17 11:42:30 +01:00
|
|
|
|
2016-01-21 16:48:37 +01:00
|
|
|
pub use network::host::PeerId;
|
|
|
|
pub use network::host::PacketId;
|
|
|
|
pub use network::host::NetworkContext;
|
|
|
|
pub use network::service::NetworkService;
|
|
|
|
pub use network::host::NetworkIoMessage;
|
2016-01-13 23:13:57 +01:00
|
|
|
pub use network::host::NetworkIoMessage::User as UserMessage;
|
2016-01-21 16:48:37 +01:00
|
|
|
pub use network::error::NetworkError;
|
2016-01-13 11:31:37 +01:00
|
|
|
|
2016-01-21 16:48:37 +01:00
|
|
|
use io::TimerToken;
|
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.
|
2016-01-13 11:31:37 +01:00
|
|
|
/// `Message` is the type for message data.
|
2016-01-21 16:48:37 +01:00
|
|
|
pub trait NetworkProtocolHandler<Message>: Sync + Send where Message: Send + Sync + Clone {
|
2016-01-13 13:56:48 +01:00
|
|
|
/// Initialize the handler
|
2016-01-21 16:48:37 +01:00
|
|
|
fn initialize(&self, _io: &NetworkContext<Message>) {}
|
2015-12-30 12:23:36 +01:00
|
|
|
/// Called when new network packet received.
|
2016-01-21 16:48:37 +01:00
|
|
|
fn read(&self, io: &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.
|
2016-01-21 16:48:37 +01:00
|
|
|
fn connected(&self, io: &NetworkContext<Message>, peer: &PeerId);
|
2015-12-30 12:23:36 +01:00
|
|
|
/// Called when a previously connected peer disconnects.
|
2016-01-21 16:48:37 +01:00
|
|
|
fn disconnected(&self, io: &NetworkContext<Message>, peer: &PeerId);
|
2016-01-13 11:31:37 +01:00
|
|
|
/// Timer function called after a timeout created with `NetworkContext::timeout`.
|
2016-01-21 16:48:37 +01:00
|
|
|
fn timeout(&self, _io: &NetworkContext<Message>, _timer: TimerToken) {}
|
2016-01-13 11:31:37 +01:00
|
|
|
/// Called when a broadcasted message is received. The message can only be sent from a different IO handler.
|
2016-01-21 16:48:37 +01:00
|
|
|
fn message(&self, _io: &NetworkContext<Message>, _message: &Message) {}
|
2015-12-02 12:07:46 +01:00
|
|
|
}
|
2015-12-17 11:42:30 +01:00
|
|
|
|