(server) fix: Process results in thread itself instead of handling function
This commit is contained in:
@ -1,5 +1,6 @@
|
|||||||
use tokio::io::{ErrorKind, Interest};
|
use tokio::io::{self, ErrorKind, Interest};
|
||||||
use tokio::net::{TcpListener, TcpStream};
|
use tokio::net::{TcpListener, TcpStream};
|
||||||
|
use tokio::time::{self, Duration};
|
||||||
|
|
||||||
pub(crate) struct TcpServer;
|
pub(crate) struct TcpServer;
|
||||||
|
|
||||||
@ -11,7 +12,33 @@ impl TcpServer {
|
|||||||
Ok((socket, addr)) => {
|
Ok((socket, addr)) => {
|
||||||
log::info!("New client connected: {addr}");
|
log::info!("New client connected: {addr}");
|
||||||
|
|
||||||
tokio::spawn(async { Self::process(socket).await });
|
tokio::spawn(async move {
|
||||||
|
let mut retry_times = 0;
|
||||||
|
|
||||||
|
while retry_times <= 3 {
|
||||||
|
let result = Self::process(&socket).await;
|
||||||
|
|
||||||
|
match result {
|
||||||
|
Ok(_) => continue,
|
||||||
|
Err(e) if e.kind() == ErrorKind::WouldBlock => continue,
|
||||||
|
Err(e) => {
|
||||||
|
if retry_times == 3 {
|
||||||
|
log::error!("Failed to process this socket: {e}");
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
retry_times += 1;
|
||||||
|
|
||||||
|
log::error!(
|
||||||
|
"Failed to process this socket: {e}. Retry {retry_times} time(s)..."
|
||||||
|
);
|
||||||
|
|
||||||
|
time::sleep(Duration::from_secs(1)).await;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
Err(e) => log::error!("Couldn't get client: {e:?}"),
|
Err(e) => log::error!("Couldn't get client: {e:?}"),
|
||||||
}
|
}
|
||||||
@ -20,44 +47,26 @@ impl TcpServer {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn process(socket: TcpStream) {
|
async fn process(socket: &TcpStream) -> io::Result<()> {
|
||||||
loop {
|
let ready = socket
|
||||||
match socket.ready(Interest::READABLE | Interest::WRITABLE).await {
|
.ready(Interest::READABLE | Interest::WRITABLE)
|
||||||
Ok(ready) => {
|
.await?;
|
||||||
if ready.is_readable() {
|
if ready.is_readable() {
|
||||||
let mut buffer = Vec::with_capacity(1024);
|
// Size of per received data shound be smaller than 1KiB.
|
||||||
match socket.try_read_buf(&mut buffer) {
|
let mut buffer = [0; 1024];
|
||||||
// We need to write to client after reading from that,
|
let size = socket.try_read(&mut buffer)?;
|
||||||
// so we use `continue` here instead if `break`.
|
|
||||||
Ok(_) => {
|
|
||||||
log::info!("Received contents");
|
|
||||||
|
|
||||||
// TODO: Start dispatching messages here.
|
log::info!("Received {size} Bytes message.")
|
||||||
|
|
||||||
continue;
|
// TODO: Start dispatching messages here.
|
||||||
}
|
|
||||||
Err(e) if e.kind() == ErrorKind::WouldBlock => continue,
|
|
||||||
Err(e) => {
|
|
||||||
log::error!("Failed to read from TCP client socket: {e}");
|
|
||||||
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if ready.is_writable() {
|
|
||||||
match socket.try_write(b"Hello from server written in Rust!") {
|
|
||||||
Ok(_) => continue,
|
|
||||||
Err(e) if e.kind() == ErrorKind::WouldBlock => continue,
|
|
||||||
Err(e) => {
|
|
||||||
log::error!("Failed to write to TCP client socket: {e}");
|
|
||||||
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Err(e) => log::error!("Failed to wait for any states: {e}"),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
if ready.is_writable() {
|
||||||
|
// TODO: Start sending back message here.
|
||||||
|
let size = socket.try_write(b"Hello from server written in Rust!")?;
|
||||||
|
|
||||||
|
log::info!("Sent {size} Bytes message.")
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user