26#ifndef QB_IO_PROT_BASE_H_
27#define QB_IO_PROT_BASE_H_
32namespace qb::protocol::base {
47template <
typename _IO_,
char _EndByte = '\0'>
49 std::size_t _offset = 0u;
54 constexpr static const char end = _EndByte;
94 const auto &buffer = this->
_io.in();
95 auto it = buffer.begin() + _offset;
96 while (it != buffer.end()) {
97 if (*it == _EndByte) {
103 _offset = it - buffer.begin();
131template <
typename _IO_,
typename _Trait>
133 constexpr static const std::size_t _SizeBytes =
134 sizeof(_Trait::_EndBytes) - 1;
135 std::size_t _offset = 0u;
140 constexpr static const auto end = _Trait::_EndBytes;
180 const auto &buffer = this->_in_buffer;
181 auto i = buffer.begin() + _offset;
183 if ((buffer.end() - i) < _SizeBytes)
186 const auto end = buffer.end() - _SizeBytes + 1;
188 if (!std::memcmp(i, _Trait::_EndBytes, _SizeBytes)) {
190 return i - buffer.begin() + _SizeBytes;
194 _offset = i - buffer.begin();
222template <
typename _IO_,
typename _Size = u
int16_t>
224 constexpr static const std::size_t SIZEOF =
sizeof(_Size);
261 auto &buffer = this->
_io.in();
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()));
269 _size = *
reinterpret_cast<_Size *
>(buffer.begin());
270 buffer.free_front(SIZEOF);
272 if (buffer.size() >= _size) {
273 const auto ret = _size;
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));
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
Protocol interfaces for message processing in the asynchronous IO framework.
Implementation of a dynamic extensible buffer.