[code] remove server struct

This commit is contained in:
ddidderr 2024-11-14 16:04:56 +01:00
parent a2a630893f
commit c00b7dbe9c
Signed by: ddidderr
GPG Key ID: 3841F1C27E6F0E14

View File

@ -27,8 +27,6 @@ mod assets;
static KEY_PEM: &str = include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/../../key.pem")); static KEY_PEM: &str = include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/../../key.pem"));
static CERT_PEM: &str = include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/../../cert.pem")); static CERT_PEM: &str = include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/../../cert.pem"));
struct Server;
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
struct ServerCtx { struct ServerCtx {
handler: RequestHandler, handler: RequestHandler,
@ -40,75 +38,68 @@ struct ConnectionCtx {
remote_addr: String, remote_addr: String,
} }
impl Server { async fn run(addr: SocketAddr, db: GameDB) -> eyre::Result<()> {
async fn run(addr: SocketAddr, db: GameDB) -> eyre::Result<()> { let mut server = QuicServer::builder()
let mut server = QuicServer::builder() .with_tls((CERT_PEM, KEY_PEM))?
.with_tls((CERT_PEM, KEY_PEM))? .with_io(addr)?
.with_io(addr)? .start()?;
.start()?;
let server_ctx = Arc::new(ServerCtx { let server_ctx = Arc::new(ServerCtx {
handler: RequestHandler::new(db), handler: RequestHandler::new(db),
});
while let Some(mut connection) = server.accept().await {
let conn_ctx = Arc::new(ConnectionCtx {
server_ctx: server_ctx.clone(),
remote_addr: maybe_addr!(connection.remote_addr()),
}); });
// spawn a new task for the connection
tokio::spawn(async move {
tracing::info!("{} connected", conn_ctx.remote_addr);
while let Some(mut connection) = server.accept().await { while let Ok(Some(stream)) = connection.accept_bidirectional_stream().await {
let conn_ctx = Arc::new(ConnectionCtx { let (mut rx, mut tx) = stream.split();
server_ctx: server_ctx.clone(),
remote_addr: maybe_addr!(connection.remote_addr()),
});
// spawn a new task for the connection
tokio::spawn(async move {
tracing::info!("{} connected", conn_ctx.remote_addr);
while let Ok(Some(stream)) = connection.accept_bidirectional_stream().await { let conn_ctx = conn_ctx.clone();
let (mut rx, mut tx) = stream.split(); // spawn a new task for the stream
tokio::spawn(async move {
tracing::trace!("{} stream opened", conn_ctx.remote_addr);
let conn_ctx = conn_ctx.clone(); // handle streams
// spawn a new task for the stream while let Ok(Some(data)) = rx.receive().await {
tokio::spawn(async move { tracing::trace!(
tracing::trace!("{} stream opened", conn_ctx.remote_addr); "{} msg: (raw): {}",
conn_ctx.remote_addr,
String::from_utf8_lossy(&data)
);
// handle streams let request = Request::decode(data);
while let Ok(Some(data)) = rx.receive().await { tracing::debug!("{} msg: {:?}", conn_ctx.remote_addr, request);
tracing::trace!( let response = conn_ctx.server_ctx.handler.handle_request(request).await;
"{} msg: (raw): {}", tracing::trace!("{} server response: {:?}", conn_ctx.remote_addr, response);
conn_ctx.remote_addr, let raw_response = response.encode();
String::from_utf8_lossy(&data) tracing::trace!(
); "{} server response (raw): {}",
conn_ctx.remote_addr,
String::from_utf8_lossy(&raw_response)
);
let request = Request::decode(data); // write response back to client
tracing::debug!("{} msg: {:?}", conn_ctx.remote_addr, request); if let Err(e) = tx.write_all(&raw_response).await {
let response = tracing::error!(?e);
conn_ctx.server_ctx.handler.handle_request(request).await;
tracing::trace!(
"{} server response: {:?}",
conn_ctx.remote_addr,
response
);
let raw_response = response.encode();
tracing::trace!(
"{} server response (raw): {}",
conn_ctx.remote_addr,
String::from_utf8_lossy(&raw_response)
);
// write response back to client
if let Err(e) = tx.write_all(&raw_response).await {
tracing::error!(?e);
}
// close the stream
if let Err(e) = tx.close().await {
tracing::error!(?e);
}
} }
});
}
});
}
Ok(()) // close the stream
if let Err(e) = tx.close().await {
tracing::error!(?e);
}
}
});
}
});
} }
Ok(())
} }
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
@ -225,5 +216,5 @@ async fn main() -> eyre::Result<()> {
tracing::info!("Server listening on {}:{}", cli.ip, cli.port); tracing::info!("Server listening on {}:{}", cli.ip, cli.port);
Server::run(SocketAddr::from((cli.ip, cli.port)), game_db).await run(SocketAddr::from((cli.ip, cli.port)), game_db).await
} }