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> {
#[derive(Debug)] #[derive(Debug)]
pub enum Packet<'a> { pub enum Packet<'a> {
Challenge(Option<u32>), Challenge(Option<u32>),
ServerAdd(u32, ServerInfo<&'a str>), ServerAdd(Option<u32>, ServerInfo<&'a str>),
ServerRemove, ServerRemove,
QueryServers(Region, Filter<'a>), QueryServers(Region, Filter<'a>),
} }

6
src/master_server.rs

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

13
src/server_info.rs

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

Loading…
Cancel
Save