libxr  1.0
Want to be the best embedded framework
Loading...
Searching...
No Matches
LibXR::Topic Class Reference

主题(Topic)管理类 / Topic management class More...

#include <message.hpp>

Collaboration diagram for LibXR::Topic:
[legend]

Data Structures

struct  ASyncBlock
 异步订阅块,继承自 SuberBlock Asynchronous subscription block, inheriting from SuberBlock More...
 
class  ASyncSubscriber
 异步订阅者类,用于订阅异步数据 Asynchronous subscriber class for subscribing to asynchronous data More...
 
struct  Block
 存储主题(Topic)数据的结构体。Structure storing topic data. More...
 
struct  CallbackBlock
 回调订阅块,继承自 SuberBlock Callback subscription block, inheriting from SuberBlock More...
 
class  Domain
 主题域(Domain)管理器,用于组织多个主题。Domain manager for organizing multiple topics. More...
 
struct  QueueBlock
 队列订阅块,继承自 SuberBlock Queue subscription block, inheriting from SuberBlock More...
 
class  QueuedSubscriber
 构造函数,使用名称和无锁队列进行初始化 Constructor using a name and a lock-free queue More...
 
class  Server
 服务器类,负责解析数据并将其分发到相应的主题 Server class responsible for parsing data and distributing it to corresponding topics More...
 
struct  SuberBlock
 订阅者信息存储结构。Structure storing subscriber information. More...
 
struct  SyncBlock
 同步订阅者存储结构。Structure storing synchronous subscriber data. More...
 
class  SyncSubscriber
 同步订阅者类,允许同步方式接收数据。Synchronous subscriber class allowing data reception in a synchronous manner. More...
 

Public Types

enum class  SuberType : uint8_t { SYNC , ASYNC , QUEUE , CALLBACK }
 订阅者类型。Subscriber type. More...
 
enum class  ASyncSubscriberState : uint32_t { IDLE = 0 , WAITING = 1 , DATA_READY = UINT32_MAX }
 
typedef RBTree< uint32_t >::Node< Block > * TopicHandle
 主题句柄,指向存储数据的红黑树节点。Handle pointing to a red-black tree node storing data.
 
typedef LibXR::Topic::ASyncBlock ASyncBlock
 
typedef LibXR::Topic::QueueBlock QueueBlock
 
using Callback = LibXR::Callback<LibXR::RawData &>
 
typedef LibXR::Topic::CallbackBlock CallbackBlock
 

Public Member Functions

void RegisterCallback (Callback &cb)
 注册回调函数 Registers a callback function
 
 Topic ()
 默认构造函数,创建一个空的 Topic 实例 Default constructor, creates an empty Topic instance
 
 Topic (const char *name, uint32_t max_length, Domain *domain=nullptr, bool multi_publisher=false, bool cache=false, bool check_length=false)
 构造函数,使用指定名称、最大长度、域及其他选项初始化主题 Constructor to initialize a topic with the specified name, maximum length, domain, and options
 
 Topic (TopicHandle topic)
 通过句柄构造主题 Constructs a topic from a topic handle
 
void EnableCache ()
 启用主题的缓存功能 Enables caching for the topic
 
template<typename Data >
void Publish (Data &data)
 发布数据 Publishes data
 
void Publish (void *addr, uint32_t size)
 以原始地址和大小发布数据 Publishes data using raw address and size
 
template<typename Data >
void PublishFromCallback (Data &data, bool in_isr)
 在回调函数中发布数据 Publishes data in a callback
 
void PublishFromCallback (void *addr, uint32_t size, bool in_isr)
 在回调函数中以原始地址和大小发布数据 Publishes data using raw address and size in a callback
 
template<SizeLimitMode Mode = SizeLimitMode::MORE>
ErrorCode DumpData (RawData data, bool pack=false)
 转储数据 Dump data
 
template<typename Data >
ErrorCode DumpData (PackedData< Data > &data)
 转储数据到 PackedData Dumps data into PackedData format
 
