parent
593fbc96ed
commit
b1ad04ed50
|
|
@ -90,6 +90,8 @@ impl Server {
|
|||
info!("server: spawning threads");
|
||||
let _ = thread::Builder::new().name("Server Thread".to_string()).spawn(move || {
|
||||
'outer: loop {
|
||||
std::thread::sleep(Duration::from_millis(100));
|
||||
|
||||
// get messages from the servers channel.
|
||||
info!("server: getting messages");
|
||||
for i in receiver.try_iter() {
|
||||
|
|
@ -107,6 +109,9 @@ impl Server {
|
|||
let _ = &stream.flush();
|
||||
}
|
||||
}
|
||||
ServerMessages::Disconnect(uuid) => {
|
||||
client_map.lock().unwrap().remove(&uuid);
|
||||
},
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
|
@ -114,53 +119,56 @@ impl Server {
|
|||
info!("server: checking for new connections");
|
||||
if let Ok((mut stream, _addr)) = listener.accept() {
|
||||
stream.set_read_timeout(Some(Duration::from_millis(10000))).unwrap();
|
||||
let _ = stream.set_nonblocking(false);
|
||||
|
||||
let request = Commands::Request(None);
|
||||
//request.to_string();
|
||||
let _ = stream.write_all(&request.to_string().as_bytes());
|
||||
let _ = stream.flush();
|
||||
let _ = stream.read(&mut buffer).unwrap();
|
||||
|
||||
let incoming_message = String::from(String::from_utf8_lossy(&buffer));
|
||||
let command = Commands::from(incoming_message);
|
||||
// clears the buffer.
|
||||
buffer.zeroize();
|
||||
|
||||
match command {
|
||||
Commands::Connect(Some(data)) => {
|
||||
let uuid = data.get("uuid").unwrap();
|
||||
let username = data.get("name").unwrap();
|
||||
let address = data.get("host").unwrap();
|
||||
|
||||
info!("{}", format!("Server: new Client connection: _addr = {}", address ));
|
||||
|
||||
let client = Client::new(stream, sender.clone(), uuid.clone(), username.clone(), address.clone());
|
||||
|
||||
client_map.lock().unwrap().insert(uuid.to_string(), client);
|
||||
|
||||
let params: HashMap<String, String> = [(String::from("name"), username.clone()), (String::from("host"), address.clone()), (String::from("uuid"), uuid.clone())].iter().cloned().collect();
|
||||
let new_client = Commands::Client(Some(params));
|
||||
|
||||
let _ = client_map.lock().unwrap().iter().map(|(_k, v)| v.sender.send(new_client.clone()));
|
||||
},
|
||||
|
||||
// TODO: - correct connection reset error when getting info.
|
||||
Commands::Info(None) => {
|
||||
info!("Server: info requested");
|
||||
let mut params: HashMap<String, String> = HashMap::new();
|
||||
params.insert(String::from("name"), server_details.0.clone());
|
||||
params.insert(String::from("owner"), server_details.1.clone());
|
||||
|
||||
let command = Commands::Info(Some(params));
|
||||
|
||||
stream.write_all(command.to_string().as_bytes()).unwrap();
|
||||
stream.flush().unwrap();
|
||||
},
|
||||
_ => {
|
||||
info!("Server: Invalid command sent");
|
||||
let _ = stream.write_all(Commands::Error(None).to_string().as_bytes());
|
||||
let _ = stream.flush();
|
||||
},
|
||||
let _ = stream.read(&mut buffer);
|
||||
if let Ok(size) = stream.read(&mut buffer) {
|
||||
let incoming_message = String::from(String::from_utf8_lossy(&buffer));
|
||||
let command = Commands::from(incoming_message);
|
||||
info!("Server: new connection sent - {:?}", command);
|
||||
// clears the buffer.
|
||||
buffer.zeroize();
|
||||
|
||||
match command {
|
||||
Commands::Connect(Some(data)) => {
|
||||
let uuid = data.get("uuid").unwrap();
|
||||
let username = data.get("name").unwrap();
|
||||
let address = data.get("host").unwrap();
|
||||
|
||||
info!("{}", format!("Server: new Client connection: _addr = {}", address ));
|
||||
|
||||
let client = Client::new(stream, sender.clone(), uuid.clone(), username.clone(), address.clone());
|
||||
|
||||
client_map.lock().unwrap().insert(uuid.to_string(), client);
|
||||
|
||||
let params: HashMap<String, String> = [(String::from("name"), username.clone()), (String::from("host"), address.clone()), (String::from("uuid"), uuid.clone())].iter().cloned().collect();
|
||||
let new_client = Commands::Client(Some(params));
|
||||
|
||||
let _ = client_map.lock().unwrap().iter().map(|(_k, v)| v.sender.send(new_client.clone()));
|
||||
},
|
||||
|
||||
// TODO: - correct connection reset error when getting info.
|
||||
Commands::Info(None) => {
|
||||
info!("Server: info requested");
|
||||
let mut params: HashMap<String, String> = HashMap::new();
|
||||
params.insert(String::from("name"), server_details.0.clone());
|
||||
params.insert(String::from("owner"), server_details.1.clone());
|
||||
|
||||
let command = Commands::Info(Some(params));
|
||||
|
||||
stream.write_all(command.to_string().as_bytes()).expect("Server -Info: writing failed");
|
||||
stream.flush().expect("Server -Info: flushing errored");
|
||||
},
|
||||
_ => {
|
||||
info!("Server: Invalid command sent");
|
||||
let _ = stream.write_all(Commands::Error(None).to_string().as_bytes());
|
||||
let _ = stream.flush();
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
// TODO: end -
|
||||
|
|
|
|||
Loading…
Reference in New Issue