Skip to content
Merged
Show file tree
Hide file tree
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
28 changes: 3 additions & 25 deletions src/adnl/src/adnl/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,30 +124,8 @@ pub struct AdnlClient {
}

impl AdnlClient {
/// Connect to server
/// Connect to server.
pub async fn connect(config: &AdnlClientConfig) -> Result<Self> {
let socket = socket2::Socket::new(socket2::Domain::IPV4, socket2::Type::STREAM, None)?;
socket.set_reuse_address(true)?;
socket.set_linger(Some(Duration::from_secs(0)))?;
//socket.bind(&"0.0.0.0:0".parse::<SocketAddr>()?.into())?;
socket.connect_timeout(&config.server_address.into(), config.timeouts.write())?;
socket.set_nonblocking(true)?;

let mut stream = AdnlStream::from_stream_with_timeouts(
tokio::net::TcpStream::from_std(socket.into())?,
config.timeouts(),
);

let mut crypto = Self::send_init_packet(&mut stream, config).await?;
if let Some(client_key) = &config.client_key {
Self::tcp_auth_handshake(&mut crypto, &mut stream, client_key).await?;
}
Ok(Self { crypto, stream })
}

/// Like [`Self::connect`], but uses `tokio::net::TcpStream::connect` so the
/// runtime worker is not parked while the kernel waits on an unresponsive peer.
pub async fn timeout_connect(config: &AdnlClientConfig) -> Result<Self> {
let connect_timeout = config.timeouts.write();
let tcp = tokio::time::timeout(
connect_timeout,
Expand All @@ -156,11 +134,11 @@ impl AdnlClient {
.await
.map_err(|_| {
error!(
"ADNL TCP connect to {} timed out after {:?}",
"ADNL connect to {} timed out after {:?}",
config.server_address, connect_timeout,
)
})?
.map_err(|e| error!("ADNL TCP connect to {} failed: {}", config.server_address, e))?;
.map_err(|e| error!("ADNL connect to {} failed: {}", config.server_address, e))?;

socket2::SockRef::from(&tcp).set_linger(Some(Duration::from_secs(0)))?;

Expand Down
4 changes: 2 additions & 2 deletions src/node-control/control-client/src/client_adnl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ impl ControlClientAdnl {
pub async fn connect(&mut self) -> anyhow::Result<()> {
if self.adnl.is_none() {
self.adnl = Some(
AdnlClient::timeout_connect(&self.config)
AdnlClient::connect(&self.config)
.await
.context("failed to connect to Control Server")?,
);
Expand Down Expand Up @@ -370,7 +370,7 @@ impl ControlClientAdnl {
}
}

self.adnl = Some(AdnlClient::timeout_connect(&self.config).await?);
self.adnl = Some(AdnlClient::connect(&self.config).await?);
Ok(())
}

Expand Down
Loading