template<typename Data >
ErrorCode DumpData (Data &data)
 转储数据到普通数据结构 Dumps data into a normal data structure
 
 operator TopicHandle ()
 Topic 转换为 TopicHandle Converts Topic to TopicHandle
 
uint32_t GetKey () const
 获取主题的键值 Gets the key value of the topic
 

Static Public Member Functions

static void Lock (TopicHandle topic)
 锁定主题,防止其被多个订阅者同时访问。Lock the topic to prevent it from being accessed by multiple subscribers at the same time.
 
static void Unlock (TopicHandle topic)
 解锁主题。Unlock the topic.
 
static void LockFromCallback (TopicHandle topic)
 从回调中锁定主题,防止其被多个订阅者同时访问。Lock the topic from a callback to prevent it from being accessed by multiple subscribers at the same time.
 
static void UnlockFromCallback (TopicHandle topic)
 从回调中解锁主题。Unlock the topic from a callback.
 
template<typename Data >
static Topic CreateTopic (const char *name, Domain *domain=nullptr, bool multi_publisher=false, bool cache=false, bool check_length=true)
 创建一个新的主题 Creates a new topic
 
static TopicHandle Find (const char *name, Domain *domain=nullptr)
 在指定域中查找主题 Finds a topic in the specified domain
 
template<typename Data >
static TopicHandle FindOrCreate (const char *name, Domain *domain=nullptr, bool cache=false, bool check_length=true)
 在指定域中查找或创建主题 Finds or creates a topic in the specified domain
 
static void PackData (uint32_t topic_name_crc32, RawData buffer, RawData source)
 打包数据
 
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
 

Private Types

enum class  LockState : uint32_t { UNLOCKED = 0 , LOCKED = 1 , USE_MUTEX = UINT32_MAX }
 

Private Attributes

TopicHandle block_ = nullptr
 主题句柄,指向当前主题的内存块 Topic handle pointing to the memory block of the current topic
 

Static Private Attributes

static RBTree< uint32_t > * domain_ = nullptr
 主题域的红黑树结构,存储不同的主题 Red-Black Tree structure for storing different topics in the domain
 
static Domaindef_domain_ = nullptr
 默认的主题域,所有未指定域的主题都会归入此域 Default domain where all topics without a specified domain are assigned
 

Detailed Description

主题(Topic)管理类 / Topic management class

该类提供了基于发布-订阅模式的主题管理,支持同步、异步、队列和回调订阅者,以及数据的缓存和校验机制。 This class provides topic management based on the publish-subscribe model, supporting synchronous, asynchronous, queue-based, and callback subscribers, as well as data caching and validation mechanisms.

Definition at line 30 of file message.hpp.

Member Typedef Documentation

◆ Callback

Definition at line 451 of file message.hpp.

◆ TopicHandle

主题句柄,指向存储数据的红黑树节点。Handle pointing to a red-black tree node storing data.

Definition at line 145 of file message.hpp.

Member Enumeration Documentation

◆ ASyncSubscriberState

enum class LibXR::Topic::ASyncSubscriberState : uint32_t
strong

Definition at line 288 of file message.hpp.

289 {
290 IDLE = 0,
291 WAITING = 1,
292 DATA_READY = UINT32_MAX
293 };

◆ LockState

enum class LibXR::Topic::LockState : uint32_t
strongprivate

Definition at line 32 of file message.hpp.

33 {
34 UNLOCKED = 0,
35 LOCKED = 1,
36 USE_MUTEX = UINT32_MAX,
37 };

◆ SuberType

enum class LibXR::Topic::SuberType : uint8_t
strong

订阅者类型。Subscriber type.

Enumerator
SYNC 

同步订阅者。Synchronous subscriber.

ASYNC 

异步订阅者。Asynchronous subscriber.

QUEUE 

队列订阅者。Queued subscriber.

CALLBACK 

回调订阅者。Callback subscriber.

Definition at line 202 of file message.hpp.

203 {
204 SYNC,
205 ASYNC,
206 QUEUE,
207 CALLBACK,
208 };
@ SYNC
同步订阅者。Synchronous subscriber.
@ ASYNC
异步订阅者。Asynchronous subscriber.
@ QUEUE
队列订阅者。Queued subscriber.
@ CALLBACK
回调订阅者。Callback subscriber.

