1
0
mirror of https://github.com/fafhrd91/actix-net synced 2024-11-30 18:44:36 +01:00

remove usage of mio::net::TcpSocket (#413)

This commit is contained in:
fakeshadow 2021-11-07 22:18:23 +08:00 committed by GitHub
parent 161d1ee94b
commit 3f49d8ab54
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 31 additions and 24 deletions

View File

@ -29,6 +29,7 @@ futures-core = { version = "0.3.7", default-features = false, features = ["alloc
log = "0.4" log = "0.4"
mio = { version = "0.7.6", features = ["os-poll", "net"] } mio = { version = "0.7.6", features = ["os-poll", "net"] }
num_cpus = "1.13" num_cpus = "1.13"
socket2 = "0.4.2"
tokio = { version = "1.5.1", features = ["sync"] } tokio = { version = "1.5.1", features = ["sync"] }
[dev-dependencies] [dev-dependencies]

View File

@ -8,7 +8,8 @@ use crate::{
server::ServerCommand, server::ServerCommand,
service::{InternalServiceFactory, ServiceFactory, StreamNewService}, service::{InternalServiceFactory, ServiceFactory, StreamNewService},
socket::{ socket::{
MioListener, MioTcpListener, MioTcpSocket, StdSocketAddr, StdTcpListener, ToSocketAddrs, create_mio_tcp_listener, MioListener, MioTcpListener, StdSocketAddr, StdTcpListener,
ToSocketAddrs,
}, },
worker::ServerWorkerConfig, worker::ServerWorkerConfig,
Server, Server,
@ -263,7 +264,7 @@ pub(super) fn bind_addr<S: ToSocketAddrs>(
let mut success = false; let mut success = false;
let mut sockets = Vec::new(); let mut sockets = Vec::new();
for addr in addr.to_socket_addrs()? { for addr in addr.to_socket_addrs()? {
match create_tcp_listener(addr, backlog) { match create_mio_tcp_listener(addr, backlog) {
Ok(lst) => { Ok(lst) => {
success = true; success = true;
sockets.push(lst); sockets.push(lst);
@ -283,14 +284,3 @@ pub(super) fn bind_addr<S: ToSocketAddrs>(
)) ))
} }
} }
fn create_tcp_listener(addr: StdSocketAddr, backlog: u32) -> io::Result<MioTcpListener> {
let socket = match addr {
StdSocketAddr::V4(_) => MioTcpSocket::new_v4()?,
StdSocketAddr::V6(_) => MioTcpSocket::new_v6()?,
};
socket.set_reuseaddr(true)?;
socket.bind(addr)?;
socket.listen(backlog)
}

View File

@ -2,7 +2,7 @@ pub(crate) use std::net::{
SocketAddr as StdSocketAddr, TcpListener as StdTcpListener, ToSocketAddrs, SocketAddr as StdSocketAddr, TcpListener as StdTcpListener, ToSocketAddrs,
}; };
pub(crate) use mio::net::{TcpListener as MioTcpListener, TcpSocket as MioTcpSocket}; pub(crate) use mio::net::TcpListener as MioTcpListener;
#[cfg(unix)] #[cfg(unix)]
pub(crate) use { pub(crate) use {
mio::net::UnixListener as MioUnixListener, mio::net::UnixListener as MioUnixListener,
@ -223,6 +223,22 @@ mod unix_impl {
} }
} }
pub(crate) fn create_mio_tcp_listener(
addr: StdSocketAddr,
backlog: u32,
) -> io::Result<MioTcpListener> {
use socket2::{Domain, Protocol, Socket, Type};
let socket = Socket::new(Domain::for_address(addr), Type::STREAM, Some(Protocol::TCP))?;
socket.set_reuse_address(true)?;
socket.set_nonblocking(true)?;
socket.bind(&addr.into())?;
socket.listen(backlog as i32)?;
Ok(MioTcpListener::from_std(StdTcpListener::from(socket)))
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
@ -234,11 +250,8 @@ mod tests {
assert_eq!(format!("{}", addr), "127.0.0.1:8080"); assert_eq!(format!("{}", addr), "127.0.0.1:8080");
let addr: StdSocketAddr = "127.0.0.1:0".parse().unwrap(); let addr: StdSocketAddr = "127.0.0.1:0".parse().unwrap();
let socket = MioTcpSocket::new_v4().unwrap(); let lst = create_mio_tcp_listener(addr, 128).unwrap();
socket.set_reuseaddr(true).unwrap(); let lst = MioListener::Tcp(lst);
socket.bind(addr).unwrap();
let tcp = socket.listen(128).unwrap();
let lst = MioListener::Tcp(tcp);
assert!(format!("{:?}", lst).contains("TcpListener")); assert!(format!("{:?}", lst).contains("TcpListener"));
assert!(format!("{}", lst).contains("127.0.0.1")); assert!(format!("{}", lst).contains("127.0.0.1"));
} }

View File

@ -5,14 +5,17 @@ use std::{net, thread, time::Duration};
use actix_rt::{net::TcpStream, time::sleep}; use actix_rt::{net::TcpStream, time::sleep};
use actix_server::Server; use actix_server::Server;
use actix_service::fn_service; use actix_service::fn_service;
use socket2::{Domain, Protocol, Socket, Type};
fn unused_addr() -> net::SocketAddr { fn unused_addr() -> net::SocketAddr {
let addr: net::SocketAddr = "127.0.0.1:0".parse().unwrap(); let addr: net::SocketAddr = "127.0.0.1:0".parse().unwrap();
let socket = mio::net::TcpSocket::new_v4().unwrap(); let socket =
socket.bind(addr).unwrap(); Socket::new(Domain::for_address(addr), Type::STREAM, Some(Protocol::TCP)).unwrap();
socket.set_reuseaddr(true).unwrap(); socket.set_reuse_address(true).unwrap();
let tcp = socket.listen(32).unwrap(); socket.set_nonblocking(true).unwrap();
tcp.local_addr().unwrap() socket.bind(&addr.into()).unwrap();
socket.listen(32).unwrap();
net::TcpListener::from(socket).local_addr().unwrap()
} }
#[test] #[test]