Apollo 9.0
自动驾驶开放平台
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_TRANSMITTER_H_
18#define CYBER_TRANSPORT_TRANSMITTER_TRANSMITTER_H_
19
20#include <cstdint>
21#include <memory>
22#include <string>
23
28
29namespace apollo {
30namespace cyber {
31namespace transport {
32
35
36template <typename M>
37class Transmitter : public Endpoint {
38 public:
39 using MessagePtr = std::shared_ptr<M>;
40
41 explicit Transmitter(const RoleAttributes& attr);
42 virtual ~Transmitter();
43
44 virtual void Enable() = 0;
45 virtual void Disable() = 0;
46
47 virtual bool AcquireMessage(std::shared_ptr<M>& msg) = 0;
48
49 virtual void Enable(const RoleAttributes& opposite_attr);
50 virtual void Disable(const RoleAttributes& opposite_attr);
51
52 virtual bool Transmit(const MessagePtr& msg);
53 virtual bool Transmit(const MessagePtr& msg, const MessageInfo& msg_info) = 0;
54
55 uint64_t NextSeqNum() { return ++seq_num_; }
56
57 uint64_t seq_num() const { return seq_num_; }
58
59 protected:
60 uint64_t seq_num_;
62 std::shared_ptr<::bvar::Adder<int>> msg_counter_;
63};
64
65template <typename M>
67 : Endpoint(attr), seq_num_(0) {
71 statistics::Statistics::Instance()->CreateAdder<int>(Endpoint::attr_);
72}
73
74template <typename M>
76
77template <typename M>
79 (*msg_counter_) << 1;
80 msg_info_.set_seq_num(NextSeqNum());
81 msg_info_.set_msg_seq_num(msg_counter_->get_value());
82 msg_info_.set_send_time(Time::Now().ToNanosecond());
83 PerfEventCache::Instance()->AddTransportEvent(
84 TransPerf::TRANSMIT_BEGIN, attr_.channel_id(), msg_info_.seq_num());
85 return Transmit(msg, msg_info_);
86}
87
88template <typename M>
89void Transmitter<M>::Enable(const RoleAttributes& opposite_attr) {
90 (void)opposite_attr;
91 Enable();
92}
93
94template <typename M>
95void Transmitter<M>::Disable(const RoleAttributes& opposite_attr) {
96 (void)opposite_attr;
97 Disable();
98}
99
100} // namespace transport
101} // namespace cyber
102} // namespace apollo
103
104#endif // CYBER_TRANSPORT_TRANSMITTER_TRANSMITTER_H_
static Time Now()
get the current time.
Definition time.cc:57
void set_seq_num(uint64_t seq_num)
void set_sender_id(const Identity &sender_id)
std::shared_ptr<::bvar::Adder< int > > msg_counter_
Definition transmitter.h:62
virtual bool AcquireMessage(std::shared_ptr< M > &msg)=0
Transmitter(const RoleAttributes &attr)
Definition transmitter.h:66
virtual bool Transmit(const MessagePtr &msg)
Definition transmitter.h:78
virtual bool Transmit(const MessagePtr &msg, const MessageInfo &msg_info)=0
class register implement