Constructor & Destructor Documentation

◆ Topic() [1/3]

Topic::Topic ( )

默认构造函数,创建一个空的 Topic 实例 Default constructor, creates an empty Topic instance

Definition at line 127 of file message.cpp.

127{}

◆ Topic() [2/3]

Topic::Topic ( const char * name,
uint32_t max_length,
Domain * domain = nullptr,
bool multi_publisher = false,
bool cache = false,
bool check_length = false )

构造函数,使用指定名称、最大长度、域及其他选项初始化主题 Constructor to initialize a topic with the specified name, maximum length, domain, and options

Parameters
name主题名称 Topic name
max_length数据的最大长度 Maximum length of data
domain主题所属的域(默认为 nullptr)Domain to which the topic belongs (default: nullptr)
multi_publisher是否允许多个订阅者(默认为 false)Whether to allow multiple subscribers (default: false)
cache是否启用缓存(默认为 false)Whether to enable caching (default: false)
check_length是否检查数据长度(默认为 false)Whether to check data length (default: false)

Definition at line 129 of file message.cpp.

131{
132 if (!def_domain_)
133 {
134 if (!def_domain_)
135 {
136 def_domain_ = new Domain("libxr_def_domain");
137 }
138 }
139
140 if (domain == nullptr)
141 {
142 domain = def_domain_;
143 }
144
145 auto crc32 = CRC32::Calculate(name, strlen(name));
146
147 auto topic = domain->node_->data_.Search<Block>(crc32);
148
149 if (topic)
150 {
151 ASSERT(topic->data_.max_length == max_length);
152 ASSERT(topic->data_.check_length == check_length);
153
154 if (multi_publisher && !topic->data_.mutex)
155 {
156 ASSERT(false);
157 }
158
159 block_ = topic;
160 }
161 else
162 {
163 block_ = new RBTree<uint32_t>::Node<Block>;
164 block_->data_.max_length = max_length;
165 block_->data_.crc32 = crc32;
166 block_->data_.data.addr_ = nullptr;
167 block_->data_.cache = false;
168 block_->data_.check_length = check_length;
169
170 if (multi_publisher)
171 {
172 block_->data_.mutex = new Mutex();
173 block_->data_.busy.store(LockState::USE_MUTEX, std::memory_order_release);
174 }
175 else
176 {
177 block_->data_.mutex = nullptr;
178 block_->data_.busy.store(LockState::UNLOCKED, std::memory_order_release);
179 }
180
181 domain->node_->data_.Insert(*block_, crc32);
182 }
183
184 if (cache && !block_->data_.cache)
185 {
186 EnableCache();
187 }
188}
static uint32_t Calculate(const void *raw, size_t len)
计算数据的 CRC32 校验码 / Computes the CRC32 checksum for the given data
Definition crc.hpp:251
互斥锁类,提供线程同步机制 (Mutex class providing thread synchronization mechanisms).
Definition mutex.hpp:18
红黑树的泛型数据节点,继承自 BaseNode (Generic data node for Red-Black Tree, inheriting from BaseNode).
Definition rbt.hpp:64
Data data_
存储的数据 (Stored data).
Definition rbt.hpp:99
static Domain * def_domain_
默认的主题域,所有未指定域的主题都会归入此域 Default domain where all topics without a specified domain are assigned
Definition message.hpp:786
void EnableCache()
启用主题的缓存功能 Enables caching for the topic
Definition message.cpp:204
TopicHandle block_
主题句柄,指向当前主题的内存块 Topic handle pointing to the memory block of the current topic
Definition message.hpp:774

◆ Topic() [3/3]

Topic::Topic ( TopicHandle topic)

通过句柄构造主题 Constructs a topic from a topic handle

Parameters
topic主题句柄 Topic handle

Definition at line 190 of file message.cpp.

190: block_(topic) {}

Member Function Documentation

◆ CreateTopic()

