read packet with id list

This commit is contained in:
MeexReay 2025-05-05 04:29:55 +03:00
parent da74907bcc
commit 62e7e5bf28
4 changed files with 23 additions and 22 deletions

View File

@ -197,13 +197,14 @@ impl ClientContext {
Ok(())
}
/// Please avoid using of this bullshit
pub fn read_any_packet(self: &Arc<Self>) -> Result<Packet, ServerError> {
if self.read_loop.load(Ordering::SeqCst) {
loop {
if let Some(packet) = self.packet_buffer.lock().unwrap().pop_front() {
return Ok(packet);
}
thread::sleep(Duration::from_millis(10));
thread::sleep(Duration::from_millis(4));
}
} else {
let state = self.state();
@ -237,19 +238,19 @@ impl ClientContext {
}
}
pub fn read_packet(self: &Arc<Self>, id: u8) -> Result<Packet, ServerError> {
pub fn read_packet(self: &Arc<Self>, ids: &[u8]) -> Result<Packet, ServerError> {
if self.read_loop.load(Ordering::SeqCst) {
loop {
{
let mut locked = self.packet_buffer.lock().unwrap();
for (i, packet) in locked.clone().iter().enumerate() {
if packet.id() == id {
if ids.contains(&packet.id()) {
locked.remove(i);
return Ok(packet.clone());
}
}
}
thread::sleep(Duration::from_millis(10));
thread::sleep(Duration::from_millis(4));
}
} else {
let packet = match self.read_any_packet() {
@ -260,7 +261,7 @@ impl ClientContext {
}
};
if packet.id() != id {
if ids.contains(&packet.id()) {
Err(ServerError::UnexpectedPacket(packet.id()))
} else {
Ok(packet)
@ -268,7 +269,7 @@ impl ClientContext {
}
}
pub fn push_back(self: &Arc<Self>, packet: Packet){
pub fn push_packet_back(self: &Arc<Self>, packet: Packet){
self.packet_buffer.lock().unwrap().push_back(packet)
}

View File

@ -70,7 +70,7 @@ impl ProtocolHelper {
self.client
.write_packet(&Packet::empty(clientbound::configuration::FINISH))?;
self.client
.read_packet(serverbound::configuration::ACKNOWLEDGE_FINISH)?;
.read_packet(&[serverbound::configuration::ACKNOWLEDGE_FINISH])?;
self.client.set_state(ConnectionState::Play)?;
Ok(())
}
@ -85,7 +85,7 @@ impl ProtocolHelper {
self.client
.write_packet(&Packet::empty(clientbound::play::START_CONFIGURATION))?;
self.client
.read_packet(serverbound::play::ACKNOWLEDGE_CONFIGURATION)?;
.read_packet(&[serverbound::play::ACKNOWLEDGE_CONFIGURATION])?;
self.client.set_state(ConnectionState::Configuration)?;
Ok(())
}
@ -100,14 +100,14 @@ impl ProtocolHelper {
let time = SystemTime::now();
self.client
.write_packet(&Packet::empty(clientbound::play::PING))?;
self.client.read_packet(serverbound::play::PONG)?;
self.client.read_packet(&[serverbound::play::PONG])?;
Ok(SystemTime::now().duration_since(time).unwrap())
}
ConnectionState::Configuration => {
let time = SystemTime::now();
self.client
.write_packet(&Packet::empty(clientbound::configuration::PING))?;
self.client.read_packet(serverbound::configuration::PONG)?;
self.client.read_packet(&[serverbound::configuration::PONG])?;
Ok(SystemTime::now().duration_since(time).unwrap())
}
_ => Err(ServerError::UnexpectedState),
@ -149,7 +149,7 @@ impl ProtocolHelper {
let mut packet = self
.client
.read_packet(serverbound::configuration::COOKIE_RESPONSE)?;
.read_packet(&[serverbound::configuration::COOKIE_RESPONSE])?;
packet.read_string()?;
let data = if packet.read_boolean()? {
let n = packet.read_usize_varint()?;
@ -167,7 +167,7 @@ impl ProtocolHelper {
let mut packet = self
.client
.read_packet(serverbound::play::COOKIE_RESPONSE)?;
.read_packet(&[serverbound::play::COOKIE_RESPONSE])?;
packet.read_string()?;
let data = if packet.read_boolean()? {
let n = packet.read_usize_varint()?;
@ -199,7 +199,7 @@ impl ProtocolHelper {
let mut packet = self
.client
.read_packet(serverbound::login::PLUGIN_RESPONSE)?;
.read_packet(&[serverbound::login::PLUGIN_RESPONSE])?;
let identifier = packet.read_varint()?;
let data = if packet.read_boolean()? {
let mut data = Vec::new();

View File

@ -21,7 +21,7 @@ pub fn handle_connection(
// Получение пакетов производится через client.conn(),
// ВАЖНО: не помещать сам client.conn() в переменные,
// он должен сразу убиваться иначе соединение гдето задедлочится
let mut packet = client.read_packet(serverbound::handshake::HANDSHAKE)?;
let mut packet = client.read_packet(&[serverbound::handshake::HANDSHAKE])?;
let protocol_version = packet.read_varint()?; // Получаем версия протокола, может быть отрицательным если наш клиент дэбил
let server_address = packet.read_string()?; // Получаем домен/адрес сервера к которому пытается подключиться клиент, например "play.example.com", а не айпи
@ -86,7 +86,7 @@ pub fn handle_connection(
client.set_state(ConnectionState::Login)?; // Мы находимся в режиме Login
// Читаем пакет Login Start
let mut packet = client.read_packet(serverbound::login::START)?;
let mut packet = client.read_packet(&[serverbound::login::START])?;
let name = packet.read_string()?;
let uuid = packet.read_uuid()?;
@ -115,14 +115,14 @@ pub fn handle_connection(
p.write_varint(0)
})?)?;
client.read_packet(serverbound::login::ACKNOWLEDGED)?; // Пакет Login Acknowledged
client.read_packet(&[serverbound::login::ACKNOWLEDGED])?; // Пакет Login Acknowledged
client.set_state(ConnectionState::Configuration)?; // Мы перешли в режим Configuration
// Получение бренда клиента из Serverbound Plugin Message
// Identifier канала откуда берется бренд: minecraft:brand
let brand = loop {
let mut packet = client.read_packet(serverbound::configuration::PLUGIN_MESSAGE)?; // Пакет Serverbound Plugin Message
let mut packet = client.read_packet(&[serverbound::configuration::PLUGIN_MESSAGE])?; // Пакет Serverbound Plugin Message
let identifier = packet.read_string()?;
@ -136,7 +136,7 @@ pub fn handle_connection(
}
};
let mut packet = client.read_packet(serverbound::configuration::CLIENT_INFORMATION)?; // Пакет Client Information
let mut packet = client.read_packet(&[serverbound::configuration::CLIENT_INFORMATION])?; // Пакет Client Information
let locale = packet.read_string()?; // for example: en_us
let view_distance = packet.read_signed_byte()?; // client-side render distance in chunks
@ -172,7 +172,7 @@ pub fn handle_connection(
handle_configuration_state(client.clone())?;
client.write_packet(&Packet::empty(clientbound::configuration::FINISH))?;
client.read_packet(serverbound::configuration::ACKNOWLEDGE_FINISH)?;
client.read_packet(&[serverbound::configuration::ACKNOWLEDGE_FINISH])?;
client.set_state(ConnectionState::Play)?; // Мы перешли в режим Play

View File

@ -44,7 +44,7 @@ pub fn handle_configuration_state(
packet.write_string("1.21.5")?;
client.write_packet(&packet)?;
client.read_packet(serverbound::configuration::KNOWN_PACKS)?;
client.read_packet(&[serverbound::configuration::KNOWN_PACKS])?;
send_registry_data(client.clone())?;
send_update_tags(client.clone())
@ -209,7 +209,7 @@ pub fn send_keep_alive(client: Arc<ClientContext>) -> Result<(), ServerError> {
packet.write_long(timestamp)?;
client.write_packet(&packet)?;
let mut packet = client.read_packet(serverbound::play::KEEP_ALIVE)?;
let mut packet = client.read_packet(&[serverbound::play::KEEP_ALIVE])?;
let timestamp2 = packet.read_long()?;
if timestamp2 != timestamp {
// Послать клиента нахуй
@ -283,7 +283,7 @@ pub fn handle_play_state(
client.set_rotation((yaw, pitch));
},
_ => {
client.push_back(packet);
client.push_packet_back(packet);
}
}
}