libxr
Embedded C++ framework for bare-metal, RTOS and Linux
Loading...
Searching...
No Matches
LibXR::Topic Class Reference

发布订阅主题 / Publish-subscribe topic More...

#include <topic.hpp>

Classes

struct  ASyncBlock
 异步订阅者自己挂的数据块 / Data block owned by one asynchronous subscriber More...
 
class  ASyncSubscriber
 先 StartWaiting(),再自己来取数据的订阅者 / Subscriber that first calls StartWaiting() and later pulls the data itself More...
 
struct  Block
 topic 运行时状态块 / Runtime state block of one topic More...
 
class  Callback
 每次发布时直接执行函数的订阅句柄 / Subscription handle that runs a function on each publish More...
 
struct  CallbackBlock
 挂在 topic 订阅链表里的回调记录 / Callback record stored in the topic subscriber list More...
 
class  Domain
 topic 所属的命名域 / Naming domain that groups topics More...
 
struct  Message
 带时间戳和 payload 副本的消息对象 / Message object carrying a timestamp and a payload copy More...
 
struct  MessageView
 带时间戳和 payload 指针的只读消息视图 / Read-only message view carrying a timestamp and a payload pointer More...
 
struct  QueueBlock
 队列订阅者自己挂的数据块 / Data block owned by one queued subscriber More...
 
class  QueuedSubscriber
 每次发布都往队列里塞一份数据的订阅者 / Subscriber that pushes one entry into a queue on each publish More...
 
struct  RawMessageView
 带时间戳和只读原始 payload 视图的消息 / Message carrying a timestamp and a read-only raw payload view More...
 
class  Server
 将字节流解析成 packet 并发布到已注册 topic 的状态机 / State machine that parses byte streams into packets and publishes them into registered topics More...
 
struct  SuberBlock
 所有订阅块共用的公共头 / Common header shared by all subscriber blocks More...
 
struct  SyncBlock
 同步订阅者自己挂的数据块 / Data block owned by one synchronous subscriber More...
 
class  SyncSubscriber
 调用 Wait() 收消息的订阅者 / Subscriber that receives messages by calling Wait() More...
 

Public Types

enum class  ASyncSubscriberState : uint32_t { IDLE = 0 , WAITING = 1 , DATA_READY = UINT32_MAX }
 异步订阅者本地缓冲区的状态 / State of the async subscriber's local buffer More...
 
enum class  SuberType : uint8_t { SYNC , ASYNC , QUEUE , CALLBACK }
 topic 支持的订阅者种类 / Subscriber kinds supported by a topic More...
 
typedef RBTree< uint32_t >::Node< Block > * TopicHandle
 指向一个 topic 运行时状态块的句柄 / Handle pointing to one topic runtime state block
 

Public Member Functions

void RegisterCallback (Callback &cb)
 注册一个回调订阅者 / Register one callback subscriber
 
 Topic ()
 构造一个空 topic 视图 / Construct one empty topic view
 
 Topic (const char *name, TypeID::ID payload_type_id, size_t payload_size, size_t payload_alignment, Domain *domain=nullptr, bool multi_publisher=false)
 用显式类型契约构造或查找一个 topic / Construct or look up one topic with an explicit runtime type contract
 
 Topic (TopicHandle topic)
 从已有句柄构造一个 topic 视图 / Construct one topic view from an existing handle
 
size_t PayloadSize () const
 获取该 topic 固定 payload 字节数 / Get the fixed payload size of this topic
 
size_t PayloadAlignment () const
 获取该 topic payload 的对齐要求 / Get the payload alignment requirement of this topic
 
template<typename Data >
void Publish (Data &data)
 在普通上下文里发布一条消息,并自动取当前时间戳 / Publish one message in normal context and stamp it with the current time
 
template<typename Data >
void Publish (Data &data, MicrosecondTimestamp timestamp)
 在普通上下文里按指定时间戳发布一条消息 / Publish one message in normal context with an explicit timestamp
 