template<typename Data >
static Topic LibXR::Topic::CreateTopic ( const char * name,
Domain * domain = nullptr,
bool multi_publisher = false,
bool cache = false,
bool check_length = true )
inlinestatic

创建一个新的主题 Creates a new topic

Template Parameters
Data主题数据类型 Topic data type
Parameters
name主题名称 Topic name
domain主题所属的域(默认为 nullptr)Domain to which the topic belongs (default: nullptr)
multi_publisher是否允许多个订阅者(默认为 false)Whether to allow multiple subscribers (default: false)
cache是否启用缓存(默认为 false)Whether to enable caching (default: false)
check_length是否检查数据长度(默认为 true)Whether to check data length (default: true)
Returns
创建的 Topic 实例 The created Topic instance

Definition at line 508 of file message.hpp.

511 {
512 return Topic(name, sizeof(Data), domain, multi_publisher, cache, check_length);
513 }
Topic()
默认构造函数,创建一个空的 Topic 实例 Default constructor, creates an empty Topic instance
Definition message.cpp:127

◆ DumpData() [1/3]

template<typename Data >
ErrorCode LibXR::Topic::DumpData ( Data & data)
inline

转储数据到普通数据结构 Dumps data into a normal data structure

Template Parameters
Data数据类型 Data type
Parameters
data存储数据的变量 Variable to store the data

Definition at line 675 of file message.hpp.

676 {
677 if (block_->data_.data.addr_ == nullptr)
678 {
679 return ErrorCode::EMPTY;
680 }
681
682 ASSERT(sizeof(Data) == block_->data_.data.size_);
683
684 return DumpData<SizeLimitMode::NONE>(data, false);
685 }
ErrorCode DumpData(RawData data, bool pack=false)
转储数据 Dump data
Definition message.hpp:614

◆ DumpData() [2/3]

template<typename Data >
ErrorCode LibXR::Topic::DumpData ( PackedData< Data > & data)
inline

转储数据到 PackedData Dumps data into PackedData format

Template Parameters
Data数据类型 Data type
Parameters
data存储数据的 PackedData 结构 PackedData structure to store data

Definition at line 656 of file message.hpp.

657 {
658 if (block_->data_.data.addr_ == nullptr)
659 {
660 return ErrorCode::EMPTY;
661 }
662
663 ASSERT(sizeof(Data) == block_->data_.data.size_);
664
665 return DumpData<SizeLimitMode::NONE>(RawData(data), true);
666 }

◆ DumpData() [3/3]

template<SizeLimitMode Mode = SizeLimitMode::MORE>
ErrorCode LibXR::Topic::DumpData ( RawData data,
bool pack = false )
inline

转储数据 Dump data

Template Parameters
Mode数据大小检查模式 Size limit check mode
Parameters
data需要转储的数据 Data to be dumped
pack是否打包数据 Pack data
Returns
ErrorCode

Definition at line 614 of file message.hpp.

615 {
616 if (block_->data_.data.addr_ == nullptr)
617 {
618 return ErrorCode::EMPTY;
619 }
620
621 if (!pack)
622 {
623 Assert::SizeLimitCheck<Mode>(block_->data_.data.size_, data.size_);
624 Lock(block_);
625 memcpy(data.addr_, block_->data_.data.addr_, block_->data_.data.size_);
626 Unlock(block_);
627 }
628 else
629 {
630 Assert::SizeLimitCheck<Mode>(PACK_BASE_SIZE + block_->data_.data.size_, data.size_);
631
632 Lock(block_);
633 PackData(block_->data_.crc32, data, block_->data_.data);
634 Unlock(block_);
635 }
636
637 return ErrorCode::OK;
638 }
static void SizeLimitCheck(size_t limit, size_t size)
在非调试模式下的占位大小检查函数(无实际作用)。 Dummy size limit check for non-debug builds.
static void Unlock(TopicHandle topic)
解锁主题。Unlock the topic.
Definition message.cpp:46
static void Lock(TopicHandle topic)
锁定主题,防止其被多个订阅者同时访问。Lock the topic to prevent it from being accessed by multiple subscribers at the sa...
Definition message.cpp:28
static void PackData(uint32_t topic_name_crc32, RawData buffer, RawData source)
打包数据
Definition message.cpp:351

