message format improvements
This commit is contained in:
parent
436c2406ab
commit
0de646a74e
@ -1,9 +1,11 @@
|
|||||||
use prompted::input;
|
use prompted::input;
|
||||||
use rust_irc::codes;
|
use rust_irc::{codes, clear};
|
||||||
use std::io::{Read, Write};
|
use std::io::{Read, Write};
|
||||||
use std::net::TcpStream;
|
use std::net::TcpStream;
|
||||||
use std::thread;
|
use std::thread;
|
||||||
|
|
||||||
|
use crate::client;
|
||||||
|
|
||||||
fn no_param_op(opcode: u8, stream: &mut TcpStream) {
|
fn no_param_op(opcode: u8, stream: &mut TcpStream) {
|
||||||
stream.write(&[opcode]).unwrap();
|
stream.write(&[opcode]).unwrap();
|
||||||
}
|
}
|
||||||
@ -38,7 +40,7 @@ fn two_param_op(opcode: u8, stream: &mut TcpStream, param0: &str, param1: &str)
|
|||||||
stream.write(&out_buf).unwrap();
|
stream.write(&out_buf).unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
fn read_messages(mut stream: TcpStream) {
|
fn read_messages(mut stream: TcpStream, nick: &str) {
|
||||||
let mut buffer: [u8; 1024] = [0; 1024];
|
let mut buffer: [u8; 1024] = [0; 1024];
|
||||||
loop {
|
loop {
|
||||||
match stream.read(&mut buffer) {
|
match stream.read(&mut buffer) {
|
||||||
@ -47,7 +49,7 @@ fn read_messages(mut stream: TcpStream) {
|
|||||||
break; //Server closed connection
|
break; //Server closed connection
|
||||||
}
|
}
|
||||||
let msg_bytes: &[u8] = &buffer[..size];
|
let msg_bytes: &[u8] = &buffer[..size];
|
||||||
process_message(msg_bytes);
|
process_message(msg_bytes, nick);
|
||||||
}
|
}
|
||||||
Err(_) => {
|
Err(_) => {
|
||||||
break;
|
break;
|
||||||
@ -56,11 +58,10 @@ fn read_messages(mut stream: TcpStream) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn process_message(msg_bytes: &[u8]) {
|
fn process_message(msg_bytes: &[u8], nick: &str) {
|
||||||
match msg_bytes[0] {
|
match msg_bytes[0] {
|
||||||
codes::ERROR =>
|
codes::ERROR =>
|
||||||
{
|
{
|
||||||
#[cfg(debug_assertions)]
|
|
||||||
match msg_bytes[1] {
|
match msg_bytes[1] {
|
||||||
codes::error::INVALID_ROOM => {
|
codes::error::INVALID_ROOM => {
|
||||||
println!("Operation Performed on an invalid room. Try again");
|
println!("Operation Performed on an invalid room. Try again");
|
||||||
@ -76,8 +77,7 @@ fn process_message(msg_bytes: &[u8]) {
|
|||||||
disconnect();
|
disconnect();
|
||||||
}
|
}
|
||||||
_ => {
|
_ => {
|
||||||
#[cfg(debug_assertions)]
|
println!("Error code: {:x?}", msg_bytes[1]);
|
||||||
println!("Unknown error code {:x?}", msg_bytes[1]);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -91,19 +91,23 @@ fn process_message(msg_bytes: &[u8]) {
|
|||||||
match params.split_once(" ") {
|
match params.split_once(" ") {
|
||||||
Some((room, remainder)) => match remainder.split_once(" ") {
|
Some((room, remainder)) => match remainder.split_once(" ") {
|
||||||
Some((user, msg)) => {
|
Some((user, msg)) => {
|
||||||
println!("[{}]:[{}]: {}", room, user, msg);
|
if user != nick {
|
||||||
|
println!("[{}]:[{}]: {}", room, user, msg);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
None => {
|
||||||
|
eprintln!("Malformed message recieved");
|
||||||
}
|
}
|
||||||
None => {}
|
|
||||||
},
|
},
|
||||||
_ => {
|
None => {
|
||||||
println!("Malformed message recieved");
|
eprintln!("Malformed message recieved");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
codes::RESPONSE_OK => {
|
codes::RESPONSE_OK => {
|
||||||
#[cfg(debug_assertions)]
|
// #[cfg(debug_assertions)]
|
||||||
println!("RESPONSE_OK");
|
// println!("RESPONSE_OK");
|
||||||
}
|
}
|
||||||
codes::RESPONSE => {
|
codes::RESPONSE => {
|
||||||
let message = String::from_utf8(msg_bytes[1..msg_bytes.len()].to_vec()).unwrap();
|
let message = String::from_utf8(msg_bytes[1..msg_bytes.len()].to_vec()).unwrap();
|
||||||
@ -119,6 +123,7 @@ fn process_message(msg_bytes: &[u8]) {
|
|||||||
fn disconnect() {}
|
fn disconnect() {}
|
||||||
|
|
||||||
fn help() {
|
fn help() {
|
||||||
|
clear();
|
||||||
println!("Available commands:");
|
println!("Available commands:");
|
||||||
println!("/quit <- Disconnect and stop the client");
|
println!("/quit <- Disconnect and stop the client");
|
||||||
println!("/rooms <- List all of the rooms on the server");
|
println!("/rooms <- List all of the rooms on the server");
|
||||||
@ -132,13 +137,18 @@ fn help() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn start() {
|
pub fn start() {
|
||||||
println!("Starting the IRC client. No spaces allowed in nicknames or room names");
|
clear();
|
||||||
|
println!("Starting the IRC client. No spaces allowed in nicknames or room names. /help to see available commands");
|
||||||
let mut nick: String;
|
let mut nick: String;
|
||||||
let mut active_room: String = String::new();
|
let mut active_room: String = String::new();
|
||||||
loop {
|
loop {
|
||||||
nick = input!("Enter your nickname : ");
|
nick = input!("Enter your nickname : ");
|
||||||
if nick.contains(" ") {
|
if nick.contains(" ") {
|
||||||
println!("May not contain spaces. Try again");
|
println!("May not contain spaces . Try again");
|
||||||
|
|
||||||
|
}
|
||||||
|
else if nick.is_empty() {
|
||||||
|
println!("May not be empty . Try again");
|
||||||
} else {
|
} else {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@ -152,15 +162,16 @@ pub fn start() {
|
|||||||
|
|
||||||
//another stream for reading messages
|
//another stream for reading messages
|
||||||
let stream_clone: TcpStream = stream.try_clone().expect("Failed to clone stream");
|
let stream_clone: TcpStream = stream.try_clone().expect("Failed to clone stream");
|
||||||
|
let nick_clone = nick.clone();
|
||||||
thread::spawn(move || {
|
thread::spawn(move || {
|
||||||
read_messages(stream_clone);
|
read_messages(stream_clone, &nick_clone);
|
||||||
});
|
});
|
||||||
|
|
||||||
//try to register the nickname
|
//try to register the nickname
|
||||||
one_param_op(codes::client::REGISTER_NICK, &mut stream, &nick);
|
one_param_op(codes::client::REGISTER_NICK, &mut stream, &nick);
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
let inp: String = input!(":");
|
let inp: String = input!("\n[{}]:",active_room);
|
||||||
let mut args: std::str::SplitWhitespace<'_> = inp.split_whitespace();
|
let mut args: std::str::SplitWhitespace<'_> = inp.split_whitespace();
|
||||||
let command: Option<&str> = args.next();
|
let command: Option<&str> = args.next();
|
||||||
|
|
||||||
@ -189,6 +200,7 @@ pub fn start() {
|
|||||||
},
|
},
|
||||||
"/show" => match param {
|
"/show" => match param {
|
||||||
Some(room) => {
|
Some(room) => {
|
||||||
|
clear();
|
||||||
active_room = room.to_string();
|
active_room = room.to_string();
|
||||||
}
|
}
|
||||||
None => {
|
None => {
|
||||||
|
@ -8,7 +8,7 @@ use std::{
|
|||||||
};
|
};
|
||||||
|
|
||||||
use prompted::input;
|
use prompted::input;
|
||||||
use rust_irc::codes;
|
use rust_irc::{codes, clear};
|
||||||
|
|
||||||
const SERVER_ADDRESS: &str = "0.0.0.0:6667";
|
const SERVER_ADDRESS: &str = "0.0.0.0:6667";
|
||||||
const MAX_USERS: usize = 20;
|
const MAX_USERS: usize = 20;
|
||||||
@ -28,7 +28,6 @@ impl Server {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn message_room(room: &str, message: &str) {}
|
|
||||||
|
|
||||||
fn message(room: &str, msg: &str, sender: &str, server: &Arc<Mutex<Server>>) {
|
fn message(room: &str, msg: &str, sender: &str, server: &Arc<Mutex<Server>>) {
|
||||||
println!("message fn {} {}", room, msg);
|
println!("message fn {} {}", room, msg);
|
||||||
@ -70,7 +69,6 @@ fn message(room: &str, msg: &str, sender: &str, server: &Arc<Mutex<Server>>) {
|
|||||||
let stream: Option<&mut TcpStream> = server.users.get_mut(user);
|
let stream: Option<&mut TcpStream> = server.users.get_mut(user);
|
||||||
match stream {
|
match stream {
|
||||||
Some(str) => {
|
Some(str) => {
|
||||||
println!("the buf {:?}, ", out_buf);
|
|
||||||
str.write_all(&out_buf).unwrap();
|
str.write_all(&out_buf).unwrap();
|
||||||
}
|
}
|
||||||
None => {
|
None => {
|
||||||
@ -99,7 +97,7 @@ fn broadcast(op: u8, server: &Arc<Mutex<Server>>, message: &str) {
|
|||||||
let mut unlocked_server: std::sync::MutexGuard<'_, Server> = server.lock().unwrap();
|
let mut unlocked_server: std::sync::MutexGuard<'_, Server> = server.lock().unwrap();
|
||||||
let streams = unlocked_server.users.values_mut();
|
let streams = unlocked_server.users.values_mut();
|
||||||
for stream in streams {
|
for stream in streams {
|
||||||
stream.write_all(&out_buf);
|
stream.write_all(&out_buf).unwrap();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -275,6 +273,7 @@ pub fn start() {
|
|||||||
let listener: TcpListener = TcpListener::bind(SERVER_ADDRESS).expect("Failed to bind to port");
|
let listener: TcpListener = TcpListener::bind(SERVER_ADDRESS).expect("Failed to bind to port");
|
||||||
let server: Arc<Mutex<Server>> = Arc::new(Mutex::new(Server::new()));
|
let server: Arc<Mutex<Server>> = Arc::new(Mutex::new(Server::new()));
|
||||||
let server_outer: Arc<Mutex<Server>> = Arc::clone(&server);
|
let server_outer: Arc<Mutex<Server>> = Arc::clone(&server);
|
||||||
|
clear();
|
||||||
println!("Server listening on {}", SERVER_ADDRESS);
|
println!("Server listening on {}", SERVER_ADDRESS);
|
||||||
|
|
||||||
thread::spawn(move || {
|
thread::spawn(move || {
|
||||||
|
Reference in New Issue
Block a user