1
0
mirror of https://github.com/fafhrd91/actix-net synced 2024-11-27 15:42:57 +01:00

server: Replace net2 crate with socket2

This commit is contained in:
Yuki Okushi 2020-05-19 08:17:44 +09:00
parent 49a6f525be
commit 8218a098e8
No known key found for this signature in database
GPG Key ID: B0986C85C0E2DAA1
4 changed files with 21 additions and 13 deletions

View File

@ -1,5 +1,11 @@
# Changes
## [Unreleased]
### Changed
* Replace deprecated `net2` crate with `socket2`
## [1.0.2] - 2020-02-26
### Fixed

View File

@ -29,7 +29,7 @@ actix-utils = "1.0.4"
log = "0.4"
num_cpus = "1.11"
mio = "0.6.19"
net2 = "0.2"
socket2 = "0.3"
futures-channel = { version = "0.3.4", default-features = false }
futures-util = { version = "0.3.4", default-features = false, features = ["sink"] }
slab = "0.4"

View File

@ -12,7 +12,7 @@ use futures_util::future::ready;
use futures_util::stream::FuturesUnordered;
use futures_util::{ready, future::Future, FutureExt, stream::Stream, StreamExt};
use log::{error, info};
use net2::TcpBuilder;
use socket2::{Domain, Protocol, Socket, Type};
use crate::accept::{AcceptLoop, AcceptNotify, Command};
use crate::config::{ConfiguredService, ServiceConfig};
@ -487,11 +487,13 @@ pub(super) fn bind_addr<S: net::ToSocketAddrs>(
}
fn create_tcp_listener(addr: net::SocketAddr, backlog: i32) -> io::Result<net::TcpListener> {
let builder = match addr {
net::SocketAddr::V4(_) => TcpBuilder::new_v4()?,
net::SocketAddr::V6(_) => TcpBuilder::new_v6()?,
let domain = match addr {
net::SocketAddr::V4(_) => Domain::ipv4(),
net::SocketAddr::V6(_) => Domain::ipv6(),
};
builder.reuse_address(true)?;
builder.bind(addr)?;
Ok(builder.listen(backlog)?)
let socket = Socket::new(domain, Type::stream(), Some(Protocol::tcp()))?;
socket.set_reuse_address(true)?;
socket.bind(&addr.into())?;
socket.listen(backlog)?;
Ok(socket.into_tcp_listener())
}

View File

@ -5,14 +5,14 @@ use std::{net, thread, time};
use actix_server::Server;
use actix_service::fn_service;
use futures_util::future::{lazy, ok};
use net2::TcpBuilder;
use socket2::{Domain, Protocol, Socket, Type};
fn unused_addr() -> net::SocketAddr {
let addr: net::SocketAddr = "127.0.0.1:0".parse().unwrap();
let socket = TcpBuilder::new_v4().unwrap();
socket.bind(&addr).unwrap();
socket.reuse_address(true).unwrap();
let tcp = socket.to_tcp_listener().unwrap();
let socket = Socket::new(Domain::ipv4(), Type::stream(), Some(Protocol::tcp())).unwrap();
socket.bind(&addr.into()).unwrap();
socket.set_reuse_address(true).unwrap();
let tcp = socket.into_tcp_listener();
tcp.local_addr().unwrap()
}