mirror of
https://github.com/Astatin3/unshell.git
synced 2026-06-09 06:47:59 -06:00
89 lines
2.3 KiB
Rust
89 lines
2.3 KiB
Rust
use std::{
|
|
io::{Read, Write},
|
|
net::{TcpListener, TcpStream},
|
|
sync::{Arc, Mutex},
|
|
thread::{self, JoinHandle},
|
|
};
|
|
|
|
use crate::*;
|
|
|
|
pub struct ListenerRuntime {
|
|
thread_handle: JoinHandle<()>,
|
|
// listener: TcpListener,
|
|
streams: Arc<Mutex<Vec<TcpStream>>>,
|
|
// reader: BufReader<TcpListener>,
|
|
// writer: BufWriter<TcpListener>,
|
|
}
|
|
|
|
impl ListenerRuntime {
|
|
pub fn new() -> ListenerRuntime {
|
|
// info!("Starting listener runtime on {}",);
|
|
let listener = TcpListener::bind("127.0.0.1:1234").unwrap();
|
|
let streams = Arc::new(Mutex::new(Vec::new()));
|
|
|
|
let streams_clone = streams.clone();
|
|
|
|
let thread_handle = thread::spawn(move || {
|
|
let streams = streams_clone.clone();
|
|
for stream in listener.incoming() {
|
|
let stream = stream.unwrap();
|
|
println!("New connection from {}", stream.peer_addr().unwrap());
|
|
streams.lock().unwrap().push(stream);
|
|
}
|
|
});
|
|
Self {
|
|
thread_handle,
|
|
streams,
|
|
}
|
|
}
|
|
|
|
pub fn send(&mut self, announcement: &Announcement) -> Result<(), std::io::Error> {
|
|
let bytes = announcement.encode();
|
|
|
|
let mut streams = self.streams.lock().unwrap();
|
|
|
|
for stream in streams.iter_mut() {
|
|
stream.write_all(&u32::to_be_bytes(bytes.len() as u32))?;
|
|
stream.write_all(&bytes)?;
|
|
stream.flush()?;
|
|
}
|
|
|
|
println!("Announcement {:?} sent", announcement);
|
|
|
|
Ok(())
|
|
}
|
|
|
|
pub fn recv(&mut self) -> Result<Announcement, ModuleError> {
|
|
let stream = &mut self.streams.lock().unwrap()[0];
|
|
|
|
let mut size_buf = [0u8; 4];
|
|
stream.read_exact(&mut size_buf).unwrap();
|
|
let size = u32::from_be_bytes(size_buf);
|
|
|
|
let mut buf = vec![0u8; size as usize];
|
|
|
|
stream.read_exact(&mut buf).unwrap();
|
|
|
|
if let Some(announcement) = Announcement::decode(&buf) {
|
|
Ok(announcement)
|
|
} else {
|
|
Err(ModuleError::Error("Failed to decode announcement".into()))
|
|
}
|
|
}
|
|
}
|
|
|
|
impl ModuleRuntime for ListenerRuntime {
|
|
// fn init(&mut self) {}
|
|
|
|
fn is_running(&self) -> bool {
|
|
true
|
|
}
|
|
|
|
fn kill(self: Box<Self>) {
|
|
if !self.thread_handle.is_finished() {
|
|
let _ = self.thread_handle.join();
|
|
}
|
|
// drop(self);
|
|
}
|
|
}
|