kill connection on error
fix confirm teleportation error add ticks alive message
This commit is contained in:
parent
caa08c9c25
commit
48f493695e
@ -4,7 +4,7 @@ use std::{
|
||||
}, thread, time::Duration
|
||||
};
|
||||
|
||||
use rust_mc_proto::{MinecraftConnection, Packet, ProtocolError};
|
||||
use rust_mc_proto::{MinecraftConnection, Packet};
|
||||
use uuid::Uuid;
|
||||
|
||||
use crate::server::{ServerError, context::ServerContext, protocol::ConnectionState};
|
||||
@ -120,11 +120,8 @@ impl ClientContext {
|
||||
if !cancelled {
|
||||
match self.conn.write().unwrap().write_packet(&packet) {
|
||||
Ok(_) => {},
|
||||
Err(ProtocolError::ConnectionClosedError) => {
|
||||
self.is_alive.store(false, Ordering::SeqCst);
|
||||
return Err(ServerError::ConnectionClosed);
|
||||
},
|
||||
Err(e) => {
|
||||
self.is_alive.store(false, Ordering::SeqCst);
|
||||
return Err(e.into());
|
||||
}
|
||||
};
|
||||
@ -139,14 +136,11 @@ impl ClientContext {
|
||||
|
||||
let mut conn = self.conn.read().unwrap().try_clone()?; // так можно делать т.к сокет это просто поинтер
|
||||
|
||||
loop {
|
||||
while self.is_alive() {
|
||||
let mut packet = match conn.read_packet() {
|
||||
Ok(v) => v,
|
||||
Err(ProtocolError::ConnectionClosedError) => {
|
||||
self.is_alive.store(false, Ordering::SeqCst);
|
||||
return Err(ServerError::ConnectionClosed);
|
||||
},
|
||||
Err(e) => {
|
||||
self.is_alive.store(false, Ordering::SeqCst);
|
||||
return Err(e.into());
|
||||
}
|
||||
};
|
||||
@ -169,6 +163,8 @@ impl ClientContext {
|
||||
self.packet_buffer.lock().unwrap().push_back(packet);
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn read_any_packet(self: &Arc<Self>) -> Result<Packet, ServerError> {
|
||||
@ -185,11 +181,8 @@ impl ClientContext {
|
||||
loop {
|
||||
let mut packet = match self.conn.write().unwrap().read_packet() {
|
||||
Ok(v) => v,
|
||||
Err(ProtocolError::ConnectionClosedError) => {
|
||||
self.is_alive.store(false, Ordering::SeqCst);
|
||||
return Err(ServerError::ConnectionClosed);
|
||||
},
|
||||
Err(e) => {
|
||||
self.is_alive.store(false, Ordering::SeqCst);
|
||||
return Err(e.into());
|
||||
}
|
||||
};
|
||||
@ -231,11 +224,8 @@ impl ClientContext {
|
||||
} else {
|
||||
let packet = match self.read_any_packet() {
|
||||
Ok(v) => v,
|
||||
Err(ServerError::ConnectionClosed) => {
|
||||
self.is_alive.store(false, Ordering::SeqCst);
|
||||
return Err(ServerError::ConnectionClosed);
|
||||
},
|
||||
Err(e) => {
|
||||
self.is_alive.store(false, Ordering::SeqCst);
|
||||
return Err(e);
|
||||
}
|
||||
};
|
||||
|
@ -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 crate::server::{player::context::ClientContext, ServerError};
|
||||
use crate::server::{data::{text_component::TextComponent, ReadWriteNBT}, player::context::ClientContext, ServerError};
|
||||
|
||||
use super::id::*;
|
||||
|
||||
@ -122,8 +122,6 @@ pub fn sync_player_pos(
|
||||
|
||||
client.write_packet(&packet)?;
|
||||
|
||||
client.read_packet(serverbound::play::CONFIRM_TELEPORTATION)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@ -216,6 +214,13 @@ pub fn send_keep_alive(client: Arc<ClientContext>) -> Result<(), ServerError> {
|
||||
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(
|
||||
client: Arc<ClientContext>, // Контекст клиента
|
||||
@ -247,6 +252,8 @@ pub fn handle_play_state(
|
||||
// do something
|
||||
}
|
||||
|
||||
send_system_message(client.clone(), TextComponent::rainbow(format!("Ticks alive: {}", ticks_alive)), true)?;
|
||||
|
||||
thread::sleep(Duration::from_millis(50)); // 1 tick
|
||||
ticks_alive += 1;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user