blob: f994ee7f4f3d49480057421f239669b9bd124da4 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
use snafu::ResultExt as _;
use tokio::prelude::*;
#[derive(Debug, snafu::Snafu)]
pub enum Error {
#[snafu(display("failed to parse address: {}", source))]
ParseAddress { source: std::net::AddrParseError },
#[snafu(display("failed to bind: {}", source))]
Bind { source: tokio::io::Error },
}
pub type Result<T> = std::result::Result<T, Error>;
pub fn cmd<'a, 'b>(app: clap::App<'a, 'b>) -> clap::App<'a, 'b> {
app.about("Run a termcast server")
}
pub fn run<'a>(_matches: &clap::ArgMatches<'a>) -> super::Result<()> {
run_impl().context(super::Server)
}
fn run_impl() -> Result<()> {
let addr = "127.0.0.1:8000".parse().context(ParseAddress)?;
let listener = tokio::net::TcpListener::bind(&addr).context(Bind)?;
let server = listener
.incoming()
.map_err(|e| {
eprintln!("accept failed: {}", e);
})
.for_each(|sock| {
crate::protocol::Message::read_async(sock)
.map(|msg| match msg {
crate::protocol::Message::StartCasting { username } => {
println!("got a connection from {}", username);
}
})
.map_err(|e| {
eprintln!("failed to read message: {}", e);
})
});
tokio::run(server);
Ok(())
}
|