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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
use std::cmp::Ordering;
use std::mem;
use std::time::Instant;
use crate::frame::{Frame, FrameType};
use crate::packet::PacketBuilder;
use crate::recovery::RecoveryToken;
use crate::{CloseError, ConnectionError};
#[derive(Clone, Debug, PartialEq, Eq)]
/// The state of the Connection.
pub enum State {
Init,
WaitInitial,
Handshaking,
Connected,
Confirmed,
Closing {
error: ConnectionError,
timeout: Instant,
},
Draining {
error: ConnectionError,
timeout: Instant,
},
Closed(ConnectionError),
}
impl State {
#[must_use]
pub fn connected(&self) -> bool {
matches!(self, Self::Connected | Self::Confirmed)
}
#[must_use]
pub fn closed(&self) -> bool {
matches!(self, Self::Closing { .. } | Self::Draining { .. } | Self::Closed(_))
}
}
// Implement `PartialOrd` so that we can enforce monotonic state progression.
impl PartialOrd for State {
#[allow(clippy::match_same_arms)] // Lint bug: rust-lang/rust-clippy#860
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
if mem::discriminant(self) == mem::discriminant(other) {
return Some(Ordering::Equal);
}
Some(match (self, other) {
(Self::Init, _) => Ordering::Less,
(_, Self::Init) => Ordering::Greater,
(Self::WaitInitial, _) => Ordering::Less,
(_, Self::WaitInitial) => Ordering::Greater,
(Self::Handshaking, _) => Ordering::Less,
(_, Self::Handshaking) => Ordering::Greater,
(Self::Connected, _) => Ordering::Less,
(_, Self::Connected) => Ordering::Greater,
(Self::Confirmed, _) => Ordering::Less,
(_, Self::Confirmed) => Ordering::Greater,
(Self::Closing { .. }, _) => Ordering::Less,
(_, Self::Closing { .. }) => Ordering::Greater,
(Self::Draining { .. }, _) => Ordering::Less,
(_, Self::Draining { .. }) => Ordering::Greater,
(Self::Closed(_), _) => unreachable!(),
})
}
}
impl Ord for State {
fn cmp(&self, other: &Self) -> Ordering {
if mem::discriminant(self) == mem::discriminant(other) {
return Ordering::Equal;
}
match (self, other) {
(Self::Init, _) => Ordering::Less,
(_, Self::Init) => Ordering::Greater,
(Self::WaitInitial, _) => Ordering::Less,
(_, Self::WaitInitial) => Ordering::Greater,
(Self::Handshaking, _) => Ordering::Less,
(_, Self::Handshaking) => Ordering::Greater,
(Self::Connected, _) => Ordering::Less,
(_, Self::Connected) => Ordering::Greater,
(Self::Confirmed, _) => Ordering::Less,
(_, Self::Confirmed) => Ordering::Greater,
(Self::Closing { .. }, _) => Ordering::Less,
(_, Self::Closing { .. }) => Ordering::Greater,
(Self::Draining { .. }, _) => Ordering::Less,
(_, Self::Draining { .. }) => Ordering::Greater,
(Self::Closed(_), _) => unreachable!(),
}
}
}
type ClosingFrame = Frame<'static>;
/// `StateSignaling` manages whether we need to send HANDSHAKE_DONE and CONNECTION_CLOSE.
/// Valid state transitions are:
/// * Idle -> HandshakeDone: at the server when the handshake completes
/// * HandshakeDone -> Idle: when a HANDSHAKE_DONE frame is sent
/// * Idle/HandshakeDone -> Closing/Draining: when closing or draining
/// * Closing/Draining -> CloseSent: after sending CONNECTION_CLOSE
/// * CloseSent -> Closing: any time a new CONNECTION_CLOSE is needed
/// * -> Reset: from any state in case of a stateless reset
#[derive(Debug, Clone, PartialEq)]
pub enum StateSignaling {
Idle,
HandshakeDone,
/// These states save the frame that needs to be sent.
Closing(ClosingFrame),
Draining(ClosingFrame),
/// This state saves the frame that might need to be sent again.
/// If it is `None`, then we are draining and don't send.
CloseSent(Option<ClosingFrame>),
Reset,
}
impl StateSignaling {
pub fn handshake_done(&mut self) {
if *self != Self::Idle {
debug_assert!(false, "StateSignaling must be in Idle state.");
return;
}
*self = Self::HandshakeDone
}
pub fn write_done(&mut self, builder: &mut PacketBuilder) -> Option<RecoveryToken> {
if *self == Self::HandshakeDone && builder.remaining() >= 1 {
*self = Self::Idle;
builder.encode_varint(Frame::HandshakeDone.get_type());
Some(RecoveryToken::HandshakeDone)
} else {
None
}
}
fn make_close_frame(
error: ConnectionError,
frame_type: FrameType,
message: impl AsRef<str>,
) -> ClosingFrame {
let reason_phrase = message.as_ref().as_bytes().to_owned();
Frame::ConnectionClose {
error_code: CloseError::from(error),
frame_type,
reason_phrase,
}
}
pub fn close(
&mut self,
error: ConnectionError,
frame_type: FrameType,
message: impl AsRef<str>,
) {
if *self != Self::Reset {
*self = Self::Closing(Self::make_close_frame(error, frame_type, message));
}
}
pub fn drain(
&mut self,
error: ConnectionError,
frame_type: FrameType,
message: impl AsRef<str>,
) {
if *self != Self::Reset {
*self = Self::Draining(Self::make_close_frame(error, frame_type, message));
}
}
/// If a close is pending, take a frame.
pub fn close_frame(&mut self) -> Option<ClosingFrame> {
match self {
Self::Closing(frame) => {
// When we are closing, we might need to send the close frame again.
let frame = mem::replace(frame, Frame::Padding);
*self = Self::CloseSent(Some(frame.clone()));
Some(frame)
}
Self::Draining(frame) => {
// When we are draining, just send once.
let frame = mem::replace(frame, Frame::Padding);
*self = Self::CloseSent(None);
Some(frame)
}
_ => None,
}
}
/// If a close can be sent again, prepare to send it again.
pub fn send_close(&mut self) {
if let Self::CloseSent(Some(frame)) = self {
let frame = mem::replace(frame, Frame::Padding);
*self = Self::Closing(frame);
}
}
/// We just got a stateless reset. Terminate.
pub fn reset(&mut self) {
*self = Self::Reset;
}
}
|