mirror of
https://github.com/MedzikUser/servers
synced 2024-08-14 23:57:48 +00:00
add missing doc and handle if client sent an empty buffer
This commit is contained in:
parent
b0c019525e
commit
2c144ae14a
5 changed files with 35 additions and 11 deletions
|
@ -19,6 +19,8 @@
|
|||
//!
|
||||
//! Go to [plugins](plugins) module
|
||||
|
||||
#![warn(missing_docs)]
|
||||
|
||||
pub mod commands;
|
||||
pub mod plugins;
|
||||
pub mod tcp;
|
||||
|
|
|
@ -19,6 +19,7 @@ pub trait Plugin: Any + Send + Sync {
|
|||
async fn on_plugin_unload(&self);
|
||||
}
|
||||
|
||||
/// Trait with function to register plugin.
|
||||
pub trait PluginRegistrar {
|
||||
/// Function to register the plugin
|
||||
fn register(&mut self, plugin: Box<dyn Plugin>);
|
||||
|
@ -32,11 +33,12 @@ impl PluginRegistrar for PluginManager {
|
|||
|
||||
/// Plugin Manager
|
||||
pub struct PluginManager {
|
||||
/// Vector with all loaded plugins.
|
||||
pub plugins: Vec<Box<dyn Plugin>>,
|
||||
}
|
||||
|
||||
impl PluginManager {
|
||||
/// Create empty `PluginManager`
|
||||
/// Create empty `PluginManager`.
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
plugins: Vec::new(),
|
||||
|
@ -50,8 +52,10 @@ impl Default for PluginManager {
|
|||
}
|
||||
}
|
||||
|
||||
/// Plugin Manager Type
|
||||
pub type PluginManagerType = Arc<PluginManager>;
|
||||
|
||||
/// Trait with command functions to implement on struct.
|
||||
#[async_trait]
|
||||
pub trait Command: Any + Send + Sync {
|
||||
/// Command name
|
||||
|
@ -69,11 +73,12 @@ pub trait Command: Any + Send + Sync {
|
|||
|
||||
/// Command Manager
|
||||
pub struct CommandManager {
|
||||
/// Vector with all commands.
|
||||
pub commands: Vec<Box<dyn Command>>,
|
||||
}
|
||||
|
||||
impl CommandManager {
|
||||
/// Create empty `CommandManager`
|
||||
/// Create empty `CommandManager`.
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
commands: Vec::new(),
|
||||
|
@ -87,10 +92,12 @@ impl Default for CommandManager {
|
|||
}
|
||||
}
|
||||
|
||||
/// Command Manager Type
|
||||
pub type CommandManagerType = Arc<CommandManager>;
|
||||
|
||||
/// Trait with function to register command.
|
||||
pub trait CommandRegistrar {
|
||||
/// Function to register the plugin and the commands in the plugin
|
||||
/// Function to register the plugin and the commands in the plugin.
|
||||
fn register(&mut self, command: Box<dyn Command>);
|
||||
}
|
||||
|
||||
|
@ -100,6 +107,7 @@ impl CommandRegistrar for CommandManager {
|
|||
}
|
||||
}
|
||||
|
||||
/// Trait with event functions to implement on struct.
|
||||
#[async_trait]
|
||||
pub trait Event: Any + Send + Sync {
|
||||
/// Event name (onConnect, onSend)
|
||||
|
@ -110,6 +118,7 @@ pub trait Event: Any + Send + Sync {
|
|||
|
||||
/// Event Manager
|
||||
pub struct EventManager {
|
||||
/// Vector with all events loaded from plugins.
|
||||
pub events: Vec<Box<dyn Event>>,
|
||||
}
|
||||
|
||||
|
@ -126,10 +135,13 @@ impl Default for EventManager {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
/// Event Manager Type
|
||||
pub type EventManagerType = Arc<EventManager>;
|
||||
|
||||
/// Trait with function to register event.
|
||||
pub trait EventRegistrar {
|
||||
/// Function to register the plugin and the commands in the plugin
|
||||
/// Function to register the plugin and the commands in the plugin.
|
||||
fn register(&mut self, command: Box<dyn Event>);
|
||||
}
|
||||
|
||||
|
|
|
@ -71,7 +71,7 @@
|
|||
//! }
|
||||
//! }
|
||||
//!
|
||||
//! /// Register plugin and command
|
||||
//! /// Register plugin and event
|
||||
//! #[no_mangle]
|
||||
//! pub fn plugin_entry(
|
||||
//! plugin: &mut dyn PluginRegistrar,
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
#![allow(clippy::unused_io_amount)]
|
||||
|
||||
/// Max size of a TCP packet
|
||||
pub const MAX_PACKET_LEN: usize = 65536;
|
||||
|
||||
use std::{
|
||||
|
@ -7,8 +8,9 @@ use std::{
|
|||
net::TcpStream,
|
||||
};
|
||||
|
||||
/// TCP Client stream
|
||||
/// TCP Client
|
||||
pub struct Client {
|
||||
/// TCP stream of this client
|
||||
pub stream: TcpStream,
|
||||
}
|
||||
|
||||
|
@ -18,7 +20,7 @@ impl Client {
|
|||
Self { stream }
|
||||
}
|
||||
|
||||
/// Read message/buffer from Client
|
||||
/// Read message/buffer from client
|
||||
pub fn read(&mut self) -> anyhow::Result<String> {
|
||||
// allocate an empty buffer
|
||||
let mut buf = [0; MAX_PACKET_LEN];
|
||||
|
@ -26,13 +28,13 @@ impl Client {
|
|||
// read buffer from stream
|
||||
self.stream.read(&mut buf)?;
|
||||
|
||||
// encode &[u8] to a String and replace null spaces (empty `\0` bytes)
|
||||
// encode &[u8] to a String and delete null bytes (empty `\0` bytes)
|
||||
let decoded = String::from_utf8(buf.to_vec())?.replace('\0', "");
|
||||
|
||||
Ok(decoded)
|
||||
}
|
||||
|
||||
/// Send message to Client
|
||||
/// Send message to client
|
||||
pub fn send(&mut self, content: &str) -> io::Result<()> {
|
||||
// add a new line at the end of the content
|
||||
let content = format!("{content}\n\r");
|
||||
|
|
|
@ -24,8 +24,16 @@ pub async fn handle_connection(
|
|||
// run `onSend` events from plugins
|
||||
check_event(&mut client, &events, "onSend").await;
|
||||
|
||||
// split message by whitespace
|
||||
let args: &Vec<&str> = &buf.split_ascii_whitespace().collect();
|
||||
// split message by whitespaces
|
||||
let args: Vec<&str> = buf.split_ascii_whitespace().collect();
|
||||
|
||||
// client sent an empty buffer
|
||||
if args.is_empty() {
|
||||
client.send("empty buffer").expect("send message");
|
||||
|
||||
// don't execute the following commands because it causes panic
|
||||
continue
|
||||
}
|
||||
|
||||
// get command from args
|
||||
let cmd = args[0];
|
||||
|
|
Loading…
Reference in a new issue