template<typename Data >
void PublishFromCallback (Data &data, bool in_isr)
 在回调或 ISR 路径里发布一条消息,并自动取当前时间戳 / Publish one message from callback or ISR context and stamp it with the current time
 
template<typename Data >
void PublishFromCallback (Data &data, MicrosecondTimestamp timestamp, bool in_isr)
 在回调或 ISR 路径里按指定时间戳发布一条消息 / Publish one message from callback or ISR context with an explicit timestamp
 
void PublishBytesFromServer (void *payload_addr, size_t payload_size, MicrosecondTimestamp timestamp)
 供 packet/server 路径按字节发布一条消息 / Publish one message from the packet/server path using bytes already arranged as the exact payload object
 
void PublishBytesFromServerCallback (void *payload_addr, size_t payload_size, MicrosecondTimestamp timestamp, bool in_isr)
 供回调/ISR 上下文里的 packet/server 路径按字节发布一条消息 / Publish one packet/server message from callback/ISR context using bytes already arranged as the exact payload object
 
template<typename Data >
ErrorCode PackData (const Data &data, PackedData< Data > &packet)
 将一个精确类型消息打包成 packet / Pack one exact-typed message into one packet using the topic's runtime contract and current timestamp
 
template<typename Data >
ErrorCode PackData (const Data &data, PackedData< Data > &packet, MicrosecondTimestamp timestamp)
 将一个精确类型消息按指定时间戳打包成 packet / Pack one exact-typed message into one packet with the given timestamp
 
ErrorCode PackRaw (ConstRawData data, RawData packet)
 将一段 raw payload 按当前 topic 元数据打包成 packet / Pack a raw payload byte view into one packet using the current topic metadata and current timestamp
 
ErrorCode PackRaw (ConstRawData data, RawData packet, MicrosecondTimestamp timestamp)
 将一段 raw payload 按当前 topic 元数据和指定时间戳打包成 packet / Pack a raw payload byte view into one packet using the current topic metadata and timestamp
 
 operator TopicHandle ()
 把 topic 视图转换为底层句柄 / Convert this topic view to the underlying handle
 
uint32_t GetKey () const
 读取 topic 键值 / Read the key value of this topic
 

Static Public Member Functions

static void Lock (TopicHandle topic)
 在普通上下文里锁住一个 topic 发布路径 / Lock one topic publish path in normal context
 
static void Unlock (TopicHandle topic)
 在普通上下文里释放一个 topic 发布路径 / Unlock one topic publish path in normal context
 
static void LockFromCallback (TopicHandle topic)
 在回调或 ISR 路径里锁住一个 topic 发布路径 / Lock one topic publish path from callback or ISR context
 
static void UnlockFromCallback (TopicHandle topic)
 在回调或 ISR 路径里释放一个 topic 发布路径 / Unlock one topic publish path from callback or ISR context
 
static MicrosecondTimestamp NowTimestamp ()
 读取当前时间戳 / Read the current timestamp
 
template<typename Data >
static Topic CreateTopic (const char *name, Domain *domain=nullptr, bool multi_publisher=false)
 用精确类型创建或查找一个 topic / Create or look up one topic using one exact payload type
 
static TopicHandle Find (const char *name, Domain *domain=nullptr)
 按名称查找一个已存在 topic / Find one existing topic by name
 
template<typename Data >
static TopicHandle FindOrCreate (const char *name, Domain *domain=nullptr, bool multi_publisher=false)
 按精确类型查找或创建一个 topic / Find or create one topic with an exact payload type
 
static TopicHandle WaitTopic (const char *name, uint32_t timeout=UINT32_MAX, Domain *domain=nullptr)
 等待指定名称的 topic 出现 / Wait until a topic with the given name exists
 

Detailed Description

发布订阅主题 / Publish-subscribe topic

