Skip to content

Commit

Permalink
example(iroh-net): minimal use of unreliable datagram (#1967)
Browse files Browse the repository at this point in the history
Show a minimal test case of using the unreliable datagram extension of
quic with iroh-net
  • Loading branch information
dignifiedquire authored Jan 24, 2024
1 parent de7f603 commit 12e42b3
Showing 1 changed file with 49 additions and 6 deletions.
55 changes: 49 additions & 6 deletions iroh-net/examples/magic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,16 @@ struct Cli {
#[derive(Debug, Parser)]
enum Command {
Listen,
ListenUnreliable,
Connect {
peer_id: String,
node_id: String,
#[clap(long)]
addrs: Option<Vec<SocketAddr>>,
#[clap(long)]
derp_url: Option<Url>,
},
ConnectUnreliable {
node_id: String,
#[clap(long)]
addrs: Option<Vec<SocketAddr>>,
#[clap(long)]
Expand Down Expand Up @@ -65,16 +73,16 @@ async fn main() -> anyhow::Result<()> {
.await?;

let me = endpoint.node_id();
let local_addr = endpoint.local_addr()?;
let local_addr = endpoint.local_endpoints().await?;
println!("magic socket listening on {local_addr:?}");
println!("our node id: {me}");

match args.command {
Command::Listen => {
while let Some(conn) = endpoint.accept().await {
let (peer_id, alpn, conn) = accept_conn(conn).await?;
let (node_id, alpn, conn) = accept_conn(conn).await?;
info!(
"new connection from {peer_id} with ALPN {alpn} (coming from {})",
"new connection from {node_id} with ALPN {alpn} (coming from {})",
conn.remote_address()
);
tokio::spawn(async move {
Expand All @@ -92,12 +100,32 @@ async fn main() -> anyhow::Result<()> {
});
}
}
Command::ListenUnreliable => {
while let Some(conn) = endpoint.accept().await {
let (node_id, alpn, conn) = accept_conn(conn).await?;
info!(
"new (unreliable) connection from {node_id} with ALPN {alpn} (coming from {})",
conn.remote_address()
);
tokio::spawn(async move {
while let Ok(message) = conn.read_datagram().await {
let message = String::from_utf8(message.into())?;
println!("received: {message}");

let message = format!("hi! you connected to {me}. bye bye");
conn.send_datagram(message.as_bytes().to_vec().into())?;
}

Ok::<_, anyhow::Error>(())
});
}
}
Command::Connect {
peer_id,
node_id,
addrs,
derp_url,
} => {
let addr = NodeAddr::from_parts(peer_id.parse()?, derp_url, addrs.unwrap_or_default());
let addr = NodeAddr::from_parts(node_id.parse()?, derp_url, addrs.unwrap_or_default());
let conn = endpoint.connect(addr, EXAMPLE_ALPN).await?;
info!("connected");

Expand All @@ -110,6 +138,21 @@ async fn main() -> anyhow::Result<()> {
let message = String::from_utf8(message)?;
println!("received: {message}");
}
Command::ConnectUnreliable {
node_id,
addrs,
derp_url,
} => {
let addr = NodeAddr::from_parts(node_id.parse()?, derp_url, addrs.unwrap_or_default());
let conn = endpoint.connect(addr, EXAMPLE_ALPN).await?;
info!("connected");

let message = format!("hello here's {me}");
conn.send_datagram(message.as_bytes().to_vec().into())?;
let message = conn.read_datagram().await?;
let message = String::from_utf8(message.into())?;
println!("received: {message}");
}
}
Ok(())
}
Expand Down

0 comments on commit 12e42b3

Please sign in to comment.