|
1 | | -use std::io::{IoSlice, IoSliceMut, Read as _, Write as _}; |
| 1 | +use std::io::{IoSlice, IoSliceMut}; |
2 | 2 | use std::net::SocketAddr; |
3 | 3 | use std::pin::Pin; |
4 | | -use std::sync::Arc; |
5 | 4 |
|
6 | | -use crate::future; |
| 5 | +use smol::Async; |
| 6 | + |
7 | 7 | use crate::io::{self, Read, Write}; |
8 | | -use crate::rt::Watcher; |
9 | 8 | use crate::net::ToSocketAddrs; |
| 9 | +use crate::sync::Arc; |
10 | 10 | use crate::task::{Context, Poll}; |
11 | 11 |
|
12 | 12 | /// A TCP stream between a local and a remote socket. |
@@ -47,7 +47,7 @@ use crate::task::{Context, Poll}; |
47 | 47 | /// ``` |
48 | 48 | #[derive(Debug, Clone)] |
49 | 49 | pub struct TcpStream { |
50 | | - pub(super) watcher: Arc<Watcher<mio::net::TcpStream>>, |
| 50 | + pub(super) watcher: Arc<Async<std::net::TcpStream>>, |
51 | 51 | } |
52 | 52 |
|
53 | 53 | impl TcpStream { |
@@ -75,28 +75,16 @@ impl TcpStream { |
75 | 75 | let addrs = addrs.to_socket_addrs().await?; |
76 | 76 |
|
77 | 77 | for addr in addrs { |
78 | | - // mio's TcpStream::connect is non-blocking and may just be in progress |
79 | | - // when it returns with `Ok`. We therefore wait for write readiness to |
80 | | - // be sure the connection has either been established or there was an |
81 | | - // error which we check for afterwards. |
82 | | - let watcher = match mio::net::TcpStream::connect(&addr) { |
83 | | - Ok(s) => Watcher::new(s), |
| 78 | + match Async::<std::net::TcpStream>::connect(&addr).await { |
| 79 | + Ok(stream) => { |
| 80 | + return Ok(TcpStream { |
| 81 | + watcher: Arc::new(stream), |
| 82 | + }); |
| 83 | + } |
84 | 84 | Err(e) => { |
85 | 85 | last_err = Some(e); |
86 | 86 | continue; |
87 | 87 | } |
88 | | - }; |
89 | | - |
90 | | - future::poll_fn(|cx| watcher.poll_write_ready(cx)).await; |
91 | | - |
92 | | - match watcher.get_ref().take_error() { |
93 | | - Ok(None) => { |
94 | | - return Ok(TcpStream { |
95 | | - watcher: Arc::new(watcher), |
96 | | - }); |
97 | | - } |
98 | | - Ok(Some(e)) => last_err = Some(e), |
99 | | - Err(e) => last_err = Some(e), |
100 | 88 | } |
101 | 89 | } |
102 | 90 |
|
@@ -214,7 +202,7 @@ impl TcpStream { |
214 | 202 | /// # Ok(()) }) } |
215 | 203 | /// ``` |
216 | 204 | pub async fn peek(&self, buf: &mut [u8]) -> io::Result<usize> { |
217 | | - future::poll_fn(|cx| self.watcher.poll_read_with(cx, |inner| inner.peek(buf))).await |
| 205 | + self.watcher.peek(buf).await |
218 | 206 | } |
219 | 207 |
|
220 | 208 | /// Gets the value of the `TCP_NODELAY` option on this socket. |
@@ -317,7 +305,7 @@ impl Read for &TcpStream { |
317 | 305 | cx: &mut Context<'_>, |
318 | 306 | buf: &mut [u8], |
319 | 307 | ) -> Poll<io::Result<usize>> { |
320 | | - self.watcher.poll_read_with(cx, |mut inner| inner.read(buf)) |
| 308 | + Pin::new(&mut &*self.watcher).poll_read(cx, buf) |
321 | 309 | } |
322 | 310 | } |
323 | 311 |
|
@@ -353,26 +341,23 @@ impl Write for &TcpStream { |
353 | 341 | cx: &mut Context<'_>, |
354 | 342 | buf: &[u8], |
355 | 343 | ) -> Poll<io::Result<usize>> { |
356 | | - self.watcher |
357 | | - .poll_write_with(cx, |mut inner| inner.write(buf)) |
| 344 | + Pin::new(&mut &*self.watcher).poll_write(cx, buf) |
358 | 345 | } |
359 | 346 |
|
360 | 347 | fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> { |
361 | | - self.watcher.poll_write_with(cx, |mut inner| inner.flush()) |
| 348 | + Pin::new(&mut &*self.watcher).poll_flush(cx) |
362 | 349 | } |
363 | 350 |
|
364 | | - fn poll_close(self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll<io::Result<()>> { |
365 | | - self.shutdown(std::net::Shutdown::Write)?; |
366 | | - Poll::Ready(Ok(())) |
| 351 | + fn poll_close(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> { |
| 352 | + Pin::new(&mut &*self.watcher).poll_close(cx) |
367 | 353 | } |
368 | 354 | } |
369 | 355 |
|
370 | 356 | impl From<std::net::TcpStream> for TcpStream { |
371 | 357 | /// Converts a `std::net::TcpStream` into its asynchronous equivalent. |
372 | 358 | fn from(stream: std::net::TcpStream) -> TcpStream { |
373 | | - let mio_stream = mio::net::TcpStream::from_stream(stream).unwrap(); |
374 | 359 | TcpStream { |
375 | | - watcher: Arc::new(Watcher::new(mio_stream)), |
| 360 | + watcher: Arc::new(Async::new(stream).expect("TcpStream is known to be good")), |
376 | 361 | } |
377 | 362 | } |
378 | 363 | } |
@@ -403,23 +388,23 @@ cfg_unix! { |
403 | 388 | } |
404 | 389 |
|
405 | 390 | cfg_windows! { |
406 | | - // use crate::os::windows::io::{AsRawHandle, FromRawHandle, IntoRawHandle, RawHandle}; |
407 | | - // |
408 | | - // impl AsRawSocket for TcpStream { |
409 | | - // fn as_raw_socket(&self) -> RawSocket { |
410 | | - // self.raw_socket |
411 | | - // } |
412 | | - // } |
413 | | - // |
414 | | - // impl FromRawSocket for TcpStream { |
415 | | - // unsafe fn from_raw_socket(handle: RawSocket) -> TcpStream { |
416 | | - // net::TcpStream::from_raw_socket(handle).try_into().unwrap() |
417 | | - // } |
418 | | - // } |
419 | | - // |
420 | | - // impl IntoRawSocket for TcpListener { |
421 | | - // fn into_raw_socket(self) -> RawSocket { |
422 | | - // self.raw_socket |
423 | | - // } |
424 | | - // } |
| 391 | + use crate::os::windows::io::{AsRawHandle, FromRawHandle, IntoRawHandle, RawHandle}; |
| 392 | + |
| 393 | + impl AsRawSocket for TcpStream { |
| 394 | + fn as_raw_socket(&self) -> RawSocket { |
| 395 | + self.raw_socket |
| 396 | + } |
| 397 | + } |
| 398 | + |
| 399 | + impl FromRawSocket for TcpStream { |
| 400 | + unsafe fn from_raw_socket(handle: RawSocket) -> TcpStream { |
| 401 | + net::TcpStream::from_raw_socket(handle).try_into().unwrap() |
| 402 | + } |
| 403 | + } |
| 404 | + |
| 405 | + impl IntoRawSocket for TcpListener { |
| 406 | + fn into_raw_socket(self) -> RawSocket { |
| 407 | + self.raw_socket |
| 408 | + } |
| 409 | + } |
425 | 410 | } |
0 commit comments