Back to Site
Loading...
Searching...
No Matches
frame.h File Reference

Two-level wire framing: outer length-prefixed blocks, inner messages. More...

#include "core/bytes.h"
#include <cstdint>
Include dependency graph for frame.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  librats::FrameHeader
 Fixed header of an inner message. More...
 
struct  librats::Frame
 A decoded inner message. payload is a non-owning view into the source bytes. More...
 
struct  librats::framer::Block
 
struct  librats::framer::Message
 

Namespaces

namespace  librats
 
namespace  librats::framer
 

Enumerations

enum class  librats::MessageType : uint8_t {
  librats::App = 1 , librats::Control = 2 , librats::Gossip = 3 , librats::FileChunk = 4 ,
  librats::Ping = 5 , librats::Storage = 6 , librats::Typed = 7 , librats::Pex = 8
}
 Inner-message kind. Application traffic uses App, addressed by channel. More...
 

Functions

void librats::framer::encode_block (Bytes &out, ByteView body)
 Append [u32 len][body] to out.
 
Block librats::framer::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 librats::framer::encode_message (Bytes &out, FrameHeader header, ByteView payload)
 Append [type][flags][channel][payload] to out (no length prefix).
 
Message librats::framer::parse_message (ByteView inner)
 Parse an inner message from inner (header + payload). ok false if short.
 

Variables

constexpr size_t librats::framer::kLengthPrefixSize = 4
 
constexpr size_t librats::framer::kHeaderSize = 4
 type+flags+channel
 
constexpr uint32_t librats::framer::kMaxBlockSize = 64u * 1024 * 1024
 body cap
 

Detailed Description

Two-level wire framing: outer length-prefixed blocks, inner messages.

The wire is a stream of length-prefixed blocks:

┌──────────────┬───────────────────────────┐ │ length (u32) │ body │ │ 4 bytes │ length bytes │ └──────────────┴───────────────────────────┘

A block's body is opaque at this layer. The Connection uses it for two things:

  • during the handshake, the body is a raw handshake message (Noise / id);
  • once established, the body is the Session-encrypted bytes of an inner message, which has its own fixed 4-byte header:

    ┌──────┬───────┬─────────┬───────────────┐ │ type │ flags │ channel │ payload … │ │ u8 │ u8 │ u16 │ │ └──────┴───────┴─────────┴───────────────┘

Splitting "outer block" from "inner message" keeps encryption clean: the cipher wraps the whole inner message (type included), and the block layer never needs to understand it. Decoding is zero-copy — views point into the caller's buffer and are valid only until it is consumed.

Definition in file frame.h.