Apollo 9.0
自动驾驶开放平台
rtps_transmitter.h
浏览该文件的文档.
1/******************************************************************************
2 * Copyright 2018 The Apollo Authors. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *****************************************************************************/
16
17#ifndef CYBER_TRANSPORT_TRANSMITTER_RTPS_TRANSMITTER_H_
18#define CYBER_TRANSPORT_TRANSMITTER_RTPS_TRANSMITTER_H_
19
20#include <memory>
21#include <string>
22
23#include "cyber/common/log.h"
26#include "cyber/time/time.h"
30#include "fastrtps/Domain.h"
31#include "fastrtps/attributes/PublisherAttributes.h"
32#include "fastrtps/participant/Participant.h"
33#include "fastrtps/publisher/Publisher.h"
34
35namespace apollo {
36namespace cyber {
37namespace transport {
38
39template <typename M>
40class RtpsTransmitter : public Transmitter<M> {
41 public:
42 using MessagePtr = std::shared_ptr<M>;
43
45 const ParticipantPtr& participant);
46 virtual ~RtpsTransmitter();
47
48 void Enable() override;
49 void Disable() override;
50
51 void Enable(const RoleAttributes& opposite_attr) override;
52 void Disable(const RoleAttributes& opposite_attr) override;
53
54 bool Transmit(const MessagePtr& msg, const MessageInfo& msg_info) override;
55
56 bool AcquireMessage(std::shared_ptr<M>& msg);
57
58 private:
59 bool Transmit(const M& msg, const MessageInfo& msg_info);
60
61 ParticipantPtr participant_;
62 eprosima::fastrtps::Publisher* publisher_;
63};
64
65template <typename M>
66bool RtpsTransmitter<M>::AcquireMessage(std::shared_ptr<M>& msg) {
67 return false;
68}
69
70template <typename M>
72 const ParticipantPtr& participant)
73 : Transmitter<M>(attr), participant_(participant), publisher_(nullptr) {}
74
75template <typename M>
79
80template <typename M>
81void RtpsTransmitter<M>::Enable(const RoleAttributes& opposite_attr) {
82 (void)opposite_attr;
83 this->Enable();
84}
85
86template <typename M>
87void RtpsTransmitter<M>::Disable(const RoleAttributes& opposite_attr) {
88 (void)opposite_attr;
89 this->Disable();
90}
91
92template <typename M>
94 if (this->enabled_) {
95 return;
96 }
97
98 RETURN_IF_NULL(participant_);
99
100 eprosima::fastrtps::PublisherAttributes pub_attr;
102 this->attr_.channel_name(), this->attr_.qos_profile(), &pub_attr));
103 publisher_ = eprosima::fastrtps::Domain::createPublisher(
104 participant_->fastrtps_participant(), pub_attr);
105 RETURN_IF_NULL(publisher_);
106 this->enabled_ = true;
107}
108
109template <typename M>
111 if (this->enabled_) {
112 publisher_ = nullptr;
113 this->enabled_ = false;
114 }
115}
116
117template <typename M>
119 const MessageInfo& msg_info) {
120 return Transmit(*msg, msg_info);
121}
122
123template <typename M>
124bool RtpsTransmitter<M>::Transmit(const M& msg, const MessageInfo& msg_info) {
125 if (!this->enabled_) {
126 ADEBUG << "not enable.";
127 return false;
128 }
129
130 UnderlayMessage m;
131 RETURN_VAL_IF(!message::SerializeToString(msg, &m.data()), false);
132
133 uint64_t send_time = msg_info.send_time();
134
135 m.timestamp(0x0fffffff & send_time);
136 m.seq(msg_info.msg_seq_num());
137
138 eprosima::fastrtps::rtps::WriteParams wparams;
139
140 char* ptr =
141 reinterpret_cast<char*>(&wparams.related_sample_identity().writer_guid());
142
143 memcpy(ptr, msg_info.sender_id().data(), ID_SIZE);
144 memcpy(ptr + ID_SIZE, msg_info.spare_id().data(), ID_SIZE);
145
146 wparams.related_sample_identity().sequence_number().high =
147 (int32_t)((msg_info.seq_num() & 0xFFFFFFFF00000000) >> 32);
148 wparams.related_sample_identity().sequence_number().low =
149 (int32_t)(msg_info.seq_num() & 0xFFFFFFFF);
150
151 if (participant_->is_shutdown()) {
152 return false;
153 }
154 return publisher_->write(reinterpret_cast<void*>(&m), wparams);
155}
156
157} // namespace transport
158} // namespace cyber
159} // namespace apollo
160
161#endif // CYBER_TRANSPORT_TRANSMITTER_RTPS_TRANSMITTER_H_
static bool FillInPubAttr(const std::string &channel_name, const QosProfile &qos, eprosima::fastrtps::PublisherAttributes *pub_attr)
const char * data() const
Definition identity.h:44
const Identity & sender_id() const
const Identity & spare_id() const
bool Transmit(const MessagePtr &msg, const MessageInfo &msg_info) override
RtpsTransmitter(const RoleAttributes &attr, const ParticipantPtr &participant)
bool AcquireMessage(std::shared_ptr< M > &msg)
#define RETURN_IF_NULL(ptr)
Definition log.h:90
#define RETURN_VAL_IF(condition, val)
Definition log.h:114
#define ADEBUG
Definition log.h:41
#define RETURN_IF(condition)
Definition log.h:106
std::enable_if< HasSerializeToString< T >::value, bool >::type SerializeToString(const T &message, std::string *str)
std::shared_ptr< Participant > ParticipantPtr
Definition participant.h:37
constexpr uint8_t ID_SIZE
Definition identity.h:28
class register implement