libxr  1.0
Want to be the best embedded framework
Loading...
Searching...
No Matches
LibXR::Topic::Server Class Reference

服务器类,负责解析数据并将其分发到相应的主题 Server class responsible for parsing data and distributing it to corresponding topics More...

#include <message.hpp>

Collaboration diagram for LibXR::Topic::Server:
[legend]

Public Types

enum class  Status : uint8_t { WAIT_START , WAIT_TOPIC , WAIT_DATA_CRC }
 服务器解析状态枚举 Enumeration of server parsing states More...
 

Public Member Functions

 Server (size_t buffer_length)
 构造函数,初始化服务器并分配缓冲区 Constructor to initialize the server and allocate buffer
 
void Register (TopicHandle topic)
 注册一个主题 Registers a topic
 
size_t ParseData (ConstRawData data)
 解析接收到的数据 Parses received data
 

Private Attributes

Status status_
 服务器的当前解析状态 Current parsing state of the server
 
uint32_t data_len_ = 0
 当前数据长度 Current data length
 
RBTree< uint32_t > topic_map_
 主题映射表 Topic mapping table
 
BaseQueue queue_
 数据队列 Data queue
 
RawData parse_buff_
 解析数据缓冲区 Data buffer for parsing
 
TopicHandle current_topic_ = nullptr
 当前主题句柄 Current topic handle
 

Detailed Description

服务器类,负责解析数据并将其分发到相应的主题 Server class responsible for parsing data and distributing it to corresponding topics

Definition at line 722 of file message.hpp.

Member Enumeration Documentation

◆ Status

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

服务器解析状态枚举 Enumeration of server parsing states

Enumerator
WAIT_START 

等待起始标志 Waiting for start flag

WAIT_TOPIC 

等待主题信息 Waiting for topic information

WAIT_DATA_CRC 

等待数据校验 Waiting for data CRC validation

Definition at line 730 of file message.hpp.

731 {
732 WAIT_START,
733 WAIT_TOPIC,
735 };
@ WAIT_DATA_CRC
等待数据校验 Waiting for data CRC validation
@ WAIT_START
等待起始标志 Waiting for start flag
@ WAIT_TOPIC
等待主题信息 Waiting for topic information

Constructor & Destructor Documentation

◆ Server()

Topic::Server::Server ( size_t buffer_length)

构造函数,初始化服务器并分配缓冲区 Constructor to initialize the server and allocate buffer

Parameters
buffer_length缓冲区长度 Buffer length

Definition at line 399 of file message.cpp.

400 : topic_map_([](const uint32_t &a, const uint32_t &b)
401 { return static_cast<int>(a) - static_cast<int>(b); }),
402 queue_(1, buffer_length)
403{
404 /* Minimum size: header8 + crc32 + length24 + crc8 + data + crc8 = 10 */
405 ASSERT(buffer_length > PACK_BASE_SIZE);
406 parse_buff_.size_ = buffer_length;
407 parse_buff_.addr_ = new uint8_t[buffer_length];
408}
size_t size_
数据大小(字节)。 The size of the data (in bytes).
void * addr_
数据存储地址。 The storage address of the data.
RBTree< uint32_t > topic_map_
主题映射表 Topic mapping table
Definition message.hpp:763
BaseQueue queue_
数据队列 Data queue
Definition message.hpp:764
RawData parse_buff_
解析数据缓冲区 Data buffer for parsing
Definition message.hpp:765

Member Function Documentation

◆ ParseData()

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

解析接收到的数据 Parses received data

Parameters
data接收到的原始数据 Received raw data
Returns
接收到的话题数量 Received topic count

Definition at line 416 of file message.cpp.

