(server) chore:Complete basic message receive and send logic (#5)
This commit is contained in:
@ -1,4 +1,4 @@
|
||||
use tokio::io::AsyncWriteExt;
|
||||
use tokio::io::{ErrorKind, Interest};
|
||||
use tokio::net::{TcpListener, TcpStream};
|
||||
|
||||
pub(crate) struct TcpServer;
|
||||
@ -20,10 +20,44 @@ impl TcpServer {
|
||||
}
|
||||
}
|
||||
|
||||
async fn process(mut socket: TcpStream) {
|
||||
socket
|
||||
.write_all("Hello from server written in Rust!".as_bytes())
|
||||
.await
|
||||
.unwrap();
|
||||
async fn process(socket: TcpStream) {
|
||||
loop {
|
||||
match socket.ready(Interest::READABLE | Interest::WRITABLE).await {
|
||||
Ok(ready) => {
|
||||
if ready.is_readable() {
|
||||
let mut buffer = Vec::with_capacity(1024);
|
||||
match socket.try_read_buf(&mut buffer) {
|
||||
// We need to write to client after reading from that,
|
||||
// so we use `continue` here instead if `break`.
|
||||
Ok(_) => {
|
||||
log::info!("Received contents");
|
||||
|
||||
// TODO: Start dispatching messages here.
|
||||
|
||||
continue;
|
||||
}
|
||||
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(_) => break,
|
||||
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}"),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user