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:

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_ttopic_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 949 of file message.hpp.

Member Enumeration Documentation

◆ Status

服务器解析状态枚举 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 957 of file message.hpp.

958 {
959 WAIT_START,
960 WAIT_TOPIC,
962 };
@ WAIT_DATA_CRC
等待数据校验 Waiting for data CRC validation
@ WAIT_START
等待起始标志 Waiting for start flag
@ WAIT_TOPIC
等待主题信息 Waiting for topic information

Constructor & Destructor Documentation

◆ Server()

LibXR::Topic::Server::Server ( size_t  buffer_length)
inline

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

Parameters
buffer_length缓冲区长度 Buffer length

Definition at line 969 of file message.hpp.

970 : topic_map_([](const uint32_t &a, const uint32_t &b)
971 { return static_cast<int>(a) - static_cast<int>(b); }),
973 {
974 /* Minimum size: header8 + crc32 + length24 + crc8 + data + crc8 = 10 */
975 ASSERT(buffer_length >= PACK_BASE_SIZE);
978 }
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:1102
BaseQueue queue_
数据队列 Data queue
Definition message.hpp:1103
RawData parse_buff_
解析数据缓冲区 Data buffer for parsing
Definition message.hpp:1104
constexpr auto min(T1 a, T2 b) -> typename std::common_type< T1, T2 >::type
计算两个数的最小值

Member Function Documentation

◆ ParseData()

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

解析接收到的数据 Parses received data

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

Definition at line 997 of file message.hpp.

998 {
999 size_t count = 0;
1000
1001 queue_.PushBatch(data.addr_, data.size_);
1002
1004 /* 1. Check prefix */
1006 {
1007 /* Check start frame */
1008 auto queue_size = queue_.Size();
1009 for (uint32_t i = 0; i < queue_size; i++)
1010 {
1011 uint8_t prefix = 0;
1012 queue_.Peek(&prefix);
1013 if (prefix == 0xa5)
1014 {
1016 break;
1017 }
1018 queue_.Pop();
1019 }
1020 /* Not found */
1022 {
1023 return count;
1024 }
1025 }
1026
1027 /* 2. Get topic info */
1029 {
1030 /* Check size&crc */
1031 if (queue_.Size() >= sizeof(PackedDataHeader))
1032 {
1035 {
1036 auto header = reinterpret_cast<PackedDataHeader *>(parse_buff_.addr_);
1037 /* Find topic */
1038 auto node = topic_map_.Search<TopicHandle>(header->topic_name_crc32);
1039 if (node)
1040 {
1041 data_len_ = header->data_len;
1044 }
1045 else
1046 {
1048 goto check_start; // NOLINT
1049 }
1050 }
1051 else
1052 {
1054 goto check_start; // NOLINT
1055 }
1056 }
1057 else
1058 {
1059 return count;
1060 }
1061 }
1062
1064 {
1065 /* Check size&crc */
1066 if (queue_.Size() >= data_len_ + sizeof(uint8_t))
1067 {
1068 uint8_t *data =
1069 reinterpret_cast<uint8_t *>(parse_buff_.addr_) + sizeof(PackedDataHeader);
1070 queue_.PopBatch(data, data_len_ + sizeof(uint8_t));
1072 data_len_ + sizeof(PackedDataHeader) + sizeof(uint8_t)))
1073 {
1075 auto data =
1076 reinterpret_cast<uint8_t *>(parse_buff_.addr_) + sizeof(PackedDataHeader);
1077
1078 Topic(current_topic_).Publish(data, data_len_);
1079
1080 count++;
1081
1082 goto check_start; // NOLINT
1083 }
1084 else
1085 {
1086 goto check_start; // NOLINT
1087 }
1088 }
1089 else
1090 {
1091 return count;
1092 }
1093 }
1094
1095 return count;
1096 }
ErrorCode Pop(void *data=nullptr)
移除队列头部的元素 (Pop the front element from the queue).
Definition queue.hpp:114
size_t Size()
获取队列中的元素个数 (Get the number of elements in the queue).
Definition queue.hpp:305
ErrorCode PopBatch(void *data, size_t size)
批量移除多个元素 (Pop multiple elements from the queue).
Definition queue.hpp:208
ErrorCode Peek(void *data)
获取队列头部的元素但不移除 (Peek at the front element without removing it).
Definition queue.hpp:92
ErrorCode PushBatch(const void *data, size_t size)
批量推入多个元素 (Push multiple elements into the queue).
Definition queue.hpp:175
static bool Verify(const void *raw, size_t len)
验证数据的 CRC8 校验码 / Verifies the CRC8 checksum of the given data
Definition crc.hpp:91
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:1105
uint32_t data_len_
当前数据长度 Current data length
Definition message.hpp:1101
Status status_
服务器的当前解析状态 Current parsing state of the server
Definition message.hpp:1099
RBTree< uint32_t >::Node< Block > * TopicHandle
主题句柄,指向存储数据的红黑树节点。Handle pointing to a red-black tree node storing data.
Definition message.hpp:125
Topic()
默认构造函数,创建一个空的 Topic 实例 Default constructor, creates an empty Topic instance
Definition message.hpp:489

◆ Register()

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

注册一个主题 Registers a topic

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

Definition at line 985 of file message.hpp.

986 {
988 topic_map_.Insert(*node, topic->key);
989 }
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 1105 of file message.hpp.

◆ data_len_

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

当前数据长度 Current data length

Definition at line 1101 of file message.hpp.

◆ parse_buff_

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

解析数据缓冲区 Data buffer for parsing

Definition at line 1104 of file message.hpp.

◆ queue_

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

数据队列 Data queue

Definition at line 1103 of file message.hpp.

◆ status_

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

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

Definition at line 1099 of file message.hpp.

◆ topic_map_

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

主题映射表 Topic mapping table

Definition at line 1102 of file message.hpp.


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