Apollo 10.0
自动驾驶开放平台
shm_receiver.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_RECEIVER_SHM_RECEIVER_H_
18#define CYBER_TRANSPORT_RECEIVER_SHM_RECEIVER_H_
19
20#include <functional>
21
22#include "cyber/common/log.h"
26
27namespace apollo {
28namespace cyber {
29namespace transport {
30
31template <typename M>
32class ShmReceiver : public Receiver<M> {
33 public:
34 ShmReceiver(const RoleAttributes& attr,
35 const typename Receiver<M>::MessageListener& msg_listener);
36 virtual ~ShmReceiver();
37
38 void Enable() override;
39 void Disable() override;
40
41 void Enable(const RoleAttributes& opposite_attr) override;
42 void Disable(const RoleAttributes& opposite_attr) override;
43
44 private:
45 ShmDispatcherPtr dispatcher_;
46};
47
48template <typename M>
50 const RoleAttributes& attr,
51 const typename Receiver<M>::MessageListener& msg_listener)
52 : Receiver<M>(attr, msg_listener) {
53 dispatcher_ = ShmDispatcher::Instance();
54}
55
56template <typename M>
58 Disable();
59}
60
61template <typename M>
63 if (this->enabled_) {
64 return;
65 }
66
67 if (cyber::common::GlobalData::Instance()->IsChannelEnableArenaShm(
68 this->attr_.channel_id()) && message::MessageType<M>() != \
69 message::MessageType<message::RawMessage>()) {
70 auto arena_manager = ProtobufArenaManager::Instance();
71 if (!arena_manager->Enable() ||
72 !arena_manager->EnableSegment(this->attr_.channel_id())) {
73 AERROR << "arena manager enable failed.";
74 return;
75 }
76 }
77
78 dispatcher_->AddListener<M>(
79 this->attr_, std::bind(&ShmReceiver<M>::OnNewMessage, this,
80 std::placeholders::_1, std::placeholders::_2));
81 this->enabled_ = true;
82}
83
84template <typename M>
86 if (!this->enabled_) {
87 return;
88 }
89
90 dispatcher_->RemoveListener<M>(this->attr_);
91 this->enabled_ = false;
92}
93
94template <typename M>
95void ShmReceiver<M>::Enable(const RoleAttributes& opposite_attr) {
96 if (cyber::common::GlobalData::Instance()->IsChannelEnableArenaShm(
97 this->attr_.channel_id()) && message::MessageType<M>() != \
98 message::MessageType<message::RawMessage>()) {
99 auto arena_manager = ProtobufArenaManager::Instance();
100 if (!arena_manager->Enable() ||
101 !arena_manager->EnableSegment(this->attr_.channel_id())) {
102 AERROR << "arena manager enable failed.";
103 return;
104 }
105 }
106 dispatcher_->AddListener<M>(
107 this->attr_, opposite_attr,
108 std::bind(&ShmReceiver<M>::OnNewMessage, this, std::placeholders::_1,
109 std::placeholders::_2));
110}
111
112template <typename M>
113void ShmReceiver<M>::Disable(const RoleAttributes& opposite_attr) {
114 dispatcher_->RemoveListener<M>(this->attr_, opposite_attr);
115}
116
117} // namespace transport
118} // namespace cyber
119} // namespace apollo
120
121#endif // CYBER_TRANSPORT_RECEIVER_SHM_RECEIVER_H_
std::function< void(const MessagePtr &, const MessageInfo &, const RoleAttributes &)> MessageListener
Definition receiver.h:36
ShmReceiver(const RoleAttributes &attr, const typename Receiver< M >::MessageListener &msg_listener)
#define AERROR
Definition log.h:44
class register implement
Definition arena_queue.h:37