一个 Topic 表示一种精确类型的消息通道:它在每次发布时同步通知挂在上面的同步、 异步、队列或回调订阅者,但自身不缓存最近一次消息。 One Topic represents one exact-typed message channel: it synchronously notifies the attached synchronous, asynchronous, queued, or callback subscribers on each publish, but does not cache the latest message itself.

Definition at line 55 of file topic.hpp.

Member Typedef Documentation

◆ TopicHandle

指向一个 topic 运行时状态块的句柄 / Handle pointing to one topic runtime state block

Definition at line 125 of file topic.hpp.

Member Enumeration Documentation

◆ ASyncSubscriberState

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

异步订阅者本地缓冲区的状态 / State of the async subscriber's local buffer

异步订阅者本地缓冲区状态 / State of one asynchronous subscriber local buffer

Enumerator
IDLE 

当前没有等待,也没有待消费的新数据 / No wait is pending and no unread data is buffered.

WAITING 

等待下一次发布填充本地缓冲区 / Waiting for the next publish to fill the local buffer.

DATA_READY 

本地缓冲区已有一份待消费的新数据 / One unread fresh sample is buffered locally.

Definition at line 11 of file async.hpp.

◆ SuberType

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

topic 支持的订阅者种类 / Subscriber kinds supported by a topic

Enumerator
SYNC 

同步等待型订阅者 / Synchronous wait-based subscriber.

ASYNC 

异步本地缓冲型订阅者 / Asynchronous local-buffer subscriber.

QUEUE 

队列转发型订阅者 / Queue-forwarding subscriber.

CALLBACK 

回调执行型订阅者 / Callback-executing subscriber.

Definition at line 224 of file topic.hpp.

225 {
226 SYNC,
227 ASYNC,
228 QUEUE,
229 CALLBACK,
230 };
@ SYNC
同步等待型订阅者 / Synchronous wait-based subscriber.
@ ASYNC
异步本地缓冲型订阅者 / Asynchronous local-buffer subscriber.
@ QUEUE
队列转发型订阅者 / Queue-forwarding subscriber.
@ CALLBACK
回调执行型订阅者 / Callback-executing subscriber.

Constructor & Destructor Documentation

◆ Topic() [1/3]

Topic::Topic ( )

构造一个空 topic 视图 / Construct one empty topic view

Definition at line 114 of file topic.cpp.

114{}

◆ Topic() [2/3]

Topic::Topic ( const char * name,
TypeID::ID payload_type_id,
size_t payload_size,
size_t payload_alignment,
Domain * domain = nullptr,
bool multi_publisher = false )

用显式类型契约构造或查找一个 topic / Construct or look up one topic with an explicit runtime type contract

Parameters
nametopic 名称 / Topic name
payload_type_idpayload 类型标识 / Payload type identifier
payload_size该 topic 固定 payload 字节数 / Fixed payload size of this topic
payload_alignment该 topic payload 所需对齐,必须是非零 2 次幂 / Required payload alignment of this topic; must be a non-zero power of two
domain可选主题域 / Optional topic domain
multi_publisher是否允许多发布者串行化 / Whether to allow serialized multi-publisher use
Note
同名 topic 已存在但类型契约不同,或它以单发布者创建而本次要求多发布者时, 在所有构建类型中进入致命错误。 If a topic with this name exists with a different type contract, or was created for a single publisher while multi_publisher is requested, a fatal error is raised in every build type.

Definition at line 116 of file topic.cpp.

