Skip to content

Commit 02e9be4

Browse files
committed
Remove TcpSocket type
The socket2 crate provide all the functionality and more. Furthermore supporting all socket options is beyond the scope of Mio. The easier migration is to the socket2 crate, using the Socket or SockRef types. The migration for Tokio is tracked in tokio-rs/tokio#4135.
1 parent d4ce420 commit 02e9be4

File tree

13 files changed

+133
-1526
lines changed

13 files changed

+133
-1526
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ libc = "0.2.86"
4747

4848
[target.'cfg(windows)'.dependencies]
4949
miow = "0.3.6"
50-
winapi = { version = "0.3", features = ["winsock2", "mswsock", "mstcpip"] }
50+
winapi = { version = "0.3", features = ["winsock2", "mswsock"] }
5151
ntapi = "0.3"
5252

5353
[dev-dependencies]

src/net/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
//! [portability guidelines]: ../struct.Poll.html#portability
99
1010
mod tcp;
11-
pub use self::tcp::{TcpKeepalive, TcpListener, TcpSocket, TcpStream};
11+
pub use self::tcp::{TcpListener, TcpStream};
1212

1313
mod udp;
1414
pub use self::udp::UdpSocket;

src/net/tcp/listener.rs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,11 @@ use std::os::unix::io::{AsRawFd, FromRawFd, IntoRawFd, RawFd};
55
use std::os::windows::io::{AsRawSocket, FromRawSocket, IntoRawSocket, RawSocket};
66
use std::{fmt, io};
77

8-
use super::{TcpSocket, TcpStream};
98
use crate::io_source::IoSource;
9+
use crate::net::TcpStream;
10+
#[cfg(unix)]
11+
use crate::sys::tcp::set_reuseaddr;
12+
use crate::sys::tcp::{bind, listen, new_for_addr};
1013
use crate::{event, sys, Interest, Registry, Token};
1114

1215
/// A structure representing a socket server
@@ -50,7 +53,11 @@ impl TcpListener {
5053
/// 3. Bind the socket to the specified address.
5154
/// 4. Calls `listen` on the socket to prepare it to receive new connections.
5255
pub fn bind(addr: SocketAddr) -> io::Result<TcpListener> {
53-
let socket = TcpSocket::new_for_addr(addr)?;
56+
let socket = new_for_addr(addr)?;
57+
#[cfg(unix)]
58+
let listener = unsafe { TcpListener::from_raw_fd(socket) };
59+
#[cfg(windows)]
60+
let listener = unsafe { TcpListener::from_raw_socket(socket as _) };
5461

5562
// On platforms with Berkeley-derived sockets, this allows to quickly
5663
// rebind a socket, without needing to wait for the OS to clean up the
@@ -60,10 +67,11 @@ impl TcpListener {
6067
// which allows “socket hijacking”, so we explicitly don't set it here.
6168
// https://docs.microsoft.com/en-us/windows/win32/winsock/using-so-reuseaddr-and-so-exclusiveaddruse
6269
#[cfg(not(windows))]
63-
socket.set_reuseaddr(true)?;
70+
set_reuseaddr(&listener.inner, true)?;
6471

65-
socket.bind(addr)?;
66-
socket.listen(1024)
72+
bind(&listener.inner, addr)?;
73+
listen(&listener.inner, 1024)?;
74+
Ok(listener)
6775
}
6876

6977
/// Creates a new `TcpListener` from a standard `net::TcpListener`.

src/net/tcp/mod.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
mod listener;
22
pub use self::listener::TcpListener;
33

4-
mod socket;
5-
pub use self::socket::{TcpKeepalive, TcpSocket};
6-
74
mod stream;
85
pub use self::stream::TcpStream;

0 commit comments

Comments
 (0)