mirror of
https://github.com/MedzikUser/servers
synced 2024-08-14 23:57:48 +00:00
docs: comment code
This commit is contained in:
parent
25c2f0baa3
commit
84eed33e24
8 changed files with 20 additions and 9 deletions
|
@ -5,6 +5,7 @@ mod id;
|
||||||
use self::{disconnect::Disconnect, help::Help, id::ID};
|
use self::{disconnect::Disconnect, help::Help, id::ID};
|
||||||
use crate::plugins::prelude::*;
|
use crate::plugins::prelude::*;
|
||||||
|
|
||||||
|
/// Register default commands
|
||||||
pub fn register_commands() -> Vec<Box<dyn Command>> {
|
pub fn register_commands() -> Vec<Box<dyn Command>> {
|
||||||
vec![Box::new(Help), Box::new(Disconnect), Box::new(ID)]
|
vec![Box::new(Help), Box::new(Disconnect), Box::new(ID)]
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,13 +2,15 @@ use std::{collections::HashMap, sync::Mutex};
|
||||||
|
|
||||||
use lazy_static::lazy_static;
|
use lazy_static::lazy_static;
|
||||||
|
|
||||||
use crate::tcp::Client;
|
use crate::server::Client;
|
||||||
|
|
||||||
pub mod commands;
|
pub mod commands;
|
||||||
pub mod plugins;
|
pub mod plugins;
|
||||||
pub mod tcp;
|
pub mod server;
|
||||||
|
|
||||||
lazy_static! {
|
lazy_static! {
|
||||||
|
/// List with all connected clients
|
||||||
pub static ref CLIENTS: Mutex<HashMap<usize, Client>> = Mutex::new(HashMap::new());
|
pub static ref CLIENTS: Mutex<HashMap<usize, Client>> = Mutex::new(HashMap::new());
|
||||||
|
/// Next ID of the client to be add to [CLIENTS]
|
||||||
pub static ref CLIENT_NEXT: Mutex<usize> = Mutex::new(0);
|
pub static ref CLIENT_NEXT: Mutex<usize> = Mutex::new(0);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
use clap::Parser;
|
use clap::Parser;
|
||||||
use servers::tcp::server;
|
use servers::server;
|
||||||
|
|
||||||
#[derive(Debug, Parser)]
|
#[derive(Debug, Parser)]
|
||||||
#[clap(
|
#[clap(
|
||||||
|
|
|
@ -12,5 +12,5 @@ pub mod prelude {
|
||||||
pub use async_trait::async_trait;
|
pub use async_trait::async_trait;
|
||||||
|
|
||||||
pub use self::types::*;
|
pub use self::types::*;
|
||||||
pub use crate::tcp::Client;
|
pub use crate::server::Client;
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,7 +2,7 @@ use std::any::Any;
|
||||||
|
|
||||||
use async_trait::async_trait;
|
use async_trait::async_trait;
|
||||||
|
|
||||||
use crate::{plugins::manager::PluginsManager, tcp::Client};
|
use crate::{plugins::manager::PluginsManager, server::Client};
|
||||||
|
|
||||||
// A main plugin trait.
|
// A main plugin trait.
|
||||||
#[async_trait]
|
#[async_trait]
|
||||||
|
|
|
@ -14,14 +14,20 @@ use crate::plugins::manager::PluginsManagerType;
|
||||||
/// Max length of a TCP and UDP packet
|
/// Max length of a TCP and UDP packet
|
||||||
pub const MAX_PACKET_LEN: usize = 65536;
|
pub const MAX_PACKET_LEN: usize = 65536;
|
||||||
|
|
||||||
|
/// Client struct
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct Client {
|
pub struct Client {
|
||||||
|
/// ID of the client
|
||||||
pub id: usize,
|
pub id: usize,
|
||||||
|
/// Connection stream of the client
|
||||||
pub stream: ClientStream,
|
pub stream: ClientStream,
|
||||||
|
/// Custom Client Map
|
||||||
pub map: HashMap<String, ClientMapValue>,
|
pub map: HashMap<String, ClientMapValue>,
|
||||||
|
/// Plugins Manager
|
||||||
pub plugins_manager: PluginsManagerType,
|
pub plugins_manager: PluginsManagerType,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Value type of the client map entry
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub enum ClientMapValue {
|
pub enum ClientMapValue {
|
||||||
/// String type
|
/// String type
|
||||||
|
@ -165,7 +171,7 @@ impl Client {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Close the connection
|
/// Close the client connection
|
||||||
pub fn close(&self) -> anyhow::Result<()> {
|
pub fn close(&self) -> anyhow::Result<()> {
|
||||||
match &self.stream {
|
match &self.stream {
|
||||||
ClientStream::TCP(stream) => stream.shutdown(Shutdown::Both)?,
|
ClientStream::TCP(stream) => stream.shutdown(Shutdown::Both)?,
|
|
@ -1,4 +1,5 @@
|
||||||
mod client;
|
mod client;
|
||||||
pub mod server;
|
mod server;
|
||||||
|
|
||||||
pub use client::*;
|
pub use client::*;
|
||||||
|
pub use server::*;
|
|
@ -7,18 +7,19 @@ use tracing::{error, info};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
plugins::{self, manager::PluginsManagerType},
|
plugins::{self, manager::PluginsManagerType},
|
||||||
tcp::Client,
|
server::Client,
|
||||||
CLIENTS, CLIENT_NEXT,
|
CLIENTS, CLIENT_NEXT,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub const PLUGINS_DIR: &str = "plugins";
|
pub const PLUGINS_DIR: &str = "plugins";
|
||||||
|
|
||||||
lazy_static! {
|
lazy_static! {
|
||||||
|
/// Plugin manager, where you can find loaded plugins, commands and events
|
||||||
pub static ref PLUGINS_MANAGER: PluginsManagerType =
|
pub static ref PLUGINS_MANAGER: PluginsManagerType =
|
||||||
plugins::loader(PLUGINS_DIR).expect("failed to load plugins");
|
plugins::loader(PLUGINS_DIR).expect("failed to load plugins");
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Start server
|
/// Start servers
|
||||||
pub fn run(tcp_host: String, ws_host: String) -> anyhow::Result<()> {
|
pub fn run(tcp_host: String, ws_host: String) -> anyhow::Result<()> {
|
||||||
info!("Loaded {} plugins", PLUGINS_MANAGER.plugins.len());
|
info!("Loaded {} plugins", PLUGINS_MANAGER.plugins.len());
|
||||||
info!("Loaded {} commands", PLUGINS_MANAGER.commands.len());
|
info!("Loaded {} commands", PLUGINS_MANAGER.commands.len());
|
Loading…
Reference in a new issue