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 pin_project_lite::pin_project;
use std::io;
use std::pin::Pin;
use std::task::{Context, Poll};
use tokio::io::{AsyncBufRead, AsyncRead, AsyncWrite, BufStream, ReadBuf};
use tokio::net::{TcpStream, ToSocketAddrs};
pin_project! {
#[derive(Debug)]
#[must_use = "Connection do nothing unless polled"]
pub struct Connection {
#[pin]
stream: BufStream<TcpStream>
}
}
impl AsyncRead for Connection {
fn poll_read(
self: Pin<&mut Self>,
cx: &mut Context,
buf: &mut ReadBuf,
) -> Poll<io::Result<()>> {
self.project().stream.poll_read(cx, buf)
}
}
impl AsyncWrite for Connection {
fn poll_write(self: Pin<&mut Self>, cx: &mut Context, buf: &[u8]) -> Poll<io::Result<usize>> {
self.project().stream.poll_write(cx, buf)
}
fn poll_flush(self: Pin<&mut Self>, cx: &mut Context) -> Poll<io::Result<()>> {
self.project().stream.poll_flush(cx)
}
fn poll_shutdown(self: Pin<&mut Self>, cx: &mut Context) -> Poll<io::Result<()>> {
self.project().stream.poll_shutdown(cx)
}
}
impl AsyncBufRead for Connection {
fn poll_fill_buf(self: Pin<&mut Self>, cx: &mut Context) -> Poll<io::Result<&[u8]>> {
self.project().stream.poll_fill_buf(cx)
}
fn consume(self: Pin<&mut Self>, amt: usize) {
self.project().stream.consume(amt)
}
}
impl Connection {
pub async fn connect<A: ToSocketAddrs>(address: A) -> Result<Connection, io::Error> {
TcpStream::connect(address).await.map(|c| Connection {
stream: BufStream::new(c),
})
}
pub fn has_broken(&self) -> bool {
self.stream
.get_ref()
.try_read(&mut [])
.map(|value| value == 0)
.unwrap_or(true)
}
pub fn get_ref(&self) -> &TcpStream {
&self.stream.get_ref()
}
}