kill connection on error

fix confirm teleportation error
add ticks alive message
This commit is contained in:
MeexReay 2025-05-05 00:32:08 +03:00
parent caa08c9c25
commit 48f493695e
2 changed files with 18 additions and 21 deletions

View File

@ -4,7 +4,7 @@ use std::{
}, thread, time::Duration }, thread, time::Duration
}; };
use rust_mc_proto::{MinecraftConnection, Packet, ProtocolError}; use rust_mc_proto::{MinecraftConnection, Packet};
use uuid::Uuid; use uuid::Uuid;
use crate::server::{ServerError, context::ServerContext, protocol::ConnectionState}; use crate::server::{ServerError, context::ServerContext, protocol::ConnectionState};
@ -120,11 +120,8 @@ impl ClientContext {
if !cancelled { if !cancelled {
match self.conn.write().unwrap().write_packet(&packet) { match self.conn.write().unwrap().write_packet(&packet) {
Ok(_) => {}, Ok(_) => {},
Err(ProtocolError::ConnectionClosedError) => {
self.is_alive.store(false, Ordering::SeqCst);
return Err(ServerError::ConnectionClosed);
},
Err(e) => { Err(e) => {
self.is_alive.store(false, Ordering::SeqCst);
return Err(e.into()); return Err(e.into());
} }
}; };
@ -139,14 +136,11 @@ impl ClientContext {
let mut conn = self.conn.read().unwrap().try_clone()?; // так можно делать т.к сокет это просто поинтер let mut conn = self.conn.read().unwrap().try_clone()?; // так можно делать т.к сокет это просто поинтер
loop { while self.is_alive() {
let mut packet = match conn.read_packet() { let mut packet = match conn.read_packet() {
Ok(v) => v, Ok(v) => v,
Err(ProtocolError::ConnectionClosedError) => {
self.is_alive.store(false, Ordering::SeqCst);
return Err(ServerError::ConnectionClosed);
},
Err(e) => { Err(e) => {
self.is_alive.store(false, Ordering::SeqCst);
return Err(e.into()); return Err(e.into());
} }
}; };
@ -169,6 +163,8 @@ impl ClientContext {
self.packet_buffer.lock().unwrap().push_back(packet); self.packet_buffer.lock().unwrap().push_back(packet);
} }
} }
Ok(())
} }
pub fn read_any_packet(self: &Arc<Self>) -> Result<Packet, ServerError> { pub fn read_any_packet(self: &Arc<Self>) -> Result<Packet, ServerError> {
@ -185,11 +181,8 @@ impl ClientContext {
loop { loop {
let mut packet = match self.conn.write().unwrap().read_packet() { let mut packet = match self.conn.write().unwrap().read_packet() {
Ok(v) => v, Ok(v) => v,
Err(ProtocolError::ConnectionClosedError) => {
self.is_alive.store(false, Ordering::SeqCst);
return Err(ServerError::ConnectionClosed);
},
Err(e) => { Err(e) => {
self.is_alive.store(false, Ordering::SeqCst);
return Err(e.into()); return Err(e.into());
} }
}; };
@ -231,11 +224,8 @@ impl ClientContext {
} else { } else {
let packet = match self.read_any_packet() { let packet = match self.read_any_packet() {
Ok(v) => v, Ok(v) => v,
Err(ServerError::ConnectionClosed) => {
self.is_alive.store(false, Ordering::SeqCst);
return Err(ServerError::ConnectionClosed);
},
Err(e) => { Err(e) => {
self.is_alive.store(false, Ordering::SeqCst);
return Err(e); return Err(e);
} }
}; };

View File

@ -2,7 +2,7 @@ use std::{io::Cursor, sync::Arc, thread, time::{Duration, SystemTime, UNIX_EPOCH
use rust_mc_proto::{DataWriter, Packet, read_packet}; use rust_mc_proto::{DataWriter, Packet, read_packet};
use crate::server::{player::context::ClientContext, ServerError}; use crate::server::{data::{text_component::TextComponent, ReadWriteNBT}, player::context::ClientContext, ServerError};
use super::id::*; use super::id::*;
@ -122,8 +122,6 @@ pub fn sync_player_pos(
client.write_packet(&packet)?; client.write_packet(&packet)?;
client.read_packet(serverbound::play::CONFIRM_TELEPORTATION)?;
Ok(()) Ok(())
} }
@ -216,6 +214,13 @@ pub fn send_keep_alive(client: Arc<ClientContext>) -> Result<(), ServerError> {
Ok(()) Ok(())
} }
pub fn send_system_message(client: Arc<ClientContext>, message: TextComponent, is_action_bar: bool) -> Result<(), ServerError> {
let mut packet = Packet::empty(clientbound::play::SYSTEM_CHAT_MESSAGE);
packet.write_nbt(&message)?;
packet.write_boolean(is_action_bar)?;
client.write_packet(&packet)
}
// Отдельная функция для работы с самой игрой // Отдельная функция для работы с самой игрой
pub fn handle_play_state( pub fn handle_play_state(
client: Arc<ClientContext>, // Контекст клиента client: Arc<ClientContext>, // Контекст клиента
@ -247,6 +252,8 @@ pub fn handle_play_state(
// do something // do something
} }
send_system_message(client.clone(), TextComponent::rainbow(format!("Ticks alive: {}", ticks_alive)), true)?;
thread::sleep(Duration::from_millis(50)); // 1 tick thread::sleep(Duration::from_millis(50)); // 1 tick
ticks_alive += 1; ticks_alive += 1;
} }