Telnet++ 4.0.0.9
A C++ library for interacting with Telnet streams
Loading...
Searching...
No Matches
core.hpp
1#pragma once
2
3#include "telnetpp/detail/export.hpp" // IWYU pragma: export
4
5#include <algorithm>
6#include <span>
7#include <string>
8#include <cstdint>
9
10namespace telnetpp {
11
12using byte = std::uint8_t;
13using option_type = std::uint8_t;
14using command_type = std::uint8_t;
15using negotiation_type = std::uint8_t;
16using subnegotiation_content_type = byte;
17
18// TELNET Commands
19inline constexpr command_type const se = 240; // Subnegotiation End
20inline constexpr command_type const nop = 241; // No Operation
21inline constexpr command_type const dm = 242; // Data Mark
22inline constexpr command_type const brk = 243; // Break
23inline constexpr command_type const ip = 244; // Interrupt Process
24inline constexpr command_type const ao = 245; // Abort Output
25inline constexpr command_type const ayt = 246; // Are You There?
26inline constexpr command_type const ec = 247; // Erase Character
27inline constexpr command_type const el = 248; // Erase Line
28inline constexpr command_type const ga = 249; // Go Ahead
29inline constexpr command_type const sb = 250; // Subnegotiation Begin
30inline constexpr command_type const will = 251;
31inline constexpr command_type const wont = 252;
32inline constexpr command_type const do_ = 253; // NOLINT
33inline constexpr command_type const dont = 254;
34inline constexpr command_type const iac = 255; // Interpret As Command
35
36// A stream of bytes in Telnet++ is exposed as a non-owning span. It is
37// expected that the data will live for no longer than any function in
38// which it is found. For that reason, these spans should never be stored.
39// if this is necessary, it must be converted into a longer-term data
40// structure, e.g. a std::vector, or std::basic_string<byte>.
41using bytes = std::span<byte const>;
42
43// Where necessary, bytes are stored in this type, which has the small
44// string optimization, meaning that most cases will not cause an allocation.
45using byte_storage = std::basic_string<byte>;
46
47// Comparison function for bytes.
48// std::span<> does not define comparison operators by default because the
49// semantics are unclear (deep or shallow?). This named comparison function
50// is provided because we cannot define a useful operator== because ADL
51// would not find it in our namespace.
52constexpr inline auto bytes_equal = [](bytes const &lhs,
53 bytes const &rhs) noexcept {
54 return std::ranges::equal(lhs, rhs);
55};
56
57namespace literals {
58
59// A simple function to convert from string literals to stored bytes.
60inline byte_storage operator""_tb(char const *text, size_t length)
61{
62 byte_storage result;
63 result.reserve(length);
64
65 for (auto ch : std::span<char const>{text, length})
66 {
67 result.push_back(static_cast<telnetpp::byte>(ch));
68 }
69
70 return result;
71}
72
73} // namespace literals
74} // namespace telnetpp