Skip to content

Commit

Permalink
Remove TcpSocket type
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Thomasdezeeuw committed Oct 8, 2021
1 parent d4ce420 commit 02e9be4
Show file tree
Hide file tree
Showing 13 changed files with 133 additions and 1,526 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ libc = "0.2.86"

[target.'cfg(windows)'.dependencies]
miow = "0.3.6"
winapi = { version = "0.3", features = ["winsock2", "mswsock", "mstcpip"] }
winapi = { version = "0.3", features = ["winsock2", "mswsock"] }
ntapi = "0.3"

[dev-dependencies]
Expand Down
2 changes: 1 addition & 1 deletion src/net/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
//! [portability guidelines]: ../struct.Poll.html#portability
mod tcp;
pub use self::tcp::{TcpKeepalive, TcpListener, TcpSocket, TcpStream};
pub use self::tcp::{TcpListener, TcpStream};

mod udp;
pub use self::udp::UdpSocket;
Expand Down
18 changes: 13 additions & 5 deletions src/net/tcp/listener.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@ use std::os::unix::io::{AsRawFd, FromRawFd, IntoRawFd, RawFd};
use std::os::windows::io::{AsRawSocket, FromRawSocket, IntoRawSocket, RawSocket};
use std::{fmt, io};

use super::{TcpSocket, TcpStream};
use crate::io_source::IoSource;
use crate::net::TcpStream;
#[cfg(unix)]
use crate::sys::tcp::set_reuseaddr;
use crate::sys::tcp::{bind, listen, new_for_addr};
use crate::{event, sys, Interest, Registry, Token};

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

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

socket.bind(addr)?;
socket.listen(1024)
bind(&listener.inner, addr)?;
listen(&listener.inner, 1024)?;
Ok(listener)
}

/// Creates a new `TcpListener` from a standard `net::TcpListener`.
Expand Down
3 changes: 0 additions & 3 deletions src/net/tcp/mod.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
mod listener;
pub use self::listener::TcpListener;

mod socket;
pub use self::socket::{TcpKeepalive, TcpSocket};

mod stream;
pub use self::stream::TcpStream;
Loading

0 comments on commit 02e9be4

Please sign in to comment.