Back to Site
Loading...
Searching...
No Matches
frame.h
Go to the documentation of this file.
1#pragma once
2
31#include "librats/util/rats_export.h"
32#include "librats/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 Punch = 9,
49 Relay = 10,
50};
51
55 uint8_t flags = 0;
56 uint16_t channel = 0;
57};
58
64
65namespace framer {
66
67constexpr size_t kLengthPrefixSize = 4;
68constexpr size_t kHeaderSize = 4;
69constexpr uint32_t kMaxBlockSize = 64u * 1024 * 1024;
70
71// ── Outer block (length-prefixed opaque body) ───────────────────────────────
72
74RATS_API void encode_block(Bytes& out, ByteView body);
75
79RATS_API void encode_block_header(uint8_t* out, size_t body_size);
80
81struct RATS_API Block {
82 enum Status { Ok, Incomplete, Error } status = Incomplete;
83 size_t consumed = 0;
84 ByteView body{};
85
91 size_t needed = 0;
92};
93
95RATS_API Block try_take_block(const uint8_t* data, size_t size);
96
97// ── Inner message (fixed header + payload, no length prefix) ─────────────────
98
100RATS_API void encode_message(Bytes& out, FrameHeader header, ByteView payload);
101
102struct Message {
103 bool ok = false;
105};
106
109
110} // namespace framer
111} // namespace librats
Lightweight byte container aliases and a non-owning byte view.
Non-owning view over a contiguous run of bytes.
Definition bytes.h:27
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:69
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:67
constexpr size_t kHeaderSize
type+flags+channel
Definition frame.h:68
Message parse_message(ByteView inner)
Parse an inner message from inner (header + payload). ok false if short.
Definition node.h:73
MessageType
Inner-message kind. Application traffic uses App, addressed by channel.
Definition frame.h:39
@ Punch
NAT hole-punch rendezvous, relayed peer→peer (HolePunch)
@ 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)
std::vector< uint8_t > Bytes
Definition bytes.h:24
Fixed header of an inner message.
Definition frame.h:53
MessageType type
Definition frame.h:54
uint16_t channel
interned application channel id (0 for non-App)
Definition frame.h:56
A decoded inner message. payload is a non-owning view into the source bytes.
Definition frame.h:60
ByteView payload
Definition frame.h:62
FrameHeader header
Definition frame.h:61