tidy up
This commit is contained in:
parent
43babbbfab
commit
eee34eb3ff
2 changed files with 17 additions and 16 deletions
26
src/main.rs
26
src/main.rs
|
@ -5,7 +5,7 @@ use anyhow::{Context, Result};
|
||||||
use future::ready;
|
use future::ready;
|
||||||
use futures::prelude::*;
|
use futures::prelude::*;
|
||||||
use net::{
|
use net::{
|
||||||
server::{Handle, Server, run_client},
|
server::{run_client, Handle, Server},
|
||||||
ClientMessage, ServerMessage,
|
ClientMessage, ServerMessage,
|
||||||
};
|
};
|
||||||
use serde_json::{from_str, to_string};
|
use serde_json::{from_str, to_string};
|
||||||
|
@ -72,17 +72,19 @@ async fn handle_socket(game_server: Handle, websocket: WebSocket) -> Result<()>
|
||||||
)
|
)
|
||||||
});
|
});
|
||||||
|
|
||||||
let mut source = source.map_err(Into::into).try_filter_map(|msg| {
|
let mut source = source
|
||||||
ready(match msg.to_str() {
|
.map_err(Into::into)
|
||||||
Ok(json) => from_str::<ClientMessage>(json)
|
.try_filter_map(|msg| {
|
||||||
.context("Parsing JSON")
|
ready(match msg.to_str() {
|
||||||
.map(Some),
|
Ok(json) => from_str::<ClientMessage>(json)
|
||||||
Err(()) => {
|
.context("Parsing JSON")
|
||||||
debug!("Non-text message");
|
.map(Some),
|
||||||
Ok(None)
|
Err(()) => {
|
||||||
}
|
debug!("Non-text message {:?}", &msg);
|
||||||
})
|
Ok(None)
|
||||||
});
|
}
|
||||||
|
})
|
||||||
|
});
|
||||||
|
|
||||||
run_client(game_server, &mut source, &mut sink).await
|
run_client(game_server, &mut source, &mut sink).await
|
||||||
}
|
}
|
||||||
|
|
|
@ -101,16 +101,15 @@ pub async fn run_client(
|
||||||
|
|
||||||
let input_task = async {
|
let input_task = async {
|
||||||
loop {
|
loop {
|
||||||
match source.next().await {
|
match source.try_next().await? {
|
||||||
Some(Ok(msg)) => handle.server.lock().await.process_message(player_id, msg),
|
Some(msg) => handle.server.lock().await.process_message(player_id, msg),
|
||||||
Some(Err(error)) => return Err(error),
|
|
||||||
None => break Ok(()),
|
None => break Ok(()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
let result = try_join(output_task, input_task).await.map(|_| ());
|
let result = try_join(output_task, input_task).await.map(|_| ());
|
||||||
|
|
||||||
// deregister player, whether normally or via error
|
// deregister player, whether normally or due to error
|
||||||
handle.server.lock().await.remove_player(player_id);
|
handle.server.lock().await.remove_player(player_id);
|
||||||
|
|
||||||
result
|
result
|
||||||
|
|
Loading…
Reference in a new issue