Apollo 10.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() {
56 (*seq_num_) << 1;
57 return seq_num_->get_value();
58 }
59
60 uint64_t seq_num() const { return seq_num_->get_value(); }
61
62 protected:
64 std::shared_ptr<::bvar::Adder<int>> seq_num_;
65};
66
67template <typename M>
71 seq_num_ =
72 statistics::Statistics::Instance()->CreateAdder<int>(Endpoint::attr_);
73}
74
75template <typename M>
77
78template <typename M>
80 msg_info_.set_seq_num(NextSeqNum());
81 msg_info_.set_send_time(Time::Now().ToNanosecond());
82 PerfEventCache::Instance()->AddTransportEvent(
83 TransPerf::TRANSMIT_BEGIN, attr_.channel_id(), msg_info_.seq_num());
84 return Transmit(msg, msg_info_);
85}
86
87template <typename M>
88void Transmitter<M>::Enable(const RoleAttributes& opposite_attr) {
89 (void)opposite_attr;
90 Enable();
91}
92
93template <typename M>
94void Transmitter<M>::Disable(const RoleAttributes& opposite_attr) {
95 (void)opposite_attr;
96 Disable();
97}
98
99} // namespace transport
100} // namespace cyber
101} // namespace apollo
102
103#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)
virtual bool AcquireMessage(std::shared_ptr< M > &msg)=0
Transmitter(const RoleAttributes &attr)
Definition transmitter.h:68
std::shared_ptr<::bvar::Adder< int > > seq_num_
Definition transmitter.h:64
virtual bool Transmit(const MessagePtr &msg)
Definition transmitter.h:79
virtual bool Transmit(const MessagePtr &msg, const MessageInfo &msg_info)=0
class register implement
Definition arena_queue.h:37