converted check_protocol to register_player, renamed tcp_server.rs to uno_tcp.rs
This commit is contained in:
parent
2350f38e0b
commit
7400786b4c
|
|
@ -1,2 +1,3 @@
|
|||
pub mod uno_logic;
|
||||
pub mod uno_protocol;
|
||||
pub mod uno_tcp;
|
||||
|
|
@ -1,5 +1,2 @@
|
|||
pub mod tcp_server;
|
||||
|
||||
fn main() {
|
||||
tcp_server::main();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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<TcpStream>, BufWriter<TcpStream>) {
|
||||
(
|
||||
BufReader::new(stream.try_clone().expect("Could not clone TcpStreams")),
|
||||
BufWriter::new(stream),
|
||||
)
|
||||
}
|
||||
fn check_protocol(
|
||||
reader: BufReader<TcpStream>,
|
||||
mut writer: BufWriter<TcpStream>
|
||||
) -> Result<String, String> {
|
||||
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::<Vec<_>>()[1].to_owned());
|
||||
}
|
||||
|
||||
// fn register_player(player_name : &str, reader : BufReader, writer : BufWriter){
|
||||
//
|
||||
// }
|
||||
|
|
@ -55,9 +55,8 @@ pub struct PlayerState<'a> {
|
|||
}
|
||||
|
||||
pub struct PlayerConnection<'a>{
|
||||
player_name : &'a str,
|
||||
reader: BufReader<TcpStream>,
|
||||
writer: BufWriter<TcpStream>,
|
||||
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<TcpStream>, writer : BufWriter<TcpStream>) -> PlayerConnection{
|
||||
pub fn new(player_name : &str, stream : TcpStream) -> PlayerConnection{
|
||||
PlayerConnection {
|
||||
player_name : player_name,
|
||||
reader : reader,
|
||||
writer : writer
|
||||
stream : stream
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
43
src/uno_tcp.rs
Normal file
43
src/uno_tcp.rs
Normal file
|
|
@ -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<PlayerConnection<'a>, 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::<Vec<_>>()[1], stream : stream});
|
||||
}
|
||||
|
||||
// fn register_player(player_name : &str, reader : BufReader, writer : BufWriter){
|
||||
//
|
||||
// }
|
||||
Loading…
Reference in New Issue
Block a user