Implemented a tcp listener
This commit is contained in:
50
src/main.rs
50
src/main.rs
@@ -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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user