added config file

main
Kyle Johnsen 2022-02-13 16:40:26 -05:00
parent 5f2401d004
commit c2a30171e3
4 changed files with 105 additions and 8 deletions

80
Cargo.lock generated
View File

@ -7,6 +7,8 @@ name = "VelNetServerRust"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"chrono", "chrono",
"serde",
"serde_json",
] ]
[[package]] [[package]]
@ -28,6 +30,12 @@ dependencies = [
"winapi", "winapi",
] ]
[[package]]
name = "itoa"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1aab8fc367588b89dcee83ab0fd66b72b50b72fa1904d7095045ace2b0c81c35"
[[package]] [[package]]
name = "libc" name = "libc"
version = "0.2.117" version = "0.2.117"
@ -53,6 +61,72 @@ dependencies = [
"autocfg", "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]] [[package]]
name = "time" name = "time"
version = "0.1.44" version = "0.1.44"
@ -64,6 +138,12 @@ dependencies = [
"winapi", "winapi",
] ]
[[package]]
name = "unicode-xid"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8ccb82d61f80a663efe1f787a51b16b5a51e3314d6ac365b08639f52387b33f3"
[[package]] [[package]]
name = "wasi" name = "wasi"
version = "0.10.0+wasi-snapshot-preview1" version = "0.10.0+wasi-snapshot-preview1"

View File

@ -7,3 +7,5 @@ edition = "2021"
[dependencies] [dependencies]
chrono = "*" chrono = "*"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"

3
config.txt Normal file
View File

@ -0,0 +1,3 @@
{
"port":80
}

View File

@ -1,5 +1,6 @@
extern crate chrono; extern crate chrono;
extern crate serde;
extern crate serde_json;
use std::io::prelude::*; use std::io::prelude::*;
use std::thread; use std::thread;
use std::net::{TcpListener, TcpStream,UdpSocket,IpAddr,SocketAddr}; 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;
use std::sync::mpsc::{SyncSender,Receiver}; use std::sync::mpsc::{SyncSender,Receiver};
use chrono::Local; use chrono::Local;
use std::fs;
use std::fmt;
use serde::{Serialize, Deserialize};
enum ToClientTCPMessageType { enum ToClientTCPMessageType {
LoggedIn = 0, LoggedIn = 0,
RoomList = 1, RoomList = 1,
@ -62,7 +66,10 @@ struct Room {
clients: RwLock<HashMap<u32,Arc<Client>>>, clients: RwLock<HashMap<u32,Arc<Client>>>,
master_client: Arc<RwLock<Arc<Client>>> master_client: Arc<RwLock<Arc<Client>>>
} }
#[derive(Serialize, Deserialize)]
struct Config {
port: u16
}
fn read_u8(stream: &mut TcpStream) -> u8 { fn read_u8(stream: &mut TcpStream) -> u8 {
@ -666,9 +673,9 @@ fn handle_client(stream: TcpStream, client_id: u32, clients_mutex: Arc<RwLock<Ha
} }
fn tcp_listen(client_mutex: Arc<RwLock<HashMap<u32, Arc<Client>>>>, room_mutex: Arc<RwLock<HashMap<String,Arc<Room>>>>){ fn tcp_listen(client_mutex: Arc<RwLock<HashMap<u32, Arc<Client>>>>, room_mutex: Arc<RwLock<HashMap<String,Arc<Room>>>>,port:u16){
println!("{}: Started TCP Listener",Local::now().format("%Y-%m-%d %H:%M:%S")); 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; let mut next_client_id = 0;
// accept connections and process them serially // accept connections and process them serially
@ -681,9 +688,9 @@ fn tcp_listen(client_mutex: Arc<RwLock<HashMap<u32, Arc<Client>>>>, room_mutex:
println!("{}: Ended TCP Listener",Local::now().format("%Y-%m-%d %H:%M:%S")); println!("{}: Ended TCP Listener",Local::now().format("%Y-%m-%d %H:%M:%S"));
} }
fn udp_listen(client_mutex: Arc<RwLock<HashMap<u32, Arc<Client>>>>, _room_mutex: Arc<RwLock<HashMap<String,Arc<Room>>>>){ fn udp_listen(client_mutex: Arc<RwLock<HashMap<u32, Arc<Client>>>>, _room_mutex: Arc<RwLock<HashMap<String,Arc<Room>>>>,port:u16){
let mut buf = [0u8;1024]; 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")); println!("{}: UDP Thread Started",Local::now().format("%Y-%m-%d %H:%M:%S"));
loop { loop {
let res = s.recv_from(&mut buf); let res = s.recv_from(&mut buf);
@ -814,6 +821,11 @@ fn udp_listen(client_mutex: Arc<RwLock<HashMap<u32, Arc<Client>>>>, _room_mutex:
fn main() { fn main() {
println!("{}: VelNet Server Starting",Local::now().format("%Y-%m-%d %H:%M:%S")); 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<u32, Arc<Client>> = HashMap::new(); let clients: HashMap<u32, Arc<Client>> = HashMap::new();
let rooms: HashMap<String, Arc<Room>> = HashMap::new(); let rooms: HashMap<String, Arc<Room>> = HashMap::new();
let client_mutex = Arc::new(RwLock::new(clients)); let client_mutex = Arc::new(RwLock::new(clients));
@ -822,9 +834,9 @@ fn main() {
//start the UDP thread //start the UDP thread
let udp_clients = Arc::clone(&client_mutex); let udp_clients = Arc::clone(&client_mutex);
let udp_rooms = Arc::clone(&room_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 //start the TCP thread
tcp_listen(client_mutex, room_mutex); tcp_listen(client_mutex, room_mutex,config.port);
udp_handle.join().unwrap(); udp_handle.join().unwrap();
println!("{}: VelNet Ended", Local::now().format("%Y-%m-%d %H:%M:%S")); println!("{}: VelNet Ended", Local::now().format("%Y-%m-%d %H:%M:%S"));
} }