libxr  1.0
Want to be the best embedded framework
Loading...
Searching...
No Matches
LibXR::Topic::SyncSubscriber< Data > Class Template Reference

调用 Wait() 收消息的订阅者 / Subscriber that receives messages by calling Wait() More...

#include <sync.hpp>

Collaboration diagram for LibXR::Topic::SyncSubscriber< Data >:
[legend]

Public Member Functions

 SyncSubscriber (const char *name, Data &data, Domain *domain=nullptr)
 通过主题名称构造同步订阅者 / Construct a synchronous subscriber by topic name
 
 SyncSubscriber (Topic topic, Data &data)
 通过 Topic 句柄构造同步订阅者 / Construct a synchronous subscriber using a Topic handle
 
 SyncSubscriber (const SyncSubscriber &other)=delete
 禁止拷贝同步订阅者 / Copy construction is disabled for synchronous subscribers
 
SyncSubscriberoperator= (const SyncSubscriber &other)=delete
 禁止拷贝赋值同步订阅者 / Copy assignment is disabled for synchronous subscribers
 
 SyncSubscriber (SyncSubscriber &&other) noexcept
 移动构造同步订阅者 / Move-construct one synchronous subscriber
 
SyncSubscriberoperator= (SyncSubscriber &&other) noexcept
 移动赋值同步订阅者 / Move-assign one synchronous subscriber
 
ErrorCode Wait (uint32_t timeout=UINT32_MAX)
 等待接收数据 / Wait for data reception
 
MicrosecondTimestamp GetTimestamp () const
 获取最近一次接收的消息时间戳 / Get the latest received message timestamp
 

Data Fields

LockFreeList::Node< SyncBlock > * block_ = nullptr
 订阅者数据块。Subscriber data block.
 

Detailed Description

template<typename Data>
class LibXR::Topic::SyncSubscriber< Data >

调用 Wait() 收消息的订阅者 / Subscriber that receives messages by calling Wait()

通过 Wait() 接收消息的同步订阅者 / Synchronous subscriber receiving messages via Wait()

Template Parameters
Data订阅的数据类型 / Type of data being subscribed to
Data订阅的数据类型 / Subscribed data type

Definition at line 225 of file topic.hpp.

Constructor & Destructor Documentation

◆ SyncSubscriber() [1/4]

template<typename Data >
LibXR::Topic::SyncSubscriber< Data >::SyncSubscriber ( const char * name,
Data & data,
Domain * domain = nullptr )
inline

通过主题名称构造同步订阅者 / Construct a synchronous subscriber by topic name

Parameters
name主题名称 / Topic name
data用来接收消息的对象 / Destination object receiving subscribed data
domain可选的主题域 / Optional topic domain
Note
包含初始化期动态内存分配,订阅者应长期存在 / Contains initialization-time dynamic allocation; subscribers are expected to be long-lived
同步订阅者不会自建接收缓冲区,而是直接把收到的数据写进 datadata 必须至少活到订阅者不再使用为止 / Synchronous subscribers do not allocate their own receive buffer; they write incoming data directly into data, which must outlive the subscriber's use of it

Definition at line 60 of file sync.hpp.

61 : SyncSubscriber(Topic(WaitTopic(name, UINT32_MAX, domain)), data)
62 {
63 }
SyncSubscriber(const char *name, Data &data, Domain *domain=nullptr)
通过主题名称构造同步订阅者 / Construct a synchronous subscriber by topic name
Definition sync.hpp:60
static TopicHandle WaitTopic(const char *name, uint32_t timeout=UINT32_MAX, Domain *domain=nullptr)
等待指定名称的 topic 出现 / Wait until a topic with the given name exists
Definition topic.cpp:191
Topic()
构造一个空 topic 视图 / Construct one empty topic view
Definition topic.cpp:115

◆ SyncSubscriber() [2/4]

template<typename Data >
LibXR::Topic::SyncSubscriber< Data >::SyncSubscriber ( Topic topic,
Data & data )
inline

