/* ************************************************************************** */ /* */ /* ::: :::::::: */ /* client.rs :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: tomoron +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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, pub buffer: Vec, pub out_stream: Option, pub handshake: Option, 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) { let mut sent_data: Vec = 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()) } } }