|
|
|
@ -8,7 +8,9 @@ use scheduled_thread_pool::ScheduledThreadPool;
|
|
|
|
|
use crate::event::Event;
|
|
|
|
|
use crate::event_handler::EventHandler;
|
|
|
|
|
use crate::result::VentedResult;
|
|
|
|
|
use crate::server::server_events::get_server_event_handler;
|
|
|
|
|
use crate::server::VentedServer;
|
|
|
|
|
use std::io::Write;
|
|
|
|
|
|
|
|
|
|
pub struct VentedTcpServer {
|
|
|
|
|
event_handler: Arc<Mutex<EventHandler>>,
|
|
|
|
@ -16,12 +18,14 @@ pub struct VentedTcpServer {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl VentedServer for VentedTcpServer {
|
|
|
|
|
/// Starts listening on the given address
|
|
|
|
|
fn listen(&mut self, address: &str) -> VentedResult<()> {
|
|
|
|
|
let listener = TcpListener::bind(address)?;
|
|
|
|
|
for stream in listener.incoming() {
|
|
|
|
|
log::trace!("Connection received.");
|
|
|
|
|
match stream {
|
|
|
|
|
Ok(stream) => self.handle_connection(stream),
|
|
|
|
|
Err(_) => {}
|
|
|
|
|
Err(e) => log::error!("Failed to handle connection: {}", e),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -41,11 +45,29 @@ impl VentedServer for VentedTcpServer {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl VentedTcpServer {
|
|
|
|
|
/// Creates a new server that runs on the specified number of threads
|
|
|
|
|
pub fn new(num_threads: usize) -> Self {
|
|
|
|
|
let event_handler = get_server_event_handler();
|
|
|
|
|
let pool = ScheduledThreadPool::new(num_threads);
|
|
|
|
|
|
|
|
|
|
Self {
|
|
|
|
|
event_handler: Arc::new(Mutex::new(event_handler)),
|
|
|
|
|
pool,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Handles what happens on connection
|
|
|
|
|
fn handle_connection(&mut self, mut stream: TcpStream) {
|
|
|
|
|
let handler = Arc::clone(&self.event_handler);
|
|
|
|
|
self.pool.execute(move || {
|
|
|
|
|
if let Ok(event) = Event::from_bytes(&mut stream) {
|
|
|
|
|
handler.lock().handle_event(event);
|
|
|
|
|
if let Some(mut event) = handler.lock().handle_event(event) {
|
|
|
|
|
if let Err(e) = stream.write(&event.as_bytes()) {
|
|
|
|
|
log::error!("Failed to respond to event: {}", e)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
log::warn!("Failed to create an Event from received bytes.")
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|