Skip to content

Commit ee0e811

Browse files
authored
Update mio to 0.8 (#4270)
1 parent 47feaa7 commit ee0e811

File tree

3 files changed

+58
-51
lines changed

3 files changed

+58
-51
lines changed

tokio/Cargo.toml

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -49,20 +49,19 @@ macros = ["tokio-macros"]
4949
stats = []
5050
net = [
5151
"libc",
52+
"mio/net",
53+
"mio/os-ext",
5254
"mio/os-poll",
53-
"mio/os-util",
54-
"mio/tcp",
55-
"mio/udp",
56-
"mio/uds",
55+
"socket2/all",
5756
"winapi/namedpipeapi",
5857
]
5958
process = [
6059
"bytes",
6160
"once_cell",
6261
"libc",
62+
"mio/net",
63+
"mio/os-ext",
6364
"mio/os-poll",
64-
"mio/os-util",
65-
"mio/uds",
6665
"signal-hook-registry",
6766
"winapi/threadpoollegacyapiset",
6867
]
@@ -75,9 +74,9 @@ rt-multi-thread = [
7574
signal = [
7675
"once_cell",
7776
"libc",
77+
"mio/net",
78+
"mio/os-ext",
7879
"mio/os-poll",
79-
"mio/uds",
80-
"mio/os-util",
8180
"signal-hook-registry",
8281
"winapi/consoleapi",
8382
]
@@ -94,9 +93,10 @@ pin-project-lite = "0.2.0"
9493
bytes = { version = "1.0.0", optional = true }
9594
once_cell = { version = "1.5.2", optional = true }
9695
memchr = { version = "2.2", optional = true }
97-
mio = { version = "0.7.6", optional = true }
96+
mio = { version = "0.8.0", optional = true }
9897
num_cpus = { version = "1.8.0", optional = true }
9998
parking_lot = { version = "0.11.0", optional = true }
99+
socket2 = { version = "0.4.2", optional = true }
100100

101101
# Currently unstable. The API exposed by these features may be broken at any time.
102102
# Requires `--cfg tokio_unstable` to enable.
@@ -128,7 +128,6 @@ proptest = "1"
128128
rand = "0.8.0"
129129
tempfile = "3.1.0"
130130
async-stream = "0.3"
131-
socket2 = "0.4"
132131

133132
[target.'cfg(target_os = "freebsd")'.dev-dependencies]
134133
mio-aio = { version = "0.6.0", features = ["tokio"] }

tokio/src/net/tcp/socket.rs

Lines changed: 42 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use crate::net::{TcpListener, TcpStream};
22

3+
use std::convert::TryInto;
34
use std::fmt;
45
use std::io;
56
use std::net::SocketAddr;
@@ -84,7 +85,7 @@ cfg_net! {
8485
/// [`socket2`]: https://docs.rs/socket2/
8586
#[cfg_attr(docsrs, doc(alias = "connect_std"))]
8687
pub struct TcpSocket {
87-
inner: mio::net::TcpSocket,
88+
inner: socket2::Socket,
8889
}
8990
}
9091

@@ -119,7 +120,11 @@ impl TcpSocket {
119120
/// }
120121
/// ```
121122
pub fn new_v4() -> io::Result<TcpSocket> {
122-
let inner = mio::net::TcpSocket::new_v4()?;
123+
let inner = socket2::Socket::new(
124+
socket2::Domain::IPV4,
125+
socket2::Type::STREAM,
126+
Some(socket2::Protocol::TCP),
127+
)?;
123128
Ok(TcpSocket { inner })
124129
}
125130

@@ -153,7 +158,11 @@ impl TcpSocket {
153158
/// }
154159
/// ```
155160
pub fn new_v6() -> io::Result<TcpSocket> {
156-
let inner = mio::net::TcpSocket::new_v6()?;
161+
let inner = socket2::Socket::new(
162+
socket2::Domain::IPV6,
163+
socket2::Type::STREAM,
164+
Some(socket2::Protocol::TCP),
165+
)?;
157166
Ok(TcpSocket { inner })
158167
}
159168

@@ -184,7 +193,7 @@ impl TcpSocket {
184193
/// }
185194
/// ```
186195
pub fn set_reuseaddr(&self, reuseaddr: bool) -> io::Result<()> {
187-
self.inner.set_reuseaddr(reuseaddr)
196+
self.inner.set_reuse_address(reuseaddr)
188197
}
189198

190199
/// Retrieves the value set for `SO_REUSEADDR` on this socket.
@@ -210,7 +219,7 @@ impl TcpSocket {
210219
/// }
211220
/// ```
212221
pub fn reuseaddr(&self) -> io::Result<bool> {
213-
self.inner.get_reuseaddr()
222+
self.inner.reuse_address()
214223
}
215224

216225
/// Allows the socket to bind to an in-use port. Only available for unix systems
@@ -244,7 +253,7 @@ impl TcpSocket {
244253
doc(cfg(all(unix, not(target_os = "solaris"), not(target_os = "illumos"))))
245254
)]
246255
pub fn set_reuseport(&self, reuseport: bool) -> io::Result<()> {
247-
self.inner.set_reuseport(reuseport)
256+
self.inner.set_reuse_port(reuseport)
248257
}
249258

250259
/// Allows the socket to bind to an in-use port. Only available for unix systems
@@ -279,14 +288,14 @@ impl TcpSocket {
279288
doc(cfg(all(unix, not(target_os = "solaris"), not(target_os = "illumos"))))
280289
)]
281290
pub fn reuseport(&self) -> io::Result<bool> {
282-
self.inner.get_reuseport()
291+
self.inner.reuse_port()
283292
}
284293

285294
/// Sets the size of the TCP send buffer on this socket.
286295
///
287296
/// On most operating systems, this sets the `SO_SNDBUF` socket option.
288297
pub fn set_send_buffer_size(&self, size: u32) -> io::Result<()> {
289-
self.inner.set_send_buffer_size(size)
298+
self.inner.set_send_buffer_size(size as usize)
290299
}
291300

292301
/// Returns the size of the TCP send buffer for this socket.
@@ -313,14 +322,14 @@ impl TcpSocket {
313322
///
314323
/// [`set_send_buffer_size`]: #method.set_send_buffer_size
315324
pub fn send_buffer_size(&self) -> io::Result<u32> {
316-
self.inner.get_send_buffer_size()
325+
self.inner.send_buffer_size().map(|n| n as u32)
317326
}
318327

319328
/// Sets the size of the TCP receive buffer on this socket.
320329
///
321330
/// On most operating systems, this sets the `SO_RCVBUF` socket option.
322331
pub fn set_recv_buffer_size(&self, size: u32) -> io::Result<()> {
323-
self.inner.set_recv_buffer_size(size)
332+
self.inner.set_recv_buffer_size(size as usize)
324333
}
325334

326335
/// Returns the size of the TCP receive buffer for this socket.
@@ -347,7 +356,7 @@ impl TcpSocket {
347356
///
348357
/// [`set_recv_buffer_size`]: #method.set_recv_buffer_size
349358
pub fn recv_buffer_size(&self) -> io::Result<u32> {
350-
self.inner.get_recv_buffer_size()
359+
self.inner.recv_buffer_size().map(|n| n as u32)
351360
}
352361

353362
/// Sets the linger duration of this socket by setting the SO_LINGER option.
@@ -395,7 +404,9 @@ impl TcpSocket {
395404
/// }
396405
/// ```
397406
pub fn local_addr(&self) -> io::Result<SocketAddr> {
398-
self.inner.get_localaddr()
407+
self.inner
408+
.local_addr()
409+
.map(|addr| addr.as_socket().unwrap())
399410
}
400411

401412
/// Binds the socket to the given address.
@@ -427,7 +438,7 @@ impl TcpSocket {
427438
/// }
428439
/// ```
429440
pub fn bind(&self, addr: SocketAddr) -> io::Result<()> {
430-
self.inner.bind(addr)
441+
self.inner.bind(&addr.into())
431442
}
432443

433444
/// Establishes a TCP connection with a peer at the specified socket address.
@@ -463,7 +474,13 @@ impl TcpSocket {
463474
/// }
464475
/// ```
465476
pub async fn connect(self, addr: SocketAddr) -> io::Result<TcpStream> {
466-
let mio = self.inner.connect(addr)?;
477+
self.inner.connect(&addr.into())?;
478+
479+
#[cfg(windows)]
480+
let mio = unsafe { mio::net::TcpStream::from_raw_socket(self.inner.into_raw_socket()) };
481+
#[cfg(unix)]
482+
let mio = unsafe { mio::net::TcpStream::from_raw_fd(self.inner.into_raw_fd()) };
483+
467484
TcpStream::connect_mio(mio).await
468485
}
469486

@@ -503,7 +520,14 @@ impl TcpSocket {
503520
/// }
504521
/// ```
505522
pub fn listen(self, backlog: u32) -> io::Result<TcpListener> {
506-
let mio = self.inner.listen(backlog)?;
523+
let backlog = backlog.try_into().unwrap_or(i32::MAX);
524+
self.inner.listen(backlog)?;
525+
526+
#[cfg(windows)]
527+
let mio = unsafe { mio::net::TcpListener::from_raw_socket(self.inner.into_raw_socket()) };
528+
#[cfg(unix)]
529+
let mio = unsafe { mio::net::TcpListener::from_raw_fd(self.inner.into_raw_fd()) };
530+
507531
TcpListener::new(mio)
508532
}
509533

@@ -523,7 +547,7 @@ impl TcpSocket {
523547
///
524548
/// #[tokio::main]
525549
/// async fn main() -> std::io::Result<()> {
526-
///
550+
///
527551
/// let socket2_socket = Socket::new(Domain::IPV4, Type::STREAM, None)?;
528552
///
529553
/// let socket = TcpSocket::from_std_stream(socket2_socket.into());
@@ -534,16 +558,12 @@ impl TcpSocket {
534558
pub fn from_std_stream(std_stream: std::net::TcpStream) -> TcpSocket {
535559
#[cfg(unix)]
536560
{
537-
use std::os::unix::io::{FromRawFd, IntoRawFd};
538-
539561
let raw_fd = std_stream.into_raw_fd();
540562
unsafe { TcpSocket::from_raw_fd(raw_fd) }
541563
}
542564

543565
#[cfg(windows)]
544566
{
545-
use std::os::windows::io::{FromRawSocket, IntoRawSocket};
546-
547567
let raw_socket = std_stream.into_raw_socket();
548568
unsafe { TcpSocket::from_raw_socket(raw_socket) }
549569
}
@@ -572,7 +592,7 @@ impl FromRawFd for TcpSocket {
572592
/// The caller is responsible for ensuring that the socket is in
573593
/// non-blocking mode.
574594
unsafe fn from_raw_fd(fd: RawFd) -> TcpSocket {
575-
let inner = mio::net::TcpSocket::from_raw_fd(fd);
595+
let inner = socket2::Socket::from_raw_fd(fd);
576596
TcpSocket { inner }
577597
}
578598
}
@@ -607,7 +627,7 @@ impl FromRawSocket for TcpSocket {
607627
/// The caller is responsible for ensuring that the socket is in
608628
/// non-blocking mode.
609629
unsafe fn from_raw_socket(socket: RawSocket) -> TcpSocket {
610-
let inner = mio::net::TcpSocket::from_raw_socket(socket);
630+
let inner = socket2::Socket::from_raw_socket(socket);
611631
TcpSocket { inner }
612632
}
613633
}

tokio/src/net/tcp/stream.rs

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,7 @@ impl TcpStream {
387387
/// // if the readiness event is a false positive.
388388
/// match stream.try_read(&mut data) {
389389
/// Ok(n) => {
390-
/// println!("read {} bytes", n);
390+
/// println!("read {} bytes", n);
391391
/// }
392392
/// Err(ref e) if e.kind() == io::ErrorKind::WouldBlock => {
393393
/// continue;
@@ -1090,9 +1090,8 @@ impl TcpStream {
10901090
/// # }
10911091
/// ```
10921092
pub fn linger(&self) -> io::Result<Option<Duration>> {
1093-
let mio_socket = std::mem::ManuallyDrop::new(self.to_mio());
1094-
1095-
mio_socket.get_linger()
1093+
let socket = self.to_socket();
1094+
socket.linger()
10961095
}
10971096

10981097
/// Sets the linger duration of this socket by setting the SO_LINGER option.
@@ -1117,23 +1116,12 @@ impl TcpStream {
11171116
/// # }
11181117
/// ```
11191118
pub fn set_linger(&self, dur: Option<Duration>) -> io::Result<()> {
1120-
let mio_socket = std::mem::ManuallyDrop::new(self.to_mio());
1121-
1122-
mio_socket.set_linger(dur)
1119+
let socket = self.to_socket();
1120+
socket.set_linger(dur)
11231121
}
11241122

1125-
fn to_mio(&self) -> mio::net::TcpSocket {
1126-
#[cfg(windows)]
1127-
{
1128-
use std::os::windows::io::{AsRawSocket, FromRawSocket};
1129-
unsafe { mio::net::TcpSocket::from_raw_socket(self.as_raw_socket()) }
1130-
}
1131-
1132-
#[cfg(unix)]
1133-
{
1134-
use std::os::unix::io::{AsRawFd, FromRawFd};
1135-
unsafe { mio::net::TcpSocket::from_raw_fd(self.as_raw_fd()) }
1136-
}
1123+
fn to_socket(&self) -> socket2::SockRef<'_> {
1124+
socket2::SockRef::from(self)
11371125
}
11381126

11391127
/// Gets the value of the `IP_TTL` option for this socket.

0 commit comments

Comments
 (0)