use tokio_io::AsyncRead in p2p lib

This commit is contained in:
debris 2017-03-25 16:05:49 +07:00
parent fd930306f6
commit acb1cbdfd2
8 changed files with 27 additions and 16 deletions

7
Cargo.lock generated
View File

@ -699,6 +699,7 @@ dependencies = [
"serialization 0.1.0",
"time 0.1.36 (registry+https://github.com/rust-lang/crates.io-index)",
"tokio-core 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)",
"tokio-io 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
@ -1108,12 +1109,12 @@ dependencies = [
"mio 0.6.6 (registry+https://github.com/rust-lang/crates.io-index)",
"scoped-tls 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
"slab 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
"tokio-io 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
"tokio-io 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "tokio-io"
version = "0.1.0"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"bytes 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)",
@ -1319,7 +1320,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
"checksum thread_local 0.2.7 (registry+https://github.com/rust-lang/crates.io-index)" = "8576dbbfcaef9641452d5cf0df9b0e7eeab7694956dd33bb61515fb8f18cfdd5"
"checksum time 0.1.36 (registry+https://github.com/rust-lang/crates.io-index)" = "211b63c112206356ef1ff9b19355f43740fc3f85960c598a93d3a3d3ba7beade"
"checksum tokio-core 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "99e958104a67877907c1454386d5482fe8e965a55d60be834a15a44328e7dc76"
"checksum tokio-io 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "6a278fde45f1be68e44995227d426aaa4841e0980bb0a21b981092f28c3c8473"
"checksum tokio-io 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "48f55df1341bb92281f229a6030bc2abffde2c7a44c6d6b802b7687dd8be0775"
"checksum unicase 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "13a5906ca2b98c799f4b1ab4557b76367ebd6ae5ef14930ec841c74aed5f3764"
"checksum unicode-bidi 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)" = "d3a078ebdd62c0e71a709c3d53d2af693fe09fe93fbff8344aebe289b78f9032"
"checksum unicode-normalization 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = "e28fa37426fceeb5cf8f41ee273faa7c82c47dc8fba5853402841e665fcd86ff"

View File

@ -4,7 +4,8 @@ version = "0.1.0"
authors = ["debris <marek.kotewicz@gmail.com>"]
[dependencies]
tokio-core = "0.1"
tokio-core = "0.1.6"
tokio-io = "0.1.1"
parking_lot = "0.3"
futures = "0.1"
futures-cpupool = "0.1"

View File

@ -1,11 +1,12 @@
use std::{io, cmp};
use futures::{Future, Poll, Async};
use tokio_io::AsyncRead;
use message::{Message, MessageResult, Error};
use message::types::{Version, Verack};
use network::Magic;
use io::{write_message, WriteMessage, ReadMessage, read_message};
pub fn handshake<A>(a: A, magic: Magic, version: Version, min_version: u32) -> Handshake<A> where A: io::Write + io::Read {
pub fn handshake<A>(a: A, magic: Magic, version: Version, min_version: u32) -> Handshake<A> where A: io::Write + AsyncRead {
Handshake {
version: version.version(),
state: HandshakeState::SendVersion(write_message(a, version_message(magic, version))),
@ -14,7 +15,7 @@ pub fn handshake<A>(a: A, magic: Magic, version: Version, min_version: u32) -> H
}
}
pub fn accept_handshake<A>(a: A, magic: Magic, version: Version, min_version: u32) -> AcceptHandshake<A> where A: io::Write + io::Read {
pub fn accept_handshake<A>(a: A, magic: Magic, version: Version, min_version: u32) -> AcceptHandshake<A> where A: io::Write + AsyncRead {
AcceptHandshake {
version: version.version(),
state: AcceptHandshakeState::ReceiveVersion {
@ -84,7 +85,7 @@ pub struct AcceptHandshake<A> {
min_version: u32,
}
impl<A> Future for Handshake<A> where A: io::Read + io::Write {
impl<A> Future for Handshake<A> where A: AsyncRead + io::Write {
type Item = (A, MessageResult<HandshakeResult>);
type Error = io::Error;
@ -135,7 +136,7 @@ impl<A> Future for Handshake<A> where A: io::Read + io::Write {
}
}
impl<A> Future for AcceptHandshake<A> where A: io::Read + io::Write {
impl<A> Future for AcceptHandshake<A> where A: AsyncRead + io::Write {
type Item = (A, MessageResult<HandshakeResult>);
type Error = io::Error;

View File

@ -1,13 +1,14 @@
use std::io;
use futures::{Future, Poll, Async};
use tokio_core::io::{read_exact, ReadExact};
use tokio_io::io::{read_exact, ReadExact};
use tokio_io::AsyncRead;
use crypto::checksum;
use network::Magic;
use message::{Error, MessageHeader, MessageResult, Command};
use bytes::Bytes;
use io::{read_header, ReadHeader};
pub fn read_any_message<A>(a: A, magic: Magic) -> ReadAnyMessage<A> where A: io::Read {
pub fn read_any_message<A>(a: A, magic: Magic) -> ReadAnyMessage<A> where A: AsyncRead {
ReadAnyMessage {
state: ReadAnyMessageState::ReadHeader(read_header(a, magic)),
}
@ -26,7 +27,7 @@ pub struct ReadAnyMessage<A> {
state: ReadAnyMessageState<A>,
}
impl<A> Future for ReadAnyMessage<A> where A: io::Read {
impl<A> Future for ReadAnyMessage<A> where A: AsyncRead {
type Item = MessageResult<(Command, Bytes)>;
type Error = io::Error;

View File

@ -1,10 +1,12 @@
use std::io;
use futures::{Future, Poll, Async};
use tokio_core::io::{ReadExact, read_exact};
//use tokio_core::io::{ReadExact, read_exact};
use tokio_io::AsyncRead;
use tokio_io::io::{ReadExact, read_exact};
use message::{MessageHeader, MessageResult};
use network::Magic;
pub fn read_header<A>(a: A, magic: Magic) -> ReadHeader<A> where A: io::Read {
pub fn read_header<A>(a: A, magic: Magic) -> ReadHeader<A> where A: AsyncRead {
ReadHeader {
reader: read_exact(a, [0u8; 24]),
magic: magic,
@ -16,7 +18,7 @@ pub struct ReadHeader<A> {
magic: Magic,
}
impl<A> Future for ReadHeader<A> where A: io::Read {
impl<A> Future for ReadHeader<A> where A: AsyncRead {
type Item = (A, MessageResult<MessageHeader>);
type Error = io::Error;

View File

@ -1,12 +1,13 @@
use std::io;
use std::marker::PhantomData;
use futures::{Poll, Future, Async};
use tokio_io::AsyncRead;
use network::Magic;
use message::{MessageResult, Error, Payload};
use io::{read_header, ReadHeader, read_payload, ReadPayload};
pub fn read_message<M, A>(a: A, magic: Magic, version: u32) -> ReadMessage<M, A>
where A: io::Read, M: Payload {
where A: AsyncRead, M: Payload {
ReadMessage {
state: ReadMessageState::ReadHeader {
version: version,
@ -32,7 +33,7 @@ pub struct ReadMessage<M, A> {
message_type: PhantomData<M>,
}
impl<M, A> Future for ReadMessage<M, A> where A: io::Read, M: Payload {
impl<M, A> Future for ReadMessage<M, A> where A: AsyncRead, M: Payload {
type Item = (A, MessageResult<M>);
type Error = io::Error;

View File

@ -1,6 +1,7 @@
use std::sync::Arc;
use std::net::Shutdown;
use std::io::{Read, Write, Error};
use tokio_io::AsyncRead;
use tokio_core::net::TcpStream;
pub struct SharedTcpStream {
@ -32,6 +33,8 @@ impl Read for SharedTcpStream {
}
}
impl AsyncRead for SharedTcpStream {}
impl Write for SharedTcpStream {
fn write(&mut self, buf: &[u8]) -> Result<usize, Error> {
Write::write(&mut (&*self.io as &TcpStream), buf)

View File

@ -4,6 +4,7 @@ extern crate futures_cpupool;
extern crate rand;
extern crate time;
extern crate tokio_core;
extern crate tokio_io;
extern crate parking_lot;
#[macro_use]
extern crate log;