通过 Topic 句柄构造同步订阅者 / Construct a synchronous subscriber using a Topic handle

Parameters
topic订阅的主题 / Topic being subscribed to
data用来接收消息的对象 / Destination object receiving subscribed data
Note
包含初始化期动态内存分配,订阅者应长期存在 / Contains initialization-time dynamic allocation; subscribers are expected to be long-lived
同步订阅者不会自建接收缓冲区,而是直接把收到的数据写进 datadata 必须至少活到订阅者不再使用为止 / Synchronous subscribers do not allocate their own receive buffer; they write incoming data directly into data, which must outlive the subscriber's use of it

Definition at line 76 of file sync.hpp.

77 {
79
80 block_ = new LockFreeList::Node<SyncBlock>;
81 block_->data_.type = SuberType::SYNC;
82 block_->data_.timestamp = MicrosecondTimestamp();
83 block_->data_.wait_state.store(SyncBlock::WAIT_IDLE, std::memory_order_relaxed);
84 block_->data_.buff_addr = &data;
85 block_->data_.copy_payload = &Topic::CopyPayload<Data>;
86 topic.block_->data_.subers.Add(*block_);
87 }
LockFreeList::Node< SyncBlock > * block_
订阅者数据块。Subscriber data block.
Definition sync.hpp:194
@ SYNC
同步等待型订阅者。Synchronous wait-based subscriber.
static void CopyPayload(void *dst, void *payload_addr)
按精确类型把一份 payload 拷到订阅者缓冲区 / Copy one payload into a subscriber buffer using the exact type
Definition topic.hpp:605
static void CheckSubscriberType(Topic topic)
断言订阅者看到的精确 payload 类型与 topic 契约一致 / Assert that the exact payload type seen by a subscriber matches t...
Definition topic.hpp:574
@ WAIT_IDLE
当前没有挂起等待。No wait is currently pending.
Definition sync.hpp:25

◆ SyncSubscriber() [3/4]

template<typename Data >
LibXR::Topic::SyncSubscriber< Data >::SyncSubscriber ( const SyncSubscriber< Data > & other)
delete

禁止拷贝同步订阅者 / Copy construction is disabled for synchronous subscribers

Parameters
other待拷贝的同步订阅者 / Synchronous subscriber to copy from

◆ SyncSubscriber() [4/4]

template<typename Data >
LibXR::Topic::SyncSubscriber< Data >::SyncSubscriber ( SyncSubscriber< Data > && other)
inlinenoexcept

移动构造同步订阅者 / Move-construct one synchronous subscriber

Parameters
other被转移的同步订阅者 / Synchronous subscriber to move from
Note
这里只移动本地句柄指针;底层订阅块仍留在 topic 的订阅链表里,other 会被清成空句柄 / This moves only the local handle pointer; the underlying subscriber block stays registered in the topic list and other becomes empty

Definition at line 112 of file sync.hpp.

112 : block_(other.block_)
113 {
114 other.block_ = nullptr;
115 }

Member Function Documentation

◆ GetTimestamp()

template<typename Data >
MicrosecondTimestamp LibXR::Topic::SyncSubscriber< Data >::GetTimestamp ( ) const
inline

获取最近一次接收的消息时间戳 / Get the latest received message timestamp

Returns
最近一次接收的消息时间戳 / Returns the latest received message timestamp

Definition at line 192 of file sync.hpp.

192{ return block_->data_.timestamp; }

◆ operator=() [1/2]

template<typename Data >
SyncSubscriber & LibXR::Topic::SyncSubscriber< Data >::operator= ( const SyncSubscriber< Data > & other)
delete

禁止拷贝赋值同步订阅者 / Copy assignment is disabled for synchronous subscribers

Parameters
other待拷贝的同步订阅者 / Synchronous subscriber to copy from
Returns
当前同步订阅者 / Returns the current synchronous subscriber