118{
119 ASSERT(name != nullptr);
120 ASSERT(payload_type_id != nullptr);
121 ASSERT(payload_size != 0);
122 ASSERT(payload_alignment != 0);
123 ASSERT((payload_alignment & (payload_alignment - 1)) == 0);
124
125 if (domain == nullptr)
126 {
127 domain = EnsureDefaultDomain();
128 }
129
130 auto crc32 = CRC32::Calculate(name, strlen(name));
131
132 auto topic = domain->node_->data_.Search<Block>(crc32);
133
134 if (topic)
135 {
136 RequireTypeContract(topic, payload_type_id, payload_size, payload_alignment);
137
138 if (multi_publisher)
139 {
140 RequireMultiPublisher(topic);
141 }
142
143 block_ = topic;
144 }
145 else
146 {
147 block_ = new RBTree<uint32_t>::Node<Block>;
148 block_->data_.payload_type_id = payload_type_id;
149 block_->data_.payload_size = payload_size;
150 block_->data_.payload_alignment = payload_alignment;
151 block_->data_.crc32 = crc32;
152
153 if (multi_publisher)
154 {
155 block_->data_.mutex = new Mutex();
156 block_->data_.busy.store(LockState::USE_MUTEX, std::memory_order_release);
157 }
158 else
159 {
160 block_->data_.mutex = nullptr;
161 block_->data_.busy.store(LockState::UNLOCKED, std::memory_order_release);
162 }
163
164 domain->node_->data_.Insert(*block_, crc32);
165 }
166}
static uint32_t Calculate(const void *raw, size_t len)
计算数据的 CRC32 校验码 / Computes the CRC32 checksum for the given data
Definition crc_o3.cpp:35
互斥锁类,提供线程同步机制 Mutex class providing thread synchronization mechanisms.
Definition mutex.hpp:18
红黑树的泛型数据节点,继承自 BaseNode Generic data node for Red-Black Tree, inheriting from BaseNode.
Definition rbt.hpp:63
Data data_
存储的数据 / Stored data.
Definition rbt.hpp:98

◆ Topic() [3/3]

Topic::Topic ( TopicHandle topic)

从已有句柄构造一个 topic 视图 / Construct one topic view from an existing handle

Parameters
topic既有 topic 句柄 / Existing topic handle

Definition at line 168 of file topic.cpp.

168: block_(topic) {}

Member Function Documentation

◆ CreateTopic()

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

用精确类型创建或查找一个 topic / Create or look up one topic using one exact payload type

Template Parameters
Datapayload 类型 / Payload type
Parameters
nametopic 名称 / Topic name
domain可选主题域 / Optional topic domain
multi_publisher是否允许多发布者串行化 / Whether to allow serialized multi-publisher use
Returns
对应的 topic 视图 / Topic view of the requested topic

Definition at line 362 of file topic.hpp.

364 {
366 return Topic(name, TypeID::GetID<Data>(), sizeof(Data), alignof(Data), domain,
367 multi_publisher);
368 }
Topic()
构造一个空 topic 视图 / Construct one empty topic view
Definition topic.cpp:114
static ID GetID()
获取类型的唯一标识符 / Get a unique identifier for type T
constexpr void CheckTopicPayload()
在模板上下文里断言 payload 类型满足 topic 契约 / Assert in template context that one payload type satisfies the topi...
Definition topic.hpp:38

◆ Find()

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

按名称查找一个已存在 topic / Find one existing topic by name

Parameters
nametopic 名称 / Topic name
domain可选主题域 / Optional topic domain
Returns
找到则返回句柄,否则返回空 / Returns the handle when found, otherwise null

Definition at line 188 of file topic.cpp.

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

◆ FindOrCreate()

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

按精确类型查找或创建一个 topic / Find or create one topic with an exact payload type

Template Parameters
Datapayload 类型 / Payload type
Parameters
nametopic 名称 / Topic name
domain可选主题域 / Optional topic domain
multi_publisher是否允许多发布者串行化 / Whether to allow serialized multi-publisher use
Returns
topic 句柄 / Topic handle
Note
已有 topic 的冲突检查与构造函数相同,在所有构建类型中生效。 Conflicts with an existing topic are checked as in the constructor, in every build type.

Definition at line 399 of file topic.hpp.