417{
418 size_t count = 0;
419
420 queue_.PushBatch(data.addr_, data.size_);
421
422 while (true)
423 { /* 1. Check prefix */
425 {
426 /* Check start frame */
427 auto queue_size = queue_.Size();
428 for (uint32_t i = 0; i < queue_size; i++)
429 {
430 uint8_t prefix = 0;
431 queue_.Peek(&prefix);
432 if (prefix == 0xa5)
433 {
435 break;
436 }
437 queue_.Pop();
438 }
439 /* Not found */
441 {
442 return count;
443 }
444 }
445
446 /* 2. Get topic info */
448 {
449 /* Check size&crc */
450 if (queue_.Size() >= sizeof(PackedDataHeader))
451 {
452 queue_.PopBatch(parse_buff_.addr_, sizeof(PackedDataHeader));
453 if (CRC8::Verify(parse_buff_.addr_, sizeof(PackedDataHeader)))
454 {
455 auto header = reinterpret_cast<PackedDataHeader *>(parse_buff_.addr_);
456 /* Find topic */
457 auto node = topic_map_.Search<TopicHandle>(header->topic_name_crc32);
458 if (node)
459 {
460 data_len_ = header->GetDataLen();
461 current_topic_ = *node;
462 if (data_len_ + PACK_BASE_SIZE >= queue_.length_)
463 {
465 continue;
466 }
468 }
469 else
470 {
472 continue;
473 }
474 }
475 else
476 {
478 continue;
479 }
480 }
481 else
482 {
483 return count;
484 }
485 }
486
487 /* 3. Get data */
489 {
490 /* Check size&crc */
491 if (queue_.Size() >= data_len_ + sizeof(uint8_t))
492 {
493 uint8_t *data =
494 reinterpret_cast<uint8_t *>(parse_buff_.addr_) + sizeof(PackedDataHeader);
495 queue_.PopBatch(data, data_len_ + sizeof(uint8_t));
497 data_len_ + sizeof(PackedDataHeader) + sizeof(uint8_t)))
498 {
500 auto data =
501 reinterpret_cast<uint8_t *>(parse_buff_.addr_) + sizeof(PackedDataHeader);
502 if (data_len_ > current_topic_->data_.max_length)
503 {
504 data_len_ = current_topic_->data_.max_length;
505 }
506 Topic(current_topic_).Publish(data, data_len_);
507
508 count++;
509
510 continue;
511 }
512 else
513 {
515 continue;
516 }
517 }
518 else
519 {
520 return count;
521 }
522 }
523 }
524 return count;
525}
ErrorCode Peek(void *data)
获取队列头部的元素但不移除 (Peek at the front element without removing it).
Definition queue.cpp:54
size_t length_
队列最大容量 (Maximum queue capacity).
Definition queue.hpp:154
ErrorCode PushBatch(const void *data, size_t size)
批量推入多个元素 (Push multiple elements into the queue).
Definition queue.cpp:103
size_t Size() const
获取队列中的元素个数 (Get the number of elements in the queue).
Definition queue.cpp:210
ErrorCode Pop(void *data=nullptr)
移除队列头部的元素 (Pop the front element from the queue).
Definition queue.cpp:69
ErrorCode PopBatch(void *data, size_t size)
批量移除多个元素 (Pop multiple elements from the queue).
Definition queue.cpp:132
static bool Verify(const void *raw, size_t len)
验证数据的 CRC8 校验码 / Verifies the CRC8 checksum of the given data
Definition crc.hpp:91
size_t size_
数据大小(字节)。 The size of the data (in bytes).
const void * addr_
数据存储地址(常量)。 The storage address of the data (constant).
Data data_
存储的数据 (Stored data).
Definition rbt.hpp:99
Node< Data > * Search(const Key &key)
搜索红黑树中的节点 (Search for a node in the Red-Black Tree).
Definition rbt.hpp:122
TopicHandle current_topic_
当前主题句柄 Current topic handle
Definition message.hpp:766
uint32_t data_len_
当前数据长度 Current data length
Definition message.hpp:762
Status status_
服务器的当前解析状态 Current parsing state of the server
Definition message.hpp:760
RBTree< uint32_t >::Node< Block > * TopicHandle
主题句柄,指向存储数据的红黑树节点。Handle pointing to a red-black tree node storing data.
Definition message.hpp:145
Topic()
默认构造函数,创建一个空的 Topic 实例 Default constructor, creates an empty Topic instance
Definition message.cpp:127

◆ Register()

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

注册一个主题 Registers a topic

Parameters
topic需要注册的主题句柄 The topic handle to register

Definition at line 410 of file message.cpp.

411{
412 auto node = new RBTree<uint32_t>::Node<TopicHandle>(topic);
413 topic_map_.Insert(*node, topic->key);
414}
红黑树实现,支持泛型键和值,并提供线程安全操作 (Red-Black Tree implementation supporting generic keys and values with thread...
Definition rbt.hpp:24
void Insert(BaseNode &node, KeyType &&key)
在树中插入新节点 (Insert a new node into the tree).
Definition rbt.hpp:237

Field Documentation

◆ current_topic_

TopicHandle LibXR::Topic::Server::current_topic_ = nullptr
private

当前主题句柄 Current topic handle

Definition at line 766 of file message.hpp.

◆ data_len_

uint32_t LibXR::Topic::Server::data_len_ = 0
private

当前数据长度 Current data length

Definition at line 762 of file message.hpp.

◆ parse_buff_

RawData LibXR::Topic::Server::parse_buff_
private

解析数据缓冲区 Data buffer for parsing

Definition at line 765 of file message.hpp.

◆ queue_

BaseQueue LibXR::Topic::Server::queue_
private

数据队列 Data queue

Definition at line 764 of file message.hpp.

◆ status_

Status LibXR::Topic::Server::status_
private
Initial value:

服务器的当前解析状态 Current parsing state of the server

Definition at line 760 of file message.hpp.

◆ topic_map_

RBTree<uint32_t> LibXR::Topic::Server::topic_map_
private

主题映射表 Topic mapping table

Definition at line 763 of file message.hpp.


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