-
Notifications
You must be signed in to change notification settings - Fork 10
/
option.hpp
278 lines (246 loc) · 9.43 KB
/
option.hpp
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#pragma once
#include "telnetpp/session.hpp"
#include <boost/signals2.hpp>
namespace telnetpp {
//* =========================================================================
/// \brief An class that encapsulates one side of a Telnet option.
///
/// A Telnet option comprises two essential parts: an activation sequence,
/// whereby a client and a server agree upon whether the option is active
/// or not, and a subnegotiation handler in which the option-specific
/// protocol is exchanged.
///
/// \par Inheritance
///
/// For most options, only handle_subnegotiation need be overridden. This
/// is where the option-specific protocol contained in the subnegotiations
/// is received.
///
/// An option may require auxiliary classes in order to be useful. See
/// the documentation accompanying those options for more information.
///
/// \par Usage
///
/// The easiest way to use an option is to install it in a session. A
/// session is used to automatically route relevant messages to and from
/// an option. See telnetpp::session for more information.
///
/// \par Note
///
/// Most functions that telnetpp::option exposes could potentially require
/// that data be transmitted to the remote. To allow the user to specify
/// exactly how that occurs, telnetpp::option uses continuations: functions
/// where the caller specifies how the result of the operation should be
/// used.
//* =========================================================================
template <
telnetpp::command_type LocalPositive,
telnetpp::command_type LocalNegative,
telnetpp::command_type RemotePositive,
telnetpp::command_type RemoteNegative>
class option
{
public:
static constexpr auto local_positive = LocalPositive;
static constexpr auto local_negative = LocalNegative;
static constexpr auto remote_positive = RemotePositive;
static constexpr auto remote_negative = RemoteNegative;
//* =====================================================================
/// Destructor
//* =====================================================================
virtual ~option() = default;
//* =====================================================================
/// Returns the code for the option.
//* =====================================================================
[[nodiscard]] constexpr telnetpp::option_type option_code() const noexcept
{
return code_;
}
//* =====================================================================
/// \brief Returns whether the option is active.
//* =====================================================================
[[nodiscard]] constexpr bool active() const noexcept
{
return state_ == internal_state::active;
}
//* =====================================================================
/// \brief Begins the activation process for the option.
//* =====================================================================
constexpr void activate()
{
switch (state_)
{
case internal_state::inactive:
state_ = internal_state::activating;
write_negotiation(local_positive);
break;
case internal_state::activating:
break;
case internal_state::active:
on_state_changed();
break;
case internal_state::deactivating:
break;
default:
assert(!"Unimplemented");
break;
}
}
//* =====================================================================
/// \brief Begins the deactivation process for the option.
//* =====================================================================
constexpr void deactivate()
{
switch (state_)
{
case internal_state::active:
state_ = internal_state::deactivating;
write_negotiation(local_negative);
break;
case internal_state::activating:
break;
case internal_state::inactive:
on_state_changed();
break;
case internal_state::deactivating:
break;
default:
assert(!"Unimplemented");
break;
}
}
//* =====================================================================
/// \brief Negotiate with the option.
/// This should be called when the remote side either initiates a
/// negotiation request or responds to an ongoing request.
//* =====================================================================
constexpr void negotiate(telnetpp::negotiation_type neg)
{
switch (state_)
{
case internal_state::inactive:
if (neg == remote_positive)
{
state_ = internal_state::active;
write_negotiation(local_positive);
on_state_changed();
}
else
{
write_negotiation(local_negative);
}
break;
case internal_state::activating:
if (neg == remote_positive)
{
state_ = internal_state::active;
on_state_changed();
}
else
{
state_ = internal_state::inactive;
on_state_changed();
}
break;
case internal_state::active:
if (neg == remote_positive)
{
write_negotiation(local_positive);
}
else
{
state_ = internal_state::inactive;
on_state_changed();
write_negotiation(local_negative);
}
break;
case internal_state::deactivating:
if (neg == remote_positive)
{
state_ = internal_state::active;
on_state_changed();
}
else
{
state_ = internal_state::inactive;
on_state_changed();
}
break;
default:
assert(!"Unimplemented");
break;
}
}
//* =====================================================================
/// \brief Subnegotiate with the option.
/// This should be called when a subnegotiation sequence has been received
/// from the remote.
//* =====================================================================
constexpr void subnegotiate(telnetpp::bytes content)
{
if (state_ == internal_state::active)
{
handle_subnegotiation(content);
}
}
//* =====================================================================
/// \fn on_state_changed.connect
/// \brief A signal that is emitted whenever there is a change in the
/// state of the option.
///
/// This will be emitted when a solicited negotiation is complete,
/// whatever the outcome, or whenever an unsolicited negotiation results
/// in the option changing state.
///
/// The parameter of the signal is a continuation that can be called to
/// send any Telnet elements that are emitted by this process.
//* =====================================================================
boost::signals2::signal<void()> on_state_changed; // NOLINT
protected:
//* =====================================================================
/// Constructor
//* =====================================================================
constexpr explicit option(
telnetpp::session &sess, telnetpp::option_type code) noexcept
: session_{sess}, code_{code}
{
}
//* =====================================================================
/// \brief Write plain text to the session
//* =====================================================================
void write_text(telnetpp::bytes content)
{
session_.write(content);
}
//* =====================================================================
/// \brief Write a subnegotiation to the session
//* =====================================================================
void write_subnegotiation(telnetpp::bytes content)
{
session_.write(telnetpp::subnegotiation{code_, content});
}
private:
//* =====================================================================
/// \brief Write a negotiation to the session
//* =====================================================================
void write_negotiation(telnetpp::negotiation_type neg)
{
session_.write(telnetpp::negotiation{neg, code_});
}
//* =====================================================================
/// \brief Called when a subnegotiation is received while the option is
/// active. Override for option-specific functionality.
//* =====================================================================
virtual void handle_subnegotiation(telnetpp::bytes data) = 0;
enum class internal_state
{
inactive,
activating,
active,
deactivating,
};
telnetpp::session &session_;
telnetpp::option_type code_;
internal_state state_ = internal_state::inactive;
};
} // namespace telnetpp