401 {
403 auto topic = Find(name, domain);
404 if (topic != nullptr)
405 {
406 RequireTypeContract(topic, TypeID::GetID<Data>(), sizeof(Data), alignof(Data));
407 if (multi_publisher)
408 {
409 RequireMultiPublisher(topic);
410 }
411 }
412 else
413 {
414 topic = CreateTopic<Data>(name, domain, multi_publisher).block_;
415 }
416 return topic;
417 }
static Topic CreateTopic(const char *name, Domain *domain=nullptr, bool multi_publisher=false)
用精确类型创建或查找一个 topic / Create or look up one topic using one exact payload type
Definition topic.hpp:362
static TopicHandle Find(const char *name, Domain *domain=nullptr)
按名称查找一个已存在 topic / Find one existing topic by name
Definition topic.cpp:188

◆ GetKey()

uint32_t Topic::GetKey ( ) const

读取 topic 键值 / Read the key value of this topic

Returns
topic 键值 / Topic key value

Definition at line 227 of file topic.cpp.

228{
229 if (block_)
230 {
231 return block_->key;
232 }
233 else
234 {
235 return 0;
236 }
237}
Key key
节点键值 / Key associated with the node.
Definition rbt.hpp:40

◆ Lock()

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

在普通上下文里锁住一个 topic 发布路径 / Lock one topic publish path in normal context

Parameters
topic目标 topic 句柄 / Target topic handle

Definition at line 32 of file topic.cpp.

33{
34 if (topic->data_.mutex)
35 {
36 topic->data_.mutex->Lock();
37 }
38 else
39 {
40 LockState expected = LockState::UNLOCKED;
41 if (!topic->data_.busy.compare_exchange_strong(expected, LockState::LOCKED))
42 {
43 // 非 mutex topic 禁止并发发布
44 ASSERT(false);
45 return;
46 }
47 }
48}

◆ LockFromCallback()

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

在回调或 ISR 路径里锁住一个 topic 发布路径 / Lock one topic publish path from callback or ISR context

Parameters
topic目标 topic 句柄 / Target topic handle

Definition at line 62 of file topic.cpp.

63{
64 if (topic->data_.mutex)
65 {
66 ASSERT(false);
67 }
68 else
69 {
70 LockState expected = LockState::UNLOCKED;
71 if (!topic->data_.busy.compare_exchange_strong(expected, LockState::LOCKED))
72 {
73 // 回调发布路径要求外围先串行化
74 ASSERT(false);
75 return;
76 }
77 }
78}

◆ NowTimestamp()

MicrosecondTimestamp Topic::NowTimestamp ( )
static

读取当前时间戳 / Read the current timestamp

Returns
当前时间戳 / Current timestamp

Definition at line 80 of file publish.cpp.

80{ return Timebase::GetMicroseconds(); }
static MicrosecondTimestamp GetMicroseconds()
获取当前时间的微秒级时间戳。 Gets the current timestamp in microseconds.

◆ operator TopicHandle()

LibXR::Topic::operator TopicHandle ( )
inline

把 topic 视图转换为底层句柄 / Convert this topic view to the underlying handle

Returns
topic 句柄 / Topic handle

Definition at line 609 of file topic.hpp.

609{ return block_; }

◆ PackData() [1/2]

template<typename Data >
ErrorCode LibXR::Topic::PackData ( const Data & data,
PackedData< Data > & packet )
inline

将一个精确类型消息打包成 packet / Pack one exact-typed message into one packet using the topic's runtime contract and current timestamp

Template Parameters
Data负载类型 / Payload type
Parameters
data待打包 payload / Payload to pack
packet输出 packet 对象 / Output packed message object
Returns
操作结果错误码 / Error code

Definition at line 533 of file topic.hpp.

534 {
535 return PackData(data, packet, NowTimestamp());
536 }
static MicrosecondTimestamp NowTimestamp()
读取当前时间戳 / Read the current timestamp
Definition publish.cpp:80
ErrorCode PackData(const Data &data, PackedData< Data > &packet)
将一个精确类型消息打包成 packet / Pack one exact-typed message into one packet using the topic's runtime contract...
Definition topic.hpp:533

