improve Status Response
This commit is contained in:
1 parent
419513d2e7
commit
b5541ab5ba
8 files changed
+221
-22
No files matched your search
@@ -8,6 +8,9 @@ edition = "2021"
|
||||
serde = { version = "1.0", features = ["derive"] }
|
||||
serde_json = "1.0"
|
||||
|
||||
base64 = "0.22.1"
|
||||
image = { version = "0.25", default-features = false, features = ["png"]}
|
||||
|
||||
simple_logger = "5.0.0"
|
||||
log = "0.4"
|
||||
byteorder = "1"
|
||||
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 277 B |
@@ -1,10 +1,11 @@
|
||||
|
||||
|
||||
use crate::protocol::{
|
||||
client::status::{CPingResponse, CStatusResponse},
|
||||
server::{
|
||||
handshake::SHandShake,
|
||||
status::{SPingRequest, SStatusRequest},
|
||||
},
|
||||
StatusResponse, Version,
|
||||
};
|
||||
|
||||
use super::Client;
|
||||
@@ -26,15 +27,9 @@ impl ClientPacketProcessor for Client {
|
||||
|
||||
fn handle_status_request(&mut self, _status_request: SStatusRequest) {
|
||||
dbg!("sending status");
|
||||
let response = StatusResponse {
|
||||
version: Version {
|
||||
version: "1.21".into(),
|
||||
protocol: 767,
|
||||
},
|
||||
description: "Pumpkin Server".into(),
|
||||
};
|
||||
|
||||
self.send_packet(CStatusResponse::new(
|
||||
serde_json::to_string(&response).unwrap(),
|
||||
serde_json::to_string(&self.server.status_response).unwrap(),
|
||||
))
|
||||
}
|
||||
|
||||
|
||||
@@ -1,14 +1,18 @@
|
||||
use std::{
|
||||
collections::VecDeque,
|
||||
io::{self, Write},
|
||||
rc::Rc,
|
||||
};
|
||||
|
||||
use crate::protocol::{
|
||||
server::{
|
||||
handshake::SHandShake,
|
||||
status::{SPingRequest, SStatusRequest},
|
||||
use crate::{
|
||||
protocol::{
|
||||
server::{
|
||||
handshake::SHandShake,
|
||||
status::{SPingRequest, SStatusRequest},
|
||||
},
|
||||
ClientPacket, RawPacket,
|
||||
},
|
||||
ClientPacket, RawPacket,
|
||||
server::Server,
|
||||
};
|
||||
|
||||
use crate::protocol::{bytebuf::buffer::ByteBuffer, ConnectionState};
|
||||
@@ -21,6 +25,7 @@ pub mod player;
|
||||
use client_packet::ClientPacketProcessor;
|
||||
|
||||
pub struct Client {
|
||||
pub server: Rc<Server>,
|
||||
pub connection_state: ConnectionState,
|
||||
pub closed: bool,
|
||||
pub connection: TcpStream,
|
||||
@@ -29,8 +34,9 @@ pub struct Client {
|
||||
}
|
||||
|
||||
impl Client {
|
||||
pub fn new(connection: TcpStream) -> Self {
|
||||
pub fn new(server: Rc<Server>, connection: TcpStream) -> Self {
|
||||
Self {
|
||||
server,
|
||||
connection_state: ConnectionState::HandShake,
|
||||
connection,
|
||||
closed: false,
|
||||
|
||||
+10
-5
@@ -8,10 +8,14 @@ const SERVER: Token = Token(0);
|
||||
|
||||
pub mod client;
|
||||
pub mod protocol;
|
||||
pub mod server;
|
||||
|
||||
#[cfg(not(target_os = "wasi"))]
|
||||
fn main() -> io::Result<()> {
|
||||
use std::rc::Rc;
|
||||
|
||||
use client::{interrupted, Client};
|
||||
use server::Server;
|
||||
|
||||
simple_logger::SimpleLogger::new().init().unwrap();
|
||||
|
||||
@@ -22,11 +26,11 @@ fn main() -> io::Result<()> {
|
||||
|
||||
// Setup the TCP server socket.
|
||||
let addr = "127.0.0.1:25565".parse().unwrap();
|
||||
let mut server = TcpListener::bind(addr)?;
|
||||
let mut listener = TcpListener::bind(addr)?;
|
||||
|
||||
// Register the server with poll we can receive events for it.
|
||||
poll.registry()
|
||||
.register(&mut server, SERVER, Interest::READABLE)?;
|
||||
.register(&mut listener, SERVER, Interest::READABLE)?;
|
||||
|
||||
// Map of `Token` -> `TcpStream`.
|
||||
let mut connections = HashMap::new();
|
||||
@@ -35,7 +39,8 @@ fn main() -> io::Result<()> {
|
||||
|
||||
log::info!("You can connect to the server using `nc`:");
|
||||
log::info!(" $ nc 127.0.0.1 9000");
|
||||
log::info!("You'll see our welcome message and anything you type will be printed here.");
|
||||
|
||||
let server = Rc::new(Server::new());
|
||||
|
||||
loop {
|
||||
if let Err(err) = poll.poll(&mut events, None) {
|
||||
@@ -50,7 +55,7 @@ fn main() -> io::Result<()> {
|
||||
SERVER => loop {
|
||||
// Received an event for the TCP server socket, which
|
||||
// indicates we can accept an connection.
|
||||
let (mut connection, address) = match server.accept() {
|
||||
let (mut connection, address) = match listener.accept() {
|
||||
Ok((connection, address)) => (connection, address),
|
||||
Err(e) if e.kind() == io::ErrorKind::WouldBlock => {
|
||||
// If we get a `WouldBlock` error we know our
|
||||
@@ -75,7 +80,7 @@ fn main() -> io::Result<()> {
|
||||
Interest::READABLE.add(Interest::WRITABLE),
|
||||
)?;
|
||||
|
||||
connections.insert(token, Client::new(connection));
|
||||
connections.insert(token, Client::new(Rc::clone(&server), connection));
|
||||
},
|
||||
token => {
|
||||
// Maybe received an event for a TCP connection.
|
||||
|
||||
@@ -44,11 +44,26 @@ pub trait ClientPacket {
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct StatusResponse {
|
||||
pub version: Version,
|
||||
pub players: Players,
|
||||
pub description: String,
|
||||
// Players, favicon ...
|
||||
pub favicon: String, // data:image/png;base64,<data>
|
||||
// Players, favicon ...
|
||||
}
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct Version {
|
||||
pub version: String,
|
||||
pub name: String,
|
||||
pub protocol: u32,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct Players {
|
||||
pub max: u32,
|
||||
pub online: u32,
|
||||
pub sample: Sample,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct Sample {
|
||||
pub name: String,
|
||||
pub id: String, // uuid
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
use std::io::Cursor;
|
||||
|
||||
use base64::{engine::general_purpose, Engine};
|
||||
|
||||
use crate::protocol::{Players, Sample, StatusResponse, Version};
|
||||
|
||||
pub struct Server {
|
||||
pub status_response: StatusResponse,
|
||||
}
|
||||
|
||||
impl Default for Server {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
|
||||
impl Server {
|
||||
pub fn new() -> Self {
|
||||
let path = concat!(env!("CARGO_MANIFEST_DIR"), "/icon.png");
|
||||
|
||||
let status_response = StatusResponse {
|
||||
version: Version {
|
||||
name: "1.21".into(),
|
||||
protocol: 767,
|
||||
},
|
||||
players: Players {
|
||||
max: 20,
|
||||
online: 0,
|
||||
sample: Sample {
|
||||
name: "".into(),
|
||||
id: "".into(),
|
||||
},
|
||||
},
|
||||
description: "Pumpkin Server".into(),
|
||||
favicon: Self::load_icon(path),
|
||||
};
|
||||
Self { status_response }
|
||||
}
|
||||
|
||||
pub fn load_icon(path: &str) -> String {
|
||||
let mut icon = match image::open(path).map_err(|e| panic!("error loading icon: {}", e)) {
|
||||
Ok(icon) => icon,
|
||||
Err(_) => return "".into(),
|
||||
};
|
||||
icon = icon.resize_exact(64, 64, image::imageops::FilterType::Triangle);
|
||||
let mut image = Vec::new();
|
||||
icon.write_to(&mut Cursor::new(&mut image), image::ImageFormat::Png)
|
||||
.unwrap();
|
||||
let mut result = "data:image/png;base64,".to_owned();
|
||||
general_purpose::STANDARD.encode_string(image, &mut result);
|
||||
result
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user