* support for cmdline args and better error handling

* make config.toml optional

* more unwraps removed
This commit is contained in:
Ferdinand Schober
2023-06-10 15:45:19 +02:00
committed by GitHub
parent 225ef818a2
commit a1100cc8b2
3 changed files with 27 additions and 25 deletions

View File

@@ -35,27 +35,28 @@ pub struct Server {
}
impl Server {
fn handle_request(&self, mut stream: TcpStream) {
fn handle_request(&self, mut stream: TcpStream) -> Result<(), Box<dyn Error>> {
let mut buf = [0u8; 4];
stream.read_exact(&mut buf).unwrap();
stream.read_exact(&mut buf)?;
match Request::try_from(buf) {
Ok(Request::KeyMap) => {
let data = self.data.read().unwrap();
let buf = data.get(&Request::KeyMap);
match buf {
None => {
stream.write(&0u32.to_ne_bytes()).unwrap();
stream.write(&0u32.to_ne_bytes())?;
}
Some(buf) => {
stream.write(&buf[..].len().to_ne_bytes()).unwrap();
stream.write(&buf[..]).unwrap();
stream.write(&buf[..].len().to_ne_bytes())?;
stream.write(&buf[..])?;
}
}
stream.flush().unwrap();
stream.flush()?;
}
Ok(Request::Connect) => todo!(),
Err(msg) => eprintln!("{}", msg),
}
Ok(())
}
pub fn listen(port: u16) -> Result<(Server, JoinHandle<()>), Box<dyn Error>> {
@@ -70,7 +71,9 @@ impl Server {
for stream in listen_socket.incoming() {
match stream {
Ok(stream) => {
server.handle_request(stream);
if let Err(e) = server.handle_request(stream) {
eprintln!("{}", e);
}
}
Err(e) => {
eprintln!("{}", e);