[code] restructured into different crates

This commit is contained in:
2024-11-08 10:05:24 +01:00
parent 70e3aaea17
commit 04a39790b8
11 changed files with 202 additions and 301 deletions

View File

@ -0,0 +1,13 @@
[package]
name = "lanspread-client"
version = "0.1.0"
edition = "2021"
[dependencies]
# local
lanspread-db = { path = "../lanspread-db" }
# external
eyre = { workspace = true }
s2n-quic = { workspace = true }
serde_json = { workspace = true }
tokio = { workspace = true }

View File

@ -0,0 +1,65 @@
use std::{net::SocketAddr, sync::Arc};
use lanspread_db::{Game, GameDB};
use s2n_quic::{client::Connect, Client as QuicClient};
use tokio::{io::AsyncWriteExt as _, sync::Mutex};
static CERT_PEM: &str = include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/../../cert.pem"));
const SERVER_ADDR: &str = "127.0.0.1";
const SERVER_PORT: u16 = 13337;
struct Client {
db: Arc<Mutex<GameDB>>,
}
impl Client {
pub(crate) fn new() -> Self {
Client {
db: Arc::new(Mutex::new(GameDB::new())),
}
}
pub(crate) async fn run(&mut self, addr: SocketAddr) -> eyre::Result<()> {
let client = QuicClient::builder()
.with_tls(CERT_PEM)?
.with_io("0.0.0.0:0")?
.start()?;
let connect1 = Connect::new(addr).with_server_name("localhost");
let mut connection1 = client.connect(connect1).await?;
connection1.keep_alive(true)?;
let stream = connection1.open_bidirectional_stream().await?;
let (mut rx, mut tx) = stream.split();
let buf = b"get_games";
tx.write_all(&buf[..]).await?;
while let Ok(Some(data)) = rx.receive().await {
let games: Vec<Game> = serde_json::from_slice(&data)?;
self.db = Arc::new(Mutex::new(GameDB::from(games)));
tx.close().await.unwrap();
let db = self.db.lock().await;
eprintln!("received GameDB:");
for game in db.games.values() {
eprintln!("{:#?}", game);
}
}
eprintln!("server closed");
Ok(())
}
}
#[tokio::main]
async fn main() -> eyre::Result<()> {
let mut client = Client::new();
client
.run(format!("{SERVER_ADDR}:{SERVER_PORT}").parse().unwrap())
.await?;
Ok(())
}