Back to Site
Loading...
Searching...
No Matches
frame.h
Go to the documentation of this file.
1#pragma once
2
30
31#include "librats/util/rats_export.h"
32#include "librats/core/bytes.h"
33#include "librats/core/types.h"
34
35#include <cstdint>
36
37namespace librats {
38
40enum class MessageType : uint8_t {
41 App = 1,
42 Control = 2,
43 Gossip = 3,
45 Ping = 5,
46 Storage = 6,
47 Typed = 7,
48 Pex = 8,
49 Punch = 9,
50 Relay = 10,
51 Media = 11,
53 Remote = 13,
54};
55
59 uint8_t flags = 0;
60 uint16_t channel = 0;
61};
62
73
81 uint64_t tag = 0;
82 bool delivered = false;
83};
84
85namespace framer {
86
87constexpr size_t kLengthPrefixSize = 4;
88constexpr size_t kHeaderSize = 4;
89constexpr uint32_t kMaxBlockSize = 64u * 1024 * 1024;
90
91// ── Outer block (length-prefixed opaque body) ───────────────────────────────
92
94RATS_API void encode_block(Bytes& out, ByteView body);
95
99RATS_API void encode_block_header(uint8_t* out, size_t body_size);
100
101struct RATS_API Block {
102 enum Status { Ok, Incomplete, Error } status = Incomplete;
103 size_t consumed = 0;
105
111 size_t needed = 0;
112};
113
115RATS_API Block try_take_block(const uint8_t* data, size_t size);
116
117// ── Inner message (fixed header + payload, no length prefix) ─────────────────
118
120RATS_API void encode_header(uint8_t* out, FrameHeader header);
121
123RATS_API void encode_message(Bytes& out, FrameHeader header, ByteView payload);
124
125struct Message {
126 bool ok = false;
128};
129
132
133} // namespace framer
134} // 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:89
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).
void encode_header(uint8_t *out, FrameHeader header)
Write the kHeaderSize-byte [type][flags][channel] header at out.
constexpr size_t kLengthPrefixSize
Definition frame.h:87
constexpr size_t kHeaderSize
type+flags+channel
Definition frame.h:88
Message parse_message(ByteView inner)
Parse an inner message from inner (header + payload). ok false if short.
Definition node.h:75
std::vector< uint8_t > Bytes
Definition bytes.h:24
MessageType
Inner-message kind. Application traffic uses App, addressed by channel.
Definition frame.h:40
@ Punch
NAT hole-punch rendezvous, relayed peer→peer (HolePunch).
Definition frame.h:49
@ Pex
peer exchange — gossip of known peer addresses (PeerExchange)
Definition frame.h:48
@ Media
real-time media: live frames and their control plane (ratcast's MediaService)
Definition frame.h:51
@ Typed
typed JSON message exchange (MessageJson)
Definition frame.h:47
@ Storage
distributed key-value store (StorageManager)
Definition frame.h:46
@ Control
core control plane (peer exchange…)
Definition frame.h:42
@ Ping
liveness / RTT (PingService)
Definition frame.h:45
@ Rendezvous
meeting under a key at a server, which introduces the peers (Rendezvous)
Definition frame.h:52
@ Remote
remote control: input and the state that answers it, small and ordered (claimed by an application,...
Definition frame.h:53
Fixed header of an inner message.
Definition frame.h:57
MessageType type
Definition frame.h:58
uint16_t channel
interned application channel id (0 for non-App)
Definition frame.h:60
A decoded inner message. payload is a non-owning view into the source bytes.
Definition frame.h:64
Deadline deadline
A live message's deadline as its sender gave it, on this node's clock: when it arrived,...
Definition frame.h:71
ByteView payload
Definition frame.h:66
FrameHeader header
Definition frame.h:65
What became of a live message sent with a tag (SendOptions::tag), as far as its sender can tell.
Definition frame.h:79
MessageType type
Definition frame.h:80
ByteView body
the block body (when Ok); views the input
Definition frame.h:104
size_t needed
Total size of this block on the wire (prefix + body), known as soon as the prefix has been read; 0 wh...
Definition frame.h:111
size_t consumed
bytes to consume from the buffer (when Ok)
Definition frame.h:103