generated from PlexSheep/rs-base
We have a server at least #3
|
@ -44,8 +44,9 @@ impl Server {
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn handle_stream(&self, stream: (TcpStream, SocketAddr)) -> Result<()> {
|
async fn handle_stream(&self, stream: (TcpStream, SocketAddr)) -> Result<()> {
|
||||||
|
const BUF_SIZE: usize = 1024;
|
||||||
info!("start handling stream {:?}", stream.1);
|
info!("start handling stream {:?}", stream.1);
|
||||||
let mut buf = Vec::new();
|
let mut buf = [0u8; BUF_SIZE];
|
||||||
let mut reader = BufReader::new(stream.0);
|
let mut reader = BufReader::new(stream.0);
|
||||||
let mut len;
|
let mut len;
|
||||||
loop {
|
loop {
|
||||||
|
@ -61,7 +62,7 @@ impl Server {
|
||||||
humanbytes(len),
|
humanbytes(len),
|
||||||
self.decode(&buf)?
|
self.decode(&buf)?
|
||||||
);
|
);
|
||||||
buf.clear();
|
buf = [0; BUF_SIZE];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
info!("stop handling stream {:?}", stream.1);
|
info!("stop handling stream {:?}", stream.1);
|
||||||
|
@ -69,14 +70,14 @@ impl Server {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn decode(&self, buf: &Vec<u8>) -> Result<String> {
|
fn decode(&self, buf: &[u8]) -> Result<String> {
|
||||||
Ok(String::from_utf8(buf.clone())?)
|
Ok(std::str::from_utf8(buf)?.to_string())
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn read(&self, reader: &mut BufReader<TcpStream>, buf: &mut Vec<u8>) -> Result<usize> {
|
async fn read(&self, reader: &mut BufReader<TcpStream>, buf: &mut [u8]) -> Result<usize> {
|
||||||
let mut len: usize;
|
let mut len: usize;
|
||||||
let mut failsafe: u8 = 0;
|
let mut failsafe: u8 = 0;
|
||||||
while failsafe < u8::MAX {
|
while failsafe < 5 {
|
||||||
trace!("loop");
|
trace!("loop");
|
||||||
len = match timeout(self.cfg.timeout, reader.read(buf)).await {
|
len = match timeout(self.cfg.timeout, reader.read(buf)).await {
|
||||||
Ok(inner) => {
|
Ok(inner) => {
|
||||||
|
@ -98,8 +99,15 @@ impl Server {
|
||||||
Err(anyhow!("read too often, so the failsafe activated"))
|
Err(anyhow!("read too often, so the failsafe activated"))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn should_end(&self, buf: &Vec<u8>) -> bool {
|
fn should_end(&self, buf: &[u8]) -> bool {
|
||||||
debug!("eval should end: {:?}", buf);
|
let mut lb: u8 = buf[0];
|
||||||
buf.contains(&0x00)
|
let mut iter = buf.iter().skip(1).peekable();
|
||||||
|
while let Some(b) = iter.next() {
|
||||||
|
if lb != 0 && *b == 0 && **iter.peek().unwrap() == 0 {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
lb = *b;
|
||||||
|
}
|
||||||
|
return false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue