mirror of
https://github.com/denoland/deno.git
synced 2024-11-21 15:04:11 -05:00
feat(ext/net): Use socket2 crate to create TcpListener (#13808)
This commit is contained in:
parent
70690f54af
commit
7e3496403a
2 changed files with 32 additions and 4 deletions
|
@ -24,6 +24,9 @@ use deno_core::ZeroCopyBuf;
|
|||
use log::debug;
|
||||
use serde::Deserialize;
|
||||
use serde::Serialize;
|
||||
use socket2::Domain;
|
||||
use socket2::Socket;
|
||||
use socket2::Type;
|
||||
use std::borrow::Cow;
|
||||
use std::cell::RefCell;
|
||||
use std::net::SocketAddr;
|
||||
|
@ -428,8 +431,19 @@ fn listen_tcp(
|
|||
state: &mut OpState,
|
||||
addr: SocketAddr,
|
||||
) -> Result<(u32, SocketAddr), AnyError> {
|
||||
let std_listener = std::net::TcpListener::bind(&addr)?;
|
||||
std_listener.set_nonblocking(true)?;
|
||||
let domain = if addr.is_ipv4() {
|
||||
Domain::IPV4
|
||||
} else {
|
||||
Domain::IPV6
|
||||
};
|
||||
let socket = Socket::new(domain, Type::STREAM, None)?;
|
||||
#[cfg(not(windows))]
|
||||
socket.set_reuse_address(true)?;
|
||||
let socket_addr = socket2::SockAddr::from(addr);
|
||||
socket.bind(&socket_addr)?;
|
||||
socket.listen(128)?;
|
||||
socket.set_nonblocking(true)?;
|
||||
let std_listener: std::net::TcpListener = socket.into();
|
||||
let listener = TcpListener::from_std(std_listener)?;
|
||||
let local_addr = listener.local_addr()?;
|
||||
let listener_resource = TcpListenerResource {
|
||||
|
|
|
@ -54,6 +54,9 @@ use io::Error;
|
|||
use io::Read;
|
||||
use io::Write;
|
||||
use serde::Deserialize;
|
||||
use socket2::Domain;
|
||||
use socket2::Socket;
|
||||
use socket2::Type;
|
||||
use std::borrow::Cow;
|
||||
use std::cell::RefCell;
|
||||
use std::convert::From;
|
||||
|
@ -1075,8 +1078,19 @@ where
|
|||
let bind_addr = resolve_addr_sync(hostname, port)?
|
||||
.next()
|
||||
.ok_or_else(|| generic_error("No resolved address found"))?;
|
||||
let std_listener = std::net::TcpListener::bind(bind_addr)?;
|
||||
std_listener.set_nonblocking(true)?;
|
||||
let domain = if bind_addr.is_ipv4() {
|
||||
Domain::IPV4
|
||||
} else {
|
||||
Domain::IPV6
|
||||
};
|
||||
let socket = Socket::new(domain, Type::STREAM, None)?;
|
||||
#[cfg(not(windows))]
|
||||
socket.set_reuse_address(true)?;
|
||||
let socket_addr = socket2::SockAddr::from(bind_addr);
|
||||
socket.bind(&socket_addr)?;
|
||||
socket.listen(128)?;
|
||||
socket.set_nonblocking(true)?;
|
||||
let std_listener: std::net::TcpListener = socket.into();
|
||||
let tcp_listener = TcpListener::from_std(std_listener)?;
|
||||
let local_addr = tcp_listener.local_addr()?;
|
||||
|
||||
|
|
Loading…
Reference in a new issue