◆ EnableCache()

void Topic::EnableCache ( )

启用主题的缓存功能 Enables caching for the topic

Definition at line 204 of file message.cpp.

205{
206 Lock(block_);
207
208 if (!block_->data_.cache)
209 {
210 block_->data_.cache = true;
211 block_->data_.data.addr_ = new uint8_t[block_->data_.max_length];
212 }
213
214 Unlock(block_);
215}

◆ Find()

Topic::TopicHandle Topic::Find ( const char * name,
Domain * domain = nullptr )
static

在指定域中查找主题 Finds a topic in the specified domain

Parameters
name主题名称 Topic name
domain主题所属的域(默认为 nullptr)Domain to search in (default: nullptr)
Returns
主题句柄,如果找到则返回对应的句柄,否则返回 nullptr Topic handle if found, otherwise returns nullptr

Definition at line 192 of file message.cpp.

193{
194 if (domain == nullptr)
195 {
196 domain = def_domain_;
197 }
198
199 auto crc32 = CRC32::Calculate(name, strlen(name));
200
201 return domain->node_->data_.Search<Block>(crc32);
202}

◆ FindOrCreate()

template<typename Data >
static TopicHandle LibXR::Topic::FindOrCreate ( const char * name,
Domain * domain = nullptr,
bool cache = false,
bool check_length = true )
inlinestatic

在指定域中查找或创建主题 Finds or creates a topic in the specified domain

Template Parameters
Data数据类型 Data type
Parameters
name话题名称 Topic name
domain可选的域指针 Optional domain pointer (default: nullptr)
cache可选的缓存标志位 Optional cache flag (default: false)
check_length可选的数据长度检查标志位 Optional data length check flag (default: true)
Returns
TopicHandle 主题句柄 Topic handle

Definition at line 545 of file message.hpp.

547 {
548 auto topic = Find(name, domain);
549 if (topic == nullptr)
550 {
551 topic = CreateTopic<Data>(name, domain, cache, check_length).block_;
552 }
553 return topic;
554 }
static Topic CreateTopic(const char *name, Domain *domain=nullptr, bool multi_publisher=false, bool cache=false, bool check_length=true)
创建一个新的主题 Creates a new topic
Definition message.hpp:508
static TopicHandle Find(const char *name, Domain *domain=nullptr)
在指定域中查找主题 Finds a topic in the specified domain
Definition message.cpp:192

◆ GetKey()

uint32_t Topic::GetKey ( ) const

获取主题的键值 Gets the key value of the topic

Returns
uint32_t

Definition at line 387 of file message.cpp.

388{
389 if (block_)
390 {
391 return block_->key;
392 }
393 else
394 {
395 return 0;
396 }
397}
Key key
节点键值 (Key associated with the node).
Definition rbt.hpp:41

◆ Lock()

void Topic::Lock ( Topic::TopicHandle topic)
static

锁定主题,防止其被多个订阅者同时访问。Lock the topic to prevent it from being accessed by multiple subscribers at the same time.

Parameters
topic要锁定的主题。Topic to be locked.

Definition at line 28 of file message.cpp.

29{
30 if (topic->data_.mutex)
31 {
32 topic->data_.mutex->Lock();
33 }
34 else
35 {
36 LockState expected = LockState::UNLOCKED;
37 if (!topic->data_.busy.compare_exchange_strong(expected, LockState::LOCKED))
38 {
39 /* Multiple threads are trying to lock the same topic */
40 ASSERT(false);
41 return;
42 }
43 }
44}

◆ LockFromCallback()

void Topic::LockFromCallback ( Topic::TopicHandle topic)
static

从回调中锁定主题,防止其被多个订阅者同时访问。Lock the topic from a callback to prevent it from being accessed by multiple subscribers at the same time.

Parameters
topic要锁定的主题。Topic to be locked.

Definition at line 58 of file message.cpp.

