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;
|
|
|
|
/// 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;
|
2015-12-03 15:11:40 +01:00
|
|
|
mod discovery;
|
2015-12-17 11:42:30 +01:00
|
|
|
mod service;
|
2015-11-30 16:38:55 +01:00
|
|
|
|
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,
|
|
|
|
}
|
|
|
|
|
2015-11-30 16:38:55 +01:00
|
|
|
#[derive(Debug)]
|
2016-01-10 12:53:55 +01:00
|
|
|
pub enum NetworkError {
|
2015-12-02 12:07:46 +01:00
|
|
|
Auth,
|
|
|
|
BadProtocol,
|
2015-12-17 11:42:30 +01:00
|
|
|
PeerNotFound,
|
2016-01-10 12:53:55 +01:00
|
|
|
Disconnect(DisconnectReason),
|
|
|
|
Mio(::std::io::Error),
|
2015-11-30 16:38:55 +01:00
|
|
|
}
|
|
|
|
|
2016-01-10 12:53:55 +01:00
|
|
|
impl From<::rlp::DecoderError> for NetworkError {
|
|
|
|
fn from(_err: ::rlp::DecoderError) -> NetworkError {
|
|
|
|
NetworkError::Auth
|
2015-11-30 16:38:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-10 12:53:55 +01:00
|
|
|
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`.
|
2016-01-04 23:58:59 +01:00
|
|
|
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
|
|
|
|