5#include "libxr_def.hpp"
6#include "libxr_mem.hpp"
15void Topic::PackedDataHeader::SetDataLen(uint32_t len)
17 data_len_raw[0] =
static_cast<uint8_t
>(len >> 16);
18 data_len_raw[1] =
static_cast<uint8_t
>(len >> 8);
19 data_len_raw[2] =
static_cast<uint8_t
>(len);
22uint32_t Topic::PackedDataHeader::GetDataLen()
const
24 return static_cast<uint32_t
>(data_len_raw[0]) << 16 |
25 static_cast<uint32_t
>(data_len_raw[1]) << 8 |
26 static_cast<uint32_t
>(data_len_raw[2]);
31 if (topic->
data_.mutex)
33 topic->
data_.mutex->Lock();
37 LockState expected = LockState::UNLOCKED;
38 if (!topic->
data_.busy.compare_exchange_strong(expected, LockState::LOCKED))
49 if (topic->
data_.mutex)
51 topic->
data_.mutex->Unlock();
55 topic->
data_.busy.store(LockState::UNLOCKED, std::memory_order_release);
61 if (topic->
data_.mutex)
67 LockState expected = LockState::UNLOCKED;
68 if (!topic->
data_.busy.compare_exchange_strong(expected, LockState::LOCKED))
79 if (topic->
data_.mutex)
85 topic->
data_.busy.store(LockState::UNLOCKED, std::memory_order_release);
91 ASSERT(name !=
nullptr);
99 {
return static_cast<int>(a) -
static_cast<int>(b); });
107 if (domain !=
nullptr)
114 [](
const uint32_t& a,
const uint32_t& b)
115 {
return static_cast<int>(a) -
static_cast<int>(b); });
133 bool cache,
bool check_length)
143 if (domain ==
nullptr)
154 ASSERT(topic->data_.max_length == max_length);
155 ASSERT(topic->data_.check_length == check_length);
157 if (multi_publisher && !topic->data_.mutex)
176 block_->
data_.busy.store(LockState::USE_MUTEX, std::memory_order_release);
181 block_->
data_.busy.store(LockState::UNLOCKED, std::memory_order_release);
197 if (domain ==
nullptr)
229 ASSERT(size <= block_->data_.max_length);
251 auto sync =
reinterpret_cast<SyncBlock*
>(&block);
258 auto async =
reinterpret_cast<ASyncBlock*
>(&block);
259 if (async->state.load(std::memory_order_acquire) == ASyncSubscriberState::WAITING)
262 async->state.store(ASyncSubscriberState::DATA_READY, std::memory_order_release);
268 auto queue_block =
reinterpret_cast<QueueBlock*
>(&block);
269 queue_block->
fun(data, queue_block->queue);
275 cb_block->
cb.Run(
false, data);
296 ASSERT(size <= block_->data_.max_length);
318 auto sync =
reinterpret_cast<SyncBlock*
>(&block);
320 sync->sem.PostFromCallback(in_isr);
325 auto async =
reinterpret_cast<ASyncBlock*
>(&block);
326 if (async->state.load(std::memory_order_acquire) == ASyncSubscriberState::WAITING)
329 async->state.store(ASyncSubscriberState::DATA_READY, std::memory_order_release);
335 auto queue_block =
reinterpret_cast<QueueBlock*
>(&block);
336 queue_block->
fun(data, queue_block->queue);
342 cb_block->
cb.Run(in_isr, data);
356 PackedData<uint8_t>* pack =
reinterpret_cast<PackedData<uint8_t>*
>(buffer.
addr_);
360 pack->raw.header_.prefix = 0xa5;
361 pack->raw.header_.topic_name_crc32 = topic_name_crc32;
362 pack->raw.header_.SetDataLen(source.
size_);
363 pack->raw.header_.pack_header_crc8 =
364 CRC8::Calculate(&pack->raw,
sizeof(PackedDataHeader) -
sizeof(uint8_t));
365 uint8_t* crc8_pack =
reinterpret_cast<uint8_t*
>(
366 reinterpret_cast<uint8_t*
>(pack) + PACK_BASE_SIZE + source.
size_ -
sizeof(uint8_t));
376 topic =
Find(name, domain);
377 if (topic ==
nullptr)
379 if (timeout != UINT32_MAX &&
386 }
while (topic ==
nullptr);
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)
409 ASSERT(buffer_length > PACK_BASE_SIZE);
410 parse_buff_.size_ = buffer_length;
411 parse_buff_.addr_ =
new uint8_t[buffer_length];
417 topic_map_.Insert(*node, topic->
key);
428 if (status_ == Status::WAIT_START)
431 auto queue_size = queue_.Size();
432 for (uint32_t i = 0; i < queue_size; i++)
435 queue_.Peek(&prefix);
438 status_ = Status::WAIT_TOPIC;
444 if (status_ == Status::WAIT_START)
451 if (status_ == Status::WAIT_TOPIC)
454 if (queue_.Size() >=
sizeof(PackedDataHeader))
456 queue_.PopBatch(parse_buff_.addr_,
sizeof(PackedDataHeader));
457 if (
CRC8::Verify(parse_buff_.addr_,
sizeof(PackedDataHeader)))
459 auto header =
reinterpret_cast<PackedDataHeader*
>(parse_buff_.addr_);
461 auto node = topic_map_.Search<
TopicHandle>(header->topic_name_crc32);
464 data_len_ = header->GetDataLen();
465 current_topic_ = *node;
466 if (data_len_ + PACK_BASE_SIZE >= queue_.length_)
468 status_ = Status::WAIT_START;
471 status_ = Status::WAIT_DATA_CRC;
475 status_ = Status::WAIT_START;
481 status_ = Status::WAIT_START;
492 if (status_ == Status::WAIT_DATA_CRC)
495 if (queue_.Size() >= data_len_ +
sizeof(uint8_t))
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)))
503 status_ = Status::WAIT_START;
505 reinterpret_cast<uint8_t*
>(parse_buff_.addr_) +
sizeof(PackedDataHeader);
506 if (data_len_ > current_topic_->data_.max_length)
508 data_len_ = current_topic_->data_.max_length;
510 Topic(current_topic_).Publish(data, data_len_);
518 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
通用回调包装,支持动态参数传递 / Generic callback wrapper supporting dynamic argument passing
常量原始数据封装类。 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...
static void FastCopy(void *dst, const void *src, size_t size)
快速内存拷贝 / Fast memory copy
互斥锁类,提供线程同步机制 (Mutex class providing thread synchronization mechanisms).
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
static void Unlock(TopicHandle topic)
解锁主题。Unlock the topic.
static void Lock(TopicHandle topic)
锁定主题,防止其被多个订阅者同时访问。Lock the topic to prevent it from being accessed by multiple subscribers at the sa...
static void UnlockFromCallback(TopicHandle topic)
从回调中解锁主题。Unlock the topic from a callback.
static void LockFromCallback(TopicHandle topic)
从回调中锁定主题,防止其被多个订阅者同时访问。Lock the topic from a callback to prevent it from being accessed by multiple s...
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
void PublishFromCallback(Data &data, bool in_isr)
在回调函数中发布数据 Publishes data in a callback
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)
打包数据
@ OK
操作成功 | Operation successful
constexpr size_t CACHE_LINE_SIZE
缓存行大小 / Cache line size
异步订阅块,继承自 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 *)
处理数据的回调函数 Callback function to handle data
订阅者信息存储结构。Structure storing subscriber information.
SuberType type
订阅者类型。Type of subscriber.
同步订阅者存储结构。Structure storing synchronous subscriber data.