Browse Source

Reorg missing challenge error

ipv6
Denis Drakhnia 1 year ago
parent
commit
9beefd5d6d
  1. 2
      src/client.rs
  2. 6
      src/master_server.rs
  3. 13
      src/server_info.rs

2
src/client.rs

@ -38,7 +38,7 @@ impl<'a> Deref for Filter<'a> { @@ -38,7 +38,7 @@ impl<'a> Deref for Filter<'a> {
#[derive(Debug)]
pub enum Packet<'a> {
Challenge(Option<u32>),
ServerAdd(u32, ServerInfo<&'a str>),
ServerAdd(Option<u32>, ServerInfo<&'a str>),
ServerRemove,
QueryServers(Region, Filter<'a>),
}

6
src/master_server.rs

@ -39,6 +39,8 @@ pub enum Error { @@ -39,6 +39,8 @@ pub enum Error {
BindSocket(io::Error),
#[error("Failed to decode packet: {0}")]
ClientPacket(#[from] crate::client::Error),
#[error("Missing challenge in ServerInfo")]
MissingChallenge,
#[error(transparent)]
Io(#[from] io::Error),
}
@ -129,6 +131,10 @@ impl MasterServer { @@ -129,6 +131,10 @@ impl MasterServer {
self.remove_outdated_challenges();
}
Packet::ServerAdd(challenge, info) => {
let challenge = match challenge {
Some(c) => c,
None => return Err(Error::MissingChallenge),
};
let entry = match self.challenges.get(&from) {
Some(e) => e,
None => {

13
src/server_info.rs

@ -13,8 +13,6 @@ use crate::parser::{Error as ParserError, ParseValue, Parser}; @@ -13,8 +13,6 @@ use crate::parser::{Error as ParserError, ParseValue, Parser};
pub enum Error {
#[error("Invalid region")]
InvalidRegion,
#[error("Missing challenge in ServerInfo")]
MissingChallenge,
#[error(transparent)]
Parser(#[from] ParserError),
}
@ -182,7 +180,7 @@ impl<'a, T> ServerInfo<T> @@ -182,7 +180,7 @@ impl<'a, T> ServerInfo<T>
where
T: 'a + Default + ParseValue<'a, Err = ParserError>,
{
pub fn from_bytes(src: &'a [u8]) -> Result<(u32, Self, &'a [u8]), Error> {
pub fn from_bytes(src: &'a [u8]) -> Result<(Option<u32>, Self, &'a [u8]), Error> {
let mut parser = Parser::new(src);
let (challenge, info) = parser.parse()?;
let tail = match parser.end() {
@ -193,7 +191,7 @@ where @@ -193,7 +191,7 @@ where
}
}
impl<'a, T> ParseValue<'a> for (u32, ServerInfo<T>)
impl<'a, T> ParseValue<'a> for (Option<u32>, ServerInfo<T>)
where
T: 'a + Default + ParseValue<'a, Err = ParserError>,
{
@ -239,10 +237,7 @@ where @@ -239,10 +237,7 @@ where
}
}
match challenge {
Some(challenge) => Ok((challenge, info)),
None => Err(Error::MissingChallenge),
}
Ok((challenge, info))
}
}
@ -313,7 +308,7 @@ mod tests { @@ -313,7 +308,7 @@ mod tests {
assert_eq!(
ServerInfo::from_bytes(&buf[..]),
Ok((
12345678,
Some(12345678),
ServerInfo::<&str> {
protocol: 47,
players: 16,

Loading…
Cancel
Save