* chain notify trait * replaced network service with io service * fix ethcore crate warnings * refactored network service without generic * ethcore fix * ethsync refactoring * proper linking of notify * manage network interface * rpc crate rebinding * full rewire * sync internal io service * fix deadlock * fix warnings and removed async io * sync imported message propagation * fix rpc warnings * binart warnings * test fixes * rpc mocks and tests * fix util doctest * fix message name and removed empty notifier * pointers mess & dark mode fixed * fixed sync doctest * added few warnings * fix review * new convention match * fix error unwraps * doctest fix
122 lines
3.7 KiB
Rust
122 lines
3.7 KiB
Rust
// Copyright 2015, 2016 Ethcore (UK) Ltd.
|
|
// This file is part of Parity.
|
|
|
|
// Parity is free software: you can redistribute it and/or modify
|
|
// it under the terms of the GNU General Public License as published by
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
// (at your option) any later version.
|
|
|
|
// Parity is distributed in the hope that it will be useful,
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
// GNU General Public License for more details.
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
//! 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::*;
|
|
//!
|
|
//! struct MyHandler;
|
|
//!
|
|
//! impl NetworkProtocolHandler for MyHandler {
|
|
//! fn initialize(&self, io: &NetworkContext) {
|
|
//! io.register_timer(0, 1000);
|
|
//! }
|
|
//!
|
|
//! fn read(&self, io: &NetworkContext, peer: &PeerId, packet_id: u8, data: &[u8]) {
|
|
//! println!("Received {} ({} bytes) from {}", packet_id, data.len(), peer);
|
|
//! }
|
|
//!
|
|
//! fn connected(&self, io: &NetworkContext, peer: &PeerId) {
|
|
//! println!("Connected {}", peer);
|
|
//! }
|
|
//!
|
|
//! fn disconnected(&self, io: &NetworkContext, peer: &PeerId) {
|
|
//! println!("Disconnected {}", peer);
|
|
//! }
|
|
//!
|
|
//! fn timeout(&self, io: &NetworkContext, timer: TimerToken) {
|
|
//! println!("Timeout {}", timer);
|
|
//! }
|
|
//! }
|
|
//!
|
|
//! fn main () {
|
|
//! let mut service = NetworkService::new(NetworkConfiguration::new_local()).expect("Error creating network service");
|
|
//! service.register_protocol(Arc::new(MyHandler), "myproto", &[1u8]);
|
|
//! service.start().expect("Error starting service");
|
|
//!
|
|
//! // Wait for quit condition
|
|
//! // ...
|
|
//! // Drop the service
|
|
//! }
|
|
//! ```
|
|
mod host;
|
|
mod connection;
|
|
mod handshake;
|
|
mod session;
|
|
mod discovery;
|
|
mod service;
|
|
mod error;
|
|
mod node_table;
|
|
mod stats;
|
|
mod ip_utils;
|
|
|
|
#[cfg(test)]
|
|
mod tests;
|
|
|
|
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;
|
|
pub use network::error::NetworkError;
|
|
pub use network::host::NetworkConfiguration;
|
|
pub use network::stats::NetworkStats;
|
|
|
|
use io::TimerToken;
|
|
pub use network::node_table::is_valid_node_url;
|
|
|
|
const PROTOCOL_VERSION: u32 = 4;
|
|
|
|
/// 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: Sync + Send {
|
|
/// Initialize the handler
|
|
fn initialize(&self, _io: &NetworkContext) {}
|
|
/// Called when new network packet received.
|
|
fn read(&self, io: &NetworkContext, peer: &PeerId, packet_id: u8, data: &[u8]);
|
|
/// Called when new peer is connected. Only called when peer supports the same protocol.
|
|
fn connected(&self, io: &NetworkContext, peer: &PeerId);
|
|
/// Called when a previously connected peer disconnects.
|
|
fn disconnected(&self, io: &NetworkContext, peer: &PeerId);
|
|
/// Timer function called after a timeout created with `NetworkContext::timeout`.
|
|
fn timeout(&self, _io: &NetworkContext, _timer: TimerToken) {}
|
|
}
|
|
|
|
/// Non-reserved peer modes.
|
|
#[derive(Clone, Debug, PartialEq, Eq)]
|
|
pub enum NonReservedPeerMode {
|
|
/// Accept them. This is the default.
|
|
Accept,
|
|
/// Deny them.
|
|
Deny,
|
|
}
|
|
|
|
impl NonReservedPeerMode {
|
|
/// Attempt to parse the peer mode from a string.
|
|
pub fn parse(s: &str) -> Option<Self> {
|
|
match s {
|
|
"accept" => Some(NonReservedPeerMode::Accept),
|
|
"deny" => Some(NonReservedPeerMode::Deny),
|
|
_ => None,
|
|
}
|
|
}
|
|
}
|