openethereum/src/network/mod.rs

100 lines
2.8 KiB
Rust
Raw Normal View History

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 Error {
Crypto(::crypto::CryptoError),
Io(::std::io::Error),
2015-12-02 12:07:46 +01:00
Auth,
BadProtocol,
AddressParse(::std::net::AddrParseError),
AddressResolve(Option<::std::io::Error>),
NodeIdParse(::error::EthcoreError),
2015-12-17 11:42:30 +01:00
PeerNotFound,
2015-12-02 20:11:13 +01:00
Disconnect(DisconnectReason)
}
impl From<::std::io::Error> for Error {
fn from(err: ::std::io::Error) -> Error {
Error::Io(err)
}
}
impl From<::crypto::CryptoError> for Error {
fn from(err: ::crypto::CryptoError) -> Error {
Error::Crypto(err)
}
}
2015-12-22 22:23:43 +01:00
2015-12-02 12:07:46 +01:00
impl From<::std::net::AddrParseError> for Error {
fn from(err: ::std::net::AddrParseError) -> Error {
Error::AddressParse(err)
}
}
impl From<::error::EthcoreError> for Error {
fn from(err: ::error::EthcoreError) -> Error {
Error::NodeIdParse(err)
}
}
impl From<::rlp::DecoderError> for Error {
fn from(_err: ::rlp::DecoderError) -> Error {
Error::Auth
}
}
2015-12-17 11:42:30 +01:00
impl From<::mio::NotifyError<host::HostMessage>> for Error {
fn from(_err: ::mio::NotifyError<host::HostMessage>) -> Error {
Error::Io(::std::io::Error::new(::std::io::ErrorKind::ConnectionAborted, "Network IO notification error"))
}
}
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`.
2015-12-22 22:23:43 +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