p2p: fix for changes in core serialization API

This commit is contained in:
Merope Riddle 2016-11-01 02:03:12 +00:00
parent 630c4eb6fb
commit 565374bac7
2 changed files with 31 additions and 39 deletions

View file

@ -51,19 +51,15 @@ impl Handshake {
// send the first part of the handshake
let sender_addr = conn.local_addr().unwrap();
let receiver_addr = conn.peer_addr().unwrap();
let opt_err = serialize(&mut conn,
&Hand {
version: PROTOCOL_VERSION,
capabilities: FULL_SYNC,
nonce: nonce,
sender_addr: SockAddr(sender_addr),
receiver_addr: SockAddr(receiver_addr),
user_agent: USER_AGENT.to_string(),
});
match opt_err {
Some(err) => return Err(err),
None => {}
}
try!(serialize(&mut conn,
&Hand {
version: PROTOCOL_VERSION,
capabilities: FULL_SYNC,
nonce: nonce,
sender_addr: SockAddr(sender_addr),
receiver_addr: SockAddr(receiver_addr),
user_agent: USER_AGENT.to_string(),
}));
// deserialize the handshake response and do version negotiation
let shake = try!(deserialize::<Shake>(&mut conn));
@ -127,16 +123,12 @@ impl Handshake {
};
// send our reply with our info
let opt_err = serialize(&mut conn,
&Shake {
version: PROTOCOL_VERSION,
capabilities: FULL_SYNC,
user_agent: USER_AGENT.to_string(),
});
match opt_err {
Some(err) => return Err(err),
None => {}
}
try!(serialize(&mut conn,
&Shake {
version: PROTOCOL_VERSION,
capabilities: FULL_SYNC,
user_agent: USER_AGENT.to_string(),
}));
info!("Received connection from peer {:?}", peer_info);
// when more than one protocol version is supported, choosing should go here

View file

@ -18,7 +18,7 @@ use std::net::*;
use num::FromPrimitive;
use core::ser::{self, Writeable, Readable, Writer, Reader, Error};
use core::ser::{self, Writeable, Readable, Writer, Reader};
use types::*;
@ -72,12 +72,12 @@ impl MsgHeader {
}
impl Writeable for MsgHeader {
fn write(&self, writer: &mut Writer) -> Option<ser::Error> {
fn write(&self, writer: &mut Writer) -> Result<(), ser::Error> {
ser_multiwrite!(writer,
[write_u8, self.magic[0]],
[write_u8, self.magic[1]],
[write_u8, self.msg_type as u8]);
None
Ok(())
}
}
@ -111,14 +111,14 @@ pub struct Hand {
}
impl Writeable for Hand {
fn write(&self, writer: &mut Writer) -> Option<ser::Error> {
fn write(&self, writer: &mut Writer) -> Result<(), ser::Error> {
ser_multiwrite!(writer,
[write_u32, self.version],
[write_u32, self.capabilities.bits()],
[write_u64, self.nonce]);
self.sender_addr.write(writer);
self.receiver_addr.write(writer);
writer.write_vec(&mut self.user_agent.clone().into_bytes())
writer.write_bytes(self.user_agent.as_bytes())
}
}
@ -153,12 +153,12 @@ pub struct Shake {
}
impl Writeable for Shake {
fn write(&self, writer: &mut Writer) -> Option<ser::Error> {
fn write(&self, writer: &mut Writer) -> Result<(), ser::Error> {
ser_multiwrite!(writer,
[write_u32, self.version],
[write_u32, self.capabilities.bits()],
[write_vec, &mut self.user_agent.as_bytes().to_vec()]);
None
[write_bytes, self.user_agent.as_bytes()]);
Ok(())
}
}
@ -233,11 +233,11 @@ pub struct PeerError {
}
impl Writeable for PeerError {
fn write(&self, writer: &mut Writer) -> Option<ser::Error> {
fn write(&self, writer: &mut Writer) -> Result<(), ser::Error> {
ser_multiwrite!(writer,
[write_u32, self.code],
[write_vec, &mut self.message.clone().into_bytes()]);
None
[write_bytes, self.message.as_bytes()]);
Ok(())
}
}
@ -258,7 +258,7 @@ impl Readable<PeerError> for PeerError {
pub struct SockAddr(pub SocketAddr);
impl Writeable for SockAddr {
fn write(&self, writer: &mut Writer) -> Option<ser::Error> {
fn write(&self, writer: &mut Writer) -> Result<(), ser::Error> {
match self.0 {
SocketAddr::V4(sav4) => {
ser_multiwrite!(writer,
@ -267,14 +267,14 @@ impl Writeable for SockAddr {
[write_u16, sav4.port()]);
}
SocketAddr::V6(sav6) => {
try_o!(writer.write_u8(1));
try!(writer.write_u8(1));
for seg in &sav6.ip().segments() {
try_o!(writer.write_u16(*seg));
try!(writer.write_u16(*seg));
}
try_o!(writer.write_u16(sav6.port()));
try!(writer.write_u16(sav6.port()));
}
}
None
Ok(())
}
}