Telnet++  3.1.0.2
A C++ library for interacting with Telnet streams
generate_helper.hpp
1 #pragma once
2 
3 #include "telnetpp/element.hpp"
4 
5 namespace telnetpp::detail {
6 
7 //* =========================================================================
9 //* =========================================================================
10 template <class Continuation>
11 constexpr void generate_escaped(telnetpp::bytes data, Continuation &&cont)
12 {
13  telnetpp::bytes::iterator begin = data.begin();
14  telnetpp::bytes::iterator current = data.begin();
15  telnetpp::bytes::iterator end = data.end();
16 
17  // If we come across an 0xFF byte in the data, then it must be repeated.
18  // We do this by splitting the span into two, one of which ends with the
19  // 0xFF byte and the second that begins with it. In this way, the byte is
20  // duplicated without requiring any extra allocations.
21  while (current != end)
22  {
23  if (*current == telnetpp::iac)
24  {
25  cont({begin, current + 1});
26  begin = current;
27  }
28 
29  ++current;
30  }
31 
32  if (begin != end)
33  {
34  cont({begin, end});
35  }
36 }
37 
38 //* =========================================================================
40 //* =========================================================================
41 template <class Continuation>
42 constexpr void generate_command(telnetpp::command cmd, Continuation &&cont)
43 {
44  telnetpp::byte const data[] = {telnetpp::iac, cmd.value()};
45 
46  cont(data);
47 }
48 
49 //* =========================================================================
51 //* =========================================================================
52 template <class Continuation>
53 constexpr void generate_negotiation(
54  telnetpp::negotiation neg, Continuation &&cont)
55 {
56  telnetpp::byte const data[] = {
57  telnetpp::iac, neg.request(), neg.option_code()};
58 
59  cont(data);
60 }
61 
62 //* =========================================================================
64 //* =========================================================================
65 template <class Continuation>
66 constexpr void generate_subnegotiation(
67  telnetpp::subnegotiation sub, Continuation &&cont)
68 {
69  telnetpp::byte const preamble[] = {
70  telnetpp::iac, telnetpp::sb, sub.option()};
71 
72  constexpr telnetpp::byte const postamble[] = {telnetpp::iac, telnetpp::se};
73 
74  cont(preamble);
75  generate_escaped(sub.content(), cont);
76  cont(postamble);
77 }
78 
79 } // namespace telnetpp::detail
A class that encapsulates the value of a Telnet command.
Definition: command.hpp:13
constexpr command_type value() const noexcept
Returns the value of the command.
Definition: command.hpp:25
A class that encapsulates a Telnet negotiation.
Definition: negotiation.hpp:15
constexpr option_type option_code() const noexcept
Returns the option code (e.g. echo, naws) of this negotiation.
Definition: negotiation.hpp:40
constexpr negotiation_type request() const noexcept
Returns the request (will, wont, do, dont) of this negotiation.
Definition: negotiation.hpp:32
A class that encapsulates a Telnet subnegotiation.
Definition: subnegotiation.hpp:14
constexpr bytes content() const noexcept
Returns the content for this subnegotiation.
Definition: subnegotiation.hpp:35
constexpr option_type option() const noexcept
Returns the option for this subnegotiation.
Definition: subnegotiation.hpp:27