◆ PackData() [2/2]

template<typename Data >
ErrorCode LibXR::Topic::PackData ( const Data & data,
PackedData< Data > & packet,
MicrosecondTimestamp timestamp )

将一个精确类型消息按指定时间戳打包成 packet / Pack one exact-typed message into one packet with the given timestamp

按当前 topic 的名字和类型契约把一条消息打包成 packet / Pack one message into one packet using the current topic name and type contract

Template Parameters
Data负载类型 / Payload type
Parameters
data待打包 payload / Payload to pack
packet输出 packet 对象 / Output packed message object
timestamp指定时间戳 / Explicit timestamp
Returns
操作结果错误码 / Error code
Template Parameters
Datapayload 类型 / Payload type
Parameters
data待打包 payload / Payload to pack
packet输出 packet / Output packed message
timestamp要写入包头的时间戳 / Timestamp to store into the packet header
Returns
操作结果错误码 / Error code

Definition at line 112 of file packet.hpp.

114{
116 ASSERT(block_ != nullptr);
117 ASSERT(block_->data_.payload_type_id == TypeID::GetID<Data>());
118 ASSERT(block_->data_.payload_size == sizeof(Data));
119
120 PackBytes(block_->data_.crc32, RawData(packet), timestamp, ConstRawData(data));
121 return ErrorCode::OK;
122}
@ OK
操作成功 / Operation successful

◆ PackRaw() [1/2]

ErrorCode LibXR::Topic::PackRaw ( ConstRawData data,
RawData packet )
inline

将一段 raw payload 按当前 topic 元数据打包成 packet / Pack a raw payload byte view into one packet using the current topic metadata and current timestamp

Parameters
data待打包 payload 字节 / Payload bytes to pack
packet输出 packet 缓冲区 / Output packet buffer
Returns
操作结果错误码 / Error code

Definition at line 559 of file topic.hpp.

560 {
561 return PackRaw(data, packet, NowTimestamp());
562 }
ErrorCode PackRaw(ConstRawData data, RawData packet)
将一段 raw payload 按当前 topic 元数据打包成 packet / Pack a raw payload byte view into one packet using the curr...
Definition topic.hpp:559

◆ PackRaw() [2/2]

ErrorCode LibXR::Topic::PackRaw ( ConstRawData data,
RawData packet,
MicrosecondTimestamp timestamp )
inline

将一段 raw payload 按当前 topic 元数据和指定时间戳打包成 packet / Pack a raw payload byte view into one packet using the current topic metadata and timestamp

Parameters
data待打包 payload 字节 / Payload bytes to pack
packet输出 packet 缓冲区 / Output packet buffer
timestamp指定时间戳 / Explicit timestamp
Returns
操作结果错误码 / Error code

Definition at line 573 of file topic.hpp.

574 {
575 if (block_ == nullptr || data.addr_ == nullptr || packet.addr_ == nullptr)
576 {
577 return ErrorCode::PTR_NULL;
578 }
579
580 if (data.size_ != block_->data_.payload_size)
581 {
582 return ErrorCode::SIZE_ERR;
583 }
584
585 if (packet.size_ < PACK_BASE_SIZE + data.size_)
586 {
587 return ErrorCode::NO_BUFF;
588 }
589
590 PackBytes(block_->data_.crc32, packet, timestamp, data);
591 return ErrorCode::OK;
592 }
@ SIZE_ERR
尺寸错误 / Size error
@ PTR_NULL
空指针 / Null pointer
@ NO_BUFF
缓冲区不足 / Insufficient buffer

◆ PayloadAlignment()

size_t LibXR::Topic::PayloadAlignment ( ) const
inlinenodiscard

获取该 topic payload 的对齐要求 / Get the payload alignment requirement of this topic

