8using Message = std::span<const uint8_t>;
17template <
typename Sink>
58 return {
reinterpret_cast<const char*
>(message.data()), message.size() };
81 [[nodiscard]]
inline uint32_t
load_be_u32(
const uint8_t* src)
noexcept
83 return (uint32_t { src[0] } << 24) | (uint32_t { src[1] } << 16)
84 | (uint32_t { src[2] } << 8) | uint32_t { src[3] };
89 out.push_back(
static_cast<uint8_t
>(value >> 24));
90 out.push_back(
static_cast<uint8_t
>(value >> 16));
91 out.push_back(
static_cast<uint8_t
>(value >> 8));
92 out.push_back(
static_cast<uint8_t
>(value));
119 template <MessageSink Sink>
124 while (!bytes.empty()) {
125 const auto end = std::ranges::find(bytes,
static_cast<uint8_t
>(
'\n'));
126 const bool complete = end != bytes.end();
127 const auto length =
static_cast<size_t>(end - bytes.begin());
128 const Message segment = bytes.first(length);
129 bytes = complete ? bytes.subspan(length + 1) : bytes.subspan(length);
150 deliver(segment, sink, report);
172 template <
typename Sink>
175 if (!line.empty() && line.back() ==
'\r') {
176 line = line.first(line.size() - 1);
211 template <MessageSink Sink>
220 while (!bytes.empty()) {
225 bytes = bytes.subspan(take);
257 m_payload.insert(
m_payload.end(), bytes.begin(), bytes.begin() +
static_cast<std::ptrdiff_t
>(take));
258 bytes = bytes.subspan(take);
297 report.fault = fault;
320 if (std::ranges::find(payload,
static_cast<uint8_t
>(
'\n')) != payload.end()) {
323 if (!payload.empty() && payload.back() ==
'\r') {
326 out.insert(out.end(), payload.begin(), payload.end());
327 out.push_back(
static_cast<uint8_t
>(
'\n'));
340 const size_t limit = std::min<size_t>(max_payload, std::numeric_limits<uint32_t>::max());
341 if (payload.size() > limit || (payload.empty() && reject_empty)) {
345 out.insert(out.end(), payload.begin(), payload.end());
356 template <MessageSink Sink>
360 if (!bytes.empty()) {
388 m_terminator.push_back(static_cast<uint8_t>(
'\n'));
393 :
DelimiterFramer(
std::span<const uint8_t> {
reinterpret_cast<const uint8_t*
>(terminator.data()), terminator.size() }, max_message)
397 template <MessageSink Sink>
403 for (
const uint8_t
byte : bytes) {
468 if (terminator.empty()) {
472 std::vector<uint8_t> joined(payload.begin(), payload.end());
473 joined.insert(joined.end(), terminator.begin(), terminator.end());
474 const auto hit = std::ranges::search(joined, terminator);
475 if (
static_cast<size_t>(hit.begin() - joined.begin()) != payload.size()) {
479 out.insert(out.end(), joined.begin(), joined.end());
492 while (index < frame.size()) {
493 const size_t code = frame[index++];
497 const size_t run = code - 1;
498 if (index +
run > frame.size()) {
501 for (
size_t i = 0; i <
run; ++i) {
502 if (frame[index + i] == 0) {
506 out.insert(out.end(), frame.begin() +
static_cast<std::ptrdiff_t
>(index),
507 frame.begin() +
static_cast<std::ptrdiff_t
>(index +
run));
509 if (code != 0xFF && index < frame.size()) {
537 template <MessageSink Sink>
542 for (
const uint8_t
byte : bytes) {
610 const auto begin = [&] {
616 const auto close = [&] {
622 for (
const uint8_t
byte : payload) {
632 if (++code == 0xFF) {
661 template <MessageSink Sink>
671 report.
fault = fault;
674 for (
const uint8_t
byte : bytes) {
678 const bool has_message = !
m_pending.empty();
685 }
else if (!was_discarding && has_message) {
699 uint8_t value = byte;
710 }
else if (
byte ==
escape) {
736 static constexpr uint8_t
end = 0xC0;
755 for (
const uint8_t
byte : payload) {
791 [[nodiscard]] std::chrono::microseconds
gap() const noexcept {
return m_gap; }
793 template <MessageSink Sink>
814 template <MessageSink Sink>
820 if (was_discarding ||
m_pending.empty()) {
size_t buffered() const noexcept
Encoded bytes held for a frame that has not ended.
std::vector< uint8_t > m_pending
void reset() noexcept
Drop any partial frame and any discard in progress.
FrameReport push(Message bytes, Sink &&sink)
std::vector< uint8_t > m_decoded
CobsFramer(size_t max_frame=default_max_line) noexcept
std::vector< uint8_t > m_pending
FrameReport push(Message bytes, Sink &&sink)
void reset() noexcept
Drop any partial message and any discard in progress.
std::vector< uint8_t > m_terminator
size_t buffered() const noexcept
Bytes held for a message that has not ended.
bool ends_with_terminator() const
DelimiterFramer(std::span< const uint8_t > terminator, size_t max_message=default_max_line)
DelimiterFramer(std::string_view terminator, size_t max_message=default_max_line)
std::chrono::microseconds m_gap
IdleGapFramer(std::chrono::microseconds gap, size_t max_message=default_max_line) noexcept
void reset() noexcept
Drop any partial message and any discard in progress.
std::vector< uint8_t > m_pending
FrameReport push(Message bytes, Sink &&)
size_t buffered() const noexcept
Bytes held for a message that has not ended.
FrameReport flush(Sink &&sink)
End the message in progress, delivering it, or ending a discard.
std::chrono::microseconds gap() const noexcept
How long the line must stay quiet before the caller calls flush().
LengthPrefixFramer(size_t max_payload=default_max_frame, bool reject_empty=false) noexcept
static constexpr size_t header_size
FrameReport push(Message bytes, Sink &&sink)
void reset() noexcept
Drop partial input and clear a terminal fault.
bool broken() const noexcept
True after a terminal fault, until reset().
std::array< uint8_t, header_size > m_header
FrameReport fail(FrameReport &report, FrameFault fault) noexcept
std::vector< uint8_t > m_payload
size_t buffered() const noexcept
Partial header bytes plus payload bytes held for a frame that has not completed.
FrameReport push(Message bytes, Sink &&sink)
std::vector< uint8_t > m_pending
static void deliver(Message line, Sink &sink, FrameReport &report)
size_t buffered() const noexcept
Bytes held for a line that has not ended.
LineFramer(size_t max_line=default_max_line) noexcept
void reset() noexcept
Drop any partial line and any discard in progress.
static constexpr uint8_t escaped_end
size_t buffered() const noexcept
Decoded bytes held for a frame that has not ended.
std::vector< uint8_t > m_pending
FrameReport push(Message bytes, Sink &&sink)
static constexpr uint8_t end
static constexpr uint8_t escaped_escape
void reset() noexcept
Drop any partial frame and any discard in progress.
static constexpr uint8_t escape
SlipFramer(size_t max_message=default_max_line) noexcept
ClearOnExit & operator=(ClearOnExit &&)=delete
ClearOnExit(ClearOnExit &&)=delete
ClearOnExit(std::vector< uint8_t > &buffer) noexcept
ClearOnExit & operator=(const ClearOnExit &)=delete
ClearOnExit(const ClearOnExit &)=delete
std::vector< uint8_t > & m_buffer
A callable that receives each complete message.
bool cobs_decode(Message frame, std::vector< uint8_t > &out)
Decode one COBS frame, without its zero delimiter.
void append_be_u32(std::vector< uint8_t > &out, uint32_t value)
uint32_t load_be_u32(const uint8_t *src) noexcept
bool encode_line(Message payload, std::vector< uint8_t > &out)
Append a payload and its LF to out, in the form LineFramer reads back.
void encode_cobs(Message payload, std::vector< uint8_t > &out)
Append a payload in COBS, followed by its zero delimiter, to out.
void encode_slip(Message payload, std::vector< uint8_t > &out)
Append a payload in SLIP, followed by 0xC0, to out.
bool encode_length_prefixed(Message payload, std::vector< uint8_t > &out, size_t max_payload=default_max_frame, bool reject_empty=false)
Append a length prefix and a payload to out, in the form LengthPrefixFramer reads back.
bool encode_delimited(Message payload, Message terminator, std::vector< uint8_t > &out)
Append a payload and a terminator to out, in the form DelimiterFramer reads back.
std::span< const uint8_t > Message
One complete message.
constexpr size_t default_max_line
constexpr size_t default_max_frame
FrameFault
Why input was refused.
std::string_view as_text(Message message) noexcept
View a message as text without copying.
FrameReport push(Message bytes, Sink &&sink)
The framer for raw streams: every push is one message, with nothing to reassemble.