Skip to content

Commit

Permalink
refactor(client): switch from net2 to socket2 (#2206)
Browse files Browse the repository at this point in the history
net2 was recently deprecated; socket2 is the recommended alternative

Closes #2205
  • Loading branch information
Joshua Nelson authored May 19, 2020
1 parent 6fbb6db commit d5b0ee5
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 10 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ want = "0.3"

# Optional

net2 = { version = "0.2.32", optional = true }
socket2 = { version = "0.3", optional = true }

[dev-dependencies]
futures-util = { version = "0.3", default-features = false, features = ["alloc"] }
Expand All @@ -65,7 +65,7 @@ runtime = [
"tokio/rt-core",
]
tcp = [
"net2",
"socket2",
"tokio/blocking",
"tokio/tcp",
"tokio/time",
Expand Down
17 changes: 9 additions & 8 deletions src/client/connect/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ use std::time::Duration;

use futures_util::future::Either;
use http::uri::{Scheme, Uri};
use net2::TcpBuilder;
use pin_project::pin_project;
use tokio::net::TcpStream;
use tokio::time::Delay;
Expand Down Expand Up @@ -552,30 +551,32 @@ fn connect(
reuse_address: bool,
connect_timeout: Option<Duration>,
) -> io::Result<impl Future<Output = io::Result<TcpStream>>> {
let builder = match *addr {
SocketAddr::V4(_) => TcpBuilder::new_v4()?,
SocketAddr::V6(_) => TcpBuilder::new_v6()?,
use socket2::{Domain, Protocol, Socket, Type};
let domain = match *addr {
SocketAddr::V4(_) => Domain::ipv4(),
SocketAddr::V6(_) => Domain::ipv6(),
};
let socket = Socket::new(domain, Type::stream(), Some(Protocol::tcp()))?;

if reuse_address {
builder.reuse_address(reuse_address)?;
socket.set_reuse_address(true)?;
}

if let Some(ref local_addr) = *local_addr {
// Caller has requested this socket be bound before calling connect
builder.bind(SocketAddr::new(local_addr.clone(), 0))?;
socket.bind(&SocketAddr::new(local_addr.clone(), 0).into())?;
} else if cfg!(windows) {
// Windows requires a socket be bound before calling connect
let any: SocketAddr = match *addr {
SocketAddr::V4(_) => ([0, 0, 0, 0], 0).into(),
SocketAddr::V6(_) => ([0, 0, 0, 0, 0, 0, 0, 0], 0).into(),
};
builder.bind(any)?;
socket.bind(&any.into())?;
}

let addr = *addr;

let std_tcp = builder.to_tcp_stream()?;
let std_tcp = socket.into_tcp_stream();

Ok(async move {
let connect = TcpStream::connect_std(std_tcp, &addr);
Expand Down

0 comments on commit d5b0ee5

Please sign in to comment.