Move snapshot sync to a subprotocol (#2820)
This commit is contained in:
committed by
Gav Wood
parent
ff347da8d3
commit
9ec091e0cf
@@ -155,6 +155,8 @@ pub enum NetworkIoMessage {
|
||||
protocol: ProtocolId,
|
||||
/// Supported protocol versions.
|
||||
versions: Vec<u8>,
|
||||
/// Number of packet IDs reserved by the protocol.
|
||||
packet_count: u8,
|
||||
},
|
||||
/// Register a new protocol timer
|
||||
AddTimer {
|
||||
@@ -251,9 +253,8 @@ impl<'s> NetworkContext<'s> {
|
||||
self.io.channel()
|
||||
}
|
||||
|
||||
/// Disable current protocol capability for given peer. If no capabilities left peer gets disconnected.
|
||||
/// Disconnect a peer and prevent it from connecting again.
|
||||
pub fn disable_peer(&self, peer: PeerId) {
|
||||
//TODO: remove capability, disconnect if no capabilities left
|
||||
self.io.message(NetworkIoMessage::DisablePeer(peer))
|
||||
.unwrap_or_else(|e| warn!("Error sending network IO message: {:?}", e));
|
||||
}
|
||||
@@ -290,7 +291,7 @@ impl<'s> NetworkContext<'s> {
|
||||
}
|
||||
|
||||
/// Returns max version for a given protocol.
|
||||
pub fn protocol_version(&self, peer: PeerId, protocol: ProtocolId) -> Option<u8> {
|
||||
pub fn protocol_version(&self, protocol: ProtocolId, peer: PeerId) -> Option<u8> {
|
||||
let session = self.resolve_session(peer);
|
||||
session.and_then(|s| s.lock().capability_version(protocol))
|
||||
}
|
||||
@@ -1018,7 +1019,8 @@ impl IoHandler<NetworkIoMessage> for Host {
|
||||
NetworkIoMessage::AddHandler {
|
||||
ref handler,
|
||||
ref protocol,
|
||||
ref versions
|
||||
ref versions,
|
||||
ref packet_count,
|
||||
} => {
|
||||
let h = handler.clone();
|
||||
let reserved = self.reserved_nodes.read();
|
||||
@@ -1026,7 +1028,7 @@ impl IoHandler<NetworkIoMessage> for Host {
|
||||
self.handlers.write().insert(*protocol, h);
|
||||
let mut info = self.info.write();
|
||||
for v in versions {
|
||||
info.capabilities.push(CapabilityInfo { protocol: *protocol, version: *v, packet_count:0 });
|
||||
info.capabilities.push(CapabilityInfo { protocol: *protocol, version: *v, packet_count: *packet_count });
|
||||
}
|
||||
},
|
||||
NetworkIoMessage::AddTimer {
|
||||
|
||||
@@ -45,7 +45,7 @@
|
||||
//!
|
||||
//! fn main () {
|
||||
//! let mut service = NetworkService::new(NetworkConfiguration::new_local()).expect("Error creating network service");
|
||||
//! service.register_protocol(Arc::new(MyHandler), *b"myp", &[1u8]);
|
||||
//! service.register_protocol(Arc::new(MyHandler), *b"myp", 1, &[1u8]);
|
||||
//! service.start().expect("Error starting service");
|
||||
//!
|
||||
//! // Wait for quit condition
|
||||
@@ -91,13 +91,9 @@ mod ip_utils;
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
|
||||
pub use host::PeerId;
|
||||
pub use host::PacketId;
|
||||
pub use host::NetworkContext;
|
||||
pub use host::{PeerId, PacketId, ProtocolId, NetworkContext, NetworkIoMessage, NetworkConfiguration};
|
||||
pub use service::NetworkService;
|
||||
pub use host::NetworkIoMessage;
|
||||
pub use error::NetworkError;
|
||||
pub use host::NetworkConfiguration;
|
||||
pub use stats::NetworkStats;
|
||||
pub use session::SessionInfo;
|
||||
|
||||
|
||||
@@ -73,11 +73,12 @@ impl NetworkService {
|
||||
}
|
||||
|
||||
/// Regiter a new protocol handler with the event loop.
|
||||
pub fn register_protocol(&self, handler: Arc<NetworkProtocolHandler + Send + Sync>, protocol: ProtocolId, versions: &[u8]) -> Result<(), NetworkError> {
|
||||
pub fn register_protocol(&self, handler: Arc<NetworkProtocolHandler + Send + Sync>, protocol: ProtocolId, packet_count: u8, versions: &[u8]) -> Result<(), NetworkError> {
|
||||
try!(self.io_service.send_message(NetworkIoMessage::AddHandler {
|
||||
handler: handler,
|
||||
protocol: protocol,
|
||||
versions: versions.to_vec(),
|
||||
packet_count: packet_count,
|
||||
}));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
|
||||
use std::{str, io};
|
||||
use std::net::SocketAddr;
|
||||
use std::cmp::Ordering;
|
||||
use std::sync::*;
|
||||
use mio::*;
|
||||
use mio::tcp::*;
|
||||
@@ -122,7 +123,7 @@ impl ToString for PeerCapabilityInfo {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct SessionCapabilityInfo {
|
||||
pub protocol: [u8; 3],
|
||||
pub version: u8,
|
||||
@@ -130,6 +131,23 @@ pub struct SessionCapabilityInfo {
|
||||
pub id_offset: u8,
|
||||
}
|
||||
|
||||
impl PartialOrd for SessionCapabilityInfo {
|
||||
fn partial_cmp(&self, other: &SessionCapabilityInfo) -> Option<Ordering> {
|
||||
Some(self.cmp(other))
|
||||
}
|
||||
}
|
||||
|
||||
impl Ord for SessionCapabilityInfo {
|
||||
fn cmp(&self, b: &SessionCapabilityInfo) -> Ordering {
|
||||
// By protocol id first
|
||||
if self.protocol != b.protocol {
|
||||
return self.protocol.cmp(&b.protocol);
|
||||
}
|
||||
// By version
|
||||
self.version.cmp(&b.version)
|
||||
}
|
||||
}
|
||||
|
||||
const PACKET_HELLO: u8 = 0x80;
|
||||
const PACKET_DISCONNECT: u8 = 0x01;
|
||||
const PACKET_PING: u8 = 0x02;
|
||||
@@ -441,6 +459,9 @@ impl Session {
|
||||
}
|
||||
}
|
||||
|
||||
// Sort capabilities alphabeticaly.
|
||||
caps.sort();
|
||||
|
||||
i = 0;
|
||||
let mut offset: u8 = PACKET_USER;
|
||||
while i < caps.len() {
|
||||
|
||||
@@ -41,7 +41,7 @@ impl TestProtocol {
|
||||
/// Creates and register protocol with the network service
|
||||
pub fn register(service: &mut NetworkService, drop_session: bool) -> Arc<TestProtocol> {
|
||||
let handler = Arc::new(TestProtocol::new(drop_session));
|
||||
service.register_protocol(handler.clone(), *b"tst", &[42u8, 43u8]).expect("Error registering test protocol handler");
|
||||
service.register_protocol(handler.clone(), *b"tst", 1, &[42u8, 43u8]).expect("Error registering test protocol handler");
|
||||
handler
|
||||
}
|
||||
|
||||
@@ -93,7 +93,7 @@ impl NetworkProtocolHandler for TestProtocol {
|
||||
fn net_service() {
|
||||
let service = NetworkService::new(NetworkConfiguration::new_local()).expect("Error creating network service");
|
||||
service.start().unwrap();
|
||||
service.register_protocol(Arc::new(TestProtocol::new(false)), *b"myp", &[1u8]).unwrap();
|
||||
service.register_protocol(Arc::new(TestProtocol::new(false)), *b"myp", 1, &[1u8]).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
Reference in New Issue
Block a user