servers/src/tcp/handle_connection.rs

99 lines
2.7 KiB
Rust
Raw Normal View History

2022-06-04 19:34:53 +00:00
use std::io::Write;
use log::{error, trace, info};
use crate::plugins::PluginManagerType;
2022-06-04 19:34:53 +00:00
use super::Client;
2022-06-05 20:10:23 +00:00
/// Handle Client connection
2022-06-04 19:34:53 +00:00
pub async fn handle_connection(
mut client: Client,
plugin_manager: PluginManagerType,
2022-06-04 19:34:53 +00:00
) -> anyhow::Result<()> {
info!("New Client: {}", client.stream.peer_addr()?);
2022-06-04 19:34:53 +00:00
2022-06-16 17:31:09 +00:00
// run `onConnect` events from plugins
2022-06-25 21:35:34 +00:00
check_event(&mut client, &plugin_manager, "onConnect").await?;
2022-06-16 17:31:09 +00:00
2022-06-04 19:34:53 +00:00
loop {
// read client message/buffer
let buf = client.read()?;
2022-06-04 19:34:53 +00:00
2022-06-16 17:31:09 +00:00
// run `onSend` events from plugins
2022-06-25 21:35:34 +00:00
check_event(&mut client, &plugin_manager, "onSend").await?;
2022-06-16 17:31:09 +00:00
2022-06-25 21:35:34 +00:00
// split the message by whitespaces and collect it into Vec<&str>
let mut args = buf.split_ascii_whitespace().collect::<Vec<&str>>();
// client sent an empty buffer
if args.is_empty() {
2022-06-25 21:35:34 +00:00
client.send("empty buffer")?;
// don't execute the following commands because it causes panic
continue;
}
2022-06-04 19:34:53 +00:00
// get command from args
let cmd = args[0];
2022-06-25 21:35:34 +00:00
// remove command name from args
args = args[1..args.len()].to_vec();
2022-06-04 19:34:53 +00:00
// search if a command exists
for command in plugin_manager.commands.iter() {
2022-06-04 19:34:53 +00:00
// if this is the entered command
if cmd == command.name() {
trace!("Executing a command `{}`", command.name());
2022-06-16 17:31:09 +00:00
// execute command
2022-06-25 21:35:34 +00:00
match command.execute(&mut client, args, &plugin_manager).await {
Ok(_) => (),
Err(err) => {
error!("failed to execute command `{cmd}`, error message = `{err}`");
2022-06-25 21:35:34 +00:00
client.send(&format!("error: {err}"))?;
}
}
2022-06-04 19:34:53 +00:00
// don't search for more commands
break;
}
}
// if an I/O or EOF error, abort the connection
if client.stream.flush().is_err() {
// terminate connection
break;
}
}
Ok(())
}
2022-06-16 17:31:09 +00:00
2022-06-17 11:43:23 +00:00
/// Search for a events and execute it
2022-06-25 21:35:34 +00:00
async fn check_event(
client: &mut Client,
events: &PluginManagerType,
event_name: &str,
) -> anyhow::Result<()> {
2022-06-16 17:31:09 +00:00
for event in events.events.iter() {
// check if this event should be started
if event.name() == event_name {
trace!("Executing a event `{}`", event.name());
// execute event
2022-06-25 21:35:34 +00:00
match event.execute(client).await {
Ok(_) => (),
Err(err) => {
error!("failed to execute event `{event_name}`, error message = `{err}`");
2022-06-25 21:35:34 +00:00
client.send(&format!("error: {err}"))?;
}
}
2022-06-16 17:31:09 +00:00
}
}
2022-06-25 21:35:34 +00:00
Ok(())
2022-06-16 17:31:09 +00:00
}