2019-01-07 11:33:07 +01:00
|
|
|
// Copyright 2015-2019 Parity Technologies (UK) Ltd.
|
|
|
|
// This file is part of Parity Ethereum.
|
2016-02-05 13:40:41 +01:00
|
|
|
|
2019-01-07 11:33:07 +01:00
|
|
|
// Parity Ethereum is free software: you can redistribute it and/or modify
|
2016-02-05 13:40:41 +01:00
|
|
|
// 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.
|
|
|
|
|
2019-01-07 11:33:07 +01:00
|
|
|
// Parity Ethereum is distributed in the hope that it will be useful,
|
2016-02-05 13:40:41 +01:00
|
|
|
// 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
|
2019-01-07 11:33:07 +01:00
|
|
|
// along with Parity Ethereum. If not, see <http://www.gnu.org/licenses/>.
|
2016-02-05 13:40:41 +01:00
|
|
|
|
2016-02-01 15:22:42 +01:00
|
|
|
//! General IO module.
|
|
|
|
//!
|
2016-02-01 16:19:59 +01:00
|
|
|
//! Example usage for creating a network service and adding an IO handler:
|
2016-02-01 15:22:42 +01:00
|
|
|
//!
|
|
|
|
//! ```rust
|
2016-08-05 10:32:04 +02:00
|
|
|
//! extern crate ethcore_io;
|
|
|
|
//! use ethcore_io::*;
|
|
|
|
//! use std::sync::Arc;
|
2018-04-14 21:35:58 +02:00
|
|
|
//! use std::time::Duration;
|
2016-02-01 15:22:42 +01:00
|
|
|
//!
|
|
|
|
//! struct MyHandler;
|
|
|
|
//!
|
|
|
|
//! #[derive(Clone)]
|
|
|
|
//! struct MyMessage {
|
|
|
|
//! data: u32
|
|
|
|
//! }
|
|
|
|
//!
|
2016-02-01 16:20:46 +01:00
|
|
|
//! impl IoHandler<MyMessage> for MyHandler {
|
|
|
|
//! fn initialize(&self, io: &IoContext<MyMessage>) {
|
2018-04-14 21:35:58 +02:00
|
|
|
//! io.register_timer(0, Duration::from_secs(1)).unwrap();
|
2016-02-10 12:50:27 +01:00
|
|
|
//! }
|
2016-02-01 15:22:42 +01:00
|
|
|
//!
|
2016-02-10 12:50:27 +01:00
|
|
|
//! fn timeout(&self, _io: &IoContext<MyMessage>, timer: TimerToken) {
|
|
|
|
//! println!("Timeout {}", timer);
|
|
|
|
//! }
|
2016-02-01 16:20:46 +01:00
|
|
|
//!
|
2016-02-10 12:50:27 +01:00
|
|
|
//! fn message(&self, _io: &IoContext<MyMessage>, message: &MyMessage) {
|
|
|
|
//! println!("Message {}", message.data);
|
|
|
|
//! }
|
2016-02-01 16:20:46 +01:00
|
|
|
//! }
|
|
|
|
//!
|
2016-02-01 15:22:42 +01:00
|
|
|
//! fn main () {
|
|
|
|
//! let mut service = IoService::<MyMessage>::start().expect("Error creating network service");
|
|
|
|
//! service.register_handler(Arc::new(MyHandler)).unwrap();
|
|
|
|
//!
|
|
|
|
//! // Wait for quit condition
|
|
|
|
//! // ...
|
|
|
|
//! // Drop the service
|
|
|
|
//! }
|
|
|
|
//! ```
|
2018-05-10 12:34:36 +02:00
|
|
|
//!
|
|
|
|
//! # Mio vs non-mio
|
|
|
|
//!
|
|
|
|
//! This library has two modes: mio and not mio. The `mio` feature can be activated or deactivated
|
|
|
|
//! when compiling or depending on the library.
|
|
|
|
//!
|
|
|
|
//! Without mio, only timers and message-passing are available. With mio, you can also use
|
|
|
|
//! low-level sockets provided by mio.
|
|
|
|
//!
|
|
|
|
//! The non-mio mode exists because the `mio` library doesn't compile on platforms such as
|
|
|
|
//! emscripten.
|
2016-08-05 10:32:04 +02:00
|
|
|
|
2017-06-05 20:40:40 +02:00
|
|
|
//TODO: use Poll from mio
|
|
|
|
#![allow(deprecated)]
|
|
|
|
|
2018-05-10 12:34:36 +02:00
|
|
|
#[cfg(feature = "mio")]
|
|
|
|
mod service_mio;
|
|
|
|
#[cfg(not(feature = "mio"))]
|
|
|
|
mod service_non_mio;
|
|
|
|
#[cfg(feature = "mio")]
|
2016-01-21 16:48:37 +01:00
|
|
|
mod worker;
|
|
|
|
|
2018-05-10 12:34:36 +02:00
|
|
|
use std::cell::Cell;
|
2017-11-13 14:37:08 +01:00
|
|
|
use std::{fmt, error};
|
2018-05-10 12:34:36 +02:00
|
|
|
#[cfg(feature = "mio")]
|
2016-10-30 09:56:34 +01:00
|
|
|
use mio::deprecated::{EventLoop, NotifyError};
|
2018-05-10 12:34:36 +02:00
|
|
|
#[cfg(feature = "mio")]
|
2017-11-13 14:37:08 +01:00
|
|
|
use mio::Token;
|
2016-01-12 17:33:40 +01:00
|
|
|
|
2018-05-10 12:34:36 +02:00
|
|
|
thread_local! {
|
|
|
|
/// Stack size
|
|
|
|
/// Should be modified if it is changed in Rust since it is no way
|
|
|
|
/// to know or get it
|
|
|
|
pub static LOCAL_STACK_SIZE: Cell<usize> = Cell::new(::std::env::var("RUST_MIN_STACK").ok().and_then(|s| s.parse().ok()).unwrap_or(2 * 1024 * 1024));
|
|
|
|
}
|
2016-10-06 18:42:54 +02:00
|
|
|
|
2016-01-12 17:33:40 +01:00
|
|
|
#[derive(Debug)]
|
2016-02-03 16:43:48 +01:00
|
|
|
/// IO Error
|
2016-01-12 17:33:40 +01:00
|
|
|
pub enum IoError {
|
2016-02-03 16:43:48 +01:00
|
|
|
/// Low level error from mio crate
|
2018-05-10 12:34:36 +02:00
|
|
|
#[cfg(feature = "mio")]
|
2016-01-12 17:33:40 +01:00
|
|
|
Mio(::std::io::Error),
|
2016-08-05 10:32:04 +02:00
|
|
|
/// Error concerning the Rust standard library's IO subsystem.
|
|
|
|
StdIo(::std::io::Error),
|
2016-01-12 17:33:40 +01:00
|
|
|
}
|
|
|
|
|
2016-05-21 00:12:51 +02:00
|
|
|
impl fmt::Display for IoError {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
// just defer to the std implementation for now.
|
|
|
|
// we can refine the formatting when more variants are added.
|
|
|
|
match *self {
|
2018-05-10 12:34:36 +02:00
|
|
|
#[cfg(feature = "mio")]
|
2016-08-05 10:32:04 +02:00
|
|
|
IoError::Mio(ref std_err) => std_err.fmt(f),
|
|
|
|
IoError::StdIo(ref std_err) => std_err.fmt(f),
|
2016-05-21 00:12:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-13 14:37:08 +01:00
|
|
|
impl error::Error for IoError {
|
|
|
|
fn description(&self) -> &str {
|
|
|
|
"IO error"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-05 10:32:04 +02:00
|
|
|
impl From<::std::io::Error> for IoError {
|
|
|
|
fn from(err: ::std::io::Error) -> IoError {
|
|
|
|
IoError::StdIo(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-10 12:34:36 +02:00
|
|
|
#[cfg(feature = "mio")]
|
|
|
|
impl<Message> From<NotifyError<service_mio::IoMessage<Message>>> for IoError where Message: Send {
|
|
|
|
fn from(_err: NotifyError<service_mio::IoMessage<Message>>) -> IoError {
|
2016-01-12 17:33:40 +01:00
|
|
|
IoError::Mio(::std::io::Error::new(::std::io::ErrorKind::ConnectionAborted, "Network IO notification error"))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-10 12:50:27 +01:00
|
|
|
/// Generic IO handler.
|
2016-01-13 11:31:37 +01:00
|
|
|
/// All the handler function are called from within IO event loop.
|
|
|
|
/// `Message` type is used as notification data
|
2018-05-09 08:49:34 +02:00
|
|
|
pub trait IoHandler<Message>: Send + Sync where Message: Send + Sync + 'static {
|
2016-01-13 11:31:37 +01:00
|
|
|
/// Initialize the handler
|
2016-01-21 16:48:37 +01:00
|
|
|
fn initialize(&self, _io: &IoContext<Message>) {}
|
2016-01-13 11:31:37 +01:00
|
|
|
/// Timer function called after a timeout created with `HandlerIo::timeout`.
|
2016-01-21 16:48:37 +01:00
|
|
|
fn timeout(&self, _io: &IoContext<Message>, _timer: TimerToken) {}
|
2016-01-13 11:31:37 +01:00
|
|
|
/// Called when a broadcasted message is received. The message can only be sent from a different IO handler.
|
2016-01-21 16:48:37 +01:00
|
|
|
fn message(&self, _io: &IoContext<Message>, _message: &Message) {}
|
2016-01-13 11:31:37 +01:00
|
|
|
/// Called when an IO stream gets closed
|
2018-05-10 12:34:36 +02:00
|
|
|
#[cfg(feature = "mio")]
|
2016-01-21 16:48:37 +01:00
|
|
|
fn stream_hup(&self, _io: &IoContext<Message>, _stream: StreamToken) {}
|
2016-02-10 12:50:27 +01:00
|
|
|
/// Called when an IO stream can be read from
|
2018-05-10 12:34:36 +02:00
|
|
|
#[cfg(feature = "mio")]
|
2016-01-21 16:48:37 +01:00
|
|
|
fn stream_readable(&self, _io: &IoContext<Message>, _stream: StreamToken) {}
|
2016-01-13 11:31:37 +01:00
|
|
|
/// Called when an IO stream can be written to
|
2018-05-10 12:34:36 +02:00
|
|
|
#[cfg(feature = "mio")]
|
2016-01-21 16:48:37 +01:00
|
|
|
fn stream_writable(&self, _io: &IoContext<Message>, _stream: StreamToken) {}
|
|
|
|
/// Register a new stream with the event loop
|
2018-05-10 12:34:36 +02:00
|
|
|
#[cfg(feature = "mio")]
|
2016-01-21 16:48:37 +01:00
|
|
|
fn register_stream(&self, _stream: StreamToken, _reg: Token, _event_loop: &mut EventLoop<IoManager<Message>>) {}
|
|
|
|
/// Re-register a stream with the event loop
|
2018-05-10 12:34:36 +02:00
|
|
|
#[cfg(feature = "mio")]
|
2016-01-21 16:48:37 +01:00
|
|
|
fn update_stream(&self, _stream: StreamToken, _reg: Token, _event_loop: &mut EventLoop<IoManager<Message>>) {}
|
2019-08-08 09:41:22 +02:00
|
|
|
/// Deregister a stream. Called when a stream is removed from the event loop
|
2018-05-10 12:34:36 +02:00
|
|
|
#[cfg(feature = "mio")]
|
2016-01-22 18:13:59 +01:00
|
|
|
fn deregister_stream(&self, _stream: StreamToken, _event_loop: &mut EventLoop<IoManager<Message>>) {}
|
2016-01-13 11:31:37 +01:00
|
|
|
}
|
|
|
|
|
2018-05-10 12:34:36 +02:00
|
|
|
#[cfg(feature = "mio")]
|
|
|
|
pub use service_mio::{TimerToken, StreamToken, IoContext, IoService, IoChannel, IoManager, TOKENS_PER_HANDLER};
|
|
|
|
#[cfg(not(feature = "mio"))]
|
2019-08-08 09:41:22 +02:00
|
|
|
pub use crate::service_non_mio::{TimerToken, IoContext, IoService, IoChannel, TOKENS_PER_HANDLER};
|
2016-01-12 17:33:40 +01:00
|
|
|
|
2016-01-13 13:56:48 +01:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2019-08-08 09:41:22 +02:00
|
|
|
use std::{
|
|
|
|
sync::{Arc, atomic},
|
|
|
|
thread,
|
|
|
|
time::Duration,
|
|
|
|
};
|
2016-08-05 10:32:04 +02:00
|
|
|
use super::*;
|
2016-01-13 13:56:48 +01:00
|
|
|
|
2018-05-11 13:45:07 +02:00
|
|
|
// Mio's behaviour is too unstable for this test. Sometimes we have to wait a few milliseconds,
|
|
|
|
// sometimes more than 5 seconds for the message to arrive.
|
|
|
|
// Therefore we ignore this test in order to not have spurious failure when running continuous
|
|
|
|
// integration.
|
2018-05-10 12:34:36 +02:00
|
|
|
#[test]
|
2018-05-11 13:45:07 +02:00
|
|
|
#[cfg_attr(feature = "mio", ignore)]
|
2018-05-10 12:34:36 +02:00
|
|
|
fn send_message_to_handler() {
|
|
|
|
struct MyHandler(atomic::AtomicBool);
|
2016-01-13 13:56:48 +01:00
|
|
|
|
2018-05-10 12:34:36 +02:00
|
|
|
#[derive(Clone)]
|
|
|
|
struct MyMessage {
|
|
|
|
data: u32
|
|
|
|
}
|
2016-01-13 13:56:48 +01:00
|
|
|
|
2018-05-10 12:34:36 +02:00
|
|
|
impl IoHandler<MyMessage> for MyHandler {
|
|
|
|
fn message(&self, _io: &IoContext<MyMessage>, message: &MyMessage) {
|
|
|
|
assert_eq!(message.data, 5);
|
|
|
|
self.0.store(true, atomic::Ordering::SeqCst);
|
|
|
|
}
|
2016-01-13 13:56:48 +01:00
|
|
|
}
|
|
|
|
|
2018-05-10 12:34:36 +02:00
|
|
|
let handler = Arc::new(MyHandler(atomic::AtomicBool::new(false)));
|
|
|
|
|
|
|
|
let service = IoService::<MyMessage>::start().expect("Error creating network service");
|
|
|
|
service.register_handler(handler.clone()).unwrap();
|
|
|
|
|
|
|
|
service.send_message(MyMessage { data: 5 }).unwrap();
|
|
|
|
|
2018-05-11 13:45:07 +02:00
|
|
|
thread::sleep(Duration::from_secs(1));
|
2018-05-10 12:34:36 +02:00
|
|
|
assert!(handler.0.load(atomic::Ordering::SeqCst));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn timeout_working() {
|
|
|
|
struct MyHandler(atomic::AtomicBool);
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
struct MyMessage {
|
|
|
|
data: u32
|
2016-01-13 13:56:48 +01:00
|
|
|
}
|
|
|
|
|
2018-05-10 12:34:36 +02:00
|
|
|
impl IoHandler<MyMessage> for MyHandler {
|
|
|
|
fn initialize(&self, io: &IoContext<MyMessage>) {
|
|
|
|
io.register_timer_once(1234, Duration::from_millis(500)).unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
fn timeout(&self, _io: &IoContext<MyMessage>, timer: TimerToken) {
|
|
|
|
assert_eq!(timer, 1234);
|
|
|
|
assert!(!self.0.swap(true, atomic::Ordering::SeqCst));
|
|
|
|
}
|
2016-01-13 13:56:48 +01:00
|
|
|
}
|
2018-05-10 12:34:36 +02:00
|
|
|
|
|
|
|
let handler = Arc::new(MyHandler(atomic::AtomicBool::new(false)));
|
|
|
|
|
|
|
|
let service = IoService::<MyMessage>::start().expect("Error creating network service");
|
|
|
|
service.register_handler(handler.clone()).unwrap();
|
|
|
|
|
|
|
|
thread::sleep(Duration::from_secs(2));
|
|
|
|
assert!(handler.0.load(atomic::Ordering::SeqCst));
|
2016-01-13 13:56:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2018-05-10 12:34:36 +02:00
|
|
|
fn multi_timeout_working() {
|
|
|
|
struct MyHandler(atomic::AtomicUsize);
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
struct MyMessage {
|
|
|
|
data: u32
|
|
|
|
}
|
|
|
|
|
|
|
|
impl IoHandler<MyMessage> for MyHandler {
|
|
|
|
fn initialize(&self, io: &IoContext<MyMessage>) {
|
|
|
|
io.register_timer(1234, Duration::from_millis(500)).unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
fn timeout(&self, _io: &IoContext<MyMessage>, timer: TimerToken) {
|
|
|
|
assert_eq!(timer, 1234);
|
|
|
|
self.0.fetch_add(1, atomic::Ordering::SeqCst);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let handler = Arc::new(MyHandler(atomic::AtomicUsize::new(0)));
|
|
|
|
|
2016-06-17 18:26:54 +02:00
|
|
|
let service = IoService::<MyMessage>::start().expect("Error creating network service");
|
2018-05-10 12:34:36 +02:00
|
|
|
service.register_handler(handler.clone()).unwrap();
|
|
|
|
|
|
|
|
thread::sleep(Duration::from_secs(2));
|
|
|
|
assert!(handler.0.load(atomic::Ordering::SeqCst) >= 2);
|
2016-01-13 13:56:48 +01:00
|
|
|
}
|
|
|
|
}
|