Implemented a tcp listener

This commit is contained in:
2019-06-22 21:17:04 +02:00
parent e5dc929135
commit 5a7ecaee6b
5 changed files with 1627 additions and 3 deletions

View File

@@ -1,8 +1,13 @@
#[macro_use]
extern crate clap;
extern crate tokio;
mod configuration;
use crate::configuration::Settings;
use tokio::io;
use tokio::net::TcpListener;
use tokio::prelude::*;
/**
* Initiates an instance of the gopherbridge server and
@@ -14,7 +19,46 @@ fn main() {
println!("Program listens on {}", settings.listen);
println!("Loading plugins {:?}", settings.plugins);
println!("Listening on port {}", settings.port);
if settings.foreground {
println!("Server runs in the foreground");
}
let addr = format!("{}:{}", settings.listen, settings.port)
.parse()
.unwrap();
let listener = TcpListener::bind(&addr).unwrap();
let server = listener
.incoming()
.for_each(|socket| {
let (reader, writer) = socket.split();
let amount = io::copy(reader, writer);
let msg = amount.then(|result| {
match result {
Ok((amount, _, _)) => println!("wrote {} bytes", amount),
Err(e) => println!("error: {}", e),
}
Ok(())
});
tokio::spawn(msg);
Ok(())
})
.map_err(|err| {
// Handle error by stdout
println!("accept error = {:?}", err);
});
println!(
"Started the server on {}:{}",
settings.listen, settings.port
);
// Start the server
//
// This does a few things:
//
// * Start the Tokio runtime
// * Spawns the `server` task onto the runtime.
// * Blocks the current thread until the runtime becomes idle, i.e. all
// spawned tasks have completed.
tokio::run(server);
}