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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
|
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
//
use futures::AsyncRead as FuturesAsyncRead;
use futures::AsyncWrite as FuturesAsyncWrite;
use tokio::io::AsyncRead as TokioAsyncRead;
use tokio::io::AsyncWrite as TokioAsyncWrite;
use tokio::io::DuplexStream;
use tokio::net::TcpStream;
use tokio_util::compat::Compat as TokioCompat;
use tokio_util::compat::TokioAsyncReadCompatExt;
pub(crate) enum MqttConnection {
Tokio(TokioCompat<tokio::net::TcpStream>),
Duplex(TokioCompat<tokio::io::DuplexStream>),
}
impl TokioAsyncRead for MqttConnection {
fn poll_read(
mut self: std::pin::Pin<&mut Self>,
cx: &mut std::task::Context<'_>,
buf: &mut tokio::io::ReadBuf<'_>,
) -> std::task::Poll<std::io::Result<()>> {
match &mut *self {
MqttConnection::Tokio(t) => std::pin::pin!(t.get_mut()).poll_read(cx, buf),
MqttConnection::Duplex(d) => std::pin::pin!(d.get_mut()).poll_read(cx, buf),
}
}
}
impl TokioAsyncWrite for MqttConnection {
fn poll_write(
mut self: std::pin::Pin<&mut Self>,
cx: &mut std::task::Context<'_>,
buf: &[u8],
) -> std::task::Poll<Result<usize, std::io::Error>> {
match &mut *self {
MqttConnection::Tokio(t) => std::pin::pin!(t.get_mut()).poll_write(cx, buf),
MqttConnection::Duplex(d) => std::pin::pin!(d.get_mut()).poll_write(cx, buf),
}
}
fn poll_flush(
mut self: std::pin::Pin<&mut Self>,
cx: &mut std::task::Context<'_>,
) -> std::task::Poll<Result<(), std::io::Error>> {
match &mut *self {
MqttConnection::Tokio(t) => std::pin::pin!(t.get_mut()).poll_flush(cx),
MqttConnection::Duplex(d) => std::pin::pin!(d.get_mut()).poll_flush(cx),
}
}
fn poll_shutdown(
mut self: std::pin::Pin<&mut Self>,
cx: &mut std::task::Context<'_>,
) -> std::task::Poll<Result<(), std::io::Error>> {
match &mut *self {
MqttConnection::Tokio(t) => std::pin::pin!(t.get_mut()).poll_shutdown(cx),
MqttConnection::Duplex(d) => std::pin::pin!(d.get_mut()).poll_shutdown(cx),
}
}
}
impl FuturesAsyncRead for MqttConnection {
fn poll_read(
mut self: std::pin::Pin<&mut Self>,
cx: &mut std::task::Context<'_>,
buf: &mut [u8],
) -> std::task::Poll<std::io::Result<usize>> {
match &mut *self {
MqttConnection::Tokio(t) => std::pin::pin!(t).poll_read(cx, buf),
MqttConnection::Duplex(d) => std::pin::pin!(d).poll_read(cx, buf),
}
}
}
impl FuturesAsyncWrite for MqttConnection {
fn poll_write(
mut self: std::pin::Pin<&mut Self>,
cx: &mut std::task::Context<'_>,
buf: &[u8],
) -> std::task::Poll<std::io::Result<usize>> {
match &mut *self {
MqttConnection::Tokio(t) => std::pin::pin!(t).poll_write(cx, buf),
MqttConnection::Duplex(d) => std::pin::pin!(d).poll_write(cx, buf),
}
}
fn poll_flush(
mut self: std::pin::Pin<&mut Self>,
cx: &mut std::task::Context<'_>,
) -> std::task::Poll<std::io::Result<()>> {
match &mut *self {
MqttConnection::Tokio(t) => std::pin::pin!(t).poll_flush(cx),
MqttConnection::Duplex(d) => std::pin::pin!(d).poll_flush(cx),
}
}
fn poll_close(
mut self: std::pin::Pin<&mut Self>,
cx: &mut std::task::Context<'_>,
) -> std::task::Poll<std::io::Result<()>> {
match &mut *self {
MqttConnection::Tokio(t) => std::pin::pin!(t).poll_close(cx),
MqttConnection::Duplex(d) => std::pin::pin!(d).poll_close(cx),
}
}
}
pub enum MqttConnectTransport {
TokioTcp(TcpStream),
TokioDuplex(DuplexStream),
}
impl From<MqttConnectTransport> for MqttConnection {
fn from(value: MqttConnectTransport) -> Self {
match value {
MqttConnectTransport::TokioTcp(t) => MqttConnection::Tokio(t.compat()),
MqttConnectTransport::TokioDuplex(d) => MqttConnection::Duplex(d.compat()),
}
}
}
|