replaced client handling

This commit is contained in:
Emma Tebibyte 2023-06-12 22:15:18 -06:00
parent d906bea781
commit ed09e931c7
Signed by: emma
GPG Key ID: 6D661C738815E7DD

View File

@ -58,33 +58,28 @@ fn parse_message(input: &str) -> MessageOuter {
} }
} }
let message_inner = Message {
body,
from,
};
let message_outer = MessageOuter { let message_outer = MessageOuter {
auth, auth,
message: message_inner, message: Message { body, from },
to, to,
}; };
message_outer message_outer
} }
fn handle_client(mut stream: TcpStream) { fn read_from(mut stream: TcpStream) {
let mut buffer = [0; 1024]; let mut buf = Vec::new();
while let Ok(bytes_read) = stream.read(&mut buffer) {
while let Ok(bytes_read) = stream.read_to_end(&mut buf) {
if bytes_read == 0 { break } if bytes_read == 0 { break }
let message_outer = parse_message( 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 { let user = message_outer.message.from.unwrap_or_else(|| {
Some(user) => user, format!("anon")
None => format!("anon"), });
};
println!("{}: {}", user, message_outer.message.body); println!("{}: {}", user, message_outer.message.body);
} }
@ -100,7 +95,7 @@ fn main() {
match stream { match stream {
Ok(stream) => { Ok(stream) => {
thread::spawn(move || { thread::spawn(move || {
handle_client(stream); read_from(stream);
}); });
} }
Err(err) => { Err(err) => {