Skip to content

Commit

Permalink
feat: upgrade to iroh 0.29 (#491)
Browse files Browse the repository at this point in the history
iroh-net is now called just `iroh`, more details in a blog post that is
coming in the next days
  • Loading branch information
dignifiedquire authored Dec 4, 2024
1 parent ba0bb88 commit 3eb56b8
Show file tree
Hide file tree
Showing 15 changed files with 733 additions and 372 deletions.
866 changes: 600 additions & 266 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ maybe-owned = "0.3"
parking_lot = "0.12"
smallvec = "1.11"
ustr = "0.10"
iroh-net = "0.27"
iroh = "0.29"

[profile.release]
lto = true
3 changes: 1 addition & 2 deletions framework_crates/bones_framework/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -153,10 +153,9 @@ postcard = { version = "1.0", features = ["alloc"] }
rcgen = "0.12"
rustls = { version = "0.21", features = ["dangerous_configuration", "quic"] }
smallvec = "1.10"
iroh-quinn = { version = "0.11" }
tokio = { version = "1", features = ["rt-multi-thread", "macros"] }
turborand = { version = "0.10.0", features = ["atomic"] }
iroh-net = { workspace = true, features = ["discovery-local-network"] }
iroh = { workspace = true, features = ["discovery-local-network"] }

directories = "5.0"

Expand Down
17 changes: 8 additions & 9 deletions framework_crates/bones_framework/src/networking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,26 +110,25 @@ impl<T: DenseInput + Debug> ggrs::Config for GgrsConfig<T> {
}

/// The network endpoint used for all network communications.
static NETWORK_ENDPOINT: tokio::sync::OnceCell<iroh_net::Endpoint> =
tokio::sync::OnceCell::const_new();
static NETWORK_ENDPOINT: tokio::sync::OnceCell<iroh::Endpoint> = tokio::sync::OnceCell::const_new();

/// Get the network endpoint used for all communications.
pub async fn get_network_endpoint() -> &'static iroh_net::Endpoint {
pub async fn get_network_endpoint() -> &'static iroh::Endpoint {
NETWORK_ENDPOINT
.get_or_init(|| async move {
let secret_key = iroh_net::key::SecretKey::generate();
iroh_net::Endpoint::builder()
let secret_key = iroh::key::SecretKey::generate();
iroh::Endpoint::builder()
.alpns(vec![MATCH_ALPN.to_vec(), PLAY_ALPN.to_vec()])
.discovery(Box::new(
iroh_net::discovery::ConcurrentDiscovery::from_services(vec![
iroh::discovery::ConcurrentDiscovery::from_services(vec![
Box::new(
iroh_net::discovery::local_swarm_discovery::LocalSwarmDiscovery::new(
iroh::discovery::local_swarm_discovery::LocalSwarmDiscovery::new(
secret_key.public(),
)
.unwrap(),
),
Box::new(iroh_net::discovery::dns::DnsDiscovery::n0_dns()),
Box::new(iroh_net::discovery::pkarr::PkarrPublisher::n0_dns(
Box::new(iroh::discovery::dns::DnsDiscovery::n0_dns()),
Box::new(iroh::discovery::pkarr::PkarrPublisher::n0_dns(
secret_key.clone(),
)),
]),
Expand Down
2 changes: 1 addition & 1 deletion framework_crates/bones_framework/src/networking/lan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

use std::{net::IpAddr, time::Duration};

use iroh_net::{endpoint::get_remote_node_id, NodeAddr};
use iroh::{endpoint::get_remote_node_id, NodeAddr};
use mdns_sd::{ServiceDaemon, ServiceInfo};
use smallvec::SmallVec;
use tracing::warn;
Expand Down
4 changes: 1 addition & 3 deletions framework_crates/bones_framework/src/networking/online.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ use crate::{
pub use bones_matchmaker_proto::{
GameID, LobbyId, LobbyInfo, LobbyListItem, MatchInfo, PlayerIdxAssignment, MATCH_ALPN,
};
use iroh_net::Endpoint;
use iroh_net::NodeId;
use iroh_quinn::Connection;
use iroh::{endpoint::Connection, Endpoint, NodeId};
use once_cell::sync::Lazy;
use tracing::{info, warn};

Expand Down
12 changes: 6 additions & 6 deletions framework_crates/bones_framework/src/networking/socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

use bones_matchmaker_proto::PLAY_ALPN;
use bytes::Bytes;
use iroh_net::NodeAddr;
use iroh::NodeAddr;
use tracing::{info, warn};

use crate::networking::get_network_endpoint;
Expand All @@ -13,7 +13,7 @@ use super::{GameMessage, NetworkSocket, SocketTarget, RUNTIME};
/// The [`NetworkSocket`] implementation.
#[derive(Debug, Clone)]
pub struct Socket {
pub connections: Vec<(u32, iroh_quinn::Connection)>,
pub connections: Vec<(u32, iroh::endpoint::Connection)>,
pub ggrs_receiver: async_channel::Receiver<(u32, GameMessage)>,
pub reliable_receiver: async_channel::Receiver<(u32, Vec<u8>)>,
pub player_idx: u32,
Expand All @@ -23,7 +23,7 @@ pub struct Socket {
}

impl Socket {
pub fn new(player_idx: u32, connections: Vec<(u32, iroh_quinn::Connection)>) -> Self {
pub fn new(player_idx: u32, connections: Vec<(u32, iroh::endpoint::Connection)>) -> Self {
let (ggrs_sender, ggrs_receiver) = async_channel::unbounded();
let (reliable_sender, reliable_receiver) = async_channel::unbounded();

Expand Down Expand Up @@ -129,7 +129,7 @@ impl Socket {
}
}

fn get_connection(&self, idx: u32) -> &iroh_quinn::Connection {
fn get_connection(&self, idx: u32) -> &iroh::endpoint::Connection {
debug_assert!(idx < self.player_count);
// TODO: if this is too slow, optimize storage
self.connections
Expand Down Expand Up @@ -217,8 +217,8 @@ pub(super) async fn establish_peer_connections(
player_idx: u32,
player_count: u32,
peer_addrs: Vec<(u32, NodeAddr)>,
conn: Option<iroh_quinn::Connection>,
) -> anyhow::Result<Vec<(u32, iroh_quinn::Connection)>> {
conn: Option<iroh::endpoint::Connection>,
) -> anyhow::Result<Vec<(u32, iroh::endpoint::Connection)>> {
let mut peer_connections = Vec::new();
let had_og_conn = conn.is_some();
if let Some(conn) = conn {
Expand Down
3 changes: 1 addition & 2 deletions other_crates/bones_matchmaker/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,5 @@ postcard = { version = "1.0", default-features = false, features =
serde = { version = "1.0", features = ["derive"] }
tracing-subscriber = { version = "0.3", features = ["env-filter"] }
tokio = { version = "1", features = ["rt-multi-thread", "macros"] }
iroh-net = { workspace = true, features = ["discovery-local-network"] }
quinn = { version = "0.11", package = "iroh-quinn" }
iroh = { workspace = true, features = ["discovery-local-network"] }
blake3 = "1.5.3"
18 changes: 9 additions & 9 deletions other_crates/bones_matchmaker/examples/matchmaker_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,18 @@ async fn main() {
}

async fn client() -> anyhow::Result<()> {
let secret_key = iroh_net::key::SecretKey::generate();
let endpoint = iroh_net::Endpoint::builder()
let secret_key = iroh::key::SecretKey::generate();
let endpoint = iroh::Endpoint::builder()
.alpns(vec![MATCH_ALPN.to_vec(), PLAY_ALPN.to_vec()])
.discovery(Box::new(
iroh_net::discovery::ConcurrentDiscovery::from_services(vec![
iroh::discovery::ConcurrentDiscovery::from_services(vec![
Box::new(
iroh_net::discovery::local_swarm_discovery::LocalSwarmDiscovery::new(
iroh::discovery::local_swarm_discovery::LocalSwarmDiscovery::new(
secret_key.public(),
)?,
),
Box::new(iroh_net::discovery::dns::DnsDiscovery::n0_dns()),
Box::new(iroh_net::discovery::pkarr::PkarrPublisher::n0_dns(
Box::new(iroh::discovery::dns::DnsDiscovery::n0_dns()),
Box::new(iroh::discovery::pkarr::PkarrPublisher::n0_dns(
secret_key.clone(),
)),
]),
Expand All @@ -48,8 +48,8 @@ async fn client() -> anyhow::Result<()> {
let hello = Hello { i_am };
println!("o Opened client ID: {}. {hello:?}", endpoint.node_id());

let server_id: iroh_net::NodeId = std::env::args().nth(3).expect("missing node id").parse()?;
let server_addr = iroh_net::NodeAddr::new(server_id);
let server_id: iroh::NodeId = std::env::args().nth(3).expect("missing node id").parse()?;
let server_addr = iroh::NodeAddr::new(server_id);

// Connect to the server
let conn = endpoint.connect(server_addr, MATCH_ALPN).await?;
Expand Down Expand Up @@ -178,7 +178,7 @@ async fn client() -> anyhow::Result<()> {
}

// Shutdown the endpoint
endpoint.close(0u8.into(), b"done").await?;
endpoint.close().await?;

Ok(())
}
44 changes: 19 additions & 25 deletions other_crates/bones_matchmaker/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
extern crate tracing;

use std::net::{Ipv4Addr, SocketAddr, SocketAddrV4};
use std::sync::Arc;

use bones_matchmaker_proto::MATCH_ALPN;
use iroh_net::key::SecretKey;
use iroh::key::SecretKey;
use matchmaker::Matchmaker;

pub mod cli;
mod helpers;
Expand All @@ -27,7 +29,7 @@ struct Config {
print_secret_key: bool,
/// Use this secret key for the node
#[clap(short, long, env = "BONES_MATCHMAKER_SECRET_KEY")]
secret_key: Option<iroh_net::key::SecretKey>,
secret_key: Option<iroh::key::SecretKey>,
}

async fn server(args: Config) -> anyhow::Result<()> {
Expand All @@ -48,17 +50,17 @@ async fn server(args: Config) -> anyhow::Result<()> {
println!("Secret Key: {}", secret_key);
}

let endpoint = iroh_net::Endpoint::builder()
let endpoint = iroh::Endpoint::builder()
.alpns(vec![MATCH_ALPN.to_vec()])
.discovery(Box::new(
iroh_net::discovery::ConcurrentDiscovery::from_services(vec![
iroh::discovery::ConcurrentDiscovery::from_services(vec![
Box::new(
iroh_net::discovery::local_swarm_discovery::LocalSwarmDiscovery::new(
iroh::discovery::local_swarm_discovery::LocalSwarmDiscovery::new(
secret_key.public(),
)?,
),
Box::new(iroh_net::discovery::dns::DnsDiscovery::n0_dns()),
Box::new(iroh_net::discovery::pkarr::PkarrPublisher::n0_dns(
Box::new(iroh::discovery::dns::DnsDiscovery::n0_dns()),
Box::new(iroh::discovery::pkarr::PkarrPublisher::n0_dns(
secret_key.clone(),
)),
]),
Expand All @@ -74,24 +76,16 @@ async fn server(args: Config) -> anyhow::Result<()> {

println!("Node ID: {}", my_addr.node_id);

// Listen for incomming connections
while let Some(connecting) = endpoint.accept().await {
let connection = connecting.await;

match connection {
Ok(conn) => {
info!(
connection_id = conn.stable_id(),
addr = ?conn.remote_address(),
"Accepted connection from client"
);

// Spawn a task to handle the new connection
tokio::task::spawn(matchmaker::handle_connection(endpoint.clone(), conn));
}
Err(e) => error!("Error opening client connection: {e:?}"),
}
}
let matchmaker = Matchmaker::new(endpoint.clone());
let router = iroh::protocol::Router::builder(endpoint)
.accept(MATCH_ALPN, Arc::new(matchmaker))
.spawn()
.await?;

// wait for shutdown
tokio::signal::ctrl_c().await?;

router.shutdown().await?;

info!("Server shutdown");

Expand Down
15 changes: 9 additions & 6 deletions other_crates/bones_matchmaker/src/lobbies.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ use anyhow::Result;
use bones_matchmaker_proto::{
GameID, LobbyId, LobbyInfo, LobbyListItem, MatchInfo, MatchmakerResponse,
};
use iroh_net::Endpoint;
use quinn::Connection;
use iroh::{endpoint::Connection, Endpoint};
use std::collections::HashMap;

/// Handles a request to list lobbies for a specific game
pub async fn handle_list_lobbies(game_id: GameID, send: &mut quinn::SendStream) -> Result<()> {
pub async fn handle_list_lobbies(
game_id: GameID,
send: &mut iroh::endpoint::SendStream,
) -> Result<()> {
let state = MATCHMAKER_STATE.lock().await;
// Retrieve and format lobby information for the specified game
let lobbies = state
Expand Down Expand Up @@ -51,7 +53,7 @@ pub async fn handle_list_lobbies(game_id: GameID, send: &mut quinn::SendStream)
pub async fn handle_create_lobby(
conn: Connection,
lobby_info: LobbyInfo,
send: &mut quinn::SendStream,
send: &mut iroh::endpoint::SendStream,
) -> Result<()> {
let lobby_id = LobbyId(generate_unique_id());
let mut state = MATCHMAKER_STATE.lock().await;
Expand Down Expand Up @@ -86,12 +88,12 @@ pub async fn handle_create_lobby(

/// Handles a request to join an existing lobby
pub async fn handle_join_lobby(
ep: Endpoint,
ep: &Endpoint,
conn: Connection,
game_id: GameID,
lobby_id: LobbyId,
password: Option<String>,
send: &mut quinn::SendStream,
send: &mut iroh::endpoint::SendStream,
) -> Result<()> {
let mut state = MATCHMAKER_STATE.lock().await;

Expand Down Expand Up @@ -167,6 +169,7 @@ pub async fn handle_join_lobby(
{
let members = connections.1;
drop(state);
let ep = ep.clone();
tokio::spawn(async move {
if let Err(e) = start_game(ep, members, &match_info).await {
error!("Error starting match from full lobby: {:?}", e);
Expand Down
Loading

0 comments on commit 3eb56b8

Please sign in to comment.