Skip to content

Commit

Permalink
Fix panic when using bevy_matchbox signaling feature
Browse files Browse the repository at this point in the history
  • Loading branch information
johanhelsing committed Oct 27, 2023
1 parent 98f40ea commit d07bf35
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 15 deletions.
3 changes: 2 additions & 1 deletion bevy_matchbox/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ readme = "../README.md"

[features]
ggrs = ["matchbox_socket/ggrs"]
signaling = ["matchbox_signaling"]
signaling = ["dep:matchbox_signaling", "dep:async-compat"]

[dependencies]
bevy = { version = "0.11", default-features = false }
Expand All @@ -30,3 +30,4 @@ cfg-if = "1.0"

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
matchbox_signaling = { version = "0.7", path = "../matchbox_signaling", optional = true }
async-compat = { version = "0.2", optional = true }
6 changes: 3 additions & 3 deletions bevy_matchbox/src/signaling.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use std::net::SocketAddr;

use async_compat::CompatExt;
use bevy::{
ecs::system::Command,
prelude::{Commands, Resource},
Expand All @@ -14,6 +13,7 @@ use matchbox_signaling::{
},
Error, SignalingCallbacks, SignalingServer, SignalingServerBuilder, SignalingState,
};
use std::net::SocketAddr;

/// A [`SignalingServer`] as a [`Resource`].
///
Expand Down Expand Up @@ -80,7 +80,7 @@ where
impl From<SignalingServer> for MatchboxServer {
fn from(server: SignalingServer) -> Self {
let task_pool = IoTaskPool::get();
let task = task_pool.spawn(server.serve());
let task = task_pool.spawn(server.serve().compat());
MatchboxServer(task)
}
}
Expand Down
17 changes: 8 additions & 9 deletions matchbox_signaling/src/signaling_server/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,14 +130,13 @@ where
.layer(Extension(self.shared_callbacks))
.layer(Extension(self.callbacks))
.layer(Extension(self.state));
let server = axum::Server::bind(&self.socket_addr).serve(
self.router
.into_make_service_with_connect_info::<SocketAddr>(),
);
let socket_addr = server.local_addr();
SignalingServer {
server,
socket_addr,
}

let info = self
.router
.into_make_service_with_connect_info::<SocketAddr>();

let socket_addr = self.socket_addr;
// let server = axum::Server::bind(&self.socket_addr).serve(info);
SignalingServer { info, socket_addr }
}
}
9 changes: 7 additions & 2 deletions matchbox_signaling/src/signaling_server/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@ pub struct SignalingServer {
/// The socket address bound for this server
pub(crate) socket_addr: SocketAddr,

// /// The low-level axum server
// pub(crate) server: Server<AddrIncoming, IntoMakeServiceWithConnectInfo<Router, SocketAddr>>,
/// The low-level axum server
pub(crate) server: Server<AddrIncoming, IntoMakeServiceWithConnectInfo<Router, SocketAddr>>,
pub(crate) info: IntoMakeServiceWithConnectInfo<Router, SocketAddr>,
}

/// Common methods
Expand All @@ -43,7 +45,10 @@ impl SignalingServer {
/// Serve the signaling server
pub async fn serve(self) -> Result<(), crate::Error> {
// TODO: Shouldn't this return Result<!, crate::Error>?
match self.server.await {
// todo: use try_serve?
let server = axum::Server::bind(&self.socket_addr).serve(self.info);

match server.await {
Ok(()) => Ok(()),
Err(e) => Err(crate::Error::from(e)),
}
Expand Down

0 comments on commit d07bf35

Please sign in to comment.