63 lines
1.8 KiB
Rust
63 lines
1.8 KiB
Rust
#[macro_use] extern crate log;
|
|
|
|
use anyhow::{Context, Result};
|
|
use futures::stream::{FuturesUnordered, StreamExt};
|
|
use std::net::ToSocketAddrs;
|
|
use structopt::StructOpt;
|
|
use warp::{serve, ws, ws::Ws, Filter};
|
|
use ws::Message;
|
|
|
|
pub mod net;
|
|
|
|
#[derive(StructOpt)]
|
|
/// Server for base2020 lockstep protocol for multiplayer games.
|
|
struct Args {
|
|
/// The socket address to listen for connections on;
|
|
/// can be a hostname to bind to multiple hosts at once,
|
|
/// such as to listen on both IPv4 & IPv6.
|
|
listen: String,
|
|
}
|
|
|
|
#[tokio::main]
|
|
async fn main() -> Result<()> {
|
|
env_logger::init();
|
|
let args = Args::from_args();
|
|
|
|
let socket_handler = ws().map(|upgrade: Ws| {
|
|
upgrade.on_upgrade(|websocket| {
|
|
let (mut sink, mut source) = websocket.split();
|
|
async move {
|
|
loop {
|
|
match source.next().await {
|
|
Some(Ok(msg)) => {
|
|
if let Ok(text) = msg.to_str() {
|
|
debug!("Text message: {}", text);
|
|
} else {
|
|
debug!("Other message");
|
|
}
|
|
},
|
|
Some(Err(err)) => {
|
|
warn!("Websocket connection lost: {}", err);
|
|
},
|
|
None => break,
|
|
}
|
|
}
|
|
}
|
|
})
|
|
});
|
|
|
|
let addrs = args
|
|
.listen
|
|
.to_socket_addrs()
|
|
.context("Couldn't parse the listen address")?;
|
|
let servers = FuturesUnordered::new();
|
|
for addr in addrs {
|
|
let (_, server) = serve(socket_handler).try_bind_ephemeral(addr)?;
|
|
servers.push(server);
|
|
}
|
|
|
|
servers.for_each(|_| async {}).await;
|
|
|
|
Ok(())
|
|
}
|