Back to Site
Loading...
Searching...
No Matches
frame.h
Go to the documentation of this file.
1#pragma once
2
31#include "util/rats_export.h"
32#include "core/bytes.h"
33
34#include <cstdint>
35
36namespace librats {
37
39enum class MessageType : uint8_t {
40 App = 1,
41 Control = 2,
42 Gossip = 3,
43 FileChunk = 4,
44 Ping = 5,
45 Storage = 6,
46 Typed = 7,
47 Pex = 8,
48};
49
53 uint8_t flags = 0;
54 uint16_t channel = 0;
55};
56
58struct Frame {
60 ByteView payload;
61};
62
63namespace framer {
64
65constexpr size_t kLengthPrefixSize = 4;
66constexpr size_t kHeaderSize = 4;
67constexpr uint32_t kMaxBlockSize = 64u * 1024 * 1024;
68
69// ── Outer block (length-prefixed opaque body) ───────────────────────────────
70
72RATS_API void encode_block(Bytes& out, ByteView body);
73
77RATS_API void encode_block_header(uint8_t* out, size_t body_size);
78
79struct RATS_API Block {
80 enum Status { Ok, Incomplete, Error } status = Incomplete;
81 size_t consumed = 0;
82 ByteView body{};
83
89 size_t needed = 0;
90};
91
93RATS_API Block try_take_block(const uint8_t* data, size_t size);
94
95// ── Inner message (fixed header + payload, no length prefix) ─────────────────
96
98RATS_API void encode_message(Bytes& out, FrameHeader header, ByteView payload);
99
100struct Message {
101 bool ok = false;
103};
104
106RATS_API Message parse_message(ByteView inner);
107
108} // namespace framer
109} // namespace librats
void encode_block(Bytes &out, ByteView body)
Append [u32 len][body] to out.
void encode_block_header(uint8_t *out, size_t body_size)
Write just the [u32 len] prefix into out (exactly kLengthPrefixSize bytes).
constexpr uint32_t kMaxBlockSize
body cap
Definition frame.h:67
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:65
constexpr size_t kHeaderSize
type+flags+channel
Definition frame.h:66
Message parse_message(ByteView inner)
Parse an inner message from inner (header + payload). ok false if short.
Definition node.h:66
MessageType
Inner-message kind. Application traffic uses App, addressed by channel.
Definition frame.h:39
@ 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:51
MessageType type
Definition frame.h:52
uint16_t channel
interned application channel id (0 for non-App)
Definition frame.h:54
A decoded inner message. payload is a non-owning view into the source bytes.
Definition frame.h:58
ByteView payload
Definition frame.h:60
FrameHeader header
Definition frame.h:59