remove special client code designation; not really relevant
This commit is contained in:
parent
7e4883663b
commit
fe27fdd9d4
@ -87,12 +87,12 @@ fn process_message(msg_bytes: &[u8], nick: &str) {
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
codes::client::MESSAGE => {
|
codes::MESSAGE => {
|
||||||
let message: String =
|
let message: String =
|
||||||
String::from_utf8(msg_bytes[1..msg_bytes.len()].to_vec()).unwrap();
|
String::from_utf8(msg_bytes[1..msg_bytes.len()].to_vec()).unwrap();
|
||||||
println!("[server]:{}", message);
|
println!("[server]:{}", message);
|
||||||
}
|
}
|
||||||
codes::client::MESSAGE_ROOM => {
|
codes::MESSAGE_ROOM => {
|
||||||
let params = String::from_utf8(msg_bytes[1..msg_bytes.len()].to_vec()).unwrap();
|
let params = String::from_utf8(msg_bytes[1..msg_bytes.len()].to_vec()).unwrap();
|
||||||
match params.split_once(" ") {
|
match params.split_once(" ") {
|
||||||
Some((room, remainder)) => match remainder.split_once(" ") {
|
Some((room, remainder)) => match remainder.split_once(" ") {
|
||||||
@ -192,7 +192,7 @@ pub fn start() {
|
|||||||
});
|
});
|
||||||
|
|
||||||
//try to register the nickname
|
//try to register the nickname
|
||||||
one_param_op(codes::client::REGISTER_NICK, &mut stream, &nick);
|
one_param_op(codes::REGISTER_NICK, &mut stream, &nick);
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
let inp: String = input!("");
|
let inp: String = input!("");
|
||||||
@ -204,7 +204,7 @@ pub fn start() {
|
|||||||
eprintln!("Malformaed. Try /list [room-name]");
|
eprintln!("Malformaed. Try /list [room-name]");
|
||||||
}
|
}
|
||||||
_ => {
|
_ => {
|
||||||
one_param_op(codes::client::LIST_USERS_IN_ROOM, &mut stream, param);
|
one_param_op(codes::LIST_USERS_IN_ROOM, &mut stream, param);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"/join" => match param.split_once(" ") {
|
"/join" => match param.split_once(" ") {
|
||||||
@ -212,7 +212,7 @@ pub fn start() {
|
|||||||
eprintln!("Malformed. Try /join [room-name]");
|
eprintln!("Malformed. Try /join [room-name]");
|
||||||
}
|
}
|
||||||
_ => {
|
_ => {
|
||||||
one_param_op(codes::client::JOIN_ROOM, &mut stream, param);
|
one_param_op(codes::JOIN_ROOM, &mut stream, param);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
@ -221,12 +221,12 @@ pub fn start() {
|
|||||||
eprintln!("Malformed. Try /leave [room-name]");
|
eprintln!("Malformed. Try /leave [room-name]");
|
||||||
}
|
}
|
||||||
_ => {
|
_ => {
|
||||||
one_param_op(codes::client::LEAVE_ROOM, &mut stream, param);
|
one_param_op(codes::LEAVE_ROOM, &mut stream, param);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"/msg" => match param.split_once(" ") {
|
"/msg" => match param.split_once(" ") {
|
||||||
Some((room, msg)) => {
|
Some((room, msg)) => {
|
||||||
two_param_op(codes::client::MESSAGE_ROOM, &mut stream, room, msg);
|
two_param_op(codes::MESSAGE_ROOM, &mut stream, room, msg);
|
||||||
}
|
}
|
||||||
_ => {
|
_ => {
|
||||||
eprintln!("Usage: /msg [room] [message]");
|
eprintln!("Usage: /msg [room] [message]");
|
||||||
@ -242,15 +242,15 @@ pub fn start() {
|
|||||||
disconnect(&mut stream);
|
disconnect(&mut stream);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
"/rooms" => no_param_op(codes::client::LIST_ROOMS, &mut stream),
|
"/rooms" => no_param_op(codes::LIST_ROOMS, &mut stream),
|
||||||
"/users" => no_param_op(codes::client::LIST_USERS, &mut stream),
|
"/users" => no_param_op(codes::LIST_USERS, &mut stream),
|
||||||
"/help" => {
|
"/help" => {
|
||||||
help();
|
help();
|
||||||
}
|
}
|
||||||
"/" => {
|
"/" => {
|
||||||
println!("Invalid command");
|
println!("Invalid command");
|
||||||
}
|
}
|
||||||
_ => one_param_op(codes::client::MESSAGE, &mut stream, &inp),
|
_ => one_param_op(codes::MESSAGE, &mut stream, &inp),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
20
src/lib.rs
20
src/lib.rs
@ -1,16 +1,14 @@
|
|||||||
pub mod codes {
|
pub mod codes {
|
||||||
pub const TBD: u8 = 0x00;
|
pub const TBD: u8 = 0x00;
|
||||||
pub mod client {
|
pub const JOIN_ROOM: u8 = 0x01;
|
||||||
pub const JOIN_ROOM: u8 = 0x01;
|
pub const JOIN_SERVER: u8 = 0x02;
|
||||||
pub const JOIN_SERVER: u8 = 0x02;
|
pub const LEAVE_ROOM: u8 = 0x03;
|
||||||
pub const LEAVE_ROOM: u8 = 0x03;
|
pub const LIST_ROOMS: u8 = 0x04;
|
||||||
pub const LIST_ROOMS: u8 = 0x04;
|
pub const MESSAGE: u8 = 0x05;
|
||||||
pub const MESSAGE: u8 = 0x05;
|
pub const REGISTER_NICK: u8 = 0x06;
|
||||||
pub const REGISTER_NICK: u8 = 0x06;
|
pub const LIST_USERS: u8 = 0x07;
|
||||||
pub const LIST_USERS: u8 = 0x07;
|
pub const LIST_USERS_IN_ROOM: u8 = 0x08;
|
||||||
pub const LIST_USERS_IN_ROOM: u8 = 0x08;
|
pub const MESSAGE_ROOM: u8 = 0x09;
|
||||||
pub const MESSAGE_ROOM: u8 = 0x09;
|
|
||||||
}
|
|
||||||
pub const QUIT: u8 = 0x0B;
|
pub const QUIT: u8 = 0x0B;
|
||||||
pub const KEEP_ALIVE: u8 = 0x0C;
|
pub const KEEP_ALIVE: u8 = 0x0C;
|
||||||
pub const RESPONSE: u8 = 0x0D;
|
pub const RESPONSE: u8 = 0x0D;
|
||||||
|
@ -28,7 +28,7 @@ impl Server {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn message_room(room: &str, msg: &str, sender: &str, server: &Arc<Mutex<Server>>) {
|
fn message_room(room: &str, msg: &str, sender: &str, server: &Arc<Mutex<Server>>) {
|
||||||
let code_bytes = &[codes::client::MESSAGE_ROOM];
|
let code_bytes = &[codes::MESSAGE_ROOM];
|
||||||
let room_bytes = room.as_bytes();
|
let room_bytes = room.as_bytes();
|
||||||
let msg_bytes = msg.as_bytes();
|
let msg_bytes = msg.as_bytes();
|
||||||
let sender_bytes = sender.as_bytes();
|
let sender_bytes = sender.as_bytes();
|
||||||
@ -137,12 +137,12 @@ fn handle_client(
|
|||||||
) {
|
) {
|
||||||
// handle user commands
|
// handle user commands
|
||||||
match cmd_bytes[0] {
|
match cmd_bytes[0] {
|
||||||
codes::client::REGISTER_NICK => {
|
codes::REGISTER_NICK => {
|
||||||
stream
|
stream
|
||||||
.write_all(&[codes::ERROR, codes::error::ALREADY_REGISTERED])
|
.write_all(&[codes::ERROR, codes::error::ALREADY_REGISTERED])
|
||||||
.unwrap();
|
.unwrap();
|
||||||
}
|
}
|
||||||
codes::client::LIST_ROOMS => {
|
codes::LIST_ROOMS => {
|
||||||
let unlocked_server: std::sync::MutexGuard<'_, Server> = server.lock().unwrap();
|
let unlocked_server: std::sync::MutexGuard<'_, Server> = server.lock().unwrap();
|
||||||
let mut buf_out: Vec<u8> = Vec::new();
|
let mut buf_out: Vec<u8> = Vec::new();
|
||||||
buf_out.extend_from_slice(&[codes::RESPONSE]);
|
buf_out.extend_from_slice(&[codes::RESPONSE]);
|
||||||
@ -153,7 +153,7 @@ fn handle_client(
|
|||||||
stream.write(&buf_out).unwrap();
|
stream.write(&buf_out).unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
codes::client::LIST_USERS => {
|
codes::LIST_USERS => {
|
||||||
let unlocked_server: std::sync::MutexGuard<'_, Server> = server.lock().unwrap();
|
let unlocked_server: std::sync::MutexGuard<'_, Server> = server.lock().unwrap();
|
||||||
let mut buf_out: Vec<u8> = Vec::new();
|
let mut buf_out: Vec<u8> = Vec::new();
|
||||||
buf_out.extend_from_slice(&[codes::RESPONSE]);
|
buf_out.extend_from_slice(&[codes::RESPONSE]);
|
||||||
@ -164,7 +164,7 @@ fn handle_client(
|
|||||||
stream.write(&buf_out).unwrap();
|
stream.write(&buf_out).unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
codes::client::LIST_USERS_IN_ROOM => {
|
codes::LIST_USERS_IN_ROOM => {
|
||||||
let room: String = String::from_utf8_lossy(param_bytes).to_string();
|
let room: String = String::from_utf8_lossy(param_bytes).to_string();
|
||||||
let unlocked_server: std::sync::MutexGuard<'_, Server> = server.lock().unwrap();
|
let unlocked_server: std::sync::MutexGuard<'_, Server> = server.lock().unwrap();
|
||||||
let mut buf_out: Vec<u8> = Vec::new();
|
let mut buf_out: Vec<u8> = Vec::new();
|
||||||
@ -185,14 +185,14 @@ fn handle_client(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
codes::client::JOIN_ROOM => {
|
codes::JOIN_ROOM => {
|
||||||
let p: String = String::from_utf8_lossy(param_bytes).to_string();
|
let p: String = String::from_utf8_lossy(param_bytes).to_string();
|
||||||
let params: Vec<&str> = p.split_whitespace().collect();
|
let params: Vec<&str> = p.split_whitespace().collect();
|
||||||
let room = params.get(0).unwrap();
|
let room = params.get(0).unwrap();
|
||||||
join_room(server, &nickname, room, stream);
|
join_room(server, &nickname, room, stream);
|
||||||
}
|
}
|
||||||
|
|
||||||
codes::client::LEAVE_ROOM => {
|
codes::LEAVE_ROOM => {
|
||||||
let p: String = String::from_utf8_lossy(param_bytes).to_string();
|
let p: String = String::from_utf8_lossy(param_bytes).to_string();
|
||||||
let params: Vec<&str> = p.split_whitespace().collect();
|
let params: Vec<&str> = p.split_whitespace().collect();
|
||||||
let room = params.get(0).unwrap();
|
let room = params.get(0).unwrap();
|
||||||
@ -200,7 +200,7 @@ fn handle_client(
|
|||||||
}
|
}
|
||||||
|
|
||||||
//Generic message sent to all users of all rooms the clients nickname is in, except the client nickname
|
//Generic message sent to all users of all rooms the clients nickname is in, except the client nickname
|
||||||
codes::client::MESSAGE => {
|
codes::MESSAGE => {
|
||||||
let p: String = String::from_utf8_lossy(param_bytes).to_string();
|
let p: String = String::from_utf8_lossy(param_bytes).to_string();
|
||||||
|
|
||||||
message_all_senders_rooms(server, &nickname, &p, stream);
|
message_all_senders_rooms(server, &nickname, &p, stream);
|
||||||
@ -212,7 +212,7 @@ fn handle_client(
|
|||||||
}
|
}
|
||||||
|
|
||||||
//A message sent just to the users of the room passed in, except the client nickname
|
//A message sent just to the users of the room passed in, except the client nickname
|
||||||
codes::client::MESSAGE_ROOM => {
|
codes::MESSAGE_ROOM => {
|
||||||
let p: String = String::from_utf8_lossy(param_bytes).to_string();
|
let p: String = String::from_utf8_lossy(param_bytes).to_string();
|
||||||
let params: Option<(&str, &str)> = p.split_once(" ");
|
let params: Option<(&str, &str)> = p.split_once(" ");
|
||||||
match params {
|
match params {
|
||||||
@ -247,7 +247,7 @@ fn message_all_senders_rooms(
|
|||||||
let rooms: Vec<String> = get_rooms_of_user(server, sender);
|
let rooms: Vec<String> = get_rooms_of_user(server, sender);
|
||||||
let mut guard: std::sync::MutexGuard<'_, Server> = server.lock().unwrap();
|
let mut guard: std::sync::MutexGuard<'_, Server> = server.lock().unwrap();
|
||||||
let sender_bytes: &[u8] = sender.as_bytes();
|
let sender_bytes: &[u8] = sender.as_bytes();
|
||||||
let code_bytes: &[u8] = &[codes::client::MESSAGE_ROOM];
|
let code_bytes: &[u8] = &[codes::MESSAGE_ROOM];
|
||||||
let message_bytes: &[u8] = message.as_bytes();
|
let message_bytes: &[u8] = message.as_bytes();
|
||||||
let space_bytes: &[u8] = &[0x20];
|
let space_bytes: &[u8] = &[0x20];
|
||||||
for room in rooms {
|
for room in rooms {
|
||||||
@ -427,7 +427,7 @@ pub fn start() {
|
|||||||
Ok(size) => {
|
Ok(size) => {
|
||||||
let cmd_bytes: &[u8] = &buf_in[0..1];
|
let cmd_bytes: &[u8] = &buf_in[0..1];
|
||||||
let param_bytes: &[u8] = &buf_in[1..size];
|
let param_bytes: &[u8] = &buf_in[1..size];
|
||||||
if cmd_bytes[0] == codes::client::REGISTER_NICK {
|
if cmd_bytes[0] == codes::REGISTER_NICK {
|
||||||
nickname =
|
nickname =
|
||||||
String::from_utf8_lossy(param_bytes).to_string();
|
String::from_utf8_lossy(param_bytes).to_string();
|
||||||
register_nick(&server_inner, &nickname, &mut stream);
|
register_nick(&server_inner, &nickname, &mut stream);
|
||||||
@ -503,7 +503,7 @@ pub fn start() {
|
|||||||
2 => println!("Rooms: {:?}", server.lock().unwrap().rooms),
|
2 => println!("Rooms: {:?}", server.lock().unwrap().rooms),
|
||||||
3 => {
|
3 => {
|
||||||
let inp2 = input!("Enter message: ");
|
let inp2 = input!("Enter message: ");
|
||||||
broadcast(codes::client::MESSAGE, &server, &inp2);
|
broadcast(codes::MESSAGE, &server, &inp2);
|
||||||
}
|
}
|
||||||
4 => {
|
4 => {
|
||||||
let s1 = server.lock().unwrap();
|
let s1 = server.lock().unwrap();
|
||||||
|
Reference in New Issue
Block a user