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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
use std::fmt;
use std::time::Duration;
use ntex::channel::mpsc;
use ntex::codec::Framed;
use super::codec as mqtt;
use super::sink::MqttSink;
use crate::handshake::HandshakeResult;
pub struct Connect<Io> {
connect: mqtt::Connect,
sink: MqttSink,
keep_alive: Duration,
inflight: usize,
io: HandshakeResult<Io, (), mqtt::Codec, mpsc::Receiver<mqtt::Packet>>,
}
impl<Io> Connect<Io> {
pub(crate) fn new(
connect: mqtt::Connect,
io: HandshakeResult<Io, (), mqtt::Codec, mpsc::Receiver<mqtt::Packet>>,
sink: MqttSink,
inflight: usize,
) -> Self {
Self {
keep_alive: Duration::from_secs(connect.keep_alive as u64),
connect,
io,
sink,
inflight,
}
}
pub fn packet(&self) -> &mqtt::Connect {
&self.connect
}
pub fn packet_mut(&mut self) -> &mut mqtt::Connect {
&mut self.connect
}
#[inline]
pub fn io(&mut self) -> &mut Framed<Io, mqtt::Codec> {
self.io.io()
}
pub fn sink(&self) -> &MqttSink {
&self.sink
}
pub fn ack<St>(self, st: St, session_present: bool) -> ConnectAck<Io, St> {
ConnectAck::new(
self.io,
self.sink,
st,
session_present,
self.keep_alive,
self.inflight,
)
}
pub fn identifier_rejected<St>(self) -> ConnectAck<Io, St> {
ConnectAck {
io: self.io,
sink: self.sink,
session: None,
session_present: false,
return_code: mqtt::ConnectAckReason::IdentifierRejected,
keep_alive: Duration::from_secs(5),
inflight: 15,
}
}
pub fn bad_username_or_pwd<St>(self) -> ConnectAck<Io, St> {
ConnectAck {
io: self.io,
sink: self.sink,
session: None,
session_present: false,
return_code: mqtt::ConnectAckReason::BadUserNameOrPassword,
keep_alive: Duration::from_secs(5),
inflight: 15,
}
}
pub fn not_authorized<St>(self) -> ConnectAck<Io, St> {
ConnectAck {
io: self.io,
sink: self.sink,
session: None,
session_present: false,
return_code: mqtt::ConnectAckReason::NotAuthorized,
keep_alive: Duration::from_secs(5),
inflight: 15,
}
}
pub fn service_unavailable<St>(self) -> ConnectAck<Io, St> {
ConnectAck {
io: self.io,
sink: self.sink,
session: None,
session_present: false,
return_code: mqtt::ConnectAckReason::ServiceUnavailable,
keep_alive: Duration::from_secs(5),
inflight: 15,
}
}
}
impl<T> fmt::Debug for Connect<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.connect.fmt(f)
}
}
pub struct ConnectAck<Io, St> {
pub(crate) io: HandshakeResult<Io, (), mqtt::Codec, mpsc::Receiver<mqtt::Packet>>,
pub(crate) session: Option<St>,
pub(crate) session_present: bool,
pub(crate) return_code: mqtt::ConnectAckReason,
pub(crate) keep_alive: Duration,
pub(crate) inflight: usize,
pub(crate) sink: MqttSink,
}
impl<Io, St> ConnectAck<Io, St> {
pub(crate) fn new(
io: HandshakeResult<Io, (), mqtt::Codec, mpsc::Receiver<mqtt::Packet>>,
sink: MqttSink,
session: St,
session_present: bool,
keep_alive: Duration,
inflight: usize,
) -> Self {
Self {
io,
sink,
session_present,
keep_alive,
inflight,
session: Some(session),
return_code: mqtt::ConnectAckReason::ConnectionAccepted,
}
}
pub fn idle_timeout(mut self, timeout: Duration) -> Self {
self.keep_alive = timeout;
self
}
pub fn in_flight(mut self, in_flight: usize) -> Self {
self.inflight = in_flight;
self
}
}