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 898 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 906 of file message.hpp.

907 {
908 WAIT_START,
909 WAIT_TOPIC,
911 };
@ 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 918 of file message.hpp.

919 : topic_map_([](const uint32_t &a, const uint32_t &b)
920 { return static_cast<int>(a) - static_cast<int>(b); }),
921 queue_(1, buffer_length)
922 {
923 /* Minimum size: header8 + crc32 + length24 + crc8 + data + crc8 = 10 */
924 ASSERT(buffer_length > PACK_BASE_SIZE);
925 parse_buff_.size_ = buffer_length;
926 parse_buff_.addr_ = new uint8_t[buffer_length];
927 }
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:1061
BaseQueue queue_
数据队列 Data queue
Definition message.hpp:1062
RawData parse_buff_
解析数据缓冲区 Data buffer for parsing
Definition message.hpp:1063

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

947 {
948 size_t count = 0;
949
950 queue_.PushBatch(data.addr_, data.size_);
951
952 while (true)
953 { /* 1. Check prefix */
955 {
956 /* Check start frame */
957 auto queue_size = queue_.Size();
958 for (uint32_t i = 0; i < queue_size; i++)
959 {
960 uint8_t prefix = 0;
961 queue_.Peek(&prefix);
962 if (prefix == 0xa5)
963 {
965 break;
966 }
967 queue_.Pop();
968 }
969 /* Not found */
971 {
972 return count;
973 }
974 }
975
976 /* 2. Get topic info */
978 {
979 /* Check size&crc */
980 if (queue_.Size() >= sizeof(PackedDataHeader))
981 {
982 queue_.PopBatch(parse_buff_.addr_, sizeof(PackedDataHeader));
983 if (CRC8::Verify(parse_buff_.addr_, sizeof(PackedDataHeader)))
984 {
985 auto header = reinterpret_cast<PackedDataHeader *>(parse_buff_.addr_);
986 /* Find topic */
987 auto node = topic_map_.Search<TopicHandle>(header->topic_name_crc32);
988 if (node)
989 {
990 data_len_ = header->GetDataLen();
991 current_topic_ = *node;
992 if (data_len_ + PACK_BASE_SIZE >= queue_.length_)
993 {
995 continue;
996 }
998 }
999 else
1000 {
1002 continue;
1003 }
1004 }
1005 else
1006 {
1008 continue;
1009 }
1010 }
1011 else
1012 {
1013 return count;
1014 }
1015 }
1016
1017 /* 3. Get data */
1019 {
1020 /* Check size&crc */
1021 if (queue_.Size() >= data_len_ + sizeof(uint8_t))
1022 {
1023 uint8_t *data =
1024 reinterpret_cast<uint8_t *>(parse_buff_.addr_) + sizeof(PackedDataHeader);
1025 queue_.PopBatch(data, data_len_ + sizeof(uint8_t));
1027 data_len_ + sizeof(PackedDataHeader) + sizeof(uint8_t)))
1028 {
1030 auto data = reinterpret_cast<uint8_t *>(parse_buff_.addr_) +
1031 sizeof(PackedDataHeader);
1032 if (data_len_ > current_topic_->data_.max_length)
1033 {
1034 data_len_ = current_topic_->data_.max_length;
1035 }
1036 Topic(current_topic_).Publish(data, data_len_);
1037
1038 count++;
1039
1040 continue;
1041 }
1042 else
1043 {
1045 continue;
1046 }
1047 }
1048 else
1049 {
1050 return count;
1051 }
1052 }
1053 }
1054 return count;
1055 }
ErrorCode Pop(void *data=nullptr)
移除队列头部的元素 (Pop the front element from the queue).
Definition queue.hpp:124
size_t length_
队列最大容量 (Maximum queue capacity).
Definition queue.hpp:345
ErrorCode PopBatch(void *data, size_t size)
批量移除多个元素 (Pop multiple elements from the queue).
Definition queue.hpp:214
size_t Size() const
获取队列中的元素个数 (Get the number of elements in the queue).
Definition queue.hpp:312
ErrorCode Peek(void *data)
获取队列头部的元素但不移除 (Peek at the front element without removing it).
Definition queue.hpp:102
ErrorCode PushBatch(const void *data, size_t size)
批量推入多个元素 (Push multiple elements into the queue).
Definition queue.hpp:177
static bool Verify(const void *raw, size_t len)
验证数据的 CRC8 校验码 / Verifies the CRC8 checksum of the given data
Definition crc.hpp:91
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:1064
uint32_t data_len_
当前数据长度 Current data length
Definition message.hpp:1060
Status status_
服务器的当前解析状态 Current parsing state of the server
Definition message.hpp:1058
RBTree< uint32_t >::Node< Block > * TopicHandle
主题句柄,指向存储数据的红黑树节点。Handle pointing to a red-black tree node storing data.
Definition message.hpp:144
Topic()
默认构造函数,创建一个空的 Topic 实例 Default constructor, creates an empty Topic instance
Definition message.hpp:509

◆ Register()

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

注册一个主题 Registers a topic

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

Definition at line 934 of file message.hpp.

935 {
936 auto node = new RBTree<uint32_t>::Node<TopicHandle>(topic);
937 topic_map_.Insert(*node, topic->key);
938 }
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 1064 of file message.hpp.

◆ data_len_

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

当前数据长度 Current data length

Definition at line 1060 of file message.hpp.

◆ parse_buff_

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

解析数据缓冲区 Data buffer for parsing

Definition at line 1063 of file message.hpp.

◆ queue_

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

数据队列 Data queue

Definition at line 1062 of file message.hpp.

◆ status_

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

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

Definition at line 1058 of file message.hpp.

◆ topic_map_

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

主题映射表 Topic mapping table

Definition at line 1061 of file message.hpp.


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