[code] restructured into different crates
This commit is contained in:
13
crates/lanspread-client/Cargo.toml
Normal file
13
crates/lanspread-client/Cargo.toml
Normal 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 }
|
65
crates/lanspread-client/src/main.rs
Normal file
65
crates/lanspread-client/src/main.rs
Normal 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(())
|
||||
}
|
Reference in New Issue
Block a user