59{
60 if (topic->data_.mutex)
61 {
62 ASSERT(false);
63 }
64 else
65 {
66 LockState expected = LockState::UNLOCKED;
67 if (!topic->data_.busy.compare_exchange_strong(expected, LockState::LOCKED))
68 {
69 /* Multiple threads are trying to lock the same topic */
70 ASSERT(false);
71 return;
72 }
73 }
74}

◆ operator TopicHandle()

LibXR::Topic::operator TopicHandle ( )
inline

Topic 转换为 TopicHandle Converts Topic to TopicHandle

Returns
TopicHandle

Definition at line 706 of file message.hpp.

706{ return block_; }

◆ PackData()

void Topic::PackData ( uint32_t topic_name_crc32,
RawData buffer,
RawData source )
static

打包数据

Parameters
topic_name_crc32话题名称的 CRC32 校验码
buffer等待写入的包 Packed data to be written
source需要打包的数据 Data to be packed

Definition at line 351 of file message.cpp.

352{
353 PackedData<uint8_t> *pack = reinterpret_cast<PackedData<uint8_t> *>(buffer.addr_);
354
355 memcpy(&pack->raw.data_, source.addr_, source.size_);
356
357 pack->raw.header_.prefix = 0xa5;
358 pack->raw.header_.topic_name_crc32 = topic_name_crc32;
359 pack->raw.header_.SetDataLen(source.size_);
360 pack->raw.header_.pack_header_crc8 =
361 CRC8::Calculate(&pack->raw, sizeof(PackedDataHeader) - sizeof(uint8_t));
362 uint8_t *crc8_pack =
363 reinterpret_cast<uint8_t *>(reinterpret_cast<uint8_t *>(pack) + PACK_BASE_SIZE +
364 source.size_ - sizeof(uint8_t));
365 *crc8_pack = CRC8::Calculate(pack, PACK_BASE_SIZE - sizeof(uint8_t) + source.size_);
366}
static uint8_t Calculate(const void *raw, size_t len)
计算数据的 CRC8 校验码 / Computes the CRC8 checksum for the given data
Definition crc.hpp:66
size_t size_
数据大小(字节)。 The size of the data (in bytes).
void * addr_
数据存储地址。 The storage address of the data.

◆ Publish() [1/2]

template<typename Data >
void LibXR::Topic::Publish ( Data & data)
inline

发布数据 Publishes data

Template Parameters
Data数据类型 Data type
Parameters
data需要发布的数据 Data to be published

Definition at line 569 of file message.hpp.

570 {
571 Publish(&data, sizeof(Data));
572 }
void Publish(Data &data)
发布数据 Publishes data
Definition message.hpp:569

◆ Publish() [2/2]

void Topic::Publish ( void * addr,
uint32_t size )

以原始地址和大小发布数据 Publishes data using raw address and size

Parameters
addr数据的地址 Address of the data
size数据大小 Size of the data

Definition at line 217 of file message.cpp.

218{
219 Lock(block_);
220 if (block_->data_.check_length)
221 {
222 ASSERT(size == block_->data_.max_length);
223 }
224 else
225 {
226 ASSERT(size <= block_->data_.max_length);
227 }
228
229 if (block_->data_.cache)
230 {
231 memcpy(block_->data_.data.addr_, addr, size);
232 block_->data_.data.size_ = size;
233 }
234 else
235 {
236 block_->data_.data.addr_ = addr;
237 block_->data_.data.size_ = size;
238 }
239
240 RawData data = block_->data_.data;
241
242 auto foreach_fun = [&](SuberBlock &block)
243 {
244 switch (block.type)
245 {
246 case SuberType::SYNC:
247 {
248 auto sync = reinterpret_cast<SyncBlock *>(&block);
249 memcpy(sync->buff.addr_, data.addr_, data.size_);
250 sync->sem.Post();
251 break;
252 }
253 case SuberType::ASYNC:
254 {
255 auto async = reinterpret_cast<ASyncBlock *>(&block);
256 if (async->state.load(std::memory_order_acquire) == ASyncSubscriberState::WAITING)
257 {
258 memcpy(async->buff.addr_, data.addr_, data.size_);
259 async->state.store(ASyncSubscriberState::DATA_READY, std::memory_order_release);
260 }
261 break;
262 }
263 case SuberType::QUEUE:
264 {
265 auto queue_block = reinterpret_cast<QueueBlock *>(&block);
266 queue_block->fun(data, queue_block->queue, false);
267 break;
268 }
270 {
271 auto cb_block = reinterpret_cast<CallbackBlock *>(&block);
272 cb_block->cb.Run(false, data);
273 break;
274 }
275 }
276 return ErrorCode::OK;
277 };
278
279 block_->data_.subers.Foreach<SuberBlock>(foreach_fun);
280
281 Unlock(block_);
282}
提供一个回调函数的封装,实现参数绑定和回调执行。 Provides a wrapper for callback functions, enabling argument binding and inv...
Definition libxr_cb.hpp:22
原始数据封装类。 A class for encapsulating raw data.
void(* fun)(RawData &, void *, bool)
处理数据的回调函数 Callback function to handle data
Definition message.hpp:395