◆ operator=() [2/2]

template<typename Data >
SyncSubscriber & LibXR::Topic::SyncSubscriber< Data >::operator= ( SyncSubscriber< Data > && other)
inlinenoexcept

移动赋值同步订阅者 / Move-assign one synchronous subscriber

Parameters
other被转移的同步订阅者 / Synchronous subscriber to move from
Returns
当前同步订阅者 / Returns the current synchronous subscriber
Note
这里只改当前包装对象指向的订阅块,不会注销旧块;底层订阅块仍留在 topic 的订阅链表里,other 会被清成空句柄 / This only changes which subscriber block the current wrapper points to and does not unregister the old block; the underlying subscriber blocks stay in the topic list and other becomes empty

Definition at line 127 of file sync.hpp.

128 {
129 if (this != &other)
130 {
131 block_ = other.block_;
132 other.block_ = nullptr;
133 }
134 return *this;
135 }

◆ Wait()

template<typename Data >
ErrorCode LibXR::Topic::SyncSubscriber< Data >::Wait ( uint32_t timeout = UINT32_MAX)
inline

等待接收数据 / Wait for data reception

Parameters
timeout超时时间,默认为 UINT32_MAX / Timeout period, default UINT32_MAX
Returns
操作结果错误码 / Error code indicating the operation result
Note
同一时刻只允许一个等待者;若已有挂起等待,会返回 ErrorCode::BUSY / Only one waiter is allowed at a time; if another wait is already pending, this returns ErrorCode::BUSY
若发布恰好发生在超时边界,当前等待者仍会保留那次唤醒并最终返回 OK,不会把 这次唤醒漏给下一次 Wait() / If a publish arrives right at the timeout boundary, the current waiter still keeps that wakeup and eventually returns OK instead of leaking the wakeup to a later Wait()

Definition at line 151 of file sync.hpp.

152 {
153 ASSERT(block_ != nullptr);
154
155 auto& data = block_->data_;
156 uint32_t expected = SyncBlock::WAIT_IDLE;
157 if (!data.wait_state.compare_exchange_strong(expected, SyncBlock::WAITING,
158 std::memory_order_acq_rel,
159 std::memory_order_acquire))
160 {
161 return ErrorCode::BUSY;
162 }
163
164 auto wait_ans = data.sem.Wait(timeout);
165 if (wait_ans == ErrorCode::OK)
166 {
167 data.wait_state.store(SyncBlock::WAIT_IDLE, std::memory_order_release);
168 return ErrorCode::OK;
169 }
170
171 expected = SyncBlock::WAITING;
172 if (data.wait_state.compare_exchange_strong(expected, SyncBlock::WAIT_IDLE,
173 std::memory_order_acq_rel,
174 std::memory_order_acquire))
175 {
176 return wait_ans;
177 }
178
179 ASSERT(data.wait_state.load(std::memory_order_acquire) == SyncBlock::WAIT_CLAIMED);
180
181 auto finish_wait_ans = data.sem.Wait(UINT32_MAX);
182 UNUSED(finish_wait_ans);
183 ASSERT(finish_wait_ans == ErrorCode::OK);
184 data.wait_state.store(SyncBlock::WAIT_IDLE, std::memory_order_release);
185 return ErrorCode::OK;
186 }
@ BUSY
忙碌 | Busy
@ OK
操作成功 | Operation successful
@ WAITING
当前有一个挂起的等待者。One waiter is currently pending.
Definition sync.hpp:26
@ WAIT_CLAIMED
某次发布已归一个刚超时的等待者所有。One publish wakeup is reserved for a waiter that just timed out.
Definition sync.hpp:27

Field Documentation

◆ block_

template<typename Data >
LockFreeList::Node<SyncBlock>* LibXR::Topic::SyncSubscriber< Data >::block_ = nullptr

订阅者数据块。Subscriber data block.

Definition at line 194 of file sync.hpp.


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