Returns
payload 对齐要求 / Payload alignment requirement

Definition at line 434 of file topic.hpp.

435 {
436 ASSERT(block_ != nullptr);
437 return block_->data_.payload_alignment;
438 }

◆ PayloadSize()

size_t LibXR::Topic::PayloadSize ( ) const
inlinenodiscard

获取该 topic 固定 payload 字节数 / Get the fixed payload size of this topic

Returns
payload 字节数 / Payload size in bytes

Definition at line 423 of file topic.hpp.

424 {
425 ASSERT(block_ != nullptr);
426 return block_->data_.payload_size;
427 }

◆ Publish() [1/2]

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

在普通上下文里发布一条消息,并自动取当前时间戳 / Publish one message in normal context and stamp it with the current time

Template Parameters
Datapayload 类型 / Payload type
Parameters
data待发布的 payload 对象 / Payload object to publish

Definition at line 447 of file topic.hpp.

448 {
449 PublishTyped(data, NowTimestamp(), false, false);
450 }

◆ Publish() [2/2]

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

在普通上下文里按指定时间戳发布一条消息 / Publish one message in normal context with an explicit timestamp

Template Parameters
Datapayload 类型 / Payload type
Parameters
data待发布的 payload 对象 / Payload object to publish
timestamp消息时间戳 / Message timestamp

Definition at line 460 of file topic.hpp.

461 {
462 PublishTyped(data, timestamp, false, false);
463 }

◆ PublishBytesFromServer()

void LibXR::Topic::PublishBytesFromServer ( void * payload_addr,
size_t payload_size,
MicrosecondTimestamp timestamp )
inline

供 packet/server 路径按字节发布一条消息 / Publish one message from the packet/server path using bytes already arranged as the exact payload object

Parameters
payload_addr已满足对齐的 payload 起始地址 / Start address of the already-aligned payload object
payload_sizepayload 字节数,必须等于 topic 固定大小 / Payload size, must equal the fixed topic size
timestamp这条消息的时间戳 / Timestamp of this message

Definition at line 501 of file topic.hpp.

503 {
504 PublishServerBytes(payload_addr, payload_size, timestamp, false, false);
505 }

◆ PublishBytesFromServerCallback()

void LibXR::Topic::PublishBytesFromServerCallback ( void * payload_addr,
size_t payload_size,
MicrosecondTimestamp timestamp,
bool in_isr )
inline

供回调/ISR 上下文里的 packet/server 路径按字节发布一条消息 / Publish one packet/server message from callback/ISR context using bytes already arranged as the exact payload object

Parameters
payload_addr已满足对齐的 payload 起始地址 / Start address of the already-aligned payload object
payload_sizepayload 字节数,必须等于 topic 固定大小 / Payload size, must equal the fixed topic size
timestamp这条消息的时间戳 / Timestamp of this message
in_isr当前是否位于 ISR / Whether the current path is in ISR context

Definition at line 518 of file topic.hpp.

520 {
521 PublishServerBytes(payload_addr, payload_size, timestamp, true, in_isr);
522 }

◆ PublishFromCallback() [1/2]

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

在回调或 ISR 路径里发布一条消息,并自动取当前时间戳 / Publish one message from callback or ISR context and stamp it with the current time

Template Parameters
Datapayload 类型 / Payload type
Parameters
data待发布的 payload 对象 / Payload object to publish
in_isr当前是否位于 ISR / Whether the current path is in ISR context

Definition at line 473 of file topic.hpp.

474 {
475 PublishTyped(data, NowTimestamp(), true, in_isr);
476 }

◆ PublishFromCallback() [2/2]

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

在回调或 ISR 路径里按指定时间戳发布一条消息 / Publish one message from callback or ISR context with an explicit timestamp

