Files
dockermcmgr/src/minecraft/client/client.rs

68 lines
2.2 KiB
Rust

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* client.rs :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tomoron <tomoron@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/05/07 17:23:09 by tomoron #+# #+# */
/* Updated: 2026/05/30 00:27:19 by tomoron ### ########.fr */
/* */
/* ************************************************************************** */
use tokio::net::TcpStream;
use crate::minecraft::handshake::Handshake;
use crate::minecraft::varint::varint_write;
use std::fmt;
pub struct Client {
pub in_stream: TcpStream,
pub read_buf: Vec<u8>,
pub buffer: Vec<u8>,
pub out_stream: Option<TcpStream>,
pub handshake: Option<Handshake>,
pub dbPool: DbPool
}
use crate::DbPool;
impl Client {
pub fn create(stream: TcpStream, pool: DbPool) -> Self {
Self {
in_stream: stream,
read_buf: vec![0u8; 1024 * 8],
buffer: vec![],
out_stream: None,
handshake: None,
dbPool: pool
}
}
pub async fn send_packet(&self, data: Vec<u8>) {
let mut sent_data: Vec<u8> = varint_write(data.len() as i32);
sent_data.extend(data);
let _ = self.in_stream.writable().await;
match self.in_stream.try_write(sent_data.as_slice()) {
Err(e) => { eprintln!("error while sending response {:?}", e); },
_ => { }
}
}
}
impl fmt::Display for Client {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if let Some(handshake) = &self.handshake {
write!(f, "{}", handshake.server_address)
} else {
use std::os::unix::io::AsRawFd;
write!(f, "{}", self.in_stream.as_raw_fd())
}
}
}