udp protocol: rename "write" and "from_bytes" methods

This commit is contained in:
Joakim Frostegård 2024-01-29 19:38:12 +01:00
parent f30ab82371
commit 1c59972834
9 changed files with 27 additions and 27 deletions

View file

@ -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::<NetworkEndian>(PROTOCOL_IDENTIFIER)?;
@ -42,7 +42,7 @@ impl Request {
Ok(())
}
pub fn from_bytes(bytes: &[u8], max_scrape_torrents: u8) -> Result<Self, RequestParseError> {
pub fn parse_bytes(bytes: &[u8], max_scrape_torrents: u8) -> Result<Self, RequestParseError> {
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;

View file

@ -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<Self, io::Error> {
pub fn parse_bytes(mut bytes: &[u8], ipv4: bool) -> Result<Self, io::Error> {
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::<NetworkEndian>(0)?;
bytes.write_all(self.as_bytes())?;
@ -160,7 +160,7 @@ impl<I: Ip> AnnounceResponse<I> {
}
#[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::<NetworkEndian>(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::<NetworkEndian>(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::<NetworkEndian>(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;