Skip to content

Commit c443c69

Browse files
zicklagTekhnaeRaav
andauthored
refactor!: add configurable service for LAN matchmaking and fix bug when stopping hosting. (#502)
Also re-exports the `iroh` crate in the networking module. --------- Co-authored-by: Tekhnae Raav <tekhnaeraav@katharostech.com>
1 parent 2aeb58d commit c443c69

File tree

2 files changed

+12
-13
lines changed

2 files changed

+12
-13
lines changed

framework_crates/bones_framework/src/networking.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ use {
2222

2323
use crate::input::PlayerControls as PlayerControlsTrait;
2424

25+
pub use iroh;
26+
2527
pub mod input;
2628
pub mod lan;
2729
pub mod online;

framework_crates/bones_framework/src/networking/lan.rs

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,6 @@ static LAN_MATCHMAKER: Lazy<LanMatchmaker> = Lazy::new(|| {
5454
static MDNS: Lazy<ServiceDaemon> =
5555
Lazy::new(|| ServiceDaemon::new().expect("Couldn't start MDNS service discovery thread."));
5656

57-
const MDNS_SERVICE_TYPE: &str = "_jumpy._udp.local.";
58-
5957
#[derive(DerefMut, Deref)]
6058
struct Pinger(BiChannelClient<PingerRequest, PingerResponse>);
6159

@@ -86,6 +84,9 @@ pub fn stop_server(server: &ServerInfo) {
8684
if let Err(err) = stop_server_by_name(server.service.get_fullname()) {
8785
warn!("Lan: failed to stop server: {err:?}");
8886
}
87+
LAN_MATCHMAKER
88+
.try_send(LanMatchmakerRequest::StopServer)
89+
.unwrap();
8990
}
9091

9192
/// Stop hosting a server specified by name. (Use [`ServiceInfo::get_fullname()`].)
@@ -168,6 +169,7 @@ pub fn wait_game_start() -> Option<NetworkMatchSocket> {
168169

169170
/// Update server pings and turn on service discovery.
170171
pub fn prepare_to_join(
172+
service_type: &str,
171173
servers: &mut Vec<ServerInfo>,
172174
service_discovery_recv: &mut Option<ServiceDiscoveryReceiver>,
173175
ping_update_timer: &Timer,
@@ -195,7 +197,7 @@ pub fn prepare_to_join(
195197

196198
let events = service_discovery_recv.get_or_insert_with(|| {
197199
ServiceDiscoveryReceiver(
198-
MDNS.browse(MDNS_SERVICE_TYPE)
200+
MDNS.browse(service_type)
199201
.expect("Couldn't start service discovery"),
200202
)
201203
});
@@ -222,6 +224,7 @@ pub fn prepare_to_join(
222224
/// only then the returned `bool` is `true`.
223225
pub async fn prepare_to_host<'a>(
224226
host_info: &'a mut Option<ServerInfo>,
227+
service_type: &str,
225228
service_name: &str,
226229
) -> (bool, &'a mut ServerInfo) {
227230
let create_service_info = || async {
@@ -236,16 +239,10 @@ pub async fn prepare_to_host<'a>(
236239
let mut props = std::collections::HashMap::default();
237240
let addr_encoded = hex::encode(postcard::to_stdvec(&my_addr).unwrap());
238241
props.insert("node-addr".to_string(), addr_encoded);
239-
let service = mdns_sd::ServiceInfo::new(
240-
MDNS_SERVICE_TYPE,
241-
service_name,
242-
service_name,
243-
"",
244-
port,
245-
props,
246-
)
247-
.unwrap()
248-
.enable_addr_auto();
242+
let service =
243+
mdns_sd::ServiceInfo::new(service_type, service_name, service_name, "", port, props)
244+
.unwrap()
245+
.enable_addr_auto();
249246
ServerInfo {
250247
service,
251248
ping: None,

0 commit comments

Comments
 (0)