From 9bf17b0ab4bd1d1930f22f4d252c9a079c32522e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joakim=20Frosteg=C3=A5rd?= Date: Sat, 11 Apr 2020 15:03:50 +0200 Subject: [PATCH] bittorrent_udp: response_to_bytes: return Result<(), io:Error> --- aquatic/src/lib/network.rs | 2 +- .../src/bin/bench_handlers/announce.rs | 2 +- .../src/bin/bench_handlers/connect.rs | 2 +- .../src/bin/bench_handlers/scrape.rs | 2 +- bittorrent_udp/src/converters/responses.rs | 52 +++++++++---------- 5 files changed, 28 insertions(+), 32 deletions(-) diff --git a/aquatic/src/lib/network.rs b/aquatic/src/lib/network.rs index 264fd76..7942f34 100644 --- a/aquatic/src/lib/network.rs +++ b/aquatic/src/lib/network.rs @@ -209,7 +209,7 @@ fn handle_readable_socket( for (response, src) in responses.drain(..) { cursor.set_position(0); - response_to_bytes(&mut cursor, response, IpVersion::IPv4); + response_to_bytes(&mut cursor, response, IpVersion::IPv4).unwrap(); let amt = cursor.position() as usize; diff --git a/aquatic_bench/src/bin/bench_handlers/announce.rs b/aquatic_bench/src/bin/bench_handlers/announce.rs index 2b4a037..6c42a94 100644 --- a/aquatic_bench/src/bin/bench_handlers/announce.rs +++ b/aquatic_bench/src/bin/bench_handlers/announce.rs @@ -60,7 +60,7 @@ pub fn bench( cursor.set_position(0); - response_to_bytes(&mut cursor, response, IpVersion::IPv4); + response_to_bytes(&mut cursor, response, IpVersion::IPv4).unwrap(); dummy ^= cursor.get_ref()[0]; } diff --git a/aquatic_bench/src/bin/bench_handlers/connect.rs b/aquatic_bench/src/bin/bench_handlers/connect.rs index dcabc1e..b48d8d2 100644 --- a/aquatic_bench/src/bin/bench_handlers/connect.rs +++ b/aquatic_bench/src/bin/bench_handlers/connect.rs @@ -50,7 +50,7 @@ pub fn bench( cursor.set_position(0); - response_to_bytes(&mut cursor, response, IpVersion::IPv4); + response_to_bytes(&mut cursor, response, IpVersion::IPv4).unwrap(); dummy ^= cursor.get_ref()[0]; } diff --git a/aquatic_bench/src/bin/bench_handlers/scrape.rs b/aquatic_bench/src/bin/bench_handlers/scrape.rs index aa70a85..4e056d9 100644 --- a/aquatic_bench/src/bin/bench_handlers/scrape.rs +++ b/aquatic_bench/src/bin/bench_handlers/scrape.rs @@ -55,7 +55,7 @@ pub fn bench( cursor.set_position(0); - response_to_bytes(&mut cursor, response, IpVersion::IPv4); + response_to_bytes(&mut cursor, response, IpVersion::IPv4).unwrap(); dummy ^= cursor.get_ref()[0]; } diff --git a/bittorrent_udp/src/converters/responses.rs b/bittorrent_udp/src/converters/responses.rs index 2b0e6d4..bacfc34 100644 --- a/bittorrent_udp/src/converters/responses.rs +++ b/bittorrent_udp/src/converters/responses.rs @@ -12,61 +12,57 @@ pub fn response_to_bytes( bytes: &mut impl Write, response: Response, ip_version: IpVersion -){ +) -> Result<(), io::Error> { match response { Response::Connect(r) => { - bytes.write_i32::(0).unwrap(); - bytes.write_i32::(r.transaction_id.0).unwrap(); - bytes.write_i64::(r.connection_id.0).unwrap(); + bytes.write_i32::(0)?; + bytes.write_i32::(r.transaction_id.0)?; + bytes.write_i64::(r.connection_id.0)?; }, - Response::Announce(r) => { - bytes.write_i32::(1).unwrap(); - bytes.write_i32::(r.transaction_id.0).unwrap(); - bytes.write_i32::(r.announce_interval.0).unwrap(); - bytes.write_i32::(r.leechers.0).unwrap(); - bytes.write_i32::(r.seeders.0).unwrap(); + bytes.write_i32::(1)?; + bytes.write_i32::(r.transaction_id.0)?; + bytes.write_i32::(r.announce_interval.0)?; + bytes.write_i32::(r.leechers.0)?; + bytes.write_i32::(r.seeders.0)?; // Write peer IPs and ports. Silently ignore peers with wrong // IP version if ip_version == IpVersion::IPv4 { for peer in r.peers { if let IpAddr::V4(ip) = peer.ip_address { - bytes.write_all(&ip.octets()).unwrap(); - bytes.write_u16::(peer.port.0).unwrap(); + bytes.write_all(&ip.octets())?; + bytes.write_u16::(peer.port.0)?; } } } else { for peer in r.peers { if let IpAddr::V6(ip) = peer.ip_address { - bytes.write_all(&ip.octets()).unwrap(); - bytes.write_u16::(peer.port.0).unwrap(); + bytes.write_all(&ip.octets())?; + bytes.write_u16::(peer.port.0)?; } } } }, - Response::Scrape(r) => { - bytes.write_i32::(2).unwrap(); - bytes.write_i32::(r.transaction_id.0).unwrap(); + bytes.write_i32::(2)?; + bytes.write_i32::(r.transaction_id.0)?; for torrent_stat in r.torrent_stats { - bytes.write_i32::(torrent_stat.seeders.0) - .unwrap(); - bytes.write_i32::(torrent_stat.completed.0) - .unwrap(); - bytes.write_i32::(torrent_stat.leechers.0) - .unwrap(); + bytes.write_i32::(torrent_stat.seeders.0)?; + bytes.write_i32::(torrent_stat.completed.0)?; + bytes.write_i32::(torrent_stat.leechers.0)?; } }, - Response::Error(r) => { - bytes.write_i32::(3).unwrap(); - bytes.write_i32::(r.transaction_id.0).unwrap(); + bytes.write_i32::(3)?; + bytes.write_i32::(r.transaction_id.0)?; - bytes.write_all(r.message.as_bytes()).unwrap(); + bytes.write_all(r.message.as_bytes())?; }, } + + Ok(()) } @@ -186,7 +182,7 @@ mod tests { ) -> bool { let mut buf = Vec::new(); - response_to_bytes(&mut buf, response.clone(), ip_version); + response_to_bytes(&mut buf, response.clone(), ip_version).unwrap(); let r2 = response_from_bytes(&buf[..], ip_version).unwrap(); let success = response == r2;