Removed obsolete code and added documentation
This commit is contained in:
parent
6728690109
commit
ccf1cc4d54
@ -20,6 +20,7 @@ time = "0.1"
|
||||
evmjit = { path = "rust-evmjit", optional = true }
|
||||
ethash = { path = "ethash" }
|
||||
num_cpus = "0.2"
|
||||
ctrlc = "1.0"
|
||||
|
||||
[features]
|
||||
jit = ["evmjit"]
|
||||
|
@ -3,11 +3,12 @@ extern crate ethcore;
|
||||
extern crate rustc_serialize;
|
||||
extern crate log;
|
||||
extern crate env_logger;
|
||||
extern crate ctrlc;
|
||||
|
||||
use std::io::stdin;
|
||||
use std::env;
|
||||
use log::{LogLevelFilter};
|
||||
use env_logger::LogBuilder;
|
||||
use ctrlc::CtrlC;
|
||||
use util::*;
|
||||
use ethcore::client::*;
|
||||
use ethcore::service::{ClientService, NetSyncMessage};
|
||||
@ -31,13 +32,15 @@ fn main() {
|
||||
let mut service = ClientService::start(spec).unwrap();
|
||||
let io_handler = Arc::new(ClientIoHandler { client: service.client(), info: Default::default() });
|
||||
service.io().register_handler(io_handler).expect("Error registering IO handler");
|
||||
loop {
|
||||
let mut cmd = String::new();
|
||||
stdin().read_line(&mut cmd).unwrap();
|
||||
if cmd == "quit\n" || cmd == "exit\n" || cmd == "q\n" {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
let exit = Arc::new(Condvar::new());
|
||||
let e = exit.clone();
|
||||
CtrlC::set_handler(move || {
|
||||
e.notify_all();
|
||||
});
|
||||
|
||||
let mutex = Mutex::new(());
|
||||
let _ = exit.wait(mutex.lock().unwrap()).unwrap();
|
||||
}
|
||||
|
||||
struct Informant {
|
||||
|
@ -10,11 +10,14 @@ use arrayvec::*;
|
||||
use crossbeam::sync::chase_lev;
|
||||
use io::worker::{Worker, Work, WorkType};
|
||||
|
||||
/// Timer ID
|
||||
pub type TimerToken = usize;
|
||||
/// Timer ID
|
||||
pub type StreamToken = usize;
|
||||
/// IO Hadndler ID
|
||||
pub type HandlerId = usize;
|
||||
|
||||
// Tokens
|
||||
/// Maximum number of tokens a handler can use
|
||||
pub const TOKENS_PER_HANDLER: usize = 16384;
|
||||
|
||||
/// Messages used to communicate with the event loop from other threads.
|
||||
@ -49,8 +52,8 @@ pub enum IoMessage<Message> where Message: Send + Clone + Sized {
|
||||
|
||||
/// IO access point. This is passed to all IO handlers and provides an interface to the IO subsystem.
|
||||
pub struct IoContext<Message> where Message: Send + Clone + 'static {
|
||||
pub channel: IoChannel<Message>,
|
||||
pub handler: HandlerId,
|
||||
channel: IoChannel<Message>,
|
||||
handler: HandlerId,
|
||||
}
|
||||
|
||||
impl<Message> IoContext<Message> where Message: Send + Clone + 'static {
|
||||
@ -249,6 +252,7 @@ impl<Message> IoChannel<Message> where Message: Send + Clone {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Send low level io message
|
||||
pub fn send_io(&self, message: IoMessage<Message>) -> Result<(), IoError> {
|
||||
if let Some(ref channel) = self.channel {
|
||||
try!(channel.send(message))
|
||||
|
@ -19,11 +19,17 @@ pub enum DisconnectReason
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
/// Network error.
|
||||
pub enum NetworkError {
|
||||
/// Authentication error.
|
||||
Auth,
|
||||
/// Unrecognised protocol.
|
||||
BadProtocol,
|
||||
/// Peer not found.
|
||||
PeerNotFound,
|
||||
/// Peer is diconnected.
|
||||
Disconnect(DisconnectReason),
|
||||
/// Socket IO error.
|
||||
Io(IoError),
|
||||
}
|
||||
|
||||
|
@ -69,22 +69,22 @@ pub type ProtocolId = &'static str;
|
||||
pub enum NetworkIoMessage<Message> where Message: Send + Sync + Clone {
|
||||
/// Register a new protocol handler.
|
||||
AddHandler {
|
||||
/// Handler shared instance.
|
||||
handler: Arc<NetworkProtocolHandler<Message> + Sync>,
|
||||
/// Protocol Id.
|
||||
protocol: ProtocolId,
|
||||
/// Supported protocol versions.
|
||||
versions: Vec<u8>,
|
||||
},
|
||||
/// Register a new protocol timer
|
||||
AddTimer {
|
||||
/// Protocol Id.
|
||||
protocol: ProtocolId,
|
||||
/// Timer token.
|
||||
token: TimerToken,
|
||||
/// Timer delay in milliseconds.
|
||||
delay: u64,
|
||||
},
|
||||
/// Send data over the network. // TODO: remove this
|
||||
Send {
|
||||
peer: PeerId,
|
||||
packet_id: PacketId,
|
||||
protocol: ProtocolId,
|
||||
data: Vec<u8>,
|
||||
},
|
||||
/// User message
|
||||
User(Message),
|
||||
}
|
||||
@ -644,23 +644,6 @@ impl<Message> IoHandler<NetworkIoMessage<Message>> for Host<Message> where Messa
|
||||
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,
|
||||
} => {
|
||||
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"); }
|
||||
},
|
||||
&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);
|
||||
|
@ -2,7 +2,7 @@ use std::sync::*;
|
||||
use error::*;
|
||||
use network::{NetworkProtocolHandler};
|
||||
use network::error::{NetworkError};
|
||||
use network::host::{Host, NetworkIoMessage, PeerId, PacketId, ProtocolId};
|
||||
use network::host::{Host, NetworkIoMessage, ProtocolId};
|
||||
use io::*;
|
||||
|
||||
/// IO Service with networking
|
||||
@ -26,17 +26,6 @@ impl<Message> NetworkService<Message> where Message: Send + Sync + Clone + 'stat
|
||||
})
|
||||
}
|
||||
|
||||
/// Send a message over the network. Normaly `HostIo::send` should be used. This can be used from non-io threads.
|
||||
pub fn send(&mut self, peer: &PeerId, packet_id: PacketId, protocol: ProtocolId, data: &[u8]) -> Result<(), NetworkError> {
|
||||
try!(self.io_service.send_message(NetworkIoMessage::Send {
|
||||
peer: *peer,
|
||||
packet_id: packet_id,
|
||||
protocol: protocol,
|
||||
data: data.to_vec()
|
||||
}));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Regiter a new protocol handler with the event loop.
|
||||
pub fn register_protocol(&mut self, handler: Arc<NetworkProtocolHandler<Message>+Send + Sync>, protocol: ProtocolId, versions: &[u8]) -> Result<(), NetworkError> {
|
||||
try!(self.io_service.send_message(NetworkIoMessage::AddHandler {
|
||||
|
Loading…
Reference in New Issue
Block a user