X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=src%2Fpeer.rs;h=8e1c5e9eddb37ab768cae3f1c14ed4c87530f868;hb=0d474196b752d00a80209535b60c23d35a492745;hp=95028ed5f4bb3e68c97db5724b424a1ac2d71848;hpb=1e90008ac857dbf642addc27cf92ef4d7c76b51d;p=dnsseed-rust diff --git a/src/peer.rs b/src/peer.rs index 95028ed..8e1c5e9 100644 --- a/src/peer.rs +++ b/src/peer.rs @@ -18,7 +18,6 @@ use tokio::timer::Delay; use futures::sync::mpsc; use crate::printer::Printer; -use crate::timeout_stream::TimeoutStream; struct BytesCoder<'a>(&'a mut bytes::BytesMut); impl<'a> std::io::Write for BytesCoder<'a> { @@ -46,9 +45,9 @@ impl<'a> std::io::Read for BytesDecoder<'a> { struct MsgCoder<'a>(&'a Printer); impl<'a> codec::Decoder for MsgCoder<'a> { type Item = NetworkMessage; - type Error = std::io::Error; + type Error = encode::Error; - fn decode(&mut self, bytes: &mut bytes::BytesMut) -> Result, std::io::Error> { + fn decode(&mut self, bytes: &mut bytes::BytesMut) -> Result, encode::Error> { let mut decoder = BytesDecoder { buf: bytes, pos: 0 @@ -59,19 +58,24 @@ impl<'a> codec::Decoder for MsgCoder<'a> { if res.magic == Network::Bitcoin.magic() { Ok(Some(res.payload)) } else { - Err(std::io::Error::new(std::io::ErrorKind::InvalidData, "bad net magic")) + Err(encode::Error::UnexpectedNetworkMagic { + expected: Network::Bitcoin.magic(), + actual: res.magic + }) } }, Err(e) => match e { encode::Error::Io(_) => Ok(None), - encode::Error::UnrecognizedNetworkCommand(_msg) => { + encode::Error::UnrecognizedNetworkCommand(ref msg) => { decoder.buf.advance(decoder.pos); //XXX(fixthese): self.0.add_line(format!("rust-bitcoin doesn't support {}!", msg), true); - Ok(None) + if msg == "gnop" { + Err(e) + } else { Ok(None) } }, _ => { self.0.add_line(format!("Error decoding message: {:?}", e), true); - Err(std::io::Error::new(std::io::ErrorKind::InvalidData, e)) + Err(e) }, } } @@ -92,12 +96,9 @@ impl<'a> codec::Encoder for MsgCoder<'a> { } } -pub struct Peer { - -} - +pub struct Peer {} impl Peer { - pub fn new(addr: SocketAddr, timeout: Duration, printer: &'static Printer) -> impl Future, impl Stream)> { + pub fn new(addr: SocketAddr, timeout: Duration, printer: &'static Printer) -> impl Future, impl Stream)> { let connect_timeout = Delay::new(Instant::now() + timeout.clone()).then(|_| { future::err(std::io::Error::new(std::io::ErrorKind::TimedOut, "timeout reached")) }); @@ -122,9 +123,9 @@ impl Peer { nonce: 0xdeadbeef, user_agent: "/rust-bitcoin:0.18/bluematt-tokio-client:0.1/".to_string(), start_height: 0, - relay: true, + relay: false, })); - future::ok((sender, TimeoutStream::new(read, timeout))) + future::ok((sender, read)) }) } }