qb  2.0.0.0
C++17 Actor Framework
qb Issue Watch Star Fork Follow @isndev
Loading...
Searching...
No Matches
base.h
Go to the documentation of this file.
1
25
26#ifndef QB_IO_PROT_BASE_H_
27#define QB_IO_PROT_BASE_H_
28#include <cstring>
30#include "../async/protocol.h"
31
32namespace qb::protocol::base {
33
47template <typename _IO_, char _EndByte = '\0'>
49 std::size_t _offset = 0u;
50
51public:
52 constexpr static const std::size_t delimiter_size =
53 1;
54 constexpr static const char end = _EndByte;
55
59 byte_terminated() = delete;
60
64 virtual ~byte_terminated() = default;
65
71 byte_terminated(_IO_ &io) noexcept
73
80 inline std::size_t
81 shiftSize(std::size_t const size) const noexcept {
82 return static_cast<std::size_t>(size) - delimiter_size;
83 }
84
92 std::size_t
93 getMessageSize() noexcept final {
94 const auto &buffer = this->_io.in();
95 auto it = buffer.begin() + _offset;
96 while (it != buffer.end()) {
97 if (*it == _EndByte) {
98 _offset = 0; // reset
99 return (it - buffer.begin()) + delimiter_size;
100 }
101 ++it;
102 }
103 _offset = it - buffer.begin();
104 return 0;
105 }
106
110 void
111 reset() noexcept final {
112 _offset = 0;
113 }
114};
115
131template <typename _IO_, typename _Trait>
133 constexpr static const std::size_t _SizeBytes =
134 sizeof(_Trait::_EndBytes) - 1;
135 std::size_t _offset = 0u;
136
137public:
138 constexpr static const std::size_t delimiter_size =
139 _SizeBytes;
140 constexpr static const auto end = _Trait::_EndBytes;
141
146
150 virtual ~bytes_terminated() = default;
151
157 bytes_terminated(_IO_ &io) noexcept
159
166 inline std::size_t
167 shiftSize(std::size_t const size) const noexcept {
168 return static_cast<std::size_t>(size) - delimiter_size;
169 }
170
178 std::size_t
180 const auto &buffer = this->_in_buffer;
181 auto i = buffer.begin() + _offset;
182
183 if ((buffer.end() - i) < _SizeBytes)
184 return 0;
185
186 const auto end = buffer.end() - _SizeBytes + 1;
187 while (i != end) {
188 if (!std::memcmp(i, _Trait::_EndBytes, _SizeBytes)) {
189 _offset = 0; // reset
190 return i - buffer.begin() + _SizeBytes;
191 }
192 ++i;
193 }
194 _offset = i - buffer.begin();
195 return 0;
196 }
197
201 void
202 reset() noexcept final {
203 _offset = 0;
204 }
205};
206
222template <typename _IO_, typename _Size = uint16_t>
224 constexpr static const std::size_t SIZEOF = sizeof(_Size);
225 _Size _size = 0u;
226
227public:
231 size_as_header() = delete;
232
238 size_as_header(_IO_ &io) noexcept
240
246 inline std::size_t
247 shiftSize() const noexcept {
248 return SIZEOF;
249 }
250
259 std::size_t
260 getMessageSize() noexcept final {
261 auto &buffer = this->_io.in();
262
263 if (!_size && buffer.size() >= SIZEOF) {
264 if constexpr (SIZEOF == 2)
265 _size = ntohs(*reinterpret_cast<_Size *>(buffer.begin()));
266 else if constexpr (SIZEOF == 4)
267 _size = ntohl(*reinterpret_cast<_Size *>(buffer.begin()));
268 else
269 _size = *reinterpret_cast<_Size *>(buffer.begin());
270 buffer.free_front(SIZEOF);
271 }
272 if (buffer.size() >= _size) {
273 const auto ret = _size;
274 _size = 0;
275 return ret;
276 }
277 return 0;
278 }
279
289 static _Size
290 Header(std::size_t size) noexcept {
291 if constexpr (SIZEOF == 2) {
292 return htons(static_cast<_Size>(size));
293 } else if constexpr (SIZEOF == 4) {
294 return htonl(static_cast<_Size>(size));
295 } else {
296 return size;
297 }
298 }
299
303 void
304 reset() noexcept final {
305 _size = 0;
306 }
307};
308
309} // namespace qb::protocol::base
310
311#endif // QB_IO_PROT_BASE_H_
Abstract base class for I/O-component-aware protocols (CRTP).
Definition protocol.h:146
_IO_ & _io
Reference to the I/O component instance that this protocol is associated with.
Definition protocol.h:166
static constexpr const char end
End character.
Definition base.h:54
virtual ~byte_terminated()=default
Virtual destructor.
static constexpr const std::size_t delimiter_size
Delimiter size (1 byte)
Definition base.h:52
std::size_t shiftSize(std::size_t const size) const noexcept
Calculates the message size without the delimiter.
Definition base.h:81
void reset() noexcept final
Resets the protocol state.
Definition base.h:111
std::size_t getMessageSize() noexcept final
Determines the size of the next complete message.
Definition base.h:93
byte_terminated()=delete
Default constructor (deleted)
byte_terminated(_IO_ &io) noexcept
Constructor with I/O reference.
Definition base.h:71
bytes_terminated()=delete
Default constructor (deleted)
void reset() noexcept final
Resets the protocol state.
Definition base.h:202
std::size_t shiftSize(std::size_t const size) const noexcept
Calculates the message size without the delimiter.
Definition base.h:167
static constexpr const std::size_t delimiter_size
Delimiter size.
Definition base.h:138
bytes_terminated(_IO_ &io) noexcept
Constructor with I/O reference.
Definition base.h:157
virtual ~bytes_terminated()=default
Virtual destructor.
std::size_t getMessageSize()
Determines the size of the next complete message.
Definition base.h:179
static constexpr const auto end
End sequence.
Definition base.h:140
size_as_header()=delete
Default constructor.
size_as_header(_IO_ &io) noexcept
Constructor with I/O reference.
Definition base.h:238
std::size_t getMessageSize() noexcept final
Determines the size of the next complete message.
Definition base.h:260
void reset() noexcept final
Resets the protocol state.
Definition base.h:304
static _Size Header(std::size_t size) noexcept
Creates a size header for a message.
Definition base.h:290
std::size_t shiftSize() const noexcept
Returns the size of the header.
Definition base.h:247
Protocol interfaces for message processing in the asynchronous IO framework.
Implementation of a dynamic extensible buffer.