|
libxr
1.0
Want to be the best embedded framework
|
发布订阅主题 / Publish-subscribe topic More...
#include <topic.hpp>
Data Structures | |
| 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 | |
Private Types | |
| enum class | LockState : uint32_t { UNLOCKED = 0 , LOCKED = 1 , USE_MUTEX = UINT32_MAX } |
| topic 发布路径的内部锁状态 / Internal lock state of the topic publish path More... | |
Private Member Functions | |
| template<typename Data > | |
| void | PublishTyped (Data &data, MicrosecondTimestamp timestamp, bool from_callback, bool in_isr) |
| 强类型发布入口的共享实现 / Shared implementation of typed publish entry points | |
| void | PublishServerBytes (void *payload_addr, size_t payload_size, MicrosecondTimestamp timestamp, bool from_callback, bool in_isr) |
PublishBytesFromServer*() 的共享实现 / Shared implementation behind PublishBytesFromServer*() | |
Static Private Member Functions | |
| static void | EnsureDomainRegistry () |
| 确保全局域注册表已创建 / Ensure the global domain registry exists | |
| static Domain * | EnsureDefaultDomain () |
| 确保默认域已创建 / Ensure the default domain exists | |
| static void | CheckServerPublishContract (TopicHandle topic, void *payload_addr, size_t payload_size) |
| 校验 server 侧字节发布前提 / Check the preconditions of one server-side byte publish | |
| template<typename Data > | |
| static void | CheckSubscriberType (Topic topic) |
| 断言订阅者看到的精确 payload 类型与 topic 契约一致 / Assert that the exact payload type seen by a subscriber matches the topic contract | |
| template<typename Data > | |
| static void * | AllocateSubscriberBuffer () |
| 为订阅者分配一个长期存在的本地接收对象 / Allocate one long-lived local receive object for a subscriber | |
| template<typename Data > | |
| static void | CopyPayload (void *dst, void *payload_addr) |
| 按精确类型把一份 payload 拷到订阅者缓冲区 / Copy one payload into a subscriber buffer using the exact type | |
| static void | CheckPublishContract (TopicHandle topic, TypeID::ID payload_type_id, size_t payload_size, size_t payload_alignment) |
| 校验一次强类型发布的运行时契约 / Check the runtime contract of one typed publish | |
| static void | PackBytes (uint32_t topic_name_crc32, RawData buffer, MicrosecondTimestamp timestamp, ConstRawData data) |
| 将一段 payload 字节和 topic 元数据拼成 packet / Pack one payload byte range together with topic metadata into one packet | |
| static void | DispatchSubscriber (SuberBlock &block, MicrosecondTimestamp timestamp, void *payload_addr, bool from_callback, bool in_isr) |
| 将一条消息分发给一个订阅块 / Dispatch one message to one subscriber block | |
| static void | DispatchSubscribers (TopicHandle topic, MicrosecondTimestamp timestamp, void *payload_addr, bool from_callback, bool in_isr) |
| 将一条消息分发给一个 topic 上的全部订阅者 / Dispatch one message to all subscribers attached to one topic | |
Private Attributes | |
| TopicHandle | block_ = nullptr |
Static Private Attributes | |
| static RBTree< uint32_t > * | domain_ |
| 全局 topic 域注册表。Global registry of topic domains. | |
| static Domain * | def_domain_ = nullptr |
| 缺省 topic 域。Default topic domain. | |
发布订阅主题 / 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.
| RBTree<uint32_t>::Node<Block>* LibXR::Topic::TopicHandle |
|
strong |
异步订阅者本地缓冲区的状态 / State of the async subscriber's local buffer
异步订阅者本地缓冲区状态 / State of one asynchronous subscriber local buffer
|
strongprivate |
topic 发布路径的内部锁状态 / Internal lock state of the topic publish path
|
strong |
topic 支持的订阅者种类 / Subscriber kinds supported by a topic
| Topic::Topic | ( | ) |
| 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
| name | topic 名称 / Topic name |
| payload_type_id | payload 类型标识 / 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 |
Definition at line 116 of file topic.cpp.
| Topic::Topic | ( | TopicHandle | topic | ) |
|
inlinestaticprivate |
为订阅者分配一个长期存在的本地接收对象 / Allocate one long-lived local receive object for a subscriber
| Data | 对象类型 / Object type |
Definition at line 669 of file topic.hpp.
|
staticprivate |
校验一次强类型发布的运行时契约 / Check the runtime contract of one typed publish
| topic | 目标 topic 句柄 / Target topic handle |
| payload_type_id | 本次发布的精确 payload 类型标识 / Exact payload type ID of this publish |
| payload_size | 本次发布的 payload 固定字节数 / Fixed payload size of this publish |
| payload_alignment | 本次发布的 payload 对齐要求 / Payload alignment requirement of this publish |
Definition at line 82 of file publish.cpp.
|
inlinestaticprivate |
校验 server 侧字节发布前提 / Check the preconditions of one server-side byte publish
| topic | 目标 topic / Target topic |
| payload_addr | 已满足对齐的 payload 地址 / Already-aligned payload address |
| payload_size | payload 字节数 / Payload size in bytes |
Definition at line 635 of file topic.hpp.
|
inlinestaticprivate |
断言订阅者看到的精确 payload 类型与 topic 契约一致 / Assert that the exact payload type seen by a subscriber matches the topic contract
| Data | 订阅类型 / Subscriber payload type |
| topic | 目标 topic 视图 / Target topic view |
Definition at line 653 of file topic.hpp.
|
inlinestaticprivate |
按精确类型把一份 payload 拷到订阅者缓冲区 / Copy one payload into a subscriber buffer using the exact type
| Data | payload 类型 / Payload type |
| dst | 订阅者缓冲区地址 / Subscriber buffer address |
| payload_addr | 本次发布 payload 地址 / Address of the payload object of the current publish |
Definition at line 684 of file topic.hpp.
|
inlinestatic |
用精确类型创建或查找一个 topic / Create or look up one topic using one exact payload type
| Data | payload 类型 / Payload type |
| name | topic 名称 / Topic name |
| domain | 可选主题域 / Optional topic domain |
| multi_publisher | 是否允许多发布者串行化 / Whether to allow serialized multi-publisher use |
|
staticprivate |
将一条消息分发给一个订阅块 / Dispatch one message to one subscriber block
| block | 目标订阅块 / Target subscriber block |
| timestamp | 消息时间戳 / Message timestamp |
| payload_addr | 本次发布 payload 地址 / Address of the payload object of the current publish |
| from_callback | 是否来自回调路径 / Whether this publish comes from callback path |
| in_isr | 当前是否位于 ISR / Whether the current path is in ISR context |
Definition at line 12 of file publish.cpp.
|
staticprivate |
将一条消息分发给一个 topic 上的全部订阅者 / Dispatch one message to all subscribers attached to one topic
| topic | 目标 topic 句柄 / Target topic handle |
| timestamp | 消息时间戳 / Message timestamp |
| payload_addr | 本次发布 payload 地址 / Address of the payload object of the current publish |
| from_callback | 是否来自回调路径 / Whether this publish comes from callback path |
| in_isr | 当前是否位于 ISR / Whether the current path is in ISR context |
Definition at line 69 of file publish.cpp.
|
staticprivate |
确保默认域已创建 / Ensure the default domain exists
Definition at line 22 of file topic.cpp.
|
staticprivate |
确保全局域注册表已创建 / Ensure the global domain registry exists
Definition at line 13 of file topic.cpp.
|
static |
按名称查找一个已存在 topic / Find one existing topic by name
| name | topic 名称 / Topic name |
| domain | 可选主题域 / Optional topic domain |
Definition at line 172 of file topic.cpp.
|
inlinestatic |
按精确类型查找或创建一个 topic / Find or create one topic with an exact payload type
| Data | payload 类型 / Payload type |
| name | topic 名称 / Topic name |
| domain | 可选主题域 / Optional topic domain |
| multi_publisher | 是否允许多发布者串行化 / Whether to allow serialized multi-publisher use |
Definition at line 391 of file topic.hpp.
| uint32_t Topic::GetKey | ( | ) | const |
|
static |
在普通上下文里锁住一个 topic 发布路径 / Lock one topic publish path in normal context
| topic | 目标 topic 句柄 / Target topic handle |
Definition at line 32 of file topic.cpp.
|
static |
|
static |
读取当前时间戳 / Read the current timestamp
Definition at line 80 of file publish.cpp.
|
inline |
|
staticprivate |
将一段 payload 字节和 topic 元数据拼成 packet / Pack one payload byte range together with topic metadata into one packet
| topic_name_crc32 | 目标 topic 的 CRC32 键 / CRC32 key of the target topic |
| buffer | 输出原始缓冲区 / Output raw buffer |
| timestamp | 消息时间戳 / Message timestamp |
| data | 待打包的 payload 字节 / Payload bytes to pack |
Definition at line 42 of file packet.cpp.
|
inline |
将一个精确类型消息打包成 packet / Pack one exact-typed message into one packet using the topic's runtime contract and current timestamp
| Data | 负载类型 / Payload type |
| data | 待打包 payload / Payload to pack |
| packet | 输出 packet 对象 / Output packed message object |
Definition at line 525 of file topic.hpp.
| 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
| Data | 负载类型 / Payload type |
| data | 待打包 payload / Payload to pack |
| packet | 输出 packet 对象 / Output packed message object |
| timestamp | 指定时间戳 / Explicit timestamp |
| Data | payload 类型 / Payload type |
| data | 待打包 payload / Payload to pack |
| packet | 输出 packet / Output packed message |
| timestamp | 要写入包头的时间戳 / Timestamp to store into the packet header |
Definition at line 112 of file packet.hpp.
|
inline |
将一段 raw payload 按当前 topic 元数据打包成 packet / Pack a raw payload byte view into one packet using the current topic metadata and current timestamp
| data | 待打包 payload 字节 / Payload bytes to pack |
| packet | 输出 packet 缓冲区 / Output packet buffer |
Definition at line 551 of file topic.hpp.
|
inline |
将一段 raw payload 按当前 topic 元数据和指定时间戳打包成 packet / Pack a raw payload byte view into one packet using the current topic metadata and timestamp
| data | 待打包 payload 字节 / Payload bytes to pack |
| packet | 输出 packet 缓冲区 / Output packet buffer |
| timestamp | 指定时间戳 / Explicit timestamp |
Definition at line 565 of file topic.hpp.
|
inlinenodiscard |
|
inlinenodiscard |
|
inline |
在普通上下文里发布一条消息,并自动取当前时间戳 / Publish one message in normal context and stamp it with the current time
| Data | payload 类型 / Payload type |
| data | 待发布的 payload 对象 / Payload object to publish |
Definition at line 439 of file topic.hpp.
|
inline |
在普通上下文里按指定时间戳发布一条消息 / Publish one message in normal context with an explicit timestamp
| Data | payload 类型 / Payload type |
| data | 待发布的 payload 对象 / Payload object to publish |
| timestamp | 消息时间戳 / Message timestamp |
|
inline |
供 packet/server 路径按字节发布一条消息 / Publish one message from the packet/server path using bytes already arranged as the exact payload object
| payload_addr | 已满足对齐的 payload 起始地址 / Start address of the already-aligned payload object |
| payload_size | payload 字节数,必须等于 topic 固定大小 / Payload size, must equal the fixed topic size |
| timestamp | 这条消息的时间戳 / Timestamp of this message |
Definition at line 493 of file topic.hpp.
|
inline |
供回调/ISR 上下文里的 packet/server 路径按字节发布一条消息 / Publish one packet/server message from callback/ISR context using bytes already arranged as the exact payload object
| payload_addr | 已满足对齐的 payload 起始地址 / Start address of the already-aligned payload object |
| payload_size | payload 字节数,必须等于 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 |
|
inline |
在回调或 ISR 路径里发布一条消息,并自动取当前时间戳 / Publish one message from callback or ISR context and stamp it with the current time
| Data | payload 类型 / Payload type |
| data | 待发布的 payload 对象 / Payload object to publish |
| in_isr | 当前是否位于 ISR / Whether the current path is in ISR context |
|
inline |
在回调或 ISR 路径里按指定时间戳发布一条消息 / Publish one message from callback or ISR context with an explicit timestamp
| Data | payload 类型 / Payload type |
| data | 待发布的 payload 对象 / Payload object to publish |
| timestamp | 消息时间戳 / Message timestamp |
| in_isr | 当前是否位于 ISR / Whether the current path is in ISR context |
|
inlineprivate |
PublishBytesFromServer*() 的共享实现 / Shared implementation behind PublishBytesFromServer*()
| payload_addr | 已满足对齐的 payload 地址 / Already-aligned payload address |
| payload_size | payload 字节数 / Payload size in bytes |
| timestamp | 消息时间戳 / Message timestamp |
| from_callback | 是否来自回调发布路径 / Whether this publish comes from callback path |
| in_isr | 当前是否位于 ISR / Whether the current path is in ISR context |
Definition at line 792 of file topic.hpp.
|
inlineprivate |
强类型发布入口的共享实现 / Shared implementation of typed publish entry points
| Data | payload 类型 / Payload type |
| data | 待发布 payload / Payload to publish |
| timestamp | 消息时间戳 / Message timestamp |
| from_callback | 是否来自回调路径 / Whether this publish comes from callback path |
| in_isr | 当前是否位于 ISR / Whether the current path is in ISR context |
Definition at line 703 of file topic.hpp.
|
inline |
注册一个回调订阅者 / Register one callback subscriber
注册回调函数 / Register a callback function
Definition at line 606 of file callback.hpp.
|
static |
|
static |
|
static |
等待指定名称的 topic 出现 / Wait until a topic with the given name exists
| name | topic 名称 / Topic name |
| timeout | 等待超时,默认永久等待 / Timeout in ticks, default wait forever |
| domain | 可选主题域 / Optional topic domain |
Definition at line 190 of file topic.cpp.
|
private |
|
inlinestaticprivate |
|
inlinestaticprivate |