2018-03-18 20:07:20 +00:00
|
|
|
use std::io::*;
|
2018-02-26 10:52:41 +00:00
|
|
|
use std::net::TcpStream;
|
2018-03-18 20:07:20 +00:00
|
|
|
use std::thread;
|
|
|
|
|
|
|
|
static leave_msg: &str = "BYE";
|
|
|
|
|
|
|
|
// macro_rules! send {
|
|
|
|
// ($line:expr) => ({
|
|
|
|
// try!(writeln!(writer, "{}", $line));
|
|
|
|
// try!(writer.flush());
|
|
|
|
// })
|
|
|
|
// }
|
|
|
|
|
|
|
|
fn send(writer: BufWriter<&TcpStream>,text: &str) {
|
|
|
|
|
|
|
|
}
|
2018-02-26 10:52:41 +00:00
|
|
|
|
|
|
|
fn get_entry() -> String {
|
|
|
|
let mut buf = String::new();
|
|
|
|
|
|
|
|
stdin().read_line(&mut buf).unwrap();
|
|
|
|
buf.replace("\n", "").replace("\r", "")
|
|
|
|
}
|
|
|
|
|
2018-03-18 20:07:20 +00:00
|
|
|
fn write_to_server(stream: TcpStream) {
|
|
|
|
|
|
|
|
let mut writer = BufWriter::new(&stream);
|
|
|
|
|
2018-03-05 16:39:11 +00:00
|
|
|
loop {
|
2018-03-18 20:07:20 +00:00
|
|
|
match &*get_entry() {
|
|
|
|
"/quit" => {
|
|
|
|
println!("Disconnecting...");
|
|
|
|
|
|
|
|
println!("Disconnected!");
|
|
|
|
return ();
|
2018-03-05 16:39:11 +00:00
|
|
|
}
|
2018-03-18 20:07:20 +00:00
|
|
|
line => {
|
|
|
|
send(BufWriter::new(&stream), line);
|
2018-03-05 16:39:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-18 20:07:20 +00:00
|
|
|
fn exchange_with_server(stream: TcpStream) {
|
|
|
|
let server = stream.peer_addr().unwrap();
|
|
|
|
println!("Connected to {}", server);
|
|
|
|
// Buffered reading and writing
|
|
|
|
let mut reader = BufReader::new(&stream);
|
|
|
|
let mut writer = BufWriter::new(&stream);
|
|
|
|
|
|
|
|
println!("Enter `/quit` when you want to leave");
|
|
|
|
|
|
|
|
macro_rules! receive {
|
|
|
|
() => ({
|
|
|
|
let mut line = String::new();
|
|
|
|
match reader.read_line(&mut line) {
|
|
|
|
Ok(len) => {
|
|
|
|
if len == 0 {
|
|
|
|
// Reader is at EOF.
|
|
|
|
return Err(Error::new(ErrorKind::Other, "unexpected EOF"));
|
|
|
|
}
|
|
|
|
line.pop();
|
|
|
|
}
|
|
|
|
Err(e) => {
|
|
|
|
return Err(e);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
line
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
thread::spawn(move || {
|
|
|
|
write_to_server(stream.try_clone().unwrap());
|
2018-03-05 16:39:11 +00:00
|
|
|
});
|
|
|
|
|
2018-03-18 20:07:20 +00:00
|
|
|
match(|| {
|
|
|
|
loop {
|
|
|
|
let input = receive!();
|
|
|
|
println!("{}", input);
|
|
|
|
}
|
2018-02-26 10:52:41 +00:00
|
|
|
|
2018-03-18 20:07:20 +00:00
|
|
|
})() {
|
|
|
|
Ok(_) => {
|
|
|
|
println!("Left?");
|
|
|
|
}
|
|
|
|
Err(e) => {
|
|
|
|
println!("Disappeared? {}", e);
|
2018-02-26 10:52:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-05 16:39:11 +00:00
|
|
|
// fn exchange_with_server(stream: TcpStream) {
|
|
|
|
// let (chan, recv): (Sender<String>, Receiver<String>) = mpsc::channel();
|
|
|
|
// // let buf = &mut [0; 1024];
|
|
|
|
// spawn(move || {
|
|
|
|
// loop {
|
|
|
|
// let msg = recv.recv().unwrap();
|
|
|
|
// println!("{}", msg);
|
|
|
|
// }
|
|
|
|
// });
|
|
|
|
// println!("Enter `quit` or `exit` when you want to leave");
|
|
|
|
// loop {
|
|
|
|
// match &*get_entry() {
|
|
|
|
// "quit" => {
|
|
|
|
// println!("bye!");
|
|
|
|
// return;
|
|
|
|
// }
|
|
|
|
// "exit" => {
|
|
|
|
// println!("bye!");
|
|
|
|
// return;
|
|
|
|
// }
|
|
|
|
// line => {
|
|
|
|
// chan.send(format!("{}", line)).unwrap();
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
|
2018-02-26 10:52:41 +00:00
|
|
|
pub fn client(server_address: String) {
|
|
|
|
println!("Tentative de connexion a serveur...");
|
|
|
|
match TcpStream::connect(server_address) {
|
|
|
|
Ok(stream) => {
|
|
|
|
println!("Connexion au serveur réussie !");
|
|
|
|
exchange_with_server(stream);
|
|
|
|
}
|
|
|
|
Err(e) => {
|
|
|
|
println!("La connection au serveur a échoué : {}", e);
|
2018-03-05 16:39:11 +00:00
|
|
|
return;
|
2018-02-26 10:52:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|