qb  2.0.0.0
C++17 Actor Framework
qb Issue Watch Star Fork Follow @isndev
Loading...
Searching...
No Matches
text.h
Go to the documentation of this file.
1
25
26#ifndef QB_IO_PROT_TEXT_H_
27#define QB_IO_PROT_TEXT_H_
28#include "base.h"
29
30namespace qb::protocol::text {
31
47template <typename _IO_, typename _StringTrait, char _Sep = '\0'>
48class basic_text : public base::byte_terminated<_IO_, _Sep> {
49public:
53 basic_text() = delete;
54
60 basic_text(_IO_ &io) noexcept
62
71 struct message {
72 const std::size_t size;
73 const char *data;
74 _StringTrait text;
75 };
76
85 void
86 onMessage(std::size_t size) noexcept {
87 const auto &buffer = this->_io.in();
88 const auto parsed = this->shiftSize(size);
89 this->_io.on(message{parsed, buffer.cbegin(), {buffer.cbegin(), parsed}});
90 }
91};
92
107template <typename _IO_, typename _SizeHeader = uint16_t>
108class basic_binary : public base::size_as_header<_IO_, _SizeHeader> {
109public:
113 basic_binary() = delete;
114
122
130 struct message {
131 const std::size_t size;
132 const char *data;
133 };
134
143 void
144 onMessage(std::size_t size) const noexcept {
145 this->_io.on(message{size, this->_io.in().cbegin() + this->shiftSize()});
146 }
147};
148
159template <typename _IO_>
161
172template <typename _IO_>
174
185template <typename _IO_>
187
198template <typename _IO_>
199using string = basic_text<_IO_, std::string, '\0'>;
200
212template <typename _IO_>
213using command = basic_text<_IO_, std::string, '\n'>;
214
226template <typename _IO_>
227using string_view = basic_text<_IO_, const std::string_view, '\0'>;
228
239template <typename _IO_>
240using command_view = basic_text<_IO_, const std::string_view, '\n'>;
241
242} // namespace qb::protocol::text
243
244#endif // QB_IO_PROT_TEXT_H_
_IO_ & _io
Reference to the I/O component instance that this protocol is associated with.
Definition protocol.h:166
Protocol for messages delimited by a specific single byte character.
Definition base.h:48
std::size_t shiftSize(std::size_t const size) const noexcept
Definition base.h:81
Protocol for messages where the payload is preceded by a fixed-size header indicating its length.
Definition base.h:223
std::size_t shiftSize() const noexcept
Definition base.h:247
Protocol for binary messages where the payload is preceded by a fixed-size header indicating its leng...
Definition text.h:108
void onMessage(std::size_t size) const noexcept
Process a received message.
Definition text.h:144
basic_binary(_IO_ &io) noexcept
Constructor with I/O reference.
Definition text.h:120
basic_binary()=delete
Default constructor (deleted)
Protocol for text messages delimited by a specific character, yielding a specified string type.
Definition text.h:48
basic_text()=delete
Default constructor (deleted)
void onMessage(std::size_t size) noexcept
Process a received message.
Definition text.h:86
basic_text(_IO_ &io) noexcept
Constructor with I/O reference.
Definition text.h:60
basic_text< _IO_, std::string, '\n'> command
Protocol for newline-terminated (\n) commands, yielding std::string.
Definition text.h:213
basic_text< _IO_, const std::string_view, '\n'> command_view
Protocol for newline-terminated (\n) commands, yielding std::string_view (zero-copy read).
Definition text.h:240
basic_binary< _IO_, uint32_t > binary32
Binary protocol with a 32-bit (uint32_t) size header.
Definition text.h:186
basic_binary< _IO_, uint16_t > binary16
Binary protocol with a 16-bit (uint16_t) size header.
Definition text.h:173
basic_binary< _IO_, uint8_t > binary8
Binary protocol with an 8-bit (uint8_t) size header.
Definition text.h:160
basic_text< _IO_, const std::string_view, '\0'> string_view
Protocol for NULL-terminated (\0) strings, yielding std::string_view (zero-copy read).
Definition text.h:227
basic_text< _IO_, std::string, '\0'> string
Protocol for NULL-terminated (\0) strings, yielding std::string.
Definition text.h:199
Base protocol implementations for message framing in the QB IO system.
Structure representing a binary message.
Definition text.h:130
const char * data
Pointer to the raw data.
Definition text.h:132
const std::size_t size
Message size.
Definition text.h:131
Structure representing a text message.
Definition text.h:71
_StringTrait text
The message as text.
Definition text.h:74
const std::size_t size
Message size without the delimiter.
Definition text.h:72
const char * data
Pointer to the raw data.
Definition text.h:73