diff --git a/src/lib.rs b/src/lib.rs index 6f26d91..8ffcde7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,2 +1,3 @@ pub mod uno_logic; -pub mod uno_protocol; \ No newline at end of file +pub mod uno_protocol; +pub mod uno_tcp; \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index e61e3d8..f79c691 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,2 @@ -pub mod tcp_server; - fn main() { - tcp_server::main(); } diff --git a/src/tcp_server.rs b/src/tcp_server.rs deleted file mode 100644 index 70557f7..0000000 --- a/src/tcp_server.rs +++ /dev/null @@ -1,57 +0,0 @@ -use std::{ - io::{ prelude::*, BufReader, BufWriter }, - net::{ TcpListener, TcpStream }, -}; - -const PROTOCOL_ERROR_STRING: &str = "Client/Server protocol mismatch"; - -pub fn main() { - let listener = TcpListener::bind("0.0.0.0:8000").unwrap(); - - for stream in listener.incoming() { - let (reader, writer) = create_buffers(stream.unwrap().try_clone().unwrap()); - let result = check_protocol(reader, writer).unwrap(); - if result == "Uno-Enthusiast99" { - println!("It works!"); - } - } -} - -fn create_buffers(stream: TcpStream) -> (BufReader, BufWriter) { - ( - BufReader::new(stream.try_clone().expect("Could not clone TcpStreams")), - BufWriter::new(stream), - ) -} -fn check_protocol( - reader: BufReader, - mut writer: BufWriter -) -> Result { - let protocol_error_string = PROTOCOL_ERROR_STRING.to_string(); - let init_message: Vec<_> = reader - .lines() - .map(|result| result.unwrap()) - .take_while(|line| !line.is_empty()) - .collect(); - let mut init_message = init_message.iter(); - let is_valid1 = init_message.next().unwrap_or_else(|| &protocol_error_string) == "app:uno"; - let is_valid2= init_message.next().unwrap_or_else(|| &protocol_error_string) == "version:0.1"; - let player_name: &str = init_message.next().unwrap_or_else(|| &protocol_error_string); - let is_valid3 = if player_name != "invalid" && player_name.contains("player_name:") { - true - } else { - false - }; - if init_message.next() != None || !(is_valid1 && is_valid2 && is_valid3){ - writer.write(protocol_error_string.as_bytes()).unwrap(); - writer.flush().unwrap(); - return Err(protocol_error_string); - } - writer.write("Ok".as_bytes()).unwrap(); - writer.flush().unwrap(); - return Ok(player_name.split(':').collect::>()[1].to_owned()); -} - -// fn register_player(player_name : &str, reader : BufReader, writer : BufWriter){ -// -// } \ No newline at end of file diff --git a/src/uno_logic.rs b/src/uno_logic.rs index 272127a..90bf831 100644 --- a/src/uno_logic.rs +++ b/src/uno_logic.rs @@ -55,9 +55,8 @@ pub struct PlayerState<'a> { } pub struct PlayerConnection<'a>{ - player_name : &'a str, - reader: BufReader, - writer: BufWriter, + pub player_name : &'a str, + pub stream : TcpStream, } pub struct Card { @@ -167,11 +166,10 @@ impl PlayerState<'_> { } impl PlayerConnection<'_> { - pub fn new(player_name : &str, reader : BufReader, writer : BufWriter) -> PlayerConnection{ + pub fn new(player_name : &str, stream : TcpStream) -> PlayerConnection{ PlayerConnection { player_name : player_name, - reader : reader, - writer : writer + stream : stream } } } diff --git a/src/uno_tcp.rs b/src/uno_tcp.rs new file mode 100644 index 0000000..4b44fe4 --- /dev/null +++ b/src/uno_tcp.rs @@ -0,0 +1,43 @@ +use std::{ + io::{ prelude::*, BufReader, BufWriter }, + net::TcpStream, +}; + +use crate::uno_logic::PlayerConnection; + +const PROTOCOL_ERROR_STRING: &str = "Client/Server protocol mismatch"; + +pub fn register_player<'a>(mut stream : TcpStream) -> Result, String> { + let buffer : &mut [u8] = &mut []; + match stream.read(buffer) { + Ok(_) => (), + Err(_) => return Err("could not read from socket".to_owned()) + }; + let msg = match std::str::from_utf8(buffer){ + Ok(msg) => msg, + Err(_) => return Err("could not parse UTF-8".to_owned()) + }; + + + let mut lines = msg.lines(); + let is_valid1 = lines.next().unwrap_or_else(|| &PROTOCOL_ERROR_STRING) == "app:uno"; + let is_valid2= lines.next().unwrap_or_else(|| &PROTOCOL_ERROR_STRING) == "version:0.1"; + let player_name: &str = lines.next().unwrap_or_else(|| &PROTOCOL_ERROR_STRING); + let is_valid3 = if player_name != "invalid" && player_name.contains("player_name:") { + true + } else { + false + }; + if lines.next() != None || !(is_valid1 && is_valid2 && is_valid3){ + stream.write_all(PROTOCOL_ERROR_STRING.as_bytes()).unwrap(); + stream.flush().unwrap(); + return Err(PROTOCOL_ERROR_STRING.to_owned()); + } + stream.write_all("Ok".as_bytes()).unwrap(); + stream.flush().unwrap(); + return Ok(PlayerConnection { player_name : player_name[..].split(':').collect::>()[1], stream : stream}); +} + +// fn register_player(player_name : &str, reader : BufReader, writer : BufWriter){ +// +// } \ No newline at end of file