Apollo 10.0
自动驾驶开放平台
apollo::bridge::UDPBridgeReceiverComponent< T > 模板类 参考final

#include <udp_bridge_receiver_component.h>

类 apollo::bridge::UDPBridgeReceiverComponent< T > 继承关系图:
apollo::bridge::UDPBridgeReceiverComponent< T > 的协作图:

Public 成员函数

 UDPBridgeReceiverComponent ()
 
 ~UDPBridgeReceiverComponent ()
 
bool Init () override
 
std::string Name () const
 
bool MsgHandle (int fd)
 
- Public 成员函数 继承自 apollo::cyber::Component< M0, M1, M2, M3 >
 Component ()
 
 ~Component () override
 
bool Initialize (const ComponentConfig &config) override
 init the component by protobuf object.
 
bool Process (const std::shared_ptr< M0 > &msg0, const std::shared_ptr< M1 > &msg1, const std::shared_ptr< M2 > &msg2, const std::shared_ptr< M3 > &msg3)
 
- Public 成员函数 继承自 apollo::cyber::ComponentBase
virtual ~ComponentBase ()
 
virtual bool Initialize (const TimerComponentConfig &config)
 
virtual void Shutdown ()
 
template<typename T >
bool GetProtoConfig (T *config) const
 

额外继承的成员函数

- Public 类型 继承自 apollo::cyber::ComponentBase
template<typename M >
using Reader = cyber::Reader< M >
 
- Protected 成员函数 继承自 apollo::cyber::ComponentBase
virtual void Clear ()
 
const std::string & ConfigFilePath () const
 
void LoadConfigFiles (const ComponentConfig &config)
 
void LoadConfigFiles (const TimerComponentConfig &config)
 
- Protected 属性 继承自 apollo::cyber::ComponentBase
std::atomic< bool > is_shutdown_ = {false}
 
std::shared_ptr< Nodenode_ = nullptr
 
std::string config_file_path_ = ""
 
std::vector< std::shared_ptr< ReaderBase > > readers_
 

详细描述

template<typename T>
class apollo::bridge::UDPBridgeReceiverComponent< T >

在文件 udp_bridge_receiver_component.h48 行定义.

构造及析构函数说明

◆ UDPBridgeReceiverComponent()

template<typename T >
apollo::bridge::UDPBridgeReceiverComponent< T >::UDPBridgeReceiverComponent ( )

◆ ~UDPBridgeReceiverComponent()

在文件 udp_bridge_receiver_component.cc34 行定义.

34 {
35 for (auto proto : proto_list_) {
36 FREE_POINTER(proto);
37 }
38}
#define FREE_POINTER(p)
Definition macro.h:30

成员函数说明

◆ Init()

template<typename T >
bool apollo::bridge::UDPBridgeReceiverComponent< T >::Init ( )
overridevirtual

实现了 apollo::cyber::ComponentBase.

在文件 udp_bridge_receiver_component.cc41 行定义.

41 {
42 AINFO << "UDP bridge receiver init, startin...";
44 if (!this->GetProtoConfig(&udp_bridge_remote)) {
45 AINFO << "load udp bridge component proto param failed";
46 return false;
47 }
48 bind_port_ = udp_bridge_remote.bind_port();
49 proto_name_ = udp_bridge_remote.proto_name();
50 topic_name_ = udp_bridge_remote.topic_name();
51 enable_timeout_ = udp_bridge_remote.enable_timeout();
52 ADEBUG << "UDP Bridge remote port is: " << bind_port_;
53 ADEBUG << "UDP Bridge for Proto is: " << proto_name_;
54 writer_ = node_->CreateWriter<T>(topic_name_.c_str());
55
56 if (!InitSession((uint16_t)bind_port_)) {
57 return false;
58 }
59 ADEBUG << "initialize session successful.";
60 MsgDispatcher();
61 return true;
62}
bool GetProtoConfig(T *config) const
std::shared_ptr< Node > node_
#define ADEBUG
Definition log.h:41
#define AINFO
Definition log.h:42

◆ MsgHandle()

