fix multiple packet in client buffer
This commit is contained in:
@ -6,13 +6,15 @@
|
||||
/* By: tomoron <tomoron@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2026/05/07 17:23:09 by tomoron #+# #+# */
|
||||
/* Updated: 2026/05/19 18:57:29 by tomoron ### ########.fr */
|
||||
/* Updated: 2026/05/19 21:19:53 by tomoron ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
use async_std::task::sleep;
|
||||
use json::object;
|
||||
use tokio::net::TcpStream;
|
||||
use std::collections::VecDeque;
|
||||
use std::time::Duration;
|
||||
use crate::minecraft::handshake::Handshake;
|
||||
use crate::minecraft::varint::{varint_read, varint_write};
|
||||
use std::{fmt, io};
|
||||
@ -37,7 +39,7 @@ impl Client {
|
||||
}
|
||||
|
||||
pub async fn in_read(&mut self) -> Result<u8, String> {
|
||||
let mut buf = vec![0 as u8; 100];
|
||||
let mut buf = vec![0 as u8; 1024];
|
||||
|
||||
loop
|
||||
{
|
||||
@ -94,9 +96,9 @@ impl Client {
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn buffer_append(&mut self, data: Vec<u8>) -> Result<u8,String> {
|
||||
pub async fn buffer_append(&mut self, data: Vec<u8>) -> Result<(),String> {
|
||||
if data.len() == 0 {
|
||||
return Ok(0);
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
println!("[{}] buffer handling", self);
|
||||
@ -105,7 +107,7 @@ impl Client {
|
||||
println!("[{}] out stream present passing buffer", self);
|
||||
let _ = out_stream.writable().await;
|
||||
let _ = out_stream.try_write(&data);
|
||||
return Ok(0);
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
if self.buffer.len() + data.len() > 65536 {
|
||||
@ -114,18 +116,16 @@ impl Client {
|
||||
|
||||
let _ = self.buffer.extend(data);
|
||||
|
||||
if self.buffer[0] == 0 {
|
||||
return Err("invalid packet".to_string());
|
||||
}
|
||||
|
||||
if self.buffer.len() - 1 >= self.buffer[0] as usize {
|
||||
while self.buffer.len() != 0 && self.buffer.len() - 1 >= self.buffer[0] as usize {
|
||||
if self.buffer[0] == 0 {
|
||||
return Err("invalid packet".to_string());
|
||||
}
|
||||
println!("[{}] valid packet received", self);
|
||||
let len = varint_read(&mut self.buffer.clone().into())? as usize;
|
||||
self.handle_packet(self.buffer[1..=len].to_vec().into()).await?;
|
||||
self.buffer.drain(..len + 1);
|
||||
return Ok(1);
|
||||
}
|
||||
Ok(0)
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn send_packet(&self, data: Vec<u8>) {
|
||||
@ -133,6 +133,7 @@ impl Client {
|
||||
|
||||
sent_data.extend(data);
|
||||
let _ = self.in_stream.writable().await;
|
||||
println!("[{}] sending {:?}",self, sent_data);
|
||||
match self.in_stream.try_write(sent_data.as_slice()) {
|
||||
Err(e) => { eprintln!("error while sending response {:?}", e); },
|
||||
_ => { }
|
||||
|
||||
Reference in New Issue
Block a user