Skip to content

Commit

Permalink
feat: improve support for older versions
Browse files Browse the repository at this point in the history
  • Loading branch information
doinkythederp committed Mar 10, 2024
1 parent b841d32 commit 6949c75
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 11 deletions.
13 changes: 11 additions & 2 deletions src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub struct JavaServerInfo {
#[serde(deserialize_with = "de_description")]
pub description: ServerDescription,
pub favicon: Option<String>,
#[serde(rename = "deserialize_description")]
#[serde(rename = "modinfo")]
pub mod_info: Option<ServerModInfo>,
}

Expand Down Expand Up @@ -87,7 +87,16 @@ pub struct ServerDescription {
pub struct ServerModInfo {
#[serde(rename = "type")]
pub loader_type: String,
// pub mod_list: Vec<ServerModInfoMod>,
#[serde(rename = "modList")]
pub mod_list: Vec<ServerMod>,
}

#[derive(Serialize, Deserialize, Debug, Hash, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct ServerMod {
#[serde(rename = "modid")]
pub mod_id: String,
pub version: String,
}

impl std::str::FromStr for JavaServerInfo {
Expand Down
35 changes: 26 additions & 9 deletions src/protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ use tokio::{
io::{AsyncReadExt, AsyncWriteExt, BufWriter},
net::TcpStream,
};
use tracing::error;
use tracing::info;
use tracing::{debug, event, instrument, trace, Level};

mod frame;
Expand Down Expand Up @@ -52,7 +54,11 @@ pub enum ProtocolError {
backtrace: Backtrace,
},
/// Packet received out of order.
FrameOutOfOrder { backtrace: Backtrace },
FrameOutOfOrder {
backtrace: Backtrace,
expected: &'static str,
got: Frame,
},
/// Failed to parse JSON response.
#[snafu(display("Failed to parse JSON response: {source}"), context(false))]
JsonParse {
Expand Down Expand Up @@ -100,7 +106,6 @@ impl SlpProtocol {
}

/// Sends frame data over the connection as a packet.
#[instrument]
pub async fn write_frame(&mut self, frame: Frame) -> Result<(), ProtocolError> {
debug!("Writing frame: {frame:?}");

Expand Down Expand Up @@ -170,6 +175,7 @@ impl SlpProtocol {
// Attempt to parse a frame from the buffered data. If enough data
// has been buffered, the frame is returned.
if let Some(frame) = self.parse_frame(server_state)? {
debug!("Received frame: {frame:?}");
return Ok(Some(frame));
}

Expand All @@ -185,12 +191,13 @@ impl SlpProtocol {
// there is, this means that the peer closed the socket while
// sending a frame.
if self.buffer.is_empty() {
info!("Connection closed cleanly");
return Ok(None);
} else {
return Err(ProtocolError::ConnectionClosed {
backtrace: Backtrace::generate(),
});
}
error!("Connection closed unexpectedly");
return Err(ProtocolError::ConnectionClosed {
backtrace: Backtrace::generate(),
});
}
}
}
Expand Down Expand Up @@ -245,7 +252,13 @@ impl SlpProtocol {
.context(ConnectionClosedSnafu)?;
let frame_data = match frame {
Frame::StatusResponse { json } => json,
_ => return FrameOutOfOrderSnafu.fail(),
frame => {
return FrameOutOfOrderSnafu {
expected: "StatusResponse",
got: frame,
}
.fail()
}
};
Ok(JavaServerInfo::from_str(&frame_data)?)
}
Expand All @@ -266,8 +279,12 @@ impl SlpProtocol {
.await?
.context(ConnectionClosedSnafu)?;
match frame {
Frame::PingResponse { payload: _ } => Ok(ping_time.elapsed()),
_ => FrameOutOfOrderSnafu.fail(),
Frame::PingResponse { .. } | Frame::StatusResponse { .. } => Ok(ping_time.elapsed()),
frame => FrameOutOfOrderSnafu {
expected: "PingResponse",
got: frame,
}
.fail(),
}
}
}
Expand Down

0 comments on commit 6949c75

Please sign in to comment.