Changes requested in rewiew

This commit is contained in:
Marcel Märtens 2020-07-13 23:13:54 +02:00
parent 6db9c6f91b
commit c74e5e4b47
3 changed files with 9 additions and 16 deletions

View File

@ -8,7 +8,7 @@ use async_std::sync::RwLock;
use clap::{App, Arg};
use futures::executor::{block_on, ThreadPool};
use network::{ProtocolAddr, Network, Participant, Pid, PROMISES_CONSISTENCY, PROMISES_ORDERED};
use std::{sync::Arc, thread, time::Duration, collections::HashMap};
use std::{sync::Arc, thread, time::Duration};
use tracing::*;
use tracing_subscriber::EnvFilter;
@ -104,19 +104,19 @@ fn server(address: ProtocolAddr) {
let server = Arc::new(server);
std::thread::spawn(f);
let pool = ThreadPool::new().unwrap();
let participants = Arc::new(RwLock::new(HashMap::new()));
let participants = Arc::new(RwLock::new(Vec::new()));
block_on(async {
server.listen(address).await.unwrap();
loop {
let p1 = Arc::new(server.connected().await.unwrap());
let server1 = server.clone();
participants.write().await.insert(p1.remote_pid(), p1.clone());
participants.write().await.push(p1.clone());
pool.spawn_ok(client_connection(server1, p1, participants.clone()));
}
});
}
async fn client_connection(_network: Arc<Network>, participant: Arc<Participant>, participants: Arc<RwLock<HashMap<Pid, Arc<Participant>>>>) {
async fn client_connection(_network: Arc<Network>, participant: Arc<Participant>, participants: Arc<RwLock<Vec<Arc<Participant>>>>) {
let mut s1 = participant.opened().await.unwrap();
let username = s1.recv::<String>().await.unwrap();
println!("[{}] connected", username);
@ -127,7 +127,7 @@ async fn client_connection(_network: Arc<Network>, participant: Arc<Participant>
},
Ok(msg) => {
println!("[{}]: {}", username, msg);
for (_, p) in participants.read().await.iter() {
for p in participants.read().await.iter() {
match p
.open(32, PROMISES_ORDERED | PROMISES_CONSISTENCY)
.await {

View File

@ -48,7 +48,7 @@ pub fn handle_exit_ingame(server: &mut Server, entity: EcsEntity) {
pub fn handle_client_disconnect(server: &mut Server, entity: EcsEntity) -> Event {
if let Some(client) = server.state().read_storage::<Client>().get(entity) {
trace!("closing participant of client");
trace!("Closing participant of client");
let participant = client.participant.lock().unwrap().take().unwrap();
if let Err(e) = block_on(participant.disconnect()) {
debug!(

View File

@ -101,24 +101,17 @@ impl ClientInit {
},
Err(ClientError::NetworkErr(NetworkError::ConnectFailed(e))) => {
if e.kind() == std::io::ErrorKind::PermissionDenied {
warn!(
?e,
"You can't connect to the server, you are running a \
incompatible version than the server"
);
warn!(?e, "Cannot connect to server: Incompatible version");
last_err = Some(Error::ClientError(
ClientError::NetworkErr(NetworkError::ConnectFailed(e)),
));
break 'tries;
} else {
debug!(
"can't reach the server, going to retry in a few \
seconds"
);
debug!("Cannot connect to server: Timeout (retrying...)");
}
},
Err(e) => {
trace!(?e, "stopping connecting to server, due to error");
trace!(?e, "Aborting server connection attempt");
last_err = Some(Error::ClientError(e));
break 'tries;
},