openethereum/src/network/mod.rs

119 lines
3.5 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;
/// 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
/// }
/// ```
2015-11-29 11:50:28 +01:00
extern crate mio;
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;
2015-12-02 20:11:13 +01:00
#[derive(Debug, Copy, Clone)]
pub enum DisconnectReason
{
DisconnectRequested,
TCPError,
BadProtocol,
UselessPeer,
TooManyPeers,
DuplicatePeer,
IncompatibleProtocol,
NullIdentity,
ClientQuit,
UnexpectedIdentity,
LocalIdentity,
PingTimeout,
}
#[derive(Debug)]
pub enum NetworkError {
2015-12-02 12:07:46 +01:00
Auth,
BadProtocol,
2015-12-17 11:42:30 +01:00
PeerNotFound,
Disconnect(DisconnectReason),
Mio(::std::io::Error),
}
impl From<::rlp::DecoderError> for NetworkError {
fn from(_err: ::rlp::DecoderError) -> NetworkError {
NetworkError::Auth
}
}
impl From<::mio::NotifyError<host::HostMessage>> for NetworkError {
fn from(_err: ::mio::NotifyError<host::HostMessage>) -> NetworkError {
NetworkError::Mio(::std::io::Error::new(::std::io::ErrorKind::ConnectionAborted, "Network IO notification error"))
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;
pub type TimerToken = host::TimerToken;
2015-12-17 11:42:30 +01:00
pub type HandlerIo<'s> = host::HostIo<'s>;
2015-12-28 11:41:51 +01:00
pub type Message = host::UserMessage;
pub type MessageId = host::UserMessageId;
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.
/// TODO: Separate p2p networking IO from IPC IO. `timeout` and `message` should go to a more genera IO provider.
/// All the handler function are called from within IO event loop.
2015-12-17 11:42:30 +01:00
pub trait ProtocolHandler: Send {
2015-12-30 12:23:36 +01:00
/// Initialize the hadler
2015-12-17 11:42:30 +01:00
fn initialize(&mut self, io: &mut HandlerIo);
2015-12-30 12:23:36 +01:00
/// Called when new network packet received.
2015-12-22 22:23:43 +01:00
fn read(&mut self, io: &mut HandlerIo, 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.
2015-12-17 11:42:30 +01:00
fn connected(&mut self, io: &mut HandlerIo, peer: &PeerId);
2015-12-30 12:23:36 +01:00
/// Called when a previously connected peer disconnects.
2015-12-17 11:42:30 +01:00
fn disconnected(&mut self, io: &mut HandlerIo, peer: &PeerId);
2015-12-30 12:23:36 +01:00
/// Timer function called after a timeout created with `HandlerIo::timeout`.
fn timeout(&mut self, io: &mut HandlerIo, timer: TimerToken);
2015-12-30 12:23:36 +01:00
/// Called when a broadcasted message is received. The message can only be sent from a different protocol handler.
2015-12-28 11:41:51 +01:00
fn message(&mut self, io: &mut HandlerIo, message: &Message);
2015-12-02 12:07:46 +01:00
}
2015-12-17 11:42:30 +01:00
2015-12-22 22:23:43 +01:00
pub type NetworkService = service::NetworkService;
2015-12-17 11:42:30 +01:00