diff --git a/src/lib.rs b/src/lib.rs index d0313c2..cfe1d79 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,5 +1,5 @@ use std::{ - net::TcpListener, + net::{TcpListener, TcpStream}, path::{Path, PathBuf}, sync::{Arc, Mutex}, }; @@ -14,7 +14,7 @@ pub mod vfs; pub use anyhow::Result; use tungstenite::accept; -use crate::plugin::Plugin; +use crate::plugin::DynPlugin; pub use once_cell; #[derive(serde::Serialize, serde::Deserialize)] @@ -23,7 +23,7 @@ pub struct ServerConfig { } pub struct Server { - plugins: Mutex>>, + plugins: Mutex>, root: PathBuf, config: ServerConfig, } @@ -79,14 +79,16 @@ impl Server { for stream in listener.incoming() { match stream { Ok(stream) => { - Self::LOGGER.info(format!("New connection: {}", stream.peer_addr()?)); - let mut ws = accept(stream)?; - - let msg = ws.read()?; - ws.send(msg)?; + std::thread::spawn({ + let srv = self.clone(); + move || match srv.handle_client(stream) { + Ok(_) => {} + Err(e) => Self::LOGGER.error(format!("Client handler failed: {e}")), + } + }); } Err(e) => { - Self::LOGGER.error(format!("Connection failed: {}", e)); + Self::LOGGER.error(format!("Connection failed: {e}")); } } } @@ -94,7 +96,16 @@ impl Server { Ok(()) } - pub fn add_plugin(self: &Arc, plugin: Box) { + pub fn add_plugin(self: &Arc, plugin: DynPlugin) { self.plugins.lock().unwrap().push(plugin); } + + fn handle_client(self: &Arc, stream: TcpStream) -> anyhow::Result<()> { + Self::LOGGER.info(format!("New connection: {}", stream.peer_addr()?)); + let mut ws = accept(stream)?; + + let msg = ws.read()?; + ws.send(msg)?; + Ok(()) + } } diff --git a/src/loader.rs b/src/loader.rs index 7203d51..9e356cd 100644 --- a/src/loader.rs +++ b/src/loader.rs @@ -2,22 +2,22 @@ use std::path::Path; use libloading::{Library, Symbol}; -use crate::{logger, plugin::Plugin, vfs}; +use crate::{logger, plugin::DynPlugin, vfs}; logger! { const LOGGER "Loader" } -pub fn load_plugin(path: &Path) -> anyhow::Result> { +pub fn load_plugin(path: &Path) -> anyhow::Result { unsafe { LOGGER.info(format!("Loading plugin: {:?}", path)); let lib = Library::new(path)?; - let func: Symbol Box> = lib.get(b"load_plugin").unwrap(); + let func: Symbol DynPlugin> = lib.get(b"load_plugin").unwrap(); Ok(func()) } } -pub fn load_plugins(arr: &mut Vec>, path: &Path) -> crate::Result<()> { +pub fn load_plugins(arr: &mut Vec, path: &Path) -> crate::Result<()> { LOGGER.info("Loading plugins"); vfs::dir(path)?; if path.is_dir() { diff --git a/src/macros.rs b/src/macros.rs index 84ab00a..0a8fb8c 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -2,7 +2,7 @@ macro_rules! export_plugin { ($p:expr) => { #[unsafe(no_mangle)] - pub extern "C" fn load_plugin() -> Box { + pub extern "C" fn load_plugin() -> $crate::plugin::DynPlugin { $p } }; diff --git a/src/plugin.rs b/src/plugin.rs index 7dd2435..3e27c42 100644 --- a/src/plugin.rs +++ b/src/plugin.rs @@ -2,6 +2,8 @@ use std::sync::Arc; use crate::Server; +pub type DynPlugin = Box; + pub trait Plugin { fn init(&mut self, server: &Arc); }