1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
use async_trait::async_trait;
use std::os::unix::io::{AsRawFd, FromRawFd};
use tokio::io::{AsyncRead, AsyncWrite, AsyncWriteExt};
use tokio::net::{TcpStream, UnixStream};
#[async_trait]
pub trait AsyncStream: AsyncRead + AsyncWrite + Unpin + Send {
async fn try_shutdown(&mut self) -> std::io::Result<()>;
}
#[async_trait]
impl AsyncStream for TcpStream {
async fn try_shutdown(&mut self) -> std::io::Result<()> {
let _ = self.shutdown().await;
// Unfortunately, AsyncWriteExt::shutdown/AsyncWrite::poll_shutdown only ends up
// calling std::net::Shutdown::Write and seems to leave sockets in
// CLOSE-WAIT/TIME-WAIT/FIN-WAIT states.
// If the function signature for this returned self instead of &mut self,
// we could do:
//
// self.to_std().shutdown(std::net::Shutdown::Both)
//
// but tokio_native_tls has no function to allow going back to a
// tokio/mio/std stream.
// So do it the hacky way.
let std_stream = std::mem::ManuallyDrop::new(unsafe {
std::net::TcpStream::from_raw_fd(self.as_raw_fd())
});
std_stream.shutdown(std::net::Shutdown::Both)
}
}
#[async_trait]
impl AsyncStream for UnixStream {
async fn try_shutdown(&mut self) -> std::io::Result<()> {
let _ = self.shutdown().await;
// Unfortunately, AsyncWriteExt::shutdown/AsyncWrite::poll_shutdown only ends up
// calling std::net::Shutdown::Write and seems to leave sockets in
// CLOSE-WAIT/TIME-WAIT/FIN-WAIT states.
// If the function signature for this returned self instead of &mut self,
// we could do:
//
// self.to_std().shutdown(std::net::Shutdown::Both)
//
// but tokio_native_tls has no function to allow going back to a
// tokio/mio/std stream.
// So do it the hacky way.
let std_stream = std::mem::ManuallyDrop::new(unsafe {
std::net::TcpStream::from_raw_fd(self.as_raw_fd())
});
std_stream.shutdown(std::net::Shutdown::Both)
}
}
#[async_trait]
impl<IO> AsyncStream for tokio_rustls::client::TlsStream<IO>
where
IO: AsyncStream,
{
async fn try_shutdown(&mut self) -> std::io::Result<()> {
let _ = self.shutdown().await;
self.get_mut().0.try_shutdown().await
}
}
#[async_trait]
impl<IO> AsyncStream for tokio_rustls::server::TlsStream<IO>
where
IO: AsyncStream,
{
async fn try_shutdown(&mut self) -> std::io::Result<()> {
let _ = self.shutdown().await;
self.get_mut().0.try_shutdown().await
}
}