8void Topic::PackedDataHeader::SetDataLen(uint32_t len)
10 data_len_raw[0] =
static_cast<uint8_t
>(len >> 16);
11 data_len_raw[1] =
static_cast<uint8_t
>(len >> 8);
12 data_len_raw[2] =
static_cast<uint8_t
>(len);
15uint32_t Topic::PackedDataHeader::GetDataLen()
const
17 return static_cast<uint32_t
>(data_len_raw[0]) << 16 |
18 static_cast<uint32_t
>(data_len_raw[1]) << 8 |
19 static_cast<uint32_t
>(data_len_raw[2]);
30 {
return static_cast<int>(a) -
static_cast<int>(b); });
38 if (domain !=
nullptr)
45 [](
const uint32_t &a,
const uint32_t &b)
46 {
return static_cast<int>(a) -
static_cast<int>(b); });
56 auto node =
new (std::align_val_t(LIBXR_CACHE_LINE_SIZE))
74 if (domain ==
nullptr)
85 ASSERT(topic->data_.max_length == max_length);
86 ASSERT(topic->data_.check_length == check_length);
112 if (domain ==
nullptr)
142 ASSERT(size <= block_->data_.max_length);
164 auto sync =
reinterpret_cast<SyncBlock *
>(&block);
165 memcpy(sync->buff.addr_, data.
addr_, data.
size_);
171 auto async =
reinterpret_cast<ASyncBlock *
>(&block);
174 memcpy(async->buff.addr_, data.
addr_, data.
size_);
175 async->data_ready =
true;
181 auto queue_block =
reinterpret_cast<QueueBlock *
>(&block);
182 queue_block->
fun(data, queue_block->queue,
false);
188 cb_block->
cb.
Run(
false, data);
192 return ErrorCode::OK;
202 PackedData<uint8_t> *pack =
reinterpret_cast<PackedData<uint8_t> *
>(buffer.
addr_);
204 memcpy(&pack->raw.data_, source.
addr_, source.
size_);
206 pack->raw.header_.prefix = 0xa5;
207 pack->raw.header_.topic_name_crc32 = topic_name_crc32;
208 pack->raw.header_.SetDataLen(source.
size_);
209 pack->raw.header_.pack_header_crc8 =
210 CRC8::Calculate(&pack->raw,
sizeof(PackedDataHeader) -
sizeof(uint8_t));
212 reinterpret_cast<uint8_t *
>(
reinterpret_cast<uint8_t *
>(pack) + PACK_BASE_SIZE +
213 source.
size_ -
sizeof(uint8_t));
222 topic =
Find(name, domain);
223 if (topic ==
nullptr)
231 }
while (topic ==
nullptr);
249 : topic_map_([](const uint32_t &a, const uint32_t &b)
250 {
return static_cast<int>(a) -
static_cast<int>(b); }),
251 queue_(1, buffer_length)
254 ASSERT(buffer_length > PACK_BASE_SIZE);
255 parse_buff_.size_ = buffer_length;
256 parse_buff_.addr_ =
new uint8_t[buffer_length];
262 topic_map_.Insert(*node, topic->
key);
273 if (status_ == Status::WAIT_START)
276 auto queue_size = queue_.Size();
277 for (uint32_t i = 0; i < queue_size; i++)
280 queue_.Peek(&prefix);
283 status_ = Status::WAIT_TOPIC;
289 if (status_ == Status::WAIT_START)
296 if (status_ == Status::WAIT_TOPIC)
299 if (queue_.Size() >=
sizeof(PackedDataHeader))
301 queue_.PopBatch(parse_buff_.addr_,
sizeof(PackedDataHeader));
302 if (
CRC8::Verify(parse_buff_.addr_,
sizeof(PackedDataHeader)))
304 auto header =
reinterpret_cast<PackedDataHeader *
>(parse_buff_.addr_);
306 auto node = topic_map_.Search<
TopicHandle>(header->topic_name_crc32);
309 data_len_ = header->GetDataLen();
310 current_topic_ = *node;
311 if (data_len_ + PACK_BASE_SIZE >= queue_.length_)
313 status_ = Status::WAIT_START;
316 status_ = Status::WAIT_DATA_CRC;
320 status_ = Status::WAIT_START;
326 status_ = Status::WAIT_START;
337 if (status_ == Status::WAIT_DATA_CRC)
340 if (queue_.Size() >= data_len_ +
sizeof(uint8_t))
343 reinterpret_cast<uint8_t *
>(parse_buff_.addr_) +
sizeof(PackedDataHeader);
344 queue_.PopBatch(data, data_len_ +
sizeof(uint8_t));
346 data_len_ +
sizeof(PackedDataHeader) +
sizeof(uint8_t)))
348 status_ = Status::WAIT_START;
350 reinterpret_cast<uint8_t *
>(parse_buff_.addr_) +
sizeof(PackedDataHeader);
351 if (data_len_ > current_topic_->data_.max_length)
353 data_len_ = current_topic_->data_.max_length;
355 Topic(current_topic_).Publish(data, data_len_);
363 status_ = Status::WAIT_START;
static uint32_t Calculate(const void *raw, size_t len)
计算数据的 CRC32 校验码 / Computes the CRC32 checksum for the given data
static uint8_t Calculate(const void *raw, size_t len)
计算数据的 CRC8 校验码 / Computes the CRC8 checksum for the given data
static bool Verify(const void *raw, size_t len)
验证数据的 CRC8 校验码 / Verifies the CRC8 checksum of the given data
提供一个通用的回调包装,支持动态参数传递。 Provides a generic callback wrapper, supporting dynamic argument passing.
void Run(bool in_isr, PassArgs &&...args) const
执行回调函数,并传递参数。 Executes the callback function, passing the arguments.
常量原始数据封装类。 A class for encapsulating constant raw data.
size_t size_
数据大小(字节)。 The size of the data (in bytes).
const void * addr_
数据存储地址(常量)。 The storage address of the data (constant).
数据节点模板,继承自 BaseNode,用于存储具体数据类型。 Template data node that inherits from BaseNode to store specific data...
Key key
节点键值 (Key associated with the node).
红黑树的泛型数据节点,继承自 BaseNode (Generic data node for Red-Black Tree, inheriting from BaseNode).
Data data_
存储的数据 (Stored data).
红黑树实现,支持泛型键和值,并提供线程安全操作 (Red-Black Tree implementation supporting generic keys and values with thread...
Node< Data > * Search(const Key &key)
搜索红黑树中的节点 (Search for a node in the Red-Black Tree).
void Insert(BaseNode &node, KeyType &&key)
在树中插入新节点 (Insert a new node into the tree).
原始数据封装类。 A class for encapsulating raw data.
size_t size_
数据大小(字节)。 The size of the data (in bytes).
void * addr_
数据存储地址。 The storage address of the data.
static uint32_t GetTime()
获取当前系统时间(毫秒) Gets the current system time in milliseconds
static void Sleep(uint32_t milliseconds)
让线程进入休眠状态 Puts the thread to sleep
主题域(Domain)管理器,用于组织多个主题。Domain manager for organizing multiple topics.
Domain(const char *name)
构造函数,初始化或查找指定名称的主题域。Constructor initializing or looking up a domain by name.
RBTree< uint32_t >::Node< RBTree< uint32_t > > * node_
指向该域的根节点。Pointer to the root node of the domain.
void Register(TopicHandle topic)
注册一个主题 Registers a topic
size_t ParseData(ConstRawData data)
解析接收到的数据 Parses received data
Server(size_t buffer_length)
构造函数,初始化服务器并分配缓冲区 Constructor to initialize the server and allocate buffer
@ SYNC
同步订阅者。Synchronous subscriber.
@ ASYNC
异步订阅者。Asynchronous subscriber.
@ QUEUE
队列订阅者。Queued subscriber.
@ CALLBACK
回调订阅者。Callback subscriber.
void Publish(Data &data)
发布数据 Publishes data
static Domain * def_domain_
默认的主题域,所有未指定域的主题都会归入此域 Default domain where all topics without a specified domain are assigned
void EnableCache()
启用主题的缓存功能 Enables caching for the topic
static TopicHandle WaitTopic(const char *name, uint32_t timeout=UINT32_MAX, Domain *domain=nullptr)
等待主题的创建并返回其句柄 Waits for a topic to be created and returns its handle
uint32_t GetKey() const
获取主题的键值 Gets the key value of the topic
TopicHandle block_
主题句柄,指向当前主题的内存块 Topic handle pointing to the memory block of the current topic
void RegisterCallback(Callback &cb)
注册回调函数 Registers a callback function
static TopicHandle Find(const char *name, Domain *domain=nullptr)
在指定域中查找主题 Finds a topic in the specified domain
static RBTree< uint32_t > * domain_
主题域的红黑树结构,存储不同的主题 Red-Black Tree structure for storing different topics in the domain
Topic()
默认构造函数,创建一个空的 Topic 实例 Default constructor, creates an empty Topic instance
static void PackData(uint32_t topic_name_crc32, RawData buffer, RawData source)
打包数据
异步订阅块,继承自 SuberBlock Asynchronous subscription block, inheriting from SuberBlock
存储主题(Topic)数据的结构体。Structure storing topic data.
回调订阅块,继承自 SuberBlock Callback subscription block, inheriting from SuberBlock
Callback cb
订阅的回调函数 Subscribed callback function
队列订阅块,继承自 SuberBlock Queue subscription block, inheriting from SuberBlock
void(* fun)(RawData &, void *, bool)
处理数据的回调函数 Callback function to handle data
订阅者信息存储结构。Structure storing subscriber information.
SuberType type
订阅者类型。Type of subscriber.
同步订阅者存储结构。Structure storing synchronous subscriber data.