Less fragility
This commit is contained in:
parent
a7e227b5f0
commit
e4cc7cb24a
1 changed files with 24 additions and 15 deletions
|
@ -49,13 +49,11 @@ impl NetworkManager {
|
||||||
.connections
|
.connections
|
||||||
.get_mut(&message.conn_id)
|
.get_mut(&message.conn_id)
|
||||||
.expect("cant send message to an unregistered connection");
|
.expect("cant send message to an unregistered connection");
|
||||||
match message.data {
|
if let NetworkPayload::Message(msg) = message.data {
|
||||||
NetworkPayload::Message(msg) => conn
|
let res = conn.stream.send(msg.into()).await;
|
||||||
.stream
|
if let Err(err) = res {
|
||||||
.send(msg.into())
|
log::error!("Error sending message over to game instance: {:?}", err)
|
||||||
.await
|
}
|
||||||
.expect("could not send message to connection"),
|
|
||||||
_ => (),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -74,8 +72,12 @@ pub async fn listen(
|
||||||
.peer_addr()
|
.peer_addr()
|
||||||
.expect("connected streams should have a peer address");
|
.expect("connected streams should have a peer address");
|
||||||
log::info!("Peer address: {}", peer);
|
log::info!("Peer address: {}", peer);
|
||||||
let conn = accept_connection(peer, stream, incoming.clone()).await;
|
match accept_connection(peer, stream, incoming.clone()).await {
|
||||||
network.lock().await.register(conn);
|
Ok(conn) => {
|
||||||
|
network.lock().await.register(conn);
|
||||||
|
}
|
||||||
|
Err(err) => log::warn!("failed connection from {:?}: {:?}", peer, err),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -83,12 +85,12 @@ async fn accept_connection(
|
||||||
peer: SocketAddr,
|
peer: SocketAddr,
|
||||||
stream: TcpStream,
|
stream: TcpStream,
|
||||||
incoming: Sender<NetworkMessage<ClientMessage>>,
|
incoming: Sender<NetworkMessage<ClientMessage>>,
|
||||||
) -> Connection {
|
) -> Result<Connection, tungstenite::Error> {
|
||||||
// Generate random connection id
|
// Generate random connection id
|
||||||
let conn_id = rand::random();
|
let conn_id = rand::random();
|
||||||
|
|
||||||
// Get streams
|
// Get streams
|
||||||
let ws_stream = accept_async(stream).await.expect("could not accept");
|
let ws_stream = accept_async(stream).await?;
|
||||||
let (ws_sender, ws_receiver) = ws_stream.split();
|
let (ws_sender, ws_receiver) = ws_stream.split();
|
||||||
|
|
||||||
// Send event to game instance
|
// Send event to game instance
|
||||||
|
@ -102,11 +104,11 @@ async fn accept_connection(
|
||||||
task::spawn(read_loop(ws_receiver, incoming, conn_id));
|
task::spawn(read_loop(ws_receiver, incoming, conn_id));
|
||||||
|
|
||||||
// Return connection instance for network manager
|
// Return connection instance for network manager
|
||||||
Connection {
|
Ok(Connection {
|
||||||
stream: ws_sender,
|
stream: ws_sender,
|
||||||
address: peer,
|
address: peer,
|
||||||
conn_id,
|
conn_id,
|
||||||
}
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn read_loop(
|
async fn read_loop(
|
||||||
|
@ -159,7 +161,14 @@ async fn read_loop(
|
||||||
|
|
||||||
async fn write_loop(net: Arc<Mutex<NetworkManager>>, ch: Receiver<NetworkMessage<ServerMessage>>) {
|
async fn write_loop(net: Arc<Mutex<NetworkManager>>, ch: Receiver<NetworkMessage<ServerMessage>>) {
|
||||||
loop {
|
loop {
|
||||||
let msg = ch.recv().await.expect("failed to receive outgoing message");
|
match ch.recv().await {
|
||||||
net.lock().await.send(msg).await;
|
Ok(msg) => {
|
||||||
|
net.lock().await.send(msg).await;
|
||||||
|
}
|
||||||
|
Err(err) => {
|
||||||
|
log::warn!("write loop received error: {:?}", err);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue