libxr
Embedded C++ framework for bare-metal, RTOS and Linux
Loading...
Searching...
No Matches
LibXR::Topic::Server Class Reference

将字节流解析成 packet 并发布到已注册 topic 的状态机 / State machine that parses byte streams into packets and publishes them into registered topics More...

#include <server.hpp>

Public Types

enum class  Status : uint8_t { WAIT_START , WAIT_TOPIC , WAIT_DATA_CRC }
 parser 当前所在阶段 / Current stage of the parser More...
 

Public Member Functions

 Server (size_t buffer_length)
 构造 parser 并分配内部暂存队列 / Construct the parser and allocate its internal staging queue
 
void Register (TopicHandle topic)
 注册一个可接收 packet 的 topic / Register one topic that may receive parsed packets
 
size_t ParseData (ConstRawData data)
 在普通上下文里喂入一批新字节 / Feed one new byte batch in normal context
 
size_t ParseDataFromCallback (ConstRawData data, bool in_isr)
 在回调/ISR 路径里喂入一批新字节 / Feed one new byte batch in callback/ISR path
 

Detailed Description

将字节流解析成 packet 并发布到已注册 topic 的状态机 / State machine that parses byte streams into packets and publishes them into registered topics

把字节流解析成 packet 并投递到 topic 的 parser / Parser that turns byte streams into packets and delivers them into topics

Note
当前 Server 自己不维护 raw topic/cache 语义;它只依赖注册 topic 的: payload_size、payload_alignment 和名字 CRC 键。 The current Server does not restore old raw topic/cache semantics by itself; it relies only on each registered topic's payload_size, payload_alignment, and name CRC key.
当前兼容规则: 收到的 packet payload 短于 topic 固定大小时,仅保证前缀部分有效,后半段保持未定义; 长于 topic 固定大小时,仅保留前缀部分,其余字节直接截断。 Current compatibility rule: when an incoming packet payload is shorter than the topic's fixed size, only the leading prefix is guaranteed valid and the remaining tail stays unspecified; when it is longer, only the prefix matching the topic size is kept and the rest is truncated.

Definition at line 27 of file server.hpp.

Member Enumeration Documentation

◆ Status

enum class LibXR::Topic::Server::Status : uint8_t
strong

parser 当前所在阶段 / Current stage of the parser

Enumerator
WAIT_START 

正在找下一包前缀 / Searching for the next packet prefix.

WAIT_TOPIC 

已读到前缀,正在等完整头 / Prefix received; waiting for the full header.

WAIT_DATA_CRC 

头已接受,正在等 payload 和尾 CRC / Header accepted; waiting for payload plus trailing CRC.

Definition at line 34 of file server.hpp.

35 {
41 };
@ WAIT_START
正在找下一包前缀 / Searching for the next packet prefix.

Constructor & Destructor Documentation

◆ Server()

Topic::Server::Server ( size_t buffer_length)

构造 parser 并分配内部暂存队列 / Construct the parser and allocate its internal staging queue

Parameters
buffer_length内部字节队列和暂存缓冲区大小 / Size of the internal byte queue and staging buffer

Definition at line 12 of file server.cpp.

13 : topic_map_([](const uint32_t& a, const uint32_t& b) { return (a > b) - (a < b); }),
14 queue_(1, buffer_length)
15{
16 ASSERT(buffer_length > PACK_BASE_SIZE);
17 parse_buff_.size_ = buffer_length;
18 parse_buff_.addr_ =
19 new (std::align_val_t(LibXR::CACHE_LINE_SIZE)) uint8_t[parse_buff_.size_];
20}
size_t size_
数据字节数 / Data size in bytes
void * addr_
数据起始地址 / Data start address
constexpr size_t CACHE_LINE_SIZE
兼容旧代码的缓存行别名 / Backward-compatible cache-line alias for existing code
Definition libxr_def.hpp:65

Member Function Documentation

◆ ParseData()

size_t Topic::Server::ParseData ( ConstRawData data)

在普通上下文里喂入一批新字节 / Feed one new byte batch in normal context

Parameters
data新收到的原始字节 / Newly received raw bytes
Returns
成功解析并发布的包数量 / Number of packets parsed and published

Definition at line 35 of file server.cpp.

36{
37 return ParseDataRaw(data, false, false);
38}

◆ ParseDataFromCallback()

size_t Topic::Server::ParseDataFromCallback ( ConstRawData data,
bool in_isr )

在回调/ISR 路径里喂入一批新字节 / Feed one new byte batch in callback/ISR path

Parameters
data新收到的原始字节 / Newly received raw bytes
in_isr当前是否位于 ISR / Whether the current path is in ISR context
Returns
成功解析并发布的包数量 / Number of packets parsed and published

Definition at line 40 of file server.cpp.

41{
42 return ParseDataRaw(data, true, in_isr);
43}

◆ Register()

void Topic::Server::Register ( TopicHandle topic)

注册一个可接收 packet 的 topic / Register one topic that may receive parsed packets

Parameters
topic目标 topic 句柄 / Target topic handle
Note
注册时会断言该 topic 的 payload_size + PACK_BASE_SIZE 能放进本 server 的 暂存缓冲区。 Registration asserts that the topic's payload_size + PACK_BASE_SIZE fits in this server's staging buffer.
server 的内部暂存缓冲区在构造时固定按 CACHE_LINE_SIZE 对齐分配;注册时还会 断言该 topic 的 payload_alignment <= CACHE_LINE_SIZE。 The server's internal staging buffer is allocated once at CACHE_LINE_SIZE alignment during construction; registration also asserts that the topic's payload_alignment <= CACHE_LINE_SIZE.

Definition at line 22 of file server.cpp.

23{
24 ASSERT(topic != nullptr);
25 ASSERT(topic->data_.payload_size != 0);
26 ASSERT(topic->data_.payload_alignment != 0);
27 ASSERT(topic->data_.payload_alignment <= LibXR::CACHE_LINE_SIZE);
28
29 ASSERT(topic->data_.payload_size + PACK_BASE_SIZE <= parse_buff_.size_);
30
31 auto* node = new RBTree<uint32_t>::Node<TopicHandle>(topic);
32 topic_map_.Insert(*node, topic->key);
33}
红黑树实现,支持泛型键和值,并提供线程安全操作 Red-Black Tree implementation supporting generic keys and values with thread-...
Definition rbt.hpp:23
void Insert(BaseNode &node, KeyType &&key)
在树中插入新节点 / Insert a new node into the tree.
Definition rbt.hpp:235

The documentation for this class was generated from the following files: