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

771 {
772 WAIT_START,
773 WAIT_TOPIC,
775 };
@ 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
Note
包含动态内存分配。 Contains dynamic memory allocation.

Definition at line 398 of file message.cpp.

399 : topic_map_([](const uint32_t& a, const uint32_t& b)
400 { return static_cast<int>(a) - static_cast<int>(b); }),
401 queue_(1, buffer_length)
402{
403 /* Minimum size: header8 + crc32 + length24 + crc8 + data + crc8 = 10 */
404 ASSERT(buffer_length > PACK_BASE_SIZE);
405 parse_buff_.size_ = buffer_length;
406 parse_buff_.addr_ = new uint8_t[buffer_length];
407}
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:809
BaseQueue queue_
数据队列 Data queue
Definition message.hpp:810
RawData parse_buff_
解析数据缓冲区 Data buffer for parsing
Definition message.hpp:811

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 415 of file message.cpp.

416{
417 size_t count = 0;
418
419 queue_.PushBatch(data.addr_, data.size_);
420
421 while (true)
422 { /* 1. Check prefix */
424 {
425 /* Check start frame */
426 auto queue_size = queue_.Size();
427 for (uint32_t i = 0; i < queue_size; i++)
428 {
429 uint8_t prefix = 0;
430 queue_.Peek(&prefix);
431 if (prefix == 0xa5)
432 {
434 break;
435 }
436 queue_.Pop();
437 }
438 /* Not found */
440 {
441 return count;
442 }
443 }
444
445 /* 2. Get topic info */
447 {
448 /* Check size&crc */
449 if (queue_.Size() >= sizeof(PackedDataHeader))
450 {
451 queue_.PopBatch(parse_buff_.addr_, sizeof(PackedDataHeader));
452 if (CRC8::Verify(parse_buff_.addr_, sizeof(PackedDataHeader)))
453 {
454 auto header = reinterpret_cast<PackedDataHeader*>(parse_buff_.addr_);
455 /* Find topic */
456 auto node = topic_map_.Search<TopicHandle>(header->topic_name_crc32);
457 if (node)
458 {
459 data_len_ = header->GetDataLen();
460 current_topic_ = *node;
461 if (data_len_ + PACK_BASE_SIZE >= queue_.length_)
462 {
464 continue;
465 }
467 }
468 else
469 {
471 continue;
472 }
473 }
474 else
475 {
477 continue;
478 }
479 }
480 else
481 {
482 return count;
483 }
484 }
485
486 /* 3. Get data */
488 {
489 /* Check size&crc */
490 if (queue_.Size() >= data_len_ + sizeof(uint8_t))
491 {
492 uint8_t* data =
493 reinterpret_cast<uint8_t*>(parse_buff_.addr_) + sizeof(PackedDataHeader);
494 queue_.PopBatch(data, data_len_ + sizeof(uint8_t));
496 data_len_ + sizeof(PackedDataHeader) + sizeof(uint8_t)))
497 {
499 auto data =
500 reinterpret_cast<uint8_t*>(parse_buff_.addr_) + sizeof(PackedDataHeader);
501 if (data_len_ > current_topic_->data_.max_length)
502 {
503 data_len_ = current_topic_->data_.max_length;
504 }
505 Topic(current_topic_).Publish(data, data_len_);
506
507 count++;
508
509 continue;
510 }
511 else
512 {
514 continue;
515 }
516 }
517 else
518 {
519 return count;
520 }
521 }
522 }
523 return count;
524}
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:163
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:213
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:133
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:121
TopicHandle current_topic_
当前主题句柄 Current topic handle
Definition message.hpp:812
uint32_t data_len_
当前数据长度 Current data length
Definition message.hpp:808
Status status_
服务器的当前解析状态 Current parsing state of the server
Definition message.hpp:806
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
Note
包含动态内存分配。 Contains dynamic memory allocation.

Definition at line 409 of file message.cpp.

410{
411 auto node = new RBTree<uint32_t>::Node<TopicHandle>(topic);
412 topic_map_.Insert(*node, topic->key);
413}
红黑树实现,支持泛型键和值,并提供线程安全操作 (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:236

Field Documentation

◆ current_topic_

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

当前主题句柄 Current topic handle

Definition at line 812 of file message.hpp.

◆ data_len_

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

当前数据长度 Current data length

Definition at line 808 of file message.hpp.

◆ parse_buff_

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

解析数据缓冲区 Data buffer for parsing

Definition at line 811 of file message.hpp.

◆ queue_

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

数据队列 Data queue

Definition at line 810 of file message.hpp.

◆ status_

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

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

Definition at line 806 of file message.hpp.

◆ topic_map_

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

主题映射表 Topic mapping table

Definition at line 809 of file message.hpp.


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