diff --git a/src/main.rs b/src/main.rs index e1c7723..c57557f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -58,33 +58,28 @@ fn parse_message(input: &str) -> MessageOuter { } } - let message_inner = Message { - body, - from, - }; - let message_outer = MessageOuter { auth, - message: message_inner, + message: Message { body, from }, to, }; message_outer } -fn handle_client(mut stream: TcpStream) { - let mut buffer = [0; 1024]; - while let Ok(bytes_read) = stream.read(&mut buffer) { +fn read_from(mut stream: TcpStream) { + let mut buf = Vec::new(); + + while let Ok(bytes_read) = stream.read_to_end(&mut buf) { if bytes_read == 0 { break } - + let message_outer = parse_message( - String::from_utf8_lossy(&buffer[..bytes_read]).as_ref() + String::from_utf8(buf.clone()).as_ref().unwrap() ); - let user = match message_outer.message.from { - Some(user) => user, - None => format!("anon"), - }; + let user = message_outer.message.from.unwrap_or_else(|| { + format!("anon") + }); println!("{}: {}", user, message_outer.message.body); } @@ -100,7 +95,7 @@ fn main() { match stream { Ok(stream) => { thread::spawn(move || { - handle_client(stream); + read_from(stream); }); } Err(err) => {