diff --git a/Cargo.lock b/Cargo.lock index 6957443..1f59244 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -7,6 +7,8 @@ name = "VelNetServerRust" version = "0.1.0" dependencies = [ "chrono", + "serde", + "serde_json", ] [[package]] @@ -28,6 +30,12 @@ dependencies = [ "winapi", ] +[[package]] +name = "itoa" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1aab8fc367588b89dcee83ab0fd66b72b50b72fa1904d7095045ace2b0c81c35" + [[package]] name = "libc" version = "0.2.117" @@ -53,6 +61,72 @@ dependencies = [ "autocfg", ] +[[package]] +name = "proc-macro2" +version = "1.0.36" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c7342d5883fbccae1cc37a2353b09c87c9b0f3afd73f5fb9bba687a1f733b029" +dependencies = [ + "unicode-xid", +] + +[[package]] +name = "quote" +version = "1.0.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "864d3e96a899863136fc6e99f3d7cae289dafe43bf2c5ac19b70df7210c0a145" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "ryu" +version = "1.0.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "73b4b750c782965c211b42f022f59af1fbceabdd026623714f104152f1ec149f" + +[[package]] +name = "serde" +version = "1.0.136" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ce31e24b01e1e524df96f1c2fdd054405f8d7376249a5110886fb4b658484789" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde_derive" +version = "1.0.136" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "08597e7152fcd306f41838ed3e37be9eaeed2b61c42e2117266a554fab4662f9" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "serde_json" +version = "1.0.79" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e8d9fa5c3b304765ce1fd9c4c8a3de2c8db365a5b91be52f186efc675681d95" +dependencies = [ + "itoa", + "ryu", + "serde", +] + +[[package]] +name = "syn" +version = "1.0.86" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8a65b3f4ffa0092e9887669db0eae07941f023991ab58ea44da8fe8e2d511c6b" +dependencies = [ + "proc-macro2", + "quote", + "unicode-xid", +] + [[package]] name = "time" version = "0.1.44" @@ -64,6 +138,12 @@ dependencies = [ "winapi", ] +[[package]] +name = "unicode-xid" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8ccb82d61f80a663efe1f787a51b16b5a51e3314d6ac365b08639f52387b33f3" + [[package]] name = "wasi" version = "0.10.0+wasi-snapshot-preview1" diff --git a/Cargo.toml b/Cargo.toml index e95b8b4..90aeba8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,3 +7,5 @@ edition = "2021" [dependencies] chrono = "*" +serde = { version = "1.0", features = ["derive"] } +serde_json = "1.0" diff --git a/config.txt b/config.txt new file mode 100644 index 0000000..eeb9f87 --- /dev/null +++ b/config.txt @@ -0,0 +1,3 @@ +{ +"port":80 +} \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 6433669..b82f0eb 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,6 @@ extern crate chrono; - +extern crate serde; +extern crate serde_json; use std::io::prelude::*; use std::thread; use std::net::{TcpListener, TcpStream,UdpSocket,IpAddr,SocketAddr}; @@ -8,6 +9,9 @@ use std::sync::{Arc,RwLock}; use std::sync::mpsc; use std::sync::mpsc::{SyncSender,Receiver}; use chrono::Local; +use std::fs; +use std::fmt; +use serde::{Serialize, Deserialize}; enum ToClientTCPMessageType { LoggedIn = 0, RoomList = 1, @@ -62,7 +66,10 @@ struct Room { clients: RwLock>>, master_client: Arc>> } - +#[derive(Serialize, Deserialize)] +struct Config { + port: u16 +} fn read_u8(stream: &mut TcpStream) -> u8 { @@ -666,9 +673,9 @@ fn handle_client(stream: TcpStream, client_id: u32, clients_mutex: Arc>>>, room_mutex: Arc>>>){ +fn tcp_listen(client_mutex: Arc>>>, room_mutex: Arc>>>,port:u16){ println!("{}: Started TCP Listener",Local::now().format("%Y-%m-%d %H:%M:%S")); - let listener = TcpListener::bind("0.0.0.0:80").expect("could not bind port"); + let listener = TcpListener::bind(format!("0.0.0.0:{}",port)).expect("could not bind port"); let mut next_client_id = 0; // accept connections and process them serially @@ -681,9 +688,9 @@ fn tcp_listen(client_mutex: Arc>>>, room_mutex: println!("{}: Ended TCP Listener",Local::now().format("%Y-%m-%d %H:%M:%S")); } -fn udp_listen(client_mutex: Arc>>>, _room_mutex: Arc>>>){ +fn udp_listen(client_mutex: Arc>>>, _room_mutex: Arc>>>,port:u16){ let mut buf = [0u8;1024]; - let s = UdpSocket::bind("0.0.0.0:80").unwrap(); + let s = UdpSocket::bind(format!("0.0.0.0:{}",port)).unwrap(); println!("{}: UDP Thread Started",Local::now().format("%Y-%m-%d %H:%M:%S")); loop { let res = s.recv_from(&mut buf); @@ -814,6 +821,11 @@ fn udp_listen(client_mutex: Arc>>>, _room_mutex: fn main() { println!("{}: VelNet Server Starting",Local::now().format("%Y-%m-%d %H:%M:%S")); + //read the config file + let foo = fs::read_to_string("config.txt").unwrap(); + let config: Config = serde_json::from_str(&foo).unwrap(); + println!("{}",config.port); + let clients: HashMap> = HashMap::new(); let rooms: HashMap> = HashMap::new(); let client_mutex = Arc::new(RwLock::new(clients)); @@ -822,9 +834,9 @@ fn main() { //start the UDP thread let udp_clients = Arc::clone(&client_mutex); let udp_rooms = Arc::clone(&room_mutex); - let udp_handle = thread::spawn(move ||{udp_listen(udp_clients, udp_rooms);}); + let udp_handle = thread::spawn(move ||{udp_listen(udp_clients, udp_rooms, config.port);}); //start the TCP thread - tcp_listen(client_mutex, room_mutex); + tcp_listen(client_mutex, room_mutex,config.port); udp_handle.join().unwrap(); println!("{}: VelNet Ended", Local::now().format("%Y-%m-%d %H:%M:%S")); }