Template Parameters
Datapayload 类型 / Payload type
Parameters
data待发布的 payload 对象 / Payload object to publish
timestamp消息时间戳 / Message timestamp
in_isr当前是否位于 ISR / Whether the current path is in ISR context

Definition at line 487 of file topic.hpp.

488 {
489 PublishTyped(data, timestamp, true, in_isr);
490 }

◆ RegisterCallback()

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

注册一个回调订阅者 / Register one callback subscriber

注册回调函数 / Register a callback function

Parameters
cb要注册的回调句柄 / Callback handle to register
cb需要注册的回调函数 / Callback function to register
Note
包含初始化期动态内存分配,回调订阅应长期存在 / Contains initialization-time dynamic allocation; callback subscriptions are expected to be long-lived
回调在发布锁内运行,不应重入发布同一个主题 / The callback runs while the publish lock is held and should not re-enter publishing on the same topic
类型化回调的 payload 类型与 topic 不一致时,在所有构建类型中进入致命错误 / A typed callback whose payload type differs from the topic raises a fatal error in every build type

Definition at line 620 of file callback.hpp.

621{
622 if (!cb.IsRawPayloadView())
623 {
624 RequireCallbackType(block_, cb.PayloadTypeID());
625 }
626
627 auto node = new (std::align_val_t(LibXR::CONCURRENCY_ALIGNMENT))
628 LockFreeList::Node<CallbackBlock>(cb, block_->data_.payload_size);
629 block_->data_.subers.Add(*node);
630}
constexpr size_t CONCURRENCY_ALIGNMENT
并发结构对齐粒度(用于降低多核伪共享) / Alignment policy used by concurrency-oriented structures
Definition libxr_def.hpp:61

◆ Unlock()

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

在普通上下文里释放一个 topic 发布路径 / Unlock one topic publish path in normal context

Parameters
topic目标 topic 句柄 / Target topic handle

Definition at line 50 of file topic.cpp.

51{
52 if (topic->data_.mutex)
53 {
54 topic->data_.mutex->Unlock();
55 }
56 else
57 {
58 topic->data_.busy.store(LockState::UNLOCKED, std::memory_order_release);
59 }
60}

◆ UnlockFromCallback()

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

在回调或 ISR 路径里释放一个 topic 发布路径 / Unlock one topic publish path from callback or ISR context

Parameters
topic目标 topic 句柄 / Target topic handle

Definition at line 80 of file topic.cpp.

81{
82 if (topic->data_.mutex)
83 {
84 ASSERT(false);
85 }
86 else
87 {
88 topic->data_.busy.store(LockState::UNLOCKED, std::memory_order_release);
89 }
90}

◆ WaitTopic()

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

等待指定名称的 topic 出现 / Wait until a topic with the given name exists

Parameters
nametopic 名称 / Topic name
timeout等待超时,默认永久等待 / Timeout in ticks, default wait forever
domain可选主题域 / Optional topic domain
Returns
找到则返回句柄,否则返回空 / Returns the handle when found, otherwise null

Definition at line 206 of file topic.cpp.

207{
208 const uint32_t start_time = Thread::GetTime();
209 TopicHandle topic = nullptr;
210 do
211 {
212 topic = Find(name, domain);
213 if (topic == nullptr)
214 {
215 if (timeout != UINT32_MAX &&
216 static_cast<uint32_t>(Thread::GetTime() - start_time) >= timeout)
217 {
218 return nullptr;
219 }
220 Thread::Sleep(1);
221 }
222 } while (topic == nullptr);
223
224 return topic;
225}
static uint32_t GetTime()
获取当前系统时间(毫秒) Gets the current system time in milliseconds
Definition thread.cpp:35
static void Sleep(uint32_t milliseconds)
让线程进入休眠状态 Puts the thread to sleep
Definition thread.cpp:15
RBTree< uint32_t >::Node< Block > * TopicHandle
指向一个 topic 运行时状态块的句柄 / Handle pointing to one topic runtime state block
Definition topic.hpp:125

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