◆ PublishFromCallback() [1/2]

template<typename Data >
void LibXR::Topic::PublishFromCallback ( Data & data,
bool in_isr )
inline

在回调函数中发布数据 Publishes data in a callback

Template Parameters
Data数据类型 Data type
Parameters
data需要发布的数据 Data to be published
in_isr是否在中断中发布数据 Whether to publish data in an interrupt

Definition at line 590 of file message.hpp.

591 {
592 PublishFromCallback(&data, sizeof(Data), in_isr);
593 }
void PublishFromCallback(Data &data, bool in_isr)
在回调函数中发布数据 Publishes data in a callback
Definition message.hpp:590

◆ PublishFromCallback() [2/2]

void Topic::PublishFromCallback ( void * addr,
uint32_t size,
bool in_isr )

在回调函数中以原始地址和大小发布数据 Publishes data using raw address and size in a callback

Parameters
addr数据的地址 Address of the data
size数据大小 Size of the data
in_isr是否在中断中发布数据 Whether to publish data in an interrupt

Definition at line 284 of file message.cpp.

285{
287 if (block_->data_.check_length)
288 {
289 ASSERT(size == block_->data_.max_length);
290 }
291 else
292 {
293 ASSERT(size <= block_->data_.max_length);
294 }
295
296 if (block_->data_.cache)
297 {
298 memcpy(block_->data_.data.addr_, addr, size);
299 block_->data_.data.size_ = size;
300 }
301 else
302 {
303 block_->data_.data.addr_ = addr;
304 block_->data_.data.size_ = size;
305 }
306
307 RawData data = block_->data_.data;
308
309 auto foreach_fun = [&](SuberBlock &block)
310 {
311 switch (block.type)
312 {
313 case SuberType::SYNC:
314 {
315 auto sync = reinterpret_cast<SyncBlock *>(&block);
316 memcpy(sync->buff.addr_, data.addr_, data.size_);
317 sync->sem.PostFromCallback(in_isr);
318 break;
319 }
320 case SuberType::ASYNC:
321 {
322 auto async = reinterpret_cast<ASyncBlock *>(&block);
323 if (async->state.load(std::memory_order_acquire) == ASyncSubscriberState::WAITING)
324 {
325 memcpy(async->buff.addr_, data.addr_, data.size_);
326 async->state.store(ASyncSubscriberState::DATA_READY, std::memory_order_release);
327 }
328 break;
329 }
330 case SuberType::QUEUE:
331 {
332 auto queue_block = reinterpret_cast<QueueBlock *>(&block);
333 queue_block->fun(data, queue_block->queue, false);
334 break;
335 }
337 {
338 auto cb_block = reinterpret_cast<CallbackBlock *>(&block);
339 cb_block->cb.Run(in_isr, data);
340 break;
341 }
342 }
343 return ErrorCode::OK;
344 };
345
346 block_->data_.subers.Foreach<SuberBlock>(foreach_fun);
347
349}
static void UnlockFromCallback(TopicHandle topic)
从回调中解锁主题。Unlock the topic from a callback.
Definition message.cpp:76
static void LockFromCallback(TopicHandle topic)
从回调中锁定主题,防止其被多个订阅者同时访问。Lock the topic from a callback to prevent it from being accessed by multiple s...
Definition message.cpp:58

