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

770 {
771 WAIT_START,
772 WAIT_TOPIC,
774 };
@ 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 403 of file message.cpp.

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

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

421{
422 size_t count = 0;
423
424 queue_.PushBatch(data.addr_, data.size_);
425
426 while (true)
427 { /* 1. Check prefix */
429 {
430 /* Check start frame */
431 auto queue_size = queue_.Size();
432 for (uint32_t i = 0; i < queue_size; i++)
433 {
434 uint8_t prefix = 0;
435 queue_.Peek(&prefix);
436 if (prefix == 0xa5)
437 {
439 break;
440 }
441 queue_.Pop();
442 }
443 /* Not found */
445 {
446 return count;
447 }
448 }
449
450 /* 2. Get topic info */
452 {
453 /* Check size&crc */
454 if (queue_.Size() >= sizeof(PackedDataHeader))
455 {
456 queue_.PopBatch(parse_buff_.addr_, sizeof(PackedDataHeader));
457 if (CRC8::Verify(parse_buff_.addr_, sizeof(PackedDataHeader)))
458 {
459 auto header = reinterpret_cast<PackedDataHeader*>(parse_buff_.addr_);
460 /* Find topic */
461 auto node = topic_map_.Search<TopicHandle>(header->topic_name_crc32);
462 if (node)
463 {
464 data_len_ = header->GetDataLen();
465 current_topic_ = *node;
466 if (data_len_ + PACK_BASE_SIZE >= queue_.length_)
467 {
469 continue;
470 }
472 }
473 else
474 {
476 continue;
477 }
478 }
479 else
480 {
482 continue;
483 }
484 }
485 else
486 {
487 return count;
488 }
489 }
490
491 /* 3. Get data */
493 {
494 /* Check size&crc */
495 if (queue_.Size() >= data_len_ + sizeof(uint8_t))
496 {
497 uint8_t* data =
498 reinterpret_cast<uint8_t*>(parse_buff_.addr_) + sizeof(PackedDataHeader);
499 queue_.PopBatch(data, data_len_ + sizeof(uint8_t));
501 data_len_ + sizeof(PackedDataHeader) + sizeof(uint8_t)))
502 {
504 auto data =
505 reinterpret_cast<uint8_t*>(parse_buff_.addr_) + sizeof(PackedDataHeader);
506 if (data_len_ > current_topic_->data_.max_length)
507 {
508 data_len_ = current_topic_->data_.max_length;
509 }
510 Topic(current_topic_).Publish(data, data_len_);
511
512 count++;
513
514 continue;
515 }
516 else
517 {
519 continue;
520 }
521 }
522 else
523 {
524 return count;
525 }
526 }
527 }
528 return count;
529}
ErrorCode Peek(void *data)
获取队列头部的元素但不移除 (Peek at the front element without removing it).
Definition queue.cpp:56
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:105
size_t Size() const
获取队列中的元素个数 (Get the number of elements in the queue).
Definition queue.cpp:215
ErrorCode Pop(void *data=nullptr)
移除队列头部的元素 (Pop the front element from the queue).
Definition queue.cpp:71
ErrorCode PopBatch(void *data, size_t size)
批量移除多个元素 (Pop multiple elements from the queue).
Definition queue.cpp:135
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:811
uint32_t data_len_
当前数据长度 Current data length
Definition message.hpp:807
Status status_
服务器的当前解析状态 Current parsing state of the server
Definition message.hpp:805
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:130

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

415{
416 auto node = new RBTree<uint32_t>::Node<TopicHandle>(topic);
417 topic_map_.Insert(*node, topic->key);
418}
红黑树实现,支持泛型键和值,并提供线程安全操作 (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 811 of file message.hpp.

◆ data_len_

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

当前数据长度 Current data length

Definition at line 807 of file message.hpp.

◆ parse_buff_

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

解析数据缓冲区 Data buffer for parsing

Definition at line 810 of file message.hpp.

◆ queue_

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

数据队列 Data queue

Definition at line 809 of file message.hpp.

◆ status_

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

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

Definition at line 805 of file message.hpp.

◆ topic_map_

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

主题映射表 Topic mapping table

Definition at line 808 of file message.hpp.


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