diff --git a/ethash/src/lib.rs b/ethash/src/lib.rs index f7b6d2308..e87ee1a03 100644 --- a/ethash/src/lib.rs +++ b/ethash/src/lib.rs @@ -30,11 +30,13 @@ impl EthashManager { /// `nonce` - The nonce to pack into the mix pub fn compute_light(&self, block_number: u64, header_hash: &H256, nonce: u64) -> ProofOfWork { let epoch = block_number / ETHASH_EPOCH_LENGTH; - if !self.lights.read().unwrap().contains_key(&epoch) { - let mut lights = self.lights.write().unwrap(); // obtain write lock - if !lights.contains_key(&epoch) { - let light = Light::new(block_number); - lights.insert(epoch, light); + while !self.lights.read().unwrap().contains_key(&epoch) { + if let Ok(mut lights) = self.lights.try_write() + { + if !lights.contains_key(&epoch) { + let light = Light::new(block_number); + lights.insert(epoch, light); + } } } self.lights.read().unwrap().get(&epoch).unwrap().compute(header_hash, nonce) diff --git a/src/bin/client.rs b/src/bin/client.rs index a862737be..92106aad4 100644 --- a/src/bin/client.rs +++ b/src/bin/client.rs @@ -29,7 +29,7 @@ fn main() { setup_log(); let spec = ethereum::new_frontier(); let mut service = ClientService::start(spec).unwrap(); - let io_handler = Box::new(ClientIoHandler { client: service.client(), timer: 0 }); + let io_handler = Arc::new(ClientIoHandler { client: service.client() }); service.io().register_handler(io_handler).expect("Error registering IO handler"); loop { let mut cmd = String::new(); @@ -43,16 +43,15 @@ fn main() { struct ClientIoHandler { client: Arc>, - timer: TimerToken, } impl IoHandler for ClientIoHandler { - fn initialize<'s>(&'s mut self, io: &mut IoContext<'s, NetSyncMessage>) { - self.timer = io.register_timer(5000).expect("Error registering timer"); + fn initialize(&self, io: &IoContext) { + io.register_timer(0, 5000).expect("Error registering timer"); } - fn timeout<'s>(&'s mut self, _io: &mut IoContext<'s, NetSyncMessage>, timer: TimerToken) { - if self.timer == timer { + fn timeout(&self, _io: &IoContext, timer: TimerToken) { + if timer == 0 { println!("Chain info: {:?}", self.client.read().unwrap().deref().chain_info()); } } diff --git a/src/client.rs b/src/client.rs index e02ab37d8..87bdc4416 100644 --- a/src/client.rs +++ b/src/client.rs @@ -1,6 +1,5 @@ use util::*; use rocksdb::{Options, DB}; -use rocksdb::DBCompactionStyle::DBUniversalCompaction; use blockchain::{BlockChain, BlockProvider}; use views::BlockView; use error::*; @@ -113,7 +112,9 @@ impl Client { pub fn new(spec: Spec, path: &Path, message_channel: IoChannel ) -> Result { let chain = Arc::new(RwLock::new(BlockChain::new(&spec.genesis_block(), path))); let mut opts = Options::new(); + opts.set_max_open_files(256); opts.create_if_missing(true); + /* opts.set_max_open_files(256); opts.set_use_fsync(false); opts.set_bytes_per_sync(8388608); @@ -131,6 +132,7 @@ impl Client { opts.set_max_background_flushes(4); opts.set_filter_deletes(false); opts.set_disable_auto_compactions(true); + */ let mut state_path = path.to_path_buf(); state_path.push("state"); @@ -219,7 +221,7 @@ impl Client { return; } } - info!(target: "client", "Imported #{} ({})", header.number(), header.hash()); + //info!(target: "client", "Imported #{} ({})", header.number(), header.hash()); } } } diff --git a/src/service.rs b/src/service.rs index 036c99bc4..02f813c2f 100644 --- a/src/service.rs +++ b/src/service.rs @@ -22,7 +22,7 @@ impl ClientService { dir.push(H64::from(spec.genesis_header().hash()).hex()); let client = Arc::new(RwLock::new(try!(Client::new(spec, &dir, net_service.io().channel())))); EthSync::register(&mut net_service, client.clone()); - let client_io = Box::new(ClientIoHandler { + let client_io = Arc::new(ClientIoHandler { client: client.clone() }); try!(net_service.io().register_handler(client_io)); @@ -48,14 +48,14 @@ struct ClientIoHandler { } impl IoHandler for ClientIoHandler { - fn initialize<'s>(&'s mut self, _io: &mut IoContext<'s, NetSyncMessage>) { + fn initialize(&self, _io: &IoContext) { } - fn message<'s>(&'s mut self, _io: &mut IoContext<'s, NetSyncMessage>, net_message: &'s mut NetSyncMessage) { + fn message(&self, _io: &IoContext, net_message: &NetSyncMessage) { match net_message { - &mut UserMessage(ref mut message) => { + &UserMessage(ref message) => { match message { - &mut SyncMessage::BlockVerified => { + &SyncMessage::BlockVerified => { self.client.write().unwrap().import_verified_blocks(); }, _ => {}, // ignore other messages diff --git a/src/sync/chain.rs b/src/sync/chain.rs index 15fe6d1f9..af954e4ad 100644 --- a/src/sync/chain.rs +++ b/src/sync/chain.rs @@ -424,6 +424,10 @@ impl ChainSync { let peer_difficulty = self.peers.get_mut(&peer_id).expect("ChainSync: unknown peer").difficulty; if difficulty > peer_difficulty { trace!(target: "sync", "Received block {:?} with no known parent. Peer needs syncing...", h); + { + let peer = self.peers.get_mut(&peer_id).expect("ChainSync: unknown peer"); + peer.latest = header_view.sha3(); + } self.sync_peer(io, peer_id, true); } } diff --git a/src/sync/io.rs b/src/sync/io.rs index affcbc0d7..f49591a9f 100644 --- a/src/sync/io.rs +++ b/src/sync/io.rs @@ -22,14 +22,14 @@ pub trait SyncIo { } /// Wraps `NetworkContext` and the blockchain client -pub struct NetSyncIo<'s, 'h, 'io> where 'h: 's, 'io: 'h { - network: &'s mut NetworkContext<'h, 'io, SyncMessage>, +pub struct NetSyncIo<'s, 'h> where 'h: 's { + network: &'s NetworkContext<'h, SyncMessage>, chain: &'s mut BlockChainClient } -impl<'s, 'h, 'io> NetSyncIo<'s, 'h, 'io> { +impl<'s, 'h> NetSyncIo<'s, 'h> { /// Creates a new instance from the `NetworkContext` and the blockchain client reference. - pub fn new(network: &'s mut NetworkContext<'h, 'io, SyncMessage>, chain: &'s mut BlockChainClient) -> NetSyncIo<'s,'h,'io> { + pub fn new(network: &'s NetworkContext<'h, SyncMessage>, chain: &'s mut BlockChainClient) -> NetSyncIo<'s, 'h> { NetSyncIo { network: network, chain: chain, @@ -37,7 +37,7 @@ impl<'s, 'h, 'io> NetSyncIo<'s, 'h, 'io> { } } -impl<'s, 'h, 'op> SyncIo for NetSyncIo<'s, 'h, 'op> { +impl<'s, 'h> SyncIo for NetSyncIo<'s, 'h> { fn disable_peer(&mut self, peer_id: PeerId) { self.network.disable_peer(peer_id); } diff --git a/src/sync/mod.rs b/src/sync/mod.rs index da91a6889..5bcaf656e 100644 --- a/src/sync/mod.rs +++ b/src/sync/mod.rs @@ -26,9 +26,8 @@ use std::ops::*; use std::sync::*; use client::Client; use util::network::{NetworkProtocolHandler, NetworkService, NetworkContext, PeerId, NetworkIoMessage}; -use util::TimerToken; -use util::Bytes; use sync::chain::ChainSync; +use util::{Bytes, TimerToken}; use sync::io::NetSyncIo; mod chain; @@ -38,10 +37,13 @@ mod range_collection; #[cfg(test)] mod tests; +const SYNC_TIMER: usize = 0; + /// Message type for external events +#[derive(Clone)] pub enum SyncMessage { /// New block has been imported into the blockchain - NewChainBlock(Bytes), + NewChainBlock(Bytes), //TODO: use Cow /// A block is ready BlockVerified, } @@ -53,7 +55,7 @@ pub struct EthSync { /// Shared blockchain client. TODO: this should evetually become an IPC endpoint chain: Arc>, /// Sync strategy - sync: ChainSync + sync: RwLock } pub use self::chain::SyncStatus; @@ -61,52 +63,50 @@ pub use self::chain::SyncStatus; impl EthSync { /// Creates and register protocol with the network service pub fn register(service: &mut NetworkService, chain: Arc>) { - let sync = Box::new(EthSync { + let sync = Arc::new(EthSync { chain: chain, - sync: ChainSync::new(), + sync: RwLock::new(ChainSync::new()), }); - service.register_protocol(sync, "eth", &[62u8, 63u8]).expect("Error registering eth protocol handler"); + service.register_protocol(sync.clone(), "eth", &[62u8, 63u8]).expect("Error registering eth protocol handler"); } /// Get sync status pub fn status(&self) -> SyncStatus { - self.sync.status() + self.sync.read().unwrap().status() } /// Stop sync pub fn stop(&mut self, io: &mut NetworkContext) { - self.sync.abort(&mut NetSyncIo::new(io, self.chain.write().unwrap().deref_mut())); + self.sync.write().unwrap().abort(&mut NetSyncIo::new(io, self.chain.write().unwrap().deref_mut())); } /// Restart sync pub fn restart(&mut self, io: &mut NetworkContext) { - self.sync.restart(&mut NetSyncIo::new(io, self.chain.write().unwrap().deref_mut())); + self.sync.write().unwrap().restart(&mut NetSyncIo::new(io, self.chain.write().unwrap().deref_mut())); } } impl NetworkProtocolHandler for EthSync { - fn initialize(&mut self, io: &mut NetworkContext) { - self.sync.restart(&mut NetSyncIo::new(io, self.chain.write().unwrap().deref_mut())); - io.register_timer(1000).unwrap(); + fn initialize(&self, io: &NetworkContext) { + io.register_timer(SYNC_TIMER, 1000).unwrap(); } - fn read(&mut self, io: &mut NetworkContext, peer: &PeerId, packet_id: u8, data: &[u8]) { - self.sync.on_packet(&mut NetSyncIo::new(io, self.chain.write().unwrap().deref_mut()) , *peer, packet_id, data); + fn read(&self, io: &NetworkContext, peer: &PeerId, packet_id: u8, data: &[u8]) { + self.sync.write().unwrap().on_packet(&mut NetSyncIo::new(io, self.chain.write().unwrap().deref_mut()) , *peer, packet_id, data); } - fn connected(&mut self, io: &mut NetworkContext, peer: &PeerId) { - self.sync.on_peer_connected(&mut NetSyncIo::new(io, self.chain.write().unwrap().deref_mut()), *peer); + fn connected(&self, io: &NetworkContext, peer: &PeerId) { + self.sync.write().unwrap().on_peer_connected(&mut NetSyncIo::new(io, self.chain.write().unwrap().deref_mut()), *peer); } - fn disconnected(&mut self, io: &mut NetworkContext, peer: &PeerId) { - self.sync.on_peer_aborting(&mut NetSyncIo::new(io, self.chain.write().unwrap().deref_mut()), *peer); + fn disconnected(&self, io: &NetworkContext, peer: &PeerId) { + self.sync.write().unwrap().on_peer_aborting(&mut NetSyncIo::new(io, self.chain.write().unwrap().deref_mut()), *peer); } - fn timeout(&mut self, io: &mut NetworkContext, _timer: TimerToken) { - self.sync.maintain_sync(&mut NetSyncIo::new(io, self.chain.write().unwrap().deref_mut())); - } - - fn message(&mut self, _io: &mut NetworkContext, _message: &SyncMessage) { + fn timeout(&self, io: &NetworkContext, timer: TimerToken) { + if timer == SYNC_TIMER { + self.sync.write().unwrap().maintain_sync(&mut NetSyncIo::new(io, self.chain.write().unwrap().deref_mut())); + } } } diff --git a/util/Cargo.toml b/util/Cargo.toml index 02fdad17f..15dc9523a 100644 --- a/util/Cargo.toml +++ b/util/Cargo.toml @@ -22,6 +22,7 @@ rust-crypto = "0.2.34" elastic-array = "0.4" heapsize = "0.2" itertools = "0.4" +crossbeam = "0.2" slab = { git = "https://github.com/arkpar/slab.git" } sha3 = { path = "sha3" } diff --git a/util/src/io/mod.rs b/util/src/io/mod.rs index 23a8509cc..71b882fb8 100644 --- a/util/src/io/mod.rs +++ b/util/src/io/mod.rs @@ -36,13 +36,16 @@ /// } /// ``` mod service; +mod worker; + +use mio::{EventLoop, Token}; #[derive(Debug)] pub enum IoError { Mio(::std::io::Error), } -impl From<::mio::NotifyError>> for IoError where Message: Send { +impl From<::mio::NotifyError>> for IoError where Message: Send + Clone { fn from(_err: ::mio::NotifyError>) -> IoError { IoError::Mio(::std::io::Error::new(::std::io::ErrorKind::ConnectionAborted, "Network IO notification error")) } @@ -51,27 +54,32 @@ impl From<::mio::NotifyError>> for IoError /// Generic IO handler. /// All the handler function are called from within IO event loop. /// `Message` type is used as notification data -pub trait IoHandler: Send where Message: Send + 'static { +pub trait IoHandler: Send + Sync where Message: Send + Sync + Clone + 'static { /// Initialize the handler - fn initialize<'s>(&'s mut self, _io: &mut IoContext<'s, Message>) {} + fn initialize(&self, _io: &IoContext) {} /// Timer function called after a timeout created with `HandlerIo::timeout`. - fn timeout<'s>(&'s mut self, _io: &mut IoContext<'s, Message>, _timer: TimerToken) {} + fn timeout(&self, _io: &IoContext, _timer: TimerToken) {} /// Called when a broadcasted message is received. The message can only be sent from a different IO handler. - fn message<'s>(&'s mut self, _io: &mut IoContext<'s, Message>, _message: &'s mut Message) {} // TODO: make message immutable and provide internal channel for adding network handler + fn message(&self, _io: &IoContext, _message: &Message) {} /// Called when an IO stream gets closed - fn stream_hup<'s>(&'s mut self, _io: &mut IoContext<'s, Message>, _stream: StreamToken) {} + fn stream_hup(&self, _io: &IoContext, _stream: StreamToken) {} /// Called when an IO stream can be read from - fn stream_readable<'s>(&'s mut self, _io: &mut IoContext<'s, Message>, _stream: StreamToken) {} + fn stream_readable(&self, _io: &IoContext, _stream: StreamToken) {} /// Called when an IO stream can be written to - fn stream_writable<'s>(&'s mut self, _io: &mut IoContext<'s, Message>, _stream: StreamToken) {} + fn stream_writable(&self, _io: &IoContext, _stream: StreamToken) {} + /// Register a new stream with the event loop + fn register_stream(&self, _stream: StreamToken, _reg: Token, _event_loop: &mut EventLoop>) {} + /// Re-register a stream with the event loop + fn update_stream(&self, _stream: StreamToken, _reg: Token, _event_loop: &mut EventLoop>) {} } pub type TimerToken = service::TimerToken; pub type StreamToken = service::StreamToken; -pub type IoContext<'s, M> = service::IoContext<'s, M>; -pub type IoService = service::IoService; -pub type IoChannel = service::IoChannel; -//pub const USER_TOKEN_START: usize = service::USER_TOKEN; // TODO: ICE in rustc 1.7.0-nightly (49c382779 2016-01-12) +pub use io::service::IoContext; +pub type IoService = service::IoService; +pub type IoChannel = service::IoChannel; +pub type IoManager = service::IoManager; +pub const TOKENS_PER_HANDLER: usize = service::TOKENS_PER_HANDLER; #[cfg(test)] mod tests { diff --git a/util/src/io/service.rs b/util/src/io/service.rs index 4a96d19a7..a229e4022 100644 --- a/util/src/io/service.rs +++ b/util/src/io/service.rs @@ -1,148 +1,221 @@ +use std::sync::*; use std::thread::{self, JoinHandle}; +use std::collections::HashMap; use mio::*; -use mio::util::{Slab}; use hash::*; use rlp::*; use error::*; use io::{IoError, IoHandler}; +use arrayvec::*; +use crossbeam::sync::chase_lev; +use io::worker::{Worker, Work, WorkType}; pub type TimerToken = usize; pub type StreamToken = usize; +pub type HandlerId = usize; // Tokens -const MAX_USER_TIMERS: usize = 32; -const USER_TIMER: usize = 0; -const LAST_USER_TIMER: usize = USER_TIMER + MAX_USER_TIMERS - 1; -//const USER_TOKEN: usize = LAST_USER_TIMER + 1; +pub const TOKENS_PER_HANDLER: usize = 16384; /// Messages used to communicate with the event loop from other threads. -pub enum IoMessage where Message: Send + Sized { +#[derive(Clone)] +pub enum IoMessage where Message: Send + Clone + Sized { /// Shutdown the event loop Shutdown, /// Register a new protocol handler. AddHandler { - handler: Box+Send>, + handler: Arc+Send>, + }, + AddTimer { + handler_id: HandlerId, + token: TimerToken, + delay: u64, + }, + RemoveTimer { + handler_id: HandlerId, + token: TimerToken, + }, + RegisterStream { + handler_id: HandlerId, + token: StreamToken, + }, + UpdateStreamRegistration { + handler_id: HandlerId, + token: StreamToken, }, /// Broadcast a message across all protocol handlers. UserMessage(Message) } /// IO access point. This is passed to all IO handlers and provides an interface to the IO subsystem. -pub struct IoContext<'s, Message> where Message: Send + 'static { - timers: &'s mut Slab, - /// Low leve MIO Event loop for custom handler registration. - pub event_loop: &'s mut EventLoop>, +pub struct IoContext where Message: Send + Clone + 'static { + pub channel: IoChannel, + pub handler: HandlerId, } -impl<'s, Message> IoContext<'s, Message> where Message: Send + 'static { +impl IoContext where Message: Send + Clone + 'static { /// Create a new IO access point. Takes references to all the data that can be updated within the IO handler. - fn new(event_loop: &'s mut EventLoop>, timers: &'s mut Slab) -> IoContext<'s, Message> { + pub fn new(channel: IoChannel, handler: HandlerId) -> IoContext { IoContext { - event_loop: event_loop, - timers: timers, + handler: handler, + channel: channel, } } - /// Register a new IO timer. Returns a new timer token. 'IoHandler::timeout' will be called with the token. - pub fn register_timer(&mut self, ms: u64) -> Result { - match self.timers.insert(UserTimer { + /// Register a new IO timer. 'IoHandler::timeout' will be called with the token. + pub fn register_timer(&self, token: TimerToken, ms: u64) -> Result<(), UtilError> { + try!(self.channel.send_io(IoMessage::AddTimer { + token: token, delay: ms, - }) { - Ok(token) => { - self.event_loop.timeout_ms(token, ms).expect("Error registering user timer"); - Ok(token.as_usize()) - }, - _ => { panic!("Max timers reached") } - } + handler_id: self.handler, + })); + Ok(()) + } + + /// Delete a timer. + pub fn clear_timer(&self, token: TimerToken) -> Result<(), UtilError> { + try!(self.channel.send_io(IoMessage::RemoveTimer { + token: token, + handler_id: self.handler, + })); + Ok(()) + } + /// Register a new IO stream. + pub fn register_stream(&self, token: StreamToken) -> Result<(), UtilError> { + try!(self.channel.send_io(IoMessage::RegisterStream { + token: token, + handler_id: self.handler, + })); + Ok(()) + } + + /// Reregister an IO stream. + pub fn update_registration(&self, token: StreamToken) -> Result<(), UtilError> { + try!(self.channel.send_io(IoMessage::UpdateStreamRegistration { + token: token, + handler_id: self.handler, + })); + Ok(()) } /// Broadcast a message to other IO clients - pub fn message(&mut self, message: Message) { - match self.event_loop.channel().send(IoMessage::UserMessage(message)) { - Ok(_) => {} - Err(e) => { panic!("Error sending io message {:?}", e); } - } + pub fn message(&self, message: Message) { + self.channel.send(message).expect("Error seding message"); } } +#[derive(Clone)] struct UserTimer { delay: u64, + timeout: Timeout, } /// Root IO handler. Manages user handlers, messages and IO timers. -pub struct IoManager where Message: Send { - timers: Slab, - handlers: Vec>>, +pub struct IoManager where Message: Send + Sync { + timers: Arc>>, + handlers: Vec>>, + _workers: Vec, + worker_channel: chase_lev::Worker>, + work_ready: Arc, } -impl IoManager where Message: Send + 'static { +impl IoManager where Message: Send + Sync + Clone + 'static { /// Creates a new instance and registers it with the event loop. pub fn start(event_loop: &mut EventLoop>) -> Result<(), UtilError> { + let (worker, stealer) = chase_lev::deque(); + let num_workers = 4; + let work_ready_mutex = Arc::new(Mutex::new(false)); + let work_ready = Arc::new(Condvar::new()); + let workers = (0..num_workers).map(|i| + Worker::new(i, stealer.clone(), IoChannel::new(event_loop.channel()), work_ready.clone(), work_ready_mutex.clone())).collect(); + let mut io = IoManager { - timers: Slab::new_starting_at(Token(USER_TIMER), MAX_USER_TIMERS), + timers: Arc::new(RwLock::new(HashMap::new())), handlers: Vec::new(), + worker_channel: worker, + _workers: workers, + work_ready: work_ready, }; try!(event_loop.run(&mut io)); Ok(()) } } -impl Handler for IoManager where Message: Send + 'static { +impl Handler for IoManager where Message: Send + Clone + Sync + 'static { type Timeout = Token; type Message = IoMessage; - fn ready(&mut self, event_loop: &mut EventLoop, token: Token, events: EventSet) { + fn ready(&mut self, _event_loop: &mut EventLoop, token: Token, events: EventSet) { + let handler_index = token.as_usize() / TOKENS_PER_HANDLER; + let token_id = token.as_usize() % TOKENS_PER_HANDLER; + if handler_index >= self.handlers.len() { + panic!("Unexpected stream token: {}", token.as_usize()); + } + let handler = self.handlers[handler_index].clone(); + if events.is_hup() { - for h in self.handlers.iter_mut() { - h.stream_hup(&mut IoContext::new(event_loop, &mut self.timers), token.as_usize()); - } - } - else if events.is_readable() { - for h in self.handlers.iter_mut() { - h.stream_readable(&mut IoContext::new(event_loop, &mut self.timers), token.as_usize()); - } - } - else if events.is_writable() { - for h in self.handlers.iter_mut() { - h.stream_writable(&mut IoContext::new(event_loop, &mut self.timers), token.as_usize()); + self.worker_channel.push(Work { work_type: WorkType::Hup, token: token_id, handler: handler.clone(), handler_id: handler_index }); + } + else { + if events.is_readable() { + self.worker_channel.push(Work { work_type: WorkType::Readable, token: token_id, handler: handler.clone(), handler_id: handler_index }); + } + if events.is_writable() { + self.worker_channel.push(Work { work_type: WorkType::Writable, token: token_id, handler: handler.clone(), handler_id: handler_index }); } } + self.work_ready.notify_all(); } fn timeout(&mut self, event_loop: &mut EventLoop, token: Token) { - match token.as_usize() { - USER_TIMER ... LAST_USER_TIMER => { - let delay = { - let timer = self.timers.get_mut(token).expect("Unknown user timer token"); - timer.delay - }; - for h in self.handlers.iter_mut() { - h.timeout(&mut IoContext::new(event_loop, &mut self.timers), token.as_usize()); - } - event_loop.timeout_ms(token, delay).expect("Error re-registering user timer"); - } - _ => { // Just pass the event down. IoHandler is supposed to re-register it if required. - for h in self.handlers.iter_mut() { - h.timeout(&mut IoContext::new(event_loop, &mut self.timers), token.as_usize()); - } - } + let handler_index = token.as_usize() / TOKENS_PER_HANDLER; + let token_id = token.as_usize() % TOKENS_PER_HANDLER; + if handler_index >= self.handlers.len() { + panic!("Unexpected timer token: {}", token.as_usize()); + } + if let Some(timer) = self.timers.read().unwrap().get(&token.as_usize()) { + event_loop.timeout_ms(token, timer.delay).expect("Error re-registering user timer"); + let handler = self.handlers[handler_index].clone(); + self.worker_channel.push(Work { work_type: WorkType::Timeout, token: token_id, handler: handler, handler_id: handler_index }); + self.work_ready.notify_all(); } } fn notify(&mut self, event_loop: &mut EventLoop, msg: Self::Message) { - let mut m = msg; - match m { + match msg { IoMessage::Shutdown => event_loop.shutdown(), - IoMessage::AddHandler { - handler, - } => { - self.handlers.push(handler); - self.handlers.last_mut().unwrap().initialize(&mut IoContext::new(event_loop, &mut self.timers)); + IoMessage::AddHandler { handler } => { + let handler_id = { + self.handlers.push(handler.clone()); + self.handlers.len() - 1 + }; + handler.initialize(&IoContext::new(IoChannel::new(event_loop.channel()), handler_id)); }, - IoMessage::UserMessage(ref mut data) => { - for h in self.handlers.iter_mut() { - h.message(&mut IoContext::new(event_loop, &mut self.timers), data); + IoMessage::AddTimer { handler_id, token, delay } => { + let timer_id = token + handler_id * TOKENS_PER_HANDLER; + let timeout = event_loop.timeout_ms(Token(timer_id), delay).expect("Error registering user timer"); + self.timers.write().unwrap().insert(timer_id, UserTimer { delay: delay, timeout: timeout }); + }, + IoMessage::RemoveTimer { handler_id, token } => { + let timer_id = token + handler_id * TOKENS_PER_HANDLER; + if let Some(timer) = self.timers.write().unwrap().remove(&timer_id) { + event_loop.clear_timeout(timer.timeout); } + }, + IoMessage::RegisterStream { handler_id, token } => { + let handler = self.handlers.get(handler_id).expect("Unknown handler id").clone(); + handler.register_stream(token, Token(token + handler_id * TOKENS_PER_HANDLER), event_loop); + }, + IoMessage::UpdateStreamRegistration { handler_id, token } => { + let handler = self.handlers.get(handler_id).expect("Unknown handler id").clone(); + handler.update_stream(token, Token(token + handler_id * TOKENS_PER_HANDLER), event_loop); + }, + IoMessage::UserMessage(data) => { + for n in 0 .. self.handlers.len() { + let handler = self.handlers[n].clone(); + self.worker_channel.push(Work { work_type: WorkType::Message(data.clone()), token: 0, handler: handler, handler_id: n }); + } + self.work_ready.notify_all(); } } } @@ -150,11 +223,19 @@ impl Handler for IoManager where Message: Send + 'static { /// Allows sending messages into the event loop. All the IO handlers will get the message /// in the `message` callback. -pub struct IoChannel where Message: Send { +pub struct IoChannel where Message: Send + Clone{ channel: Option>> } -impl IoChannel where Message: Send { +impl Clone for IoChannel where Message: Send + Clone { + fn clone(&self) -> IoChannel { + IoChannel { + channel: self.channel.clone() + } + } +} + +impl IoChannel where Message: Send + Clone { /// Send a msessage through the channel pub fn send(&self, message: Message) -> Result<(), IoError> { if let Some(ref channel) = self.channel { @@ -163,20 +244,30 @@ impl IoChannel where Message: Send { Ok(()) } + pub fn send_io(&self, message: IoMessage) -> Result<(), IoError> { + if let Some(ref channel) = self.channel { + try!(channel.send(message)) + } + Ok(()) + } /// Create a new channel to connected to event loop. pub fn disconnected() -> IoChannel { IoChannel { channel: None } } + + fn new(channel: Sender>) -> IoChannel { + IoChannel { channel: Some(channel) } + } } /// General IO Service. Starts an event loop and dispatches IO requests. /// 'Message' is a notification message type -pub struct IoService where Message: Send + 'static { +pub struct IoService where Message: Send + Sync + Clone + 'static { thread: Option>, - host_channel: Sender> + host_channel: Sender>, } -impl IoService where Message: Send + 'static { +impl IoService where Message: Send + Sync + Clone + 'static { /// Starts IO event loop pub fn start() -> Result, UtilError> { let mut event_loop = EventLoop::new().unwrap(); @@ -191,7 +282,7 @@ impl IoService where Message: Send + 'static { } /// Regiter a IO hadnler with the event loop. - pub fn register_handler(&mut self, handler: Box+Send>) -> Result<(), IoError> { + pub fn register_handler(&mut self, handler: Arc+Send>) -> Result<(), IoError> { try!(self.host_channel.send(IoMessage::AddHandler { handler: handler, })); @@ -210,7 +301,7 @@ impl IoService where Message: Send + 'static { } } -impl Drop for IoService where Message: Send { +impl Drop for IoService where Message: Send + Sync + Clone { fn drop(&mut self) { self.host_channel.send(IoMessage::Shutdown).unwrap(); self.thread.take().unwrap().join().unwrap(); diff --git a/util/src/lib.rs b/util/src/lib.rs index 4bc47e61c..2161af34f 100644 --- a/util/src/lib.rs +++ b/util/src/lib.rs @@ -51,6 +51,7 @@ extern crate crypto as rcrypto; extern crate secp256k1; extern crate arrayvec; extern crate elastic_array; +extern crate crossbeam; pub mod standard; #[macro_use] diff --git a/util/src/network/connection.rs b/util/src/network/connection.rs index f11c10384..a2c77e8cf 100644 --- a/util/src/network/connection.rs +++ b/util/src/network/connection.rs @@ -1,5 +1,5 @@ use std::collections::VecDeque; -use mio::{Handler, Token, EventSet, EventLoop, Timeout, PollOpt, TryRead, TryWrite}; +use mio::{Handler, Token, EventSet, EventLoop, PollOpt, TryRead, TryWrite}; use mio::tcp::*; use hash::*; use sha3::*; @@ -7,6 +7,7 @@ use bytes::*; use rlp::*; use std::io::{self, Cursor, Read}; use error::*; +use io::{IoContext, StreamToken}; use network::error::NetworkError; use network::handshake::Handshake; use crypto; @@ -17,11 +18,12 @@ use rcrypto::buffer::*; use tiny_keccak::Keccak; const ENCRYPTED_HEADER_LEN: usize = 32; +const RECIEVE_PAYLOAD_TIMEOUT: u64 = 30000; /// Low level tcp connection pub struct Connection { /// Connection id (token) - pub token: Token, + pub token: StreamToken, /// Network socket pub socket: TcpStream, /// Receive buffer @@ -45,14 +47,14 @@ pub enum WriteStatus { impl Connection { /// Create a new connection with given id and socket. - pub fn new(token: Token, socket: TcpStream) -> Connection { + pub fn new(token: StreamToken, socket: TcpStream) -> Connection { Connection { token: token, socket: socket, send_queue: VecDeque::new(), rec_buf: Bytes::new(), rec_size: 0, - interest: EventSet::hup(), + interest: EventSet::hup() | EventSet::readable(), } } @@ -132,20 +134,19 @@ impl Connection { } /// Register this connection with the IO event loop. - pub fn register(&mut self, event_loop: &mut EventLoop) -> io::Result<()> { - trace!(target: "net", "connection register; token={:?}", self.token); - self.interest.insert(EventSet::readable()); - event_loop.register(&self.socket, self.token, self.interest, PollOpt::edge() | PollOpt::oneshot()).or_else(|e| { - error!("Failed to register {:?}, {:?}", self.token, e); + pub fn register_socket(&self, reg: Token, event_loop: &mut EventLoop) -> io::Result<()> { + trace!(target: "net", "connection register; token={:?}", reg); + event_loop.register(&self.socket, reg, self.interest, PollOpt::edge() | PollOpt::oneshot()).or_else(|e| { + error!("Failed to register {:?}, {:?}", reg, e); Err(e) }) } /// Update connection registration. Should be called at the end of the IO handler. - pub fn reregister(&mut self, event_loop: &mut EventLoop) -> io::Result<()> { - trace!(target: "net", "connection reregister; token={:?}", self.token); - event_loop.reregister( &self.socket, self.token, self.interest, PollOpt::edge() | PollOpt::oneshot()).or_else(|e| { - error!("Failed to reregister {:?}, {:?}", self.token, e); + pub fn update_socket(&self, reg: Token, event_loop: &mut EventLoop) -> io::Result<()> { + trace!(target: "net", "connection reregister; token={:?}", reg); + event_loop.reregister( &self.socket, reg, self.interest, PollOpt::edge() | PollOpt::oneshot()).or_else(|e| { + error!("Failed to reregister {:?}, {:?}", reg, e); Err(e) }) } @@ -182,8 +183,6 @@ pub struct EncryptedConnection { ingress_mac: Keccak, /// Read state read_state: EncryptedConnectionState, - /// Disconnect timeout - idle_timeout: Option, /// Protocol id for the last received packet protocol_id: u16, /// Payload expected to be received for the last header. @@ -192,7 +191,7 @@ pub struct EncryptedConnection { impl EncryptedConnection { /// Create an encrypted connection out of the handshake. Consumes a handshake object. - pub fn new(handshake: Handshake) -> Result { + pub fn new(mut handshake: Handshake) -> Result { let shared = try!(crypto::ecdh::agree(handshake.ecdhe.secret(), &handshake.remote_public)); let mut nonce_material = H512::new(); if handshake.originated { @@ -227,6 +226,7 @@ impl EncryptedConnection { ingress_mac.update(&mac_material); ingress_mac.update(if handshake.originated { &handshake.ack_cipher } else { &handshake.auth_cipher }); + handshake.connection.expect(ENCRYPTED_HEADER_LEN); Ok(EncryptedConnection { connection: handshake.connection, encoder: encoder, @@ -235,7 +235,6 @@ impl EncryptedConnection { egress_mac: egress_mac, ingress_mac: ingress_mac, read_state: EncryptedConnectionState::Header, - idle_timeout: None, protocol_id: 0, payload_len: 0 }) @@ -337,13 +336,14 @@ impl EncryptedConnection { } /// Readable IO handler. Tracker receive status and returns decoded packet if avaialable. - pub fn readable(&mut self, event_loop: &mut EventLoop) -> Result, UtilError> { - self.idle_timeout.map(|t| event_loop.clear_timeout(t)); + pub fn readable(&mut self, io: &IoContext) -> Result, UtilError> where Message: Send + Clone{ + io.clear_timer(self.connection.token).unwrap(); match self.read_state { EncryptedConnectionState::Header => { match try!(self.connection.readable()) { Some(data) => { try!(self.read_header(&data)); + try!(io.register_timer(self.connection.token, RECIEVE_PAYLOAD_TIMEOUT)); }, None => {} }; @@ -363,24 +363,15 @@ impl EncryptedConnection { } /// Writable IO handler. Processes send queeue. - pub fn writable(&mut self, event_loop: &mut EventLoop) -> Result<(), UtilError> { - self.idle_timeout.map(|t| event_loop.clear_timeout(t)); + pub fn writable(&mut self, io: &IoContext) -> Result<(), UtilError> where Message: Send + Clone { + io.clear_timer(self.connection.token).unwrap(); try!(self.connection.writable()); Ok(()) } - /// Register this connection with the event handler. - pub fn register>(&mut self, event_loop: &mut EventLoop) -> Result<(), UtilError> { - self.connection.expect(ENCRYPTED_HEADER_LEN); - self.idle_timeout.map(|t| event_loop.clear_timeout(t)); - self.idle_timeout = event_loop.timeout_ms(self.connection.token, 1800).ok(); - try!(self.connection.reregister(event_loop)); - Ok(()) - } - /// Update connection registration. This should be called at the end of the event loop. - pub fn reregister(&mut self, event_loop: &mut EventLoop) -> Result<(), UtilError> { - try!(self.connection.reregister(event_loop)); + pub fn update_socket(&self, reg: Token, event_loop: &mut EventLoop) -> Result<(), UtilError> { + try!(self.connection.update_socket(reg, event_loop)); Ok(()) } } diff --git a/util/src/network/handshake.rs b/util/src/network/handshake.rs index ca95808b4..ea04a5462 100644 --- a/util/src/network/handshake.rs +++ b/util/src/network/handshake.rs @@ -10,6 +10,7 @@ use network::host::{HostInfo}; use network::node::NodeId; use error::*; use network::error::NetworkError; +use io::{IoContext, StreamToken}; #[derive(PartialEq, Eq, Debug)] enum HandshakeState { @@ -33,8 +34,6 @@ pub struct Handshake { state: HandshakeState, /// Outgoing or incoming connection pub originated: bool, - /// Disconnect timeout - idle_timeout: Option, /// ECDH ephemeral pub ecdhe: KeyPair, /// Connection nonce @@ -51,16 +50,16 @@ pub struct Handshake { const AUTH_PACKET_SIZE: usize = 307; const ACK_PACKET_SIZE: usize = 210; +const HANDSHAKE_TIMEOUT: u64 = 30000; impl Handshake { /// Create a new handshake object - pub fn new(token: Token, id: &NodeId, socket: TcpStream, nonce: &H256) -> Result { + pub fn new(token: StreamToken, id: &NodeId, socket: TcpStream, nonce: &H256) -> Result { Ok(Handshake { id: id.clone(), connection: Connection::new(token, socket), originated: false, state: HandshakeState::New, - idle_timeout: None, ecdhe: try!(KeyPair::create()), nonce: nonce.clone(), remote_public: Public::new(), @@ -71,8 +70,9 @@ impl Handshake { } /// Start a handhsake - pub fn start(&mut self, host: &HostInfo, originated: bool) -> Result<(), UtilError> { + pub fn start(&mut self, io: &IoContext, host: &HostInfo, originated: bool) -> Result<(), UtilError> where Message: Send + Clone{ self.originated = originated; + io.register_timer(self.connection.token, HANDSHAKE_TIMEOUT).ok(); if originated { try!(self.write_auth(host)); } @@ -89,8 +89,8 @@ impl Handshake { } /// Readable IO handler. Drives the state change. - pub fn readable(&mut self, event_loop: &mut EventLoop, host: &HostInfo) -> Result<(), UtilError> { - self.idle_timeout.map(|t| event_loop.clear_timeout(t)); + pub fn readable(&mut self, io: &IoContext, host: &HostInfo) -> Result<(), UtilError> where Message: Send + Clone { + io.clear_timer(self.connection.token).unwrap(); match self.state { HandshakeState::ReadingAuth => { match try!(self.connection.readable()) { @@ -110,29 +110,33 @@ impl Handshake { None => {} }; }, + HandshakeState::StartSession => {}, _ => { panic!("Unexpected state"); } } if self.state != HandshakeState::StartSession { - try!(self.connection.reregister(event_loop)); + try!(io.update_registration(self.connection.token)); } Ok(()) } /// Writabe IO handler. - pub fn writable(&mut self, event_loop: &mut EventLoop, _host: &HostInfo) -> Result<(), UtilError> { - self.idle_timeout.map(|t| event_loop.clear_timeout(t)); + pub fn writable(&mut self, io: &IoContext, _host: &HostInfo) -> Result<(), UtilError> where Message: Send + Clone { + io.clear_timer(self.connection.token).unwrap(); try!(self.connection.writable()); if self.state != HandshakeState::StartSession { - try!(self.connection.reregister(event_loop)); + io.update_registration(self.connection.token).unwrap(); } Ok(()) } - /// Register the IO handler with the event loop - pub fn register>(&mut self, event_loop: &mut EventLoop) -> Result<(), UtilError> { - self.idle_timeout.map(|t| event_loop.clear_timeout(t)); - self.idle_timeout = event_loop.timeout_ms(self.connection.token, 1800).ok(); - try!(self.connection.register(event_loop)); + /// Register the socket with the event loop + pub fn register_socket>(&self, reg: Token, event_loop: &mut EventLoop) -> Result<(), UtilError> { + try!(self.connection.register_socket(reg, event_loop)); + Ok(()) + } + + pub fn update_socket>(&self, reg: Token, event_loop: &mut EventLoop) -> Result<(), UtilError> { + try!(self.connection.update_socket(reg, event_loop)); Ok(()) } diff --git a/util/src/network/host.rs b/util/src/network/host.rs index 37b58f1f0..f83cb1908 100644 --- a/util/src/network/host.rs +++ b/util/src/network/host.rs @@ -1,8 +1,9 @@ -use std::mem; use std::net::{SocketAddr}; use std::collections::{HashMap}; use std::hash::{Hasher}; use std::str::{FromStr}; +use std::sync::*; +use std::ops::*; use mio::*; use mio::tcp::*; use mio::udp::*; @@ -64,14 +65,20 @@ pub type PacketId = u8; pub type ProtocolId = &'static str; /// Messages used to communitate with the event loop from other threads. -pub enum NetworkIoMessage where Message: Send { +#[derive(Clone)] +pub enum NetworkIoMessage where Message: Send + Sync + Clone { /// Register a new protocol handler. AddHandler { - handler: Option+Send>>, + handler: Arc + Sync>, protocol: ProtocolId, versions: Vec, }, - /// Send data over the network. + AddTimer { + protocol: ProtocolId, + token: TimerToken, + delay: u64, + }, + /// Send data over the network. // TODO: remove this Send { peer: PeerId, packet_id: PacketId, @@ -104,46 +111,45 @@ impl Encodable for CapabilityInfo { } /// IO access point. This is passed to all IO handlers and provides an interface to the IO subsystem. -pub struct NetworkContext<'s, 'io, Message> where Message: Send + 'static, 'io: 's { - io: &'s mut IoContext<'io, NetworkIoMessage>, +pub struct NetworkContext<'s, Message> where Message: Send + Sync + Clone + 'static, 's { + io: &'s IoContext>, protocol: ProtocolId, - connections: &'s mut Slab, - timers: &'s mut HashMap, + connections: Arc>>, session: Option, } -impl<'s, 'io, Message> NetworkContext<'s, 'io, Message> where Message: Send + 'static, { +impl<'s, Message> NetworkContext<'s, Message> where Message: Send + Sync + Clone + 'static, { /// Create a new network IO access point. Takes references to all the data that can be updated within the IO handler. - fn new(io: &'s mut IoContext<'io, NetworkIoMessage>, + fn new(io: &'s IoContext>, protocol: ProtocolId, - session: Option, connections: &'s mut Slab, - timers: &'s mut HashMap) -> NetworkContext<'s, 'io, Message> { + session: Option, connections: Arc>>) -> NetworkContext<'s, Message> { NetworkContext { io: io, protocol: protocol, session: session, connections: connections, - timers: timers, } } /// Send a packet over the network to another peer. - pub fn send(&mut self, peer: PeerId, packet_id: PacketId, data: Vec) -> Result<(), UtilError> { - match self.connections.get_mut(peer) { - Some(&mut ConnectionEntry::Session(ref mut s)) => { - s.send_packet(self.protocol, packet_id as u8, &data).unwrap_or_else(|e| { - warn!(target: "net", "Send error: {:?}", e); - }); //TODO: don't copy vector data - }, - _ => { - warn!(target: "net", "Send: Peer does not exist"); + pub fn send(&self, peer: PeerId, packet_id: PacketId, data: Vec) -> Result<(), UtilError> { + if let Some(connection) = self.connections.read().unwrap().get(peer).map(|c| c.clone()) { + match connection.lock().unwrap().deref_mut() { + &mut ConnectionEntry::Session(ref mut s) => { + s.send_packet(self.protocol, packet_id as u8, &data).unwrap_or_else(|e| { + warn!(target: "net", "Send error: {:?}", e); + }); //TODO: don't copy vector data + }, + _ => warn!(target: "net", "Send: Peer is not connected yet") } + } else { + warn!(target: "net", "Send: Peer does not exist") } Ok(()) } /// Respond to a current network message. Panics if no there is no packet in the context. - pub fn respond(&mut self, packet_id: PacketId, data: Vec) -> Result<(), UtilError> { + pub fn respond(&self, packet_id: PacketId, data: Vec) -> Result<(), UtilError> { match self.session { Some(session) => self.send(session, packet_id, data), None => { @@ -153,31 +159,31 @@ impl<'s, 'io, Message> NetworkContext<'s, 'io, Message> where Message: Send + 's } /// Disable current protocol capability for given peer. If no capabilities left peer gets disconnected. - pub fn disable_peer(&mut self, _peer: PeerId) { + pub fn disable_peer(&self, _peer: PeerId) { //TODO: remove capability, disconnect if no capabilities left } - /// Register a new IO timer. Returns a new timer token. 'NetworkProtocolHandler::timeout' will be called with the token. - pub fn register_timer(&mut self, ms: u64) -> Result{ - match self.io.register_timer(ms) { - Ok(token) => { - self.timers.insert(token, self.protocol); - Ok(token) - }, - e => e, - } + /// Register a new IO timer. 'IoHandler::timeout' will be called with the token. + pub fn register_timer(&self, token: TimerToken, ms: u64) -> Result<(), UtilError> { + self.io.message(NetworkIoMessage::AddTimer { + token: token, + delay: ms, + protocol: self.protocol, + }); + Ok(()) } /// Returns peer identification string pub fn peer_info(&self, peer: PeerId) -> String { - match self.connections.get(peer) { - Some(&ConnectionEntry::Session(ref s)) => { - s.info.client_version.clone() - }, - _ => { - "unknown".to_string() + if let Some(connection) = self.connections.read().unwrap().get(peer).map(|c| c.clone()) { + match connection.lock().unwrap().deref() { + &ConnectionEntry::Session(ref s) => { + return s.info.client_version.clone() + }, + _ => {} } } + "unknown".to_string() } } @@ -222,26 +228,35 @@ enum ConnectionEntry { Session(Session) } -/// Root IO handler. Manages protocol handlers, IO timers and network connections. -pub struct Host where Message: Send { - pub info: HostInfo, - udp_socket: UdpSocket, - listener: TcpListener, - connections: Slab, - timers: HashMap, - nodes: HashMap, - handlers: HashMap>>, +type SharedConnectionEntry = Arc>; + +#[derive(Copy, Clone)] +struct ProtocolTimer { + pub protocol: ProtocolId, + pub token: TimerToken, // Handler level token } -impl Host where Message: Send { +/// Root IO handler. Manages protocol handlers, IO timers and network connections. +pub struct Host where Message: Send + Sync + Clone { + pub info: RwLock, + udp_socket: Mutex, + tcp_listener: Mutex, + connections: Arc>>, + nodes: RwLock>, + handlers: RwLock>>>, + timers: RwLock>, + timer_counter: RwLock, +} + +impl Host where Message: Send + Sync + Clone { pub fn new() -> Host { let config = NetworkConfiguration::new(); let addr = config.listen_address; // Setup the server socket - let listener = TcpListener::bind(&addr).unwrap(); + let tcp_listener = TcpListener::bind(&addr).unwrap(); let udp_socket = UdpSocket::bound(&addr).unwrap(); - Host:: { - info: HostInfo { + let mut host = Host:: { + info: RwLock::new(HostInfo { keys: KeyPair::create().unwrap(), config: config, nonce: H256::random(), @@ -249,39 +264,64 @@ impl Host where Message: Send { client_version: "parity".to_string(), listen_port: 0, capabilities: Vec::new(), - }, - udp_socket: udp_socket, - listener: listener, - connections: Slab::new_starting_at(FIRST_CONNECTION, MAX_CONNECTIONS), - timers: HashMap::new(), - nodes: HashMap::new(), - handlers: HashMap::new(), - } + }), + udp_socket: Mutex::new(udp_socket), + tcp_listener: Mutex::new(tcp_listener), + connections: Arc::new(RwLock::new(Slab::new_starting_at(FIRST_CONNECTION, MAX_CONNECTIONS))), + nodes: RwLock::new(HashMap::new()), + handlers: RwLock::new(HashMap::new()), + timers: RwLock::new(HashMap::new()), + timer_counter: RwLock::new(LAST_CONNECTION + 1), + }; + let port = host.info.read().unwrap().config.listen_address.port(); + host.info.write().unwrap().deref_mut().listen_port = port; + + /* + match ::ifaces::Interface::get_all().unwrap().into_iter().filter(|x| x.kind == ::ifaces::Kind::Packet && x.addr.is_some()).next() { + Some(iface) => config.public_address = iface.addr.unwrap(), + None => warn!("No public network interface"), + */ + + // self.add_node("enode://a9a921de2ff09a9a4d38b623c67b2d6b477a8e654ae95d874750cbbcb31b33296496a7b4421934e2629269e180823e52c15c2b19fc59592ec51ffe4f2de76ed7@127.0.0.1:30303"); + // GO bootnodes + host.add_node("enode://a979fb575495b8d6db44f750317d0f4622bf4c2aa3365d6af7c284339968eef29b69ad0dce72a4d8db5ebb4968de0e3bec910127f134779fbcb0cb6d3331163c@52.16.188.185:30303"); // IE + host.add_node("enode://de471bccee3d042261d52e9bff31458daecc406142b401d4cd848f677479f73104b9fdeb090af9583d3391b7f10cb2ba9e26865dd5fca4fcdc0fb1e3b723c786@54.94.239.50:30303"); // BR + host.add_node("enode://1118980bf48b0a3640bdba04e0fe78b1add18e1cd99bf22d53daac1fd9972ad650df52176e7c7d89d1114cfef2bc23a2959aa54998a46afcf7d91809f0855082@52.74.57.123:30303"); // SG + // ETH/DEV cpp-ethereum (poc-9.ethdev.com) + host.add_node("enode://979b7fa28feeb35a4741660a16076f1943202cb72b6af70d327f053e248bab9ba81760f39d0701ef1d8f89cc1fbd2cacba0710a12cd5314d5e0c9021aa3637f9@5.1.83.226:30303"); + host } - fn add_node(&mut self, id: &str) { + pub fn add_node(&mut self, id: &str) { match Node::from_str(id) { Err(e) => { warn!("Could not add node: {:?}", e); }, Ok(n) => { - self.nodes.insert(n.id.clone(), n); + self.nodes.write().unwrap().insert(n.id.clone(), n); } } } - fn maintain_network(&mut self, io: &mut IoContext>) { + pub fn client_version(&self) -> String { + self.info.read().unwrap().client_version.clone() + } + + pub fn client_id(&self) -> NodeId { + self.info.read().unwrap().id().clone() + } + + fn maintain_network(&self, io: &IoContext>) { self.connect_peers(io); - io.event_loop.timeout_ms(Token(IDLE), MAINTENANCE_TIMEOUT).unwrap(); } fn have_session(&self, id: &NodeId) -> bool { - self.connections.iter().any(|e| match e { &ConnectionEntry::Session(ref s) => s.info.id.eq(&id), _ => false }) + self.connections.read().unwrap().iter().any(|e| match e.lock().unwrap().deref() { &ConnectionEntry::Session(ref s) => s.info.id.eq(&id), _ => false }) } fn connecting_to(&self, id: &NodeId) -> bool { - self.connections.iter().any(|e| match e { &ConnectionEntry::Handshake(ref h) => h.id.eq(&id), _ => false }) + self.connections.read().unwrap().iter().any(|e| match e.lock().unwrap().deref() { &ConnectionEntry::Handshake(ref h) => h.id.eq(&id), _ => false }) } - fn connect_peers(&mut self, io: &mut IoContext>) { + fn connect_peers(&self, io: &IoContext>) { struct NodeInfo { id: NodeId, peer_type: PeerType @@ -292,13 +332,14 @@ impl Host where Message: Send { let mut req_conn = 0; //TODO: use nodes from discovery here //for n in self.node_buckets.iter().flat_map(|n| &n.nodes).map(|id| NodeInfo { id: id.clone(), peer_type: self.nodes.get(id).unwrap().peer_type}) { - for n in self.nodes.values().map(|n| NodeInfo { id: n.id.clone(), peer_type: n.peer_type }) { + let pin = self.info.read().unwrap().deref().config.pin; + for n in self.nodes.read().unwrap().values().map(|n| NodeInfo { id: n.id.clone(), peer_type: n.peer_type }) { let connected = self.have_session(&n.id) || self.connecting_to(&n.id); let required = n.peer_type == PeerType::Required; if connected && required { req_conn += 1; } - else if !connected && (!self.info.config.pin || required) { + else if !connected && (!pin || required) { to_connect.push(n); } } @@ -312,8 +353,7 @@ impl Host where Message: Send { } } - if !self.info.config.pin - { + if !pin { let pending_count = 0; //TODO: let peer_count = 0; let mut open_slots = IDEAL_PEERS - peer_count - pending_count + req_conn; @@ -328,23 +368,24 @@ impl Host where Message: Send { } } - fn connect_peer(&mut self, id: &NodeId, io: &mut IoContext>) { - if self.have_session(id) - { + fn connect_peer(&self, id: &NodeId, io: &IoContext>) { + if self.have_session(id) { warn!("Aborted connect. Node already connected."); return; } - if self.connecting_to(id) - { + if self.connecting_to(id) { warn!("Aborted connect. Node already connecting."); return; } let socket = { - let node = self.nodes.get_mut(id).unwrap(); - node.last_attempted = Some(::time::now()); - - match TcpStream::connect(&node.endpoint.address) { + let address = { + let mut nodes = self.nodes.write().unwrap(); + let node = nodes.get_mut(id).unwrap(); + node.last_attempted = Some(::time::now()); + node.endpoint.address + }; + match TcpStream::connect(&address) { Ok(socket) => socket, Err(_) => { warn!("Cannot connect to node"); @@ -353,224 +394,186 @@ impl Host where Message: Send { } }; - let nonce = self.info.next_nonce(); - match self.connections.insert_with(|token| ConnectionEntry::Handshake(Handshake::new(Token(token), id, socket, &nonce).expect("Can't create handshake"))) { - Some(token) => { - match self.connections.get_mut(token) { - Some(&mut ConnectionEntry::Handshake(ref mut h)) => { - h.start(&self.info, true) - .and_then(|_| h.register(io.event_loop)) - .unwrap_or_else (|e| { - debug!(target: "net", "Handshake create error: {:?}", e); - }); - }, - _ => {} - } - }, - None => { warn!("Max connections reached") } + let nonce = self.info.write().unwrap().next_nonce(); + if self.connections.write().unwrap().insert_with(|token| { + let mut handshake = Handshake::new(token, id, socket, &nonce).expect("Can't create handshake"); + handshake.start(io, &self.info.read().unwrap(), true).and_then(|_| io.register_stream(token)).unwrap_or_else (|e| { + debug!(target: "net", "Handshake create error: {:?}", e); + }); + Arc::new(Mutex::new(ConnectionEntry::Handshake(handshake))) + }).is_none() { + warn!("Max connections reached"); } } - - fn accept(&mut self, _io: &mut IoContext>) { + fn accept(&self, _io: &IoContext>) { trace!(target: "net", "accept"); } - fn connection_writable<'s>(&'s mut self, token: StreamToken, io: &mut IoContext<'s, NetworkIoMessage>) { - let mut kill = false; + fn connection_writable(&self, token: StreamToken, io: &IoContext>) { let mut create_session = false; - match self.connections.get_mut(token) { - Some(&mut ConnectionEntry::Handshake(ref mut h)) => { - h.writable(io.event_loop, &self.info).unwrap_or_else(|e| { - debug!(target: "net", "Handshake write error: {:?}", e); - kill = true; - }); - create_session = h.done(); - }, - Some(&mut ConnectionEntry::Session(ref mut s)) => { - s.writable(io.event_loop, &self.info).unwrap_or_else(|e| { - debug!(target: "net", "Session write error: {:?}", e); - kill = true; - }); + let mut kill = false; + if let Some(connection) = self.connections.read().unwrap().get(token).map(|c| c.clone()) { + match connection.lock().unwrap().deref_mut() { + &mut ConnectionEntry::Handshake(ref mut h) => { + match h.writable(io, &self.info.read().unwrap()) { + Err(e) => { + debug!(target: "net", "Handshake write error: {:?}", e); + kill = true; + }, + Ok(_) => () + } + if h.done() { + create_session = true; + } + }, + &mut ConnectionEntry::Session(ref mut s) => { + match s.writable(io, &self.info.read().unwrap()) { + Err(e) => { + debug!(target: "net", "Session write error: {:?}", e); + kill = true; + }, + Ok(_) => () + } + io.update_registration(token).unwrap_or_else(|e| debug!(target: "net", "Session registration error: {:?}", e)); + } } - _ => { - warn!(target: "net", "Received event for unknown connection"); - } - } + } else { warn!(target: "net", "Received event for unknown connection") } if kill { - self.kill_connection(token, io); + self.kill_connection(token, io); //TODO: mark connection as dead an check in kill_connection return; } else if create_session { self.start_session(token, io); - } - match self.connections.get_mut(token) { - Some(&mut ConnectionEntry::Session(ref mut s)) => { - s.reregister(io.event_loop).unwrap_or_else(|e| debug!(target: "net", "Session registration error: {:?}", e)); - }, - _ => (), + io.update_registration(token).unwrap_or_else(|e| debug!(target: "net", "Session registration error: {:?}", e)); } } - fn connection_closed<'s>(&'s mut self, token: TimerToken, io: &mut IoContext<'s, NetworkIoMessage>) { + fn connection_closed(&self, token: TimerToken, io: &IoContext>) { self.kill_connection(token, io); } - fn connection_readable<'s>(&'s mut self, token: StreamToken, io: &mut IoContext<'s, NetworkIoMessage>) { - let mut kill = false; - let mut create_session = false; + fn connection_readable(&self, token: StreamToken, io: &IoContext>) { let mut ready_data: Vec = Vec::new(); let mut packet_data: Option<(ProtocolId, PacketId, Vec)> = None; - match self.connections.get_mut(token) { - Some(&mut ConnectionEntry::Handshake(ref mut h)) => { - h.readable(io.event_loop, &self.info).unwrap_or_else(|e| { - debug!(target: "net", "Handshake read error: {:?}", e); - kill = true; - }); - create_session = h.done(); - }, - Some(&mut ConnectionEntry::Session(ref mut s)) => { - let sd = { s.readable(io.event_loop, &self.info).unwrap_or_else(|e| { - debug!(target: "net", "Session read error: {:?}", e); - kill = true; - SessionData::None - }) }; - match sd { - SessionData::Ready => { - for (p, _) in self.handlers.iter_mut() { - if s.have_capability(p) { - ready_data.push(p); + let mut create_session = false; + let mut kill = false; + if let Some(connection) = self.connections.read().unwrap().get(token).map(|c| c.clone()) { + match connection.lock().unwrap().deref_mut() { + &mut ConnectionEntry::Handshake(ref mut h) => { + match h.readable(io, &self.info.read().unwrap()) { + Err(e) => { + debug!(target: "net", "Handshake read error: {:?}", e); + kill = true; + }, + Ok(_) => () + } + if h.done() { + create_session = true; + } + }, + &mut ConnectionEntry::Session(ref mut s) => { + match s.readable(io, &self.info.read().unwrap()) { + Err(e) => { + debug!(target: "net", "Handshake read error: {:?}", e); + kill = true; + }, + Ok(SessionData::Ready) => { + for (p, _) in self.handlers.read().unwrap().iter() { + if s.have_capability(p) { + ready_data.push(p); + } } - } - }, - SessionData::Packet { - data, - protocol, - packet_id, - } => { - match self.handlers.get_mut(protocol) { - None => { warn!(target: "net", "No handler found for protocol: {:?}", protocol) }, - Some(_) => packet_data = Some((protocol, packet_id, data)), - } - }, - SessionData::None => {}, - } - } - _ => { - warn!(target: "net", "Received event for unknown connection"); - } - } - if kill { - self.kill_connection(token, io); - return; - } - if create_session { - self.start_session(token, io); - } - for p in ready_data { - let mut h = self.handlers.get_mut(p).unwrap(); - h.connected(&mut NetworkContext::new(io, p, Some(token), &mut self.connections, &mut self.timers), &token); - } - if let Some((p, packet_id, data)) = packet_data { - let mut h = self.handlers.get_mut(p).unwrap(); - h.read(&mut NetworkContext::new(io, p, Some(token), &mut self.connections, &mut self.timers), &token, packet_id, &data[1..]); - } - - match self.connections.get_mut(token) { - Some(&mut ConnectionEntry::Session(ref mut s)) => { - s.reregister(io.event_loop).unwrap_or_else(|e| debug!(target: "net", "Session registration error: {:?}", e)); - }, - _ => (), - } - } - - fn start_session(&mut self, token: StreamToken, io: &mut IoContext>) { - let info = &self.info; - // TODO: use slab::replace_with (currently broken) - /* - match self.connections.remove(token) { - Some(ConnectionEntry::Handshake(h)) => { - match Session::new(h, io.event_loop, info) { - Ok(session) => { - assert!(token == self.connections.insert(ConnectionEntry::Session(session)).ok().unwrap()); - }, - Err(e) => { - debug!(target: "net", "Session construction error: {:?}", e); + }, + Ok(SessionData::Packet { + data, + protocol, + packet_id, + }) => { + match self.handlers.read().unwrap().get(protocol) { + None => { warn!(target: "net", "No handler found for protocol: {:?}", protocol) }, + Some(_) => packet_data = Some((protocol, packet_id, data)), + } + }, + Ok(SessionData::None) => {}, } } - }, - _ => panic!("Error updating slab with session") - }*/ - self.connections.replace_with(token, |c| { - match c { - ConnectionEntry::Handshake(h) => Session::new(h, io.event_loop, info) - .map(|s| Some(ConnectionEntry::Session(s))) - .unwrap_or_else(|e| { - debug!(target: "net", "Session construction error: {:?}", e); - None - }), - _ => { panic!("No handshake to create a session from"); } } - }).expect("Error updating slab with session"); + } else { + warn!(target: "net", "Received event for unknown connection"); + } + if kill { + self.kill_connection(token, io); //TODO: mark connection as dead an check in kill_connection + return; + } else if create_session { + self.start_session(token, io); + io.update_registration(token).unwrap_or_else(|e| debug!(target: "net", "Session registration error: {:?}", e)); + } + for p in ready_data { + let h = self.handlers.read().unwrap().get(p).unwrap().clone(); + h.connected(&mut NetworkContext::new(io, p, Some(token), self.connections.clone()), &token); + } + if let Some((p, packet_id, data)) = packet_data { + let h = self.handlers.read().unwrap().get(p).unwrap().clone(); + h.read(&mut NetworkContext::new(io, p, Some(token), self.connections.clone()), &token, packet_id, &data[1..]); + } + io.update_registration(token).unwrap_or_else(|e| debug!(target: "net", "Token registration error: {:?}", e)); } - fn connection_timeout<'s>(&'s mut self, token: StreamToken, io: &mut IoContext<'s, NetworkIoMessage>) { + fn start_session(&self, token: StreamToken, io: &IoContext>) { + self.connections.write().unwrap().replace_with(token, |c| { + match Arc::try_unwrap(c).ok().unwrap().into_inner().unwrap() { + ConnectionEntry::Handshake(h) => { + let session = Session::new(h, io, &self.info.read().unwrap()).expect("Session creation error"); + io.update_registration(token).expect("Error updating session registration"); + Some(Arc::new(Mutex::new(ConnectionEntry::Session(session)))) + }, + _ => { None } // handshake expired + } + }).ok(); + } + + fn connection_timeout(&self, token: StreamToken, io: &IoContext>) { self.kill_connection(token, io) } - fn kill_connection<'s>(&'s mut self, token: StreamToken, io: &mut IoContext<'s, NetworkIoMessage>) { + fn kill_connection(&self, token: StreamToken, io: &IoContext>) { let mut to_disconnect: Vec = Vec::new(); - let mut remove = true; - match self.connections.get_mut(token) { - Some(&mut ConnectionEntry::Handshake(_)) => (), // just abandon handshake - Some(&mut ConnectionEntry::Session(ref mut s)) if s.is_ready() => { - for (p, _) in self.handlers.iter_mut() { - if s.have_capability(p) { - to_disconnect.push(p); - } + { + let mut connections = self.connections.write().unwrap(); + if let Some(connection) = connections.get(token).map(|c| c.clone()) { + match connection.lock().unwrap().deref_mut() { + &mut ConnectionEntry::Handshake(_) => { + connections.remove(token); + }, + &mut ConnectionEntry::Session(ref mut s) if s.is_ready() => { + for (p, _) in self.handlers.read().unwrap().iter() { + if s.have_capability(p) { + to_disconnect.push(p); + } + } + connections.remove(token); + }, + _ => {}, } - }, - _ => { - remove = false; - }, + } } for p in to_disconnect { - let mut h = self.handlers.get_mut(p).unwrap(); - h.disconnected(&mut NetworkContext::new(io, p, Some(token), &mut self.connections, &mut self.timers), &token); - } - if remove { - self.connections.remove(token); + let h = self.handlers.read().unwrap().get(p).unwrap().clone(); + h.disconnected(&mut NetworkContext::new(io, p, Some(token), self.connections.clone()), &token); } } } -impl IoHandler> for Host where Message: Send + 'static { +impl IoHandler> for Host where Message: Send + Sync + Clone + 'static { /// Initialize networking - fn initialize(&mut self, io: &mut IoContext>) { - /* - match ::ifaces::Interface::get_all().unwrap().into_iter().filter(|x| x.kind == ::ifaces::Kind::Packet && x.addr.is_some()).next() { - Some(iface) => config.public_address = iface.addr.unwrap(), - None => warn!("No public network interface"), - */ - - // Start listening for incoming connections - io.event_loop.register(&self.listener, Token(TCP_ACCEPT), EventSet::readable(), PollOpt::edge()).unwrap(); - io.event_loop.timeout_ms(Token(IDLE), MAINTENANCE_TIMEOUT).unwrap(); - // open the udp socket - io.event_loop.register(&self.udp_socket, Token(NODETABLE_RECEIVE), EventSet::readable(), PollOpt::edge()).unwrap(); - io.event_loop.timeout_ms(Token(NODETABLE_MAINTAIN), 7200).unwrap(); - let port = self.info.config.listen_address.port(); - self.info.listen_port = port; - - self.add_node("enode://a9a921de2ff09a9a4d38b623c67b2d6b477a8e654ae95d874750cbbcb31b33296496a7b4421934e2629269e180823e52c15c2b19fc59592ec51ffe4f2de76ed7@127.0.0.1:30303"); -/* // GO bootnodes - self.add_node("enode://a979fb575495b8d6db44f750317d0f4622bf4c2aa3365d6af7c284339968eef29b69ad0dce72a4d8db5ebb4968de0e3bec910127f134779fbcb0cb6d3331163c@52.16.188.185:30303"); // IE - self.add_node("enode://de471bccee3d042261d52e9bff31458daecc406142b401d4cd848f677479f73104b9fdeb090af9583d3391b7f10cb2ba9e26865dd5fca4fcdc0fb1e3b723c786@54.94.239.50:30303"); // BR - self.add_node("enode://1118980bf48b0a3640bdba04e0fe78b1add18e1cd99bf22d53daac1fd9972ad650df52176e7c7d89d1114cfef2bc23a2959aa54998a46afcf7d91809f0855082@52.74.57.123:30303"); // SG - // ETH/DEV cpp-ethereum (poc-9.ethdev.com) - self.add_node("enode://979b7fa28feeb35a4741660a16076f1943202cb72b6af70d327f053e248bab9ba81760f39d0701ef1d8f89cc1fbd2cacba0710a12cd5314d5e0c9021aa3637f9@5.1.83.226:30303");*/ + fn initialize(&self, io: &IoContext>) { + io.register_stream(TCP_ACCEPT).expect("Error registering TCP listener"); + io.register_stream(NODETABLE_RECEIVE).expect("Error registering UDP listener"); + io.register_timer(IDLE, MAINTENANCE_TIMEOUT).expect("Error registering Network idle timer"); + //io.register_timer(NODETABLE_MAINTAIN, 7200); } - fn stream_hup<'s>(&'s mut self, io: &mut IoContext<'s, NetworkIoMessage>, stream: StreamToken) { + fn stream_hup(&self, io: &IoContext>, stream: StreamToken) { trace!(target: "net", "Hup: {}", stream); match stream { FIRST_CONNECTION ... LAST_CONNECTION => self.connection_closed(stream, io), @@ -578,7 +581,7 @@ impl IoHandler> for Host where Messa }; } - fn stream_readable<'s>(&'s mut self, io: &mut IoContext<'s, NetworkIoMessage>, stream: StreamToken) { + fn stream_readable(&self, io: &IoContext>, stream: StreamToken) { match stream { FIRST_CONNECTION ... LAST_CONNECTION => self.connection_readable(stream, io), NODETABLE_RECEIVE => {}, @@ -587,65 +590,114 @@ impl IoHandler> for Host where Messa } } - fn stream_writable<'s>(&'s mut self, io: &mut IoContext<'s, NetworkIoMessage>, stream: StreamToken) { + fn stream_writable(&self, io: &IoContext>, stream: StreamToken) { match stream { FIRST_CONNECTION ... LAST_CONNECTION => self.connection_writable(stream, io), + NODETABLE_RECEIVE => {}, _ => panic!("Received unknown writable token"), } } - fn timeout<'s>(&'s mut self, io: &mut IoContext<'s, NetworkIoMessage>, token: TimerToken) { + fn timeout(&self, io: &IoContext>, token: TimerToken) { match token { IDLE => self.maintain_network(io), FIRST_CONNECTION ... LAST_CONNECTION => self.connection_timeout(token, io), NODETABLE_DISCOVERY => {}, NODETABLE_MAINTAIN => {}, - _ => match self.timers.get_mut(&token).map(|p| *p) { - Some(protocol) => match self.handlers.get_mut(protocol) { - None => { warn!(target: "net", "No handler found for protocol: {:?}", protocol) }, - Some(h) => { h.timeout(&mut NetworkContext::new(io, protocol, Some(token), &mut self.connections, &mut self.timers), token); } - }, - None => {} // time not registerd through us + _ => match self.timers.read().unwrap().get(&token).map(|p| *p) { + Some(timer) => match self.handlers.read().unwrap().get(timer.protocol).map(|h| h.clone()) { + None => { warn!(target: "net", "No handler found for protocol: {:?}", timer.protocol) }, + Some(h) => { h.timeout(&NetworkContext::new(io, timer.protocol, None, self.connections.clone()), timer.token); } + }, + None => { warn!("Unknown timer token: {}", token); } // timer is not registerd through us } } } - fn message<'s>(&'s mut self, io: &mut IoContext<'s, NetworkIoMessage>, message: &'s mut NetworkIoMessage) { + fn message(&self, io: &IoContext>, message: &NetworkIoMessage) { match message { - &mut NetworkIoMessage::AddHandler { - ref mut handler, + &NetworkIoMessage::AddHandler { + ref handler, ref protocol, ref versions } => { - let mut h = mem::replace(handler, None).unwrap(); - h.initialize(&mut NetworkContext::new(io, protocol, None, &mut self.connections, &mut self.timers)); - self.handlers.insert(protocol, h); + let h = handler.clone(); + h.initialize(&NetworkContext::new(io, protocol, None, self.connections.clone())); + self.handlers.write().unwrap().insert(protocol, h); + let mut info = self.info.write().unwrap(); for v in versions { - self.info.capabilities.push(CapabilityInfo { protocol: protocol, version: *v, packet_count:0 }); + info.capabilities.push(CapabilityInfo { protocol: protocol, version: *v, packet_count:0 }); } }, - &mut NetworkIoMessage::Send { + &NetworkIoMessage::AddTimer { + ref protocol, + ref delay, + ref token, + } => { + let handler_token = { + let mut timer_counter = self.timer_counter.write().unwrap(); + let counter = timer_counter.deref_mut(); + let handler_token = *counter; + *counter += 1; + handler_token + }; + self.timers.write().unwrap().insert(handler_token, ProtocolTimer { protocol: protocol, token: *token }); + io.register_timer(handler_token, *delay).expect("Error registering timer"); + }, + &NetworkIoMessage::Send { ref peer, ref packet_id, ref protocol, ref data, } => { - match self.connections.get_mut(*peer as usize) { - Some(&mut ConnectionEntry::Session(ref mut s)) => { - s.send_packet(protocol, *packet_id as u8, &data).unwrap_or_else(|e| { - warn!(target: "net", "Send error: {:?}", e); - }); //TODO: don't copy vector data - }, - _ => { - warn!(target: "net", "Send: Peer does not exist"); + if let Some(connection) = self.connections.read().unwrap().get(*peer).map(|c| c.clone()) { + match connection.lock().unwrap().deref_mut() { + &mut ConnectionEntry::Session(ref mut s) => { + s.send_packet(protocol, *packet_id as u8, &data).unwrap_or_else(|e| { + warn!(target: "net", "Send error: {:?}", e); + }); //TODO: don't copy vector data + }, + _ => { warn!(target: "net", "Send: Peer session not exist"); } } - } + } else { warn!(target: "net", "Send: Peer does not exist"); } }, - &mut NetworkIoMessage::User(ref message) => { - for (p, h) in self.handlers.iter_mut() { - h.message(&mut NetworkContext::new(io, p, None, &mut self.connections, &mut self.timers), &message); + &NetworkIoMessage::User(ref message) => { + for (p, h) in self.handlers.read().unwrap().iter() { + h.message(&mut NetworkContext::new(io, p, None, self.connections.clone()), &message); } } } } + + fn register_stream(&self, stream: StreamToken, reg: Token, event_loop: &mut EventLoop>>) { + match stream { + FIRST_CONNECTION ... LAST_CONNECTION => { + if let Some(connection) = self.connections.read().unwrap().get(stream).map(|c| c.clone()) { + match connection.lock().unwrap().deref() { + &ConnectionEntry::Handshake(ref h) => h.register_socket(reg, event_loop).expect("Error registering socket"), + _ => warn!("Unexpected session stream registration") + } + } else { warn!("Unexpected stream registration")} + } + NODETABLE_RECEIVE => event_loop.register(self.udp_socket.lock().unwrap().deref(), Token(NODETABLE_RECEIVE), EventSet::all(), PollOpt::edge()).expect("Error registering stream"), + TCP_ACCEPT => event_loop.register(self.tcp_listener.lock().unwrap().deref(), Token(TCP_ACCEPT), EventSet::all(), PollOpt::edge()).expect("Error registering stream"), + _ => warn!("Unexpected stream regitration") + } + } + + fn update_stream(&self, stream: StreamToken, reg: Token, event_loop: &mut EventLoop>>) { + match stream { + FIRST_CONNECTION ... LAST_CONNECTION => { + if let Some(connection) = self.connections.read().unwrap().get(stream).map(|c| c.clone()) { + match connection.lock().unwrap().deref() { + &ConnectionEntry::Handshake(ref h) => h.update_socket(reg, event_loop).expect("Error updating socket"), + &ConnectionEntry::Session(ref s) => s.update_socket(reg, event_loop).expect("Error updating socket"), + } + } else { warn!("Unexpected stream update")} + } + NODETABLE_RECEIVE => event_loop.reregister(self.udp_socket.lock().unwrap().deref(), Token(NODETABLE_RECEIVE), EventSet::all(), PollOpt::edge()).expect("Error reregistering stream"), + TCP_ACCEPT => event_loop.reregister(self.tcp_listener.lock().unwrap().deref(), Token(TCP_ACCEPT), EventSet::all(), PollOpt::edge()).expect("Error reregistering stream"), + _ => warn!("Unexpected stream update") + } + } } diff --git a/util/src/network/mod.rs b/util/src/network/mod.rs index a47e88927..6facaf704 100644 --- a/util/src/network/mod.rs +++ b/util/src/network/mod.rs @@ -56,31 +56,31 @@ mod service; mod error; mod node; -pub type PeerId = host::PeerId; -pub type PacketId = host::PacketId; -pub type NetworkContext<'s,'io, Message> = host::NetworkContext<'s, 'io, Message>; -pub type NetworkService = service::NetworkService; -pub type NetworkIoMessage = host::NetworkIoMessage; +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::host::NetworkIoMessage::User as UserMessage; -pub type NetworkError = error::NetworkError; +pub use network::error::NetworkError; -use io::*; +use io::TimerToken; /// 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: Send where Message: Send { +pub trait NetworkProtocolHandler: Sync + Send where Message: Send + Sync + Clone { /// Initialize the handler - fn initialize(&mut self, _io: &mut NetworkContext) {} + fn initialize(&self, _io: &NetworkContext) {} /// Called when new network packet received. - fn read(&mut self, io: &mut NetworkContext, peer: &PeerId, packet_id: u8, data: &[u8]); + 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(&mut self, io: &mut NetworkContext, peer: &PeerId); + fn connected(&self, io: &NetworkContext, peer: &PeerId); /// Called when a previously connected peer disconnects. - fn disconnected(&mut self, io: &mut NetworkContext, peer: &PeerId); + fn disconnected(&self, io: &NetworkContext, peer: &PeerId); /// Timer function called after a timeout created with `NetworkContext::timeout`. - fn timeout(&mut self, _io: &mut NetworkContext, _timer: TimerToken) {} + fn timeout(&self, _io: &NetworkContext, _timer: TimerToken) {} /// Called when a broadcasted message is received. The message can only be sent from a different IO handler. - fn message(&mut self, _io: &mut NetworkContext, _message: &Message) {} + fn message(&self, _io: &NetworkContext, _message: &Message) {} } diff --git a/util/src/network/service.rs b/util/src/network/service.rs index 4c333b8af..1318737a3 100644 --- a/util/src/network/service.rs +++ b/util/src/network/service.rs @@ -1,3 +1,4 @@ +use std::sync::*; use error::*; use network::{NetworkProtocolHandler}; use network::error::{NetworkError}; @@ -6,18 +7,18 @@ use io::*; /// IO Service with networking /// `Message` defines a notification data type. -pub struct NetworkService where Message: Send + 'static { +pub struct NetworkService where Message: Send + Sync + Clone + 'static { io_service: IoService>, host_info: String, } -impl NetworkService where Message: Send + 'static { +impl NetworkService where Message: Send + Sync + Clone + 'static { /// Starts IO event loop pub fn start() -> Result, UtilError> { let mut io_service = try!(IoService::>::start()); - let host = Box::new(Host::new()); - let host_info = host.info.client_version.clone(); - info!("NetworkService::start(): id={:?}", host.info.id()); + let host = Arc::new(Host::new()); + let host_info = host.client_version(); + info!("NetworkService::start(): id={:?}", host.client_id()); try!(io_service.register_handler(host)); Ok(NetworkService { io_service: io_service, @@ -37,9 +38,9 @@ impl NetworkService where Message: Send + 'static { } /// Regiter a new protocol handler with the event loop. - pub fn register_protocol(&mut self, handler: Box+Send>, protocol: ProtocolId, versions: &[u8]) -> Result<(), NetworkError> { + pub fn register_protocol(&mut self, handler: Arc+Send + Sync>, protocol: ProtocolId, versions: &[u8]) -> Result<(), NetworkError> { try!(self.io_service.send_message(NetworkIoMessage::AddHandler { - handler: Some(handler), + handler: handler, protocol: protocol, versions: versions.to_vec(), })); @@ -55,7 +56,5 @@ impl NetworkService where Message: Send + 'static { pub fn io(&mut self) -> &mut IoService> { &mut self.io_service } - - } diff --git a/util/src/network/session.rs b/util/src/network/session.rs index 828e4b062..5722ffde4 100644 --- a/util/src/network/session.rs +++ b/util/src/network/session.rs @@ -4,6 +4,7 @@ use rlp::*; use network::connection::{EncryptedConnection, Packet}; use network::handshake::Handshake; use error::*; +use io::{IoContext}; use network::error::{NetworkError, DisconnectReason}; use network::host::*; use network::node::NodeId; @@ -84,7 +85,7 @@ const PACKET_LAST: u8 = 0x7f; impl Session { /// Create a new session out of comepleted handshake. Consumes handshake object. - pub fn new>(h: Handshake, event_loop: &mut EventLoop, host: &HostInfo) -> Result { + pub fn new(h: Handshake, _io: &IoContext, host: &HostInfo) -> Result where Message: Send + Sync + Clone { let id = h.id.clone(); let connection = try!(EncryptedConnection::new(h)); let mut session = Session { @@ -99,7 +100,6 @@ impl Session { }; try!(session.write_hello(host)); try!(session.write_ping()); - try!(session.connection.register(event_loop)); Ok(session) } @@ -109,16 +109,16 @@ impl Session { } /// Readable IO handler. Returns packet data if available. - pub fn readable(&mut self, event_loop: &mut EventLoop, host: &HostInfo) -> Result { - match try!(self.connection.readable(event_loop)) { + pub fn readable(&mut self, io: &IoContext, host: &HostInfo) -> Result where Message: Send + Sync + Clone { + match try!(self.connection.readable(io)) { Some(data) => Ok(try!(self.read_packet(data, host))), None => Ok(SessionData::None) } } /// Writable IO handler. Sends pending packets. - pub fn writable(&mut self, event_loop: &mut EventLoop, _host: &HostInfo) -> Result<(), UtilError> { - self.connection.writable(event_loop) + pub fn writable(&mut self, io: &IoContext, _host: &HostInfo) -> Result<(), UtilError> where Message: Send + Sync + Clone { + self.connection.writable(io) } /// Checks if peer supports given capability @@ -127,8 +127,8 @@ impl Session { } /// Update registration with the event loop. Should be called at the end of the IO handler. - pub fn reregister(&mut self, event_loop: &mut EventLoop) -> Result<(), UtilError> { - self.connection.reregister(event_loop) + pub fn update_socket(&self, reg:Token, event_loop: &mut EventLoop) -> Result<(), UtilError> { + self.connection.update_socket(reg, event_loop) } /// Send a protocol packet to peer.