◆ RegisterCallback()

void LibXR::Topic::RegisterCallback ( Callback & cb)

注册回调函数 Registers a callback function

Parameters
cb需要注册的回调函数 The callback function to register

Definition at line 117 of file message.cpp.

118{
119 CallbackBlock block;
120 block.cb = cb;
121 block.type = SuberType::CALLBACK;
122 auto node = new (std::align_val_t(LIBXR_CACHE_LINE_SIZE))
124 block_->data_.subers.Add(*node);
125}
数据节点模板,继承自 BaseNode,用于存储具体数据类型。 Template data node that inherits from BaseNode to store specific data...

◆ Unlock()

void Topic::Unlock ( Topic::TopicHandle topic)
static

解锁主题。Unlock the topic.

Parameters
topic要解锁的主题。Topic to be unlocked.

Definition at line 46 of file message.cpp.

47{
48 if (topic->data_.mutex)
49 {
50 topic->data_.mutex->Unlock();
51 }
52 else
53 {
54 topic->data_.busy.store(LockState::UNLOCKED, std::memory_order_release);
55 }
56}

◆ UnlockFromCallback()

void Topic::UnlockFromCallback ( Topic::TopicHandle topic)
static

从回调中解锁主题。Unlock the topic from a callback.

Parameters
topic要解锁的主题。Topic to be unlocked.

Definition at line 76 of file message.cpp.

77{
78 if (topic->data_.mutex)
79 {
80 ASSERT(false);
81 }
82 else
83 {
84 topic->data_.busy.store(LockState::UNLOCKED, std::memory_order_release);
85 }
86}

◆ WaitTopic()

Topic::TopicHandle Topic::WaitTopic ( const char * name,
uint32_t timeout = UINT32_MAX,
Domain * domain = nullptr )
static

等待主题的创建并返回其句柄 Waits for a topic to be created and returns its handle

Parameters
name主题名称 The name of the topic
timeout等待的超时时间(默认 UINT32_MAX)Timeout duration to wait (default: UINT32_MAX)
domain主题所属的域(默认为 nullptr)The domain in which to search for the topic (default: nullptr)
Returns
TopicHandle 如果找到主题,则返回其句柄,否则返回 nullptr TopicHandle if the topic is found, otherwise returns nullptr

Definition at line 368 of file message.cpp.

369{
370 TopicHandle topic = nullptr;
371 do
372 {
373 topic = Find(name, domain);
374 if (topic == nullptr)
375 {
376 if (timeout <= Thread::GetTime())
377 {
378 return nullptr;
379 }
380 Thread::Sleep(1);
381 }
382 } while (topic == nullptr);
383
384 return topic;
385}
static uint32_t GetTime()
获取当前系统时间(毫秒) Gets the current system time in milliseconds
Definition thread.cpp:41
static void Sleep(uint32_t milliseconds)
让线程进入休眠状态 Puts the thread to sleep
Definition thread.cpp:16
RBTree< uint32_t >::Node< Block > * TopicHandle
主题句柄,指向存储数据的红黑树节点。Handle pointing to a red-black tree node storing data.
Definition message.hpp:145

Field Documentation

◆ block_

TopicHandle LibXR::Topic::block_ = nullptr
private

主题句柄,指向当前主题的内存块 Topic handle pointing to the memory block of the current topic

Definition at line 774 of file message.hpp.

◆ def_domain_

Domain* LibXR::Topic::def_domain_ = nullptr
inlinestaticprivate

默认的主题域,所有未指定域的主题都会归入此域 Default domain where all topics without a specified domain are assigned

Definition at line 786 of file message.hpp.

◆ domain_

RBTree<uint32_t>* LibXR::Topic::domain_ = nullptr
inlinestaticprivate

主题域的红黑树结构,存储不同的主题 Red-Black Tree structure for storing different topics in the domain

Definition at line 780 of file message.hpp.


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