Back to Site
Loading...
Searching...
No Matches
frame.h
Go to the documentation of this file.
1#pragma once
2
31#include "core/bytes.h"
32
33#include <cstdint>
34
35namespace librats {
36
38enum class MessageType : uint8_t {
39 App = 1,
40 Control = 2,
41 Gossip = 3,
42 FileChunk = 4,
43 Ping = 5,
44 Storage = 6,
45 Typed = 7,
46 Pex = 8,
47};
48
52 uint8_t flags = 0;
53 uint16_t channel = 0;
54};
55
57struct Frame {
59 ByteView payload;
60};
61
62namespace framer {
63
64constexpr size_t kLengthPrefixSize = 4;
65constexpr size_t kHeaderSize = 4;
66constexpr uint32_t kMaxBlockSize = 64u * 1024 * 1024;
67
68// ── Outer block (length-prefixed opaque body) ───────────────────────────────
69
71void encode_block(Bytes& out, ByteView body);
72
73struct Block {
75 size_t consumed = 0;
76 ByteView body{};
77};
78
80Block try_take_block(const uint8_t* data, size_t size);
81
82// ── Inner message (fixed header + payload, no length prefix) ─────────────────
83
85void encode_message(Bytes& out, FrameHeader header, ByteView payload);
86
87struct Message {
88 bool ok = false;
90};
91
93Message parse_message(ByteView inner);
94
95} // namespace framer
96} // namespace librats
void encode_block(Bytes &out, ByteView body)
Append [u32 len][body] to out.
constexpr uint32_t kMaxBlockSize
body cap
Definition frame.h:66
Block try_take_block(const uint8_t *data, size_t size)
Try to take one block from the front of [data, data+size) without copying.
void encode_message(Bytes &out, FrameHeader header, ByteView payload)
Append [type][flags][channel][payload] to out (no length prefix).
constexpr size_t kLengthPrefixSize
Definition frame.h:64
constexpr size_t kHeaderSize
type+flags+channel
Definition frame.h:65
Message parse_message(ByteView inner)
Parse an inner message from inner (header + payload). ok false if short.
Definition node.h:65
MessageType
Inner-message kind. Application traffic uses App, addressed by channel.
Definition frame.h:38
@ Pex
peer exchange — gossip of known peer addresses (PeerExchange)
@ Typed
typed JSON message exchange (MessageJson)
@ Storage
distributed key-value store (StorageManager)
@ Control
core control plane (peer exchange…)
@ Ping
liveness / RTT (PingService)
Fixed header of an inner message.
Definition frame.h:50
MessageType type
Definition frame.h:51
uint16_t channel
interned application channel id (0 for non-App)
Definition frame.h:53
A decoded inner message. payload is a non-owning view into the source bytes.
Definition frame.h:57
ByteView payload
Definition frame.h:59
FrameHeader header
Definition frame.h:58
ByteView body
the block body (when Ok); views the input
Definition frame.h:76
size_t consumed
bytes to consume from the buffer (when Ok)
Definition frame.h:75
enum librats::framer::Block::Status status