Apparently working client despite ugly code

This commit is contained in:
Phuntsok Drak-pa 2018-03-23 02:19:27 +01:00
parent 475724a306
commit ef4572c3a5

View File

@ -49,45 +49,37 @@ fn get_entry() -> String {
buf.replace("\n", "").replace("\r", "") buf.replace("\n", "").replace("\r", "")
} }
fn get_name(writer: &mut BufWriter<&TcpStream>) { fn get_name() -> String {
loop { loop {
let mut line = &*get_entry(); println!("{}", "Please enter your name:".yellow().dimmed());
line = line.trim(); let mut name = &*get_entry();
if line.len() > 20 { name = name.trim();
println!("Nickname too long, it must be at most 20 characters long"); if name.len() > 20 {
println!(
"{}",
"Nickname too long, it must be at most 20 characters long".red()
);
continue; continue;
} }
match line { match name {
"" => { "" => {
continue; continue;
} }
"/quit" => { _ => {
println!("Disconnecting..."); let spliced_name: Vec<&str> = name.split_whitespace().collect();
writeln!(writer, "BYE").unwrap(); if spliced_name.len() != 1 {
writer.flush().unwrap(); println!("{}", "Cannot use whitespace in name".red());
return ();
}
line => {
let line_str: String = String::from(line);
let spliced: Vec<&str> = line_str.split_whitespace().collect();
if spliced.len() > 1 {
println!("Cannot use whitespace in username.");
continue; continue;
} }
writeln!(writer, "{}", line).unwrap(); return String::from(name);
writer.flush().unwrap();
} }
} }
return;
} }
} }
fn write_to_server(stream: TcpStream) { fn write_to_server(stream: TcpStream) {
let mut writer = BufWriter::new(&stream); let mut writer = BufWriter::new(&stream);
// entrée du nom d'utilisateur
get_name(&mut writer);
loop { loop {
let line = &*get_entry(); let line = &*get_entry();
line.trim(); line.trim();
@ -135,6 +127,7 @@ fn exchange_with_server(stream: TcpStream) {
let stream_cpy = stream.try_clone().unwrap(); let stream_cpy = stream.try_clone().unwrap();
let mut reader = BufReader::new(&stream_cpy); let mut reader = BufReader::new(&stream_cpy);
let mut writer = BufWriter::new(&stream_cpy);
macro_rules! receive { macro_rules! receive {
() => ({ () => ({
@ -157,6 +150,41 @@ fn exchange_with_server(stream: TcpStream) {
}) })
} }
// entrée du nom d'utilisateur
writeln!(writer, "PROT {} CONNECT NEW", ::PROTOCOL).unwrap();
writer.flush().unwrap();
let _name: String = match (|| loop {
let _answer = receive!();
if _answer != "NAME REQ" {
return Err(Error::new(ErrorKind::Other, _answer));
}
let nick = get_name();
writeln!(writer, "NAME {}", nick).unwrap();
writer.flush().unwrap();
match receive!().as_str() {
"NAME OK" => {
println!("NAME OK");
nick
}
"NAME FAILURE" => {
println!("{}", "Username refused by server.".red());
continue;
}
answer => {
println!("{}{}", "Server answered: ".yellow().dimmed(), answer);
return Err(Error::new(ErrorKind::Other, answer));
}
};
})()
{
Ok(name) => String::from(name),
Err(e) => {
println!("{}", ">>> Login successful".green());
String::new()
}
};
thread::spawn(move || { thread::spawn(move || {
write_to_server(stream.try_clone().unwrap()); write_to_server(stream.try_clone().unwrap());
}); });