syndicate-rs/src/bin/syndicate-server.rs

27 lines
862 B
Rust
Raw Normal View History

2020-05-11 18:51:04 +00:00
use syndicate::{peer, spaces};
2019-10-16 15:54:58 +00:00
use std::sync::{Mutex, Arc};
use tokio::net::TcpListener;
2018-12-09 13:28:01 +00:00
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
2019-10-20 15:01:27 +00:00
let spaces = Arc::new(Mutex::new(spaces::Spaces::new()));
let mut id = 0;
let port = 8001;
let mut listener = TcpListener::bind(format!("0.0.0.0:{}", port)).await?;
println!("Listening on port {}", port);
loop {
let (stream, addr) = listener.accept().await?;
let connid = id;
2019-10-16 13:16:50 +00:00
let spaces = Arc::clone(&spaces);
2019-10-20 13:02:51 +00:00
id += 1;
tokio::spawn(async move {
2019-10-16 15:54:58 +00:00
match peer::Peer::new(connid, stream).await.run(spaces).await {
Ok(_) => println!("Connection {} ({:?}) terminated", connid, addr),
Err(e) => println!("Connection {} ({:?}) died with {:?}", connid, addr, e),
}
});
}
2018-12-09 13:28:01 +00:00
}