From 1c599728348963771a9bf0803238267ef3ec6102 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joakim=20Frosteg=C3=A5rd?= Date: Mon, 29 Jan 2024 19:38:12 +0100 Subject: [PATCH] udp protocol: rename "write" and "from_bytes" methods --- crates/udp/src/common.rs | 2 +- crates/udp/src/workers/socket/mio.rs | 4 +-- .../src/workers/socket/uring/recv_helper.rs | 2 +- .../src/workers/socket/uring/send_buffers.rs | 2 +- crates/udp/tests/common/mod.rs | 4 +-- crates/udp/tests/invalid_connection_id.rs | 2 +- crates/udp_load_test/src/worker/mod.rs | 4 +-- crates/udp_protocol/src/request.rs | 8 +++--- crates/udp_protocol/src/response.rs | 26 +++++++++---------- 9 files changed, 27 insertions(+), 27 deletions(-) diff --git a/crates/udp/src/common.rs b/crates/udp/src/common.rs index f803ea5..19f8b27 100644 --- a/crates/udp/src/common.rs +++ b/crates/udp/src/common.rs @@ -290,7 +290,7 @@ mod tests { let mut buf = Vec::new(); - response.write(&mut buf).unwrap(); + response.write_bytes(&mut buf).unwrap(); println!("Buffer len: {}", buf.len()); diff --git a/crates/udp/src/workers/socket/mio.rs b/crates/udp/src/workers/socket/mio.rs index 070e00b..38a1865 100644 --- a/crates/udp/src/workers/socket/mio.rs +++ b/crates/udp/src/workers/socket/mio.rs @@ -198,7 +198,7 @@ impl SocketWorker { } let src = CanonicalSocketAddr::new(src); - let request_parsable = match Request::from_bytes( + let request_parsable = match Request::parse_bytes( &self.buffer[..bytes_read], self.config.protocol.max_scrape_torrents, ) { @@ -431,7 +431,7 @@ impl SocketWorker { ) { let mut buffer = Cursor::new(&mut buffer[..]); - if let Err(err) = response.write(&mut buffer) { + if let Err(err) = response.write_bytes(&mut buffer) { ::log::error!("failed writing response to buffer: {:#}", err); return; diff --git a/crates/udp/src/workers/socket/uring/recv_helper.rs b/crates/udp/src/workers/socket/uring/recv_helper.rs index 4a485f6..0aef6d9 100644 --- a/crates/udp/src/workers/socket/uring/recv_helper.rs +++ b/crates/udp/src/workers/socket/uring/recv_helper.rs @@ -138,7 +138,7 @@ impl RecvHelper { let addr = CanonicalSocketAddr::new(addr); - let request = Request::from_bytes(msg.payload_data(), self.max_scrape_torrents) + let request = Request::parse_bytes(msg.payload_data(), self.max_scrape_torrents) .map_err(|err| Error::RequestParseError(err, addr))?; Ok((request, addr)) diff --git a/crates/udp/src/workers/socket/uring/send_buffers.rs b/crates/udp/src/workers/socket/uring/send_buffers.rs index 458d96f..902e251 100644 --- a/crates/udp/src/workers/socket/uring/send_buffers.rs +++ b/crates/udp/src/workers/socket/uring/send_buffers.rs @@ -196,7 +196,7 @@ impl SendBuffer { let mut cursor = Cursor::new(&mut self.bytes[..]); - match response.write(&mut cursor) { + match response.write_bytes(&mut cursor) { Ok(()) => { self.iovec.iov_len = cursor.position() as usize; diff --git a/crates/udp/tests/common/mod.rs b/crates/udp/tests/common/mod.rs index ee8e365..0656b73 100644 --- a/crates/udp/tests/common/mod.rs +++ b/crates/udp/tests/common/mod.rs @@ -104,7 +104,7 @@ pub fn request_and_response( let mut buffer = Cursor::new(&mut buffer[..]); request - .write(&mut buffer) + .write_bytes(&mut buffer) .with_context(|| "write request")?; let bytes_written = buffer.position() as usize; @@ -119,6 +119,6 @@ pub fn request_and_response( .recv_from(&mut buffer) .with_context(|| "recv response")?; - Response::from_bytes(&buffer[..bytes_read], true).with_context(|| "parse response") + Response::parse_bytes(&buffer[..bytes_read], true).with_context(|| "parse response") } } diff --git a/crates/udp/tests/invalid_connection_id.rs b/crates/udp/tests/invalid_connection_id.rs index 11579a3..7506854 100644 --- a/crates/udp/tests/invalid_connection_id.rs +++ b/crates/udp/tests/invalid_connection_id.rs @@ -77,7 +77,7 @@ fn no_response( let mut buffer = Cursor::new(&mut buffer[..]); request - .write(&mut buffer) + .write_bytes(&mut buffer) .with_context(|| "write request")?; let bytes_written = buffer.position() as usize; diff --git a/crates/udp_load_test/src/worker/mod.rs b/crates/udp_load_test/src/worker/mod.rs index b3be476..8c7c606 100644 --- a/crates/udp_load_test/src/worker/mod.rs +++ b/crates/udp_load_test/src/worker/mod.rs @@ -71,7 +71,7 @@ impl Worker { for _ in events.iter() { while let Ok(amt) = self.socket.recv(&mut self.buffer) { - match Response::from_bytes(&self.buffer[0..amt], self.addr.is_ipv4()) { + match Response::parse_bytes(&self.buffer[0..amt], self.addr.is_ipv4()) { Ok(response) => { if let Some(request) = self.process_response(response) { self.send_request(request); @@ -288,7 +288,7 @@ impl Worker { fn send_request(&mut self, request: Request) { let mut cursor = Cursor::new(self.buffer); - match request.write(&mut cursor) { + match request.write_bytes(&mut cursor) { Ok(()) => { let position = cursor.position() as usize; let inner = cursor.get_ref(); diff --git a/crates/udp_protocol/src/request.rs b/crates/udp_protocol/src/request.rs index cd2963e..8cdd10d 100644 --- a/crates/udp_protocol/src/request.rs +++ b/crates/udp_protocol/src/request.rs @@ -19,7 +19,7 @@ pub enum Request { } impl Request { - pub fn write(self, bytes: &mut impl Write) -> Result<(), io::Error> { + pub fn write_bytes(self, bytes: &mut impl Write) -> Result<(), io::Error> { match self { Request::Connect(r) => { bytes.write_i64::(PROTOCOL_IDENTIFIER)?; @@ -42,7 +42,7 @@ impl Request { Ok(()) } - pub fn from_bytes(bytes: &[u8], max_scrape_torrents: u8) -> Result { + pub fn parse_bytes(bytes: &[u8], max_scrape_torrents: u8) -> Result { let action = bytes .get(8..12) .map(|bytes| I32::from_bytes(bytes.try_into().unwrap())) @@ -323,8 +323,8 @@ mod tests { fn same_after_conversion(request: Request) -> bool { let mut buf = Vec::new(); - request.clone().write(&mut buf).unwrap(); - let r2 = Request::from_bytes(&buf[..], ::std::u8::MAX).unwrap(); + request.clone().write_bytes(&mut buf).unwrap(); + let r2 = Request::parse_bytes(&buf[..], ::std::u8::MAX).unwrap(); let success = request == r2; diff --git a/crates/udp_protocol/src/response.rs b/crates/udp_protocol/src/response.rs index 4e3353f..591abe7 100644 --- a/crates/udp_protocol/src/response.rs +++ b/crates/udp_protocol/src/response.rs @@ -18,18 +18,18 @@ pub enum Response { impl Response { #[inline] - pub fn write(&self, bytes: &mut impl Write) -> Result<(), io::Error> { + pub fn write_bytes(&self, bytes: &mut impl Write) -> Result<(), io::Error> { match self { - Response::Connect(r) => r.write(bytes), - Response::AnnounceIpv4(r) => r.write(bytes), - Response::AnnounceIpv6(r) => r.write(bytes), - Response::Scrape(r) => r.write(bytes), - Response::Error(r) => r.write(bytes), + Response::Connect(r) => r.write_bytes(bytes), + Response::AnnounceIpv4(r) => r.write_bytes(bytes), + Response::AnnounceIpv6(r) => r.write_bytes(bytes), + Response::Scrape(r) => r.write_bytes(bytes), + Response::Error(r) => r.write_bytes(bytes), } } #[inline] - pub fn from_bytes(mut bytes: &[u8], ipv4: bool) -> Result { + pub fn parse_bytes(mut bytes: &[u8], ipv4: bool) -> Result { let action = read_i32_ne(&mut bytes)?; match action.get() { @@ -137,7 +137,7 @@ pub struct ConnectResponse { impl ConnectResponse { #[inline] - pub fn write(&self, bytes: &mut impl Write) -> Result<(), io::Error> { + pub fn write_bytes(&self, bytes: &mut impl Write) -> Result<(), io::Error> { bytes.write_i32::(0)?; bytes.write_all(self.as_bytes())?; @@ -160,7 +160,7 @@ impl AnnounceResponse { } #[inline] - pub fn write(&self, bytes: &mut impl Write) -> Result<(), io::Error> { + pub fn write_bytes(&self, bytes: &mut impl Write) -> Result<(), io::Error> { bytes.write_i32::(1)?; bytes.write_all(self.fixed.as_bytes())?; bytes.write_all((*self.peers.as_slice()).as_bytes())?; @@ -186,7 +186,7 @@ pub struct ScrapeResponse { impl ScrapeResponse { #[inline] - pub fn write(&self, bytes: &mut impl Write) -> Result<(), io::Error> { + pub fn write_bytes(&self, bytes: &mut impl Write) -> Result<(), io::Error> { bytes.write_i32::(2)?; bytes.write_all(self.transaction_id.as_bytes())?; bytes.write_all((*self.torrent_stats.as_slice()).as_bytes())?; @@ -211,7 +211,7 @@ pub struct ErrorResponse { impl ErrorResponse { #[inline] - pub fn write(&self, bytes: &mut impl Write) -> Result<(), io::Error> { + pub fn write_bytes(&self, bytes: &mut impl Write) -> Result<(), io::Error> { bytes.write_i32::(3)?; bytes.write_all(self.transaction_id.as_bytes())?; bytes.write_all(self.message.as_bytes())?; @@ -304,8 +304,8 @@ mod tests { fn same_after_conversion(response: Response, ipv4: bool) -> bool { let mut buf = Vec::new(); - response.clone().write(&mut buf).unwrap(); - let r2 = Response::from_bytes(&buf[..], ipv4).unwrap(); + response.clone().write_bytes(&mut buf).unwrap(); + let r2 = Response::parse_bytes(&buf[..], ipv4).unwrap(); let success = response == r2;