Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 19 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ use std::{
io,
net::{SocketAddr, SocketAddrV4, SocketAddrV6, ToSocketAddrs},
str::FromStr,
time::Duration,
};
use tokio::{
io::{AsyncRead, AsyncWrite, AsyncWriteExt},
select,
time::timeout,
};
use tokio_util::sync::CancellationToken;

Expand All @@ -20,6 +22,8 @@ use {
tokio::net::{UnixListener, UnixStream},
};

const ONLINE_TIMEOUT: Duration = Duration::from_secs(5);

/// Create a dumb pipe between two machines, using an iroh endpoint.
///
/// One side listens, the other side connects. Both sides are identified by a
Expand Down Expand Up @@ -345,7 +349,9 @@ async fn listen_stdio(args: ListenArgs) -> Result<()> {
let secret_key = get_or_create_secret()?;
let endpoint = create_endpoint(secret_key, &args.common, vec![args.common.alpn()?]).await?;
// wait for the endpoint to figure out its home relay and addresses before making a ticket
endpoint.online().await;
if (timeout(ONLINE_TIMEOUT, endpoint.online()).await).is_err() {
eprintln!("Warning: Failed to connect to the home relay");
}
let node = endpoint.node_addr();
let mut short = node.clone();
let ticket = NodeTicket::new(node);
Expand Down Expand Up @@ -444,7 +450,9 @@ async fn connect_tcp(args: ConnectTcpArgs) -> Result<()> {
tracing::info!("tcp listening on {:?}", addrs);

// Wait for our own endpoint to be ready before trying to connect.
endpoint.online().await;
if (timeout(ONLINE_TIMEOUT, endpoint.online()).await).is_err() {
eprintln!("Warning: Failed to connect to the home relay");
}

let tcp_listener = match tokio::net::TcpListener::bind(addrs.as_slice()).await {
Ok(tcp_listener) => tcp_listener,
Expand Down Expand Up @@ -517,7 +525,9 @@ async fn listen_tcp(args: ListenTcpArgs) -> Result<()> {
let secret_key = get_or_create_secret()?;
let endpoint = create_endpoint(secret_key, &args.common, vec![args.common.alpn()?]).await?;
// wait for the endpoint to figure out its address before making a ticket
endpoint.online().await;
if (timeout(ONLINE_TIMEOUT, endpoint.online()).await).is_err() {
eprintln!("Warning: Failed to connect to the home relay");
}
let node_addr = endpoint.node_addr();
let mut short = node_addr.clone();
let ticket = NodeTicket::new(node_addr);
Expand Down Expand Up @@ -599,7 +609,9 @@ async fn listen_unix(args: ListenUnixArgs) -> Result<()> {
let secret_key = get_or_create_secret()?;
let endpoint = create_endpoint(secret_key, &args.common, vec![args.common.alpn()?]).await?;
// wait for the endpoint to figure out its address before making a ticket
endpoint.online().await;
if (timeout(ONLINE_TIMEOUT, endpoint.online()).await).is_err() {
eprintln!("Warning: Failed to connect to the home relay");
}
let node_addr = endpoint.node_addr();
let mut short = node_addr.clone();
let ticket = NodeTicket::new(node_addr);
Expand Down Expand Up @@ -714,7 +726,9 @@ async fn connect_unix(args: ConnectUnixArgs) -> Result<()> {
tracing::info!("unix listening on {:?}", socket_path);

// Wait for our own endpoint to be ready before trying to connect.
endpoint.online().await;
if (timeout(ONLINE_TIMEOUT, endpoint.online()).await).is_err() {
eprintln!("Warning: Failed to connect to the home relay");
}

// Remove existing socket file if it exists
if let Err(e) = tokio::fs::remove_file(&socket_path).await {
Expand Down
Loading