25RtpsDispatcher::RtpsDispatcher() : participant_(nullptr) {}
27RtpsDispatcher::~RtpsDispatcher() { Shutdown(); }
29void RtpsDispatcher::Shutdown() {
30 if (is_shutdown_.exchange(
true)) {
35 std::lock_guard<std::mutex> lock(subs_mutex_);
36 for (
auto& item : subs_) {
37 item.second.sub =
nullptr;
41 participant_ =
nullptr;
44void RtpsDispatcher::AddSubscriber(
const RoleAttributes& self_attr) {
45 if (participant_ ==
nullptr) {
46 AWARN <<
"please set participant firstly.";
51 std::lock_guard<std::mutex> lock(subs_mutex_);
52 if (subs_.count(channel_id) > 0) {
57 eprosima::fastrtps::SubscriberAttributes sub_attr;
62 auto listener_adapter = [
this, self_attr](uint64_t channel_id,
63 const std::shared_ptr<std::string>& msg_str,
64 const MessageInfo& msg_info) {
65 statistics::Statistics::Instance()->AddRecvCount(
66 self_attr, msg_info.msg_seq_num());
67 statistics::Statistics::Instance()->SetTotalMsgsStatus(
68 self_attr, msg_info.msg_seq_num());
69 this->OnMessage(channel_id, msg_str, msg_info);
72 new_sub.sub_listener = std::make_shared<SubListener>(listener_adapter);
74 new_sub.sub = eprosima::fastrtps::Domain::createSubscriber(
75 participant_->fastrtps_participant(), sub_attr,
76 new_sub.sub_listener.get());
78 subs_[channel_id] = new_sub;
81void RtpsDispatcher::OnMessage(uint64_t channel_id,
82 const std::shared_ptr<std::string>& msg_str,
83 const MessageInfo& msg_info) {
84 if (is_shutdown_.load()) {
89 if (msg_listeners_.Get(channel_id, &handler_base)) {
91 std::dynamic_pointer_cast<ListenerHandler<std::string>>(*handler_base);
92 handler->Run(msg_str, msg_info);
#define RETURN_IF_NULL(ptr)
#define RETURN_IF(condition)
std::shared_ptr< ListenerHandlerBase > ListenerHandlerBasePtr
optional QosProfile qos_profile
optional string channel_name
optional uint64 channel_id