6 : topic_map_([](const uint32_t& a, const uint32_t& b)
7 {
return static_cast<int>(a) -
static_cast<int>(b); }),
8 queue_(1, buffer_length)
11 ASSERT(buffer_length > PACK_BASE_SIZE);
12 parse_buff_.size_ = buffer_length;
13 parse_buff_.addr_ =
new uint8_t[buffer_length];
19 topic_map_.Insert(*node, topic->
key);
24 return ParseDataRaw(data,
false,
false);
29 return ParseDataRaw(data,
true, in_isr);
32size_t Topic::Server::ParseDataRaw(
ConstRawData data,
bool from_callback,
bool in_isr)
42 if (status_ == Status::WAIT_START && !SyncToPacketStart())
47 if (status_ == Status::WAIT_TOPIC && !ReadHeader())
52 if (status_ == Status::WAIT_DATA_CRC)
54 switch (ReadPayload(from_callback, in_isr))
56 case ParseResult::NEED_MORE:
58 case ParseResult::DROPPED:
60 case ParseResult::DELIVERED:
69bool Topic::Server::SyncToPacketStart()
71 auto queue_size = queue_.Size();
72 for (uint32_t i = 0; i < queue_size; i++)
76 if (prefix == PACKET_PREFIX)
78 status_ = Status::WAIT_TOPIC;
87bool Topic::Server::ReadHeader()
89 if (queue_.Size() <
sizeof(PackedDataHeader))
94 queue_.PopBatch(parse_buff_.addr_,
sizeof(PackedDataHeader));
95 if (!
CRC8::Verify(parse_buff_.addr_,
sizeof(PackedDataHeader)))
101 auto header =
reinterpret_cast<PackedDataHeader*
>(parse_buff_.addr_);
102 auto node = topic_map_.Search<
TopicHandle>(header->topic_name_crc32);
109 data_len_ = header->GetDataLen();
110 current_timestamp_ = header->GetTimestamp();
111 current_topic_ = *node;
112 if (data_len_ + PACK_BASE_SIZE > queue_.length_)
118 status_ = Status::WAIT_DATA_CRC;
122Topic::Server::ParseResult Topic::Server::ReadPayload(
bool from_callback,
bool in_isr)
124 if (queue_.Size() < data_len_ +
sizeof(uint8_t))
126 return ParseResult::NEED_MORE;
129 auto data =
reinterpret_cast<uint8_t*
>(parse_buff_.addr_) +
sizeof(PackedDataHeader);
130 queue_.PopBatch(data, data_len_ +
sizeof(uint8_t));
133 data_len_ +
sizeof(PackedDataHeader) +
sizeof(uint8_t)))
136 return ParseResult::DROPPED;
139 if (data_len_ > current_topic_->data_.max_length)
141 data_len_ = current_topic_->data_.max_length;
143 auto topic =
Topic(current_topic_);
146 topic.PublishFromCallback(data, data_len_, current_timestamp_, in_isr);
150 topic.Publish(data, data_len_, current_timestamp_);
153 return ParseResult::DELIVERED;
156void Topic::Server::ResetParser()
158 status_ = Status::WAIT_START;
160 current_topic_ =
nullptr;
static bool Verify(const void *raw, size_t len)
验证数据的 CRC8 校验码 / Verifies the CRC8 checksum of the given data
只读原始数据视图 / Immutable raw data view
size_t size_
数据字节数 / Data size in bytes
const void * addr_
数据起始地址 / Data start address
微秒时间戳 / Microsecond timestamp
Key key
节点键值 (Key associated with the node).
红黑树的泛型数据节点,继承自 BaseNode (Generic data node for Red-Black Tree, inheriting from BaseNode).
红黑树实现,支持泛型键和值,并提供线程安全操作 (Red-Black Tree implementation supporting generic keys and values with thread...
void Register(TopicHandle topic)
注册一个主题 Registers a topic
size_t ParseData(ConstRawData data)
解析接收到的数据 Parses received data
size_t ParseDataFromCallback(ConstRawData data, bool in_isr)
在回调函数中解析接收到的数据 Parses received data in a callback
Server(size_t buffer_length)
构造函数,初始化服务器并分配缓冲区 Constructor to initialize the server and allocate buffer
RBTree< uint32_t >::Node< Block > * TopicHandle
主题句柄,指向存储数据的红黑树节点。Handle pointing to a red-black tree node storing data.
Topic()
默认构造函数,创建一个空的 Topic 实例 Default constructor, creates an empty Topic instance