template<typename T >
bool apollo::bridge::UDPBridgeReceiverComponent< T >::MsgHandle ( int  fd)

在文件 udp_bridge_receiver_component.cc135 行定义.

135 {
136 struct sockaddr_in client_addr;
137 socklen_t sock_len = static_cast<socklen_t>(sizeof(client_addr));
138 int bytes = 0;
139 int total_recv = 2 * FRAME_SIZE;
140 char total_buf[2 * FRAME_SIZE] = {0};
141 bytes =
142 static_cast<int>(recvfrom(fd, total_buf, total_recv, 0,
143 (struct sockaddr *)&client_addr, &sock_len));
144 ADEBUG << "total recv " << bytes;
145 if (bytes <= 0 || bytes > total_recv) {
146 return false;
147 }
148 char header_flag[sizeof(BRIDGE_HEADER_FLAG) + 1] = {0};
149 size_t offset = 0;
150 memcpy(header_flag, total_buf, HEADER_FLAG_SIZE);
151 if (strcmp(header_flag, BRIDGE_HEADER_FLAG) != 0) {
152 AINFO << "header flag not match!";
153 return false;
154 }
155 offset += sizeof(BRIDGE_HEADER_FLAG) + 1;
156
157 char header_size_buf[sizeof(hsize) + 1] = {0};
158 const char *cursor = total_buf + offset;
159 memcpy(header_size_buf, cursor, sizeof(hsize));
160 hsize header_size = *(reinterpret_cast<hsize *>(header_size_buf));
161 offset += sizeof(hsize) + 1;
162 if (header_size > FRAME_SIZE || header_size < offset) {
163 AINFO << "header size is more than FRAME_SIZE or less than offset!";
164 return false;
165 }
166
167 BridgeHeader header;
168 size_t buf_size = header_size - offset;
169 cursor = total_buf + offset;
170 if (!header.Diserialize(cursor, buf_size)) {
171 AINFO << "header diserialize failed!";
172 return false;
173 }
174
175 ADEBUG << "proto name : " << header.GetMsgName().c_str();
176 ADEBUG << "proto sequence num: " << header.GetMsgID();
177 ADEBUG << "proto total frames: " << header.GetTotalFrames();
178 ADEBUG << "proto frame index: " << header.GetIndex();
179
180 std::lock_guard<std::mutex> lock(mutex_);
181 BridgeProtoDiserializedBuf<T> *proto_buf = CreateBridgeProtoBuf(header);
182 if (!proto_buf) {
183 return false;
184 }
185
186 cursor = total_buf + header_size;
187 if (header.GetFramePos() > header.GetMsgSize()) {
188 return false;
189 }
190 char *buf = proto_buf->GetBuf(header.GetFramePos());
191 // check cursor size
192 if (header.GetFrameSize() < 0 ||
193 header.GetFrameSize() > (total_recv - header_size)) {
194 return false;
195 }
196 // check buf size
197 if (header.GetFrameSize() > (header.GetMsgSize() - header.GetFramePos())) {
198 return false;
199 }
200 memcpy(buf, cursor, header.GetFrameSize());
201 proto_buf->UpdateStatus(header.GetIndex());
202 if (proto_buf->IsReadyDiserialize()) {
203 auto pb_msg = std::make_shared<T>();
204 proto_buf->Diserialized(pb_msg);
205 writer_->Write(pb_msg);
206 RemoveInvalidBuf(proto_buf->GetMsgID());
207 RemoveItem(&proto_list_, proto_buf);
208 }
209 return true;
210}
constexpr uint32_t FRAME_SIZE
Definition macro.h:36
constexpr char BRIDGE_HEADER_FLAG[]
constexpr size_t HEADER_FLAG_SIZE
bool RemoveItem(std::vector< T * > *list, const T *t)
Definition util.h:47

◆ Name()

template<typename T >
std::string apollo::bridge::UDPBridgeReceiverComponent< T >::Name ( ) const
inline

在文件 udp_bridge_receiver_component.h55 行定义.

55{ return FLAGS_bridge_module_name; }

该类的文档由以下文件生成: