2022-06-04 19:34:53 +00:00
|
|
|
use std::net::TcpListener;
|
|
|
|
|
|
|
|
use crate::{
|
2022-06-05 14:08:13 +00:00
|
|
|
plugins::loader,
|
2022-06-04 19:34:53 +00:00
|
|
|
tcp::{handle_connection, Client},
|
|
|
|
};
|
|
|
|
|
2022-06-05 14:08:13 +00:00
|
|
|
#[tokio::main]
|
|
|
|
pub async fn start_server(host: &str, port: &str) -> anyhow::Result<()> {
|
2022-06-04 19:34:53 +00:00
|
|
|
// listen Tcp server
|
|
|
|
let listener = TcpListener::bind(format!("{host}:{port}"))?;
|
|
|
|
|
|
|
|
println!("Tcp server started at: {}", listener.local_addr()?);
|
|
|
|
|
|
|
|
// load plugins and commands
|
2022-06-05 14:08:13 +00:00
|
|
|
let (command_manager, _plugin_manager) = loader()?;
|
2022-06-04 19:34:53 +00:00
|
|
|
|
|
|
|
// Accepts a new incoming connection from this listener.
|
|
|
|
while let Ok((stream, _address)) = listener.accept() {
|
|
|
|
let client = Client::new(stream);
|
|
|
|
|
2022-06-05 14:08:13 +00:00
|
|
|
let command_manager = command_manager.clone();
|
2022-06-04 19:34:53 +00:00
|
|
|
|
|
|
|
// handle client connection in new thread
|
2022-06-05 14:08:13 +00:00
|
|
|
tokio::spawn(handle_connection(client, command_manager));
|
2022-06-04 19:34:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|