Apollo 10.0
自动驾驶开放平台
udp_bridge_sender_component.cc
浏览该文件的文档.
1/******************************************************************************
2 * Copyright 2019 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
18
22
23namespace apollo {
24namespace bridge {
25
26#define BRIDGE_IMPL(pb_msg) template class UDPBridgeSenderComponent<pb_msg>
27
31
32template <typename T>
34 AINFO << "UDP bridge sender init, startin...";
36 if (!this->GetProtoConfig(&udp_bridge_remote)) {
37 AINFO << "load udp bridge component proto param failed";
38 return false;
39 }
40 remote_ip_ = udp_bridge_remote.remote_ip();
41 remote_port_ = udp_bridge_remote.remote_port();
42 proto_name_ = udp_bridge_remote.proto_name();
43 ADEBUG << "UDP Bridge remote ip is: " << remote_ip_;
44 ADEBUG << "UDP Bridge remote port is: " << remote_port_;
45 ADEBUG << "UDP Bridge for Proto is: " << proto_name_;
46 return true;
47}
48
49template <typename T>
50bool UDPBridgeSenderComponent<T>::Proc(const std::shared_ptr<T> &pb_msg) {
51 if (remote_port_ == 0 || remote_ip_.empty()) {
52 AERROR << "remote info is invalid!";
53 return false;
54 }
55
56 if (pb_msg == nullptr) {
57 AERROR << "proto msg is not ready!";
58 return false;
59 }
60
61 struct sockaddr_in server_addr;
62 server_addr.sin_addr.s_addr = inet_addr(remote_ip_.c_str());
63 server_addr.sin_family = AF_INET;
64 server_addr.sin_port = htons(static_cast<uint16_t>(remote_port_));
65 int sock_fd = socket(AF_INET, SOCK_DGRAM | SOCK_NONBLOCK, 0);
66
67 int res =
68 connect(sock_fd, (struct sockaddr *)&server_addr, sizeof(server_addr));
69 if (res < 0) {
70 close(sock_fd);
71 return false;
72 }
73
75 proto_buf.Serialize(pb_msg, proto_name_);
76 for (size_t j = 0; j < proto_buf.GetSerializedBufCount(); j++) {
77 ssize_t nbytes = send(sock_fd, proto_buf.GetSerializedBuf(j),
78 proto_buf.GetSerializedBufSize(j), 0);
79 if (nbytes != static_cast<ssize_t>(proto_buf.GetSerializedBufSize(j))) {
80 break;
81 }
82 }
83 close(sock_fd);
84
85 return true;
86}
87
90
91} // namespace bridge
92} // namespace apollo
#define BRIDGE_IMPL(type)
const char * GetSerializedBuf(size_t index) const
bool Serialize(const std::shared_ptr< T > &proto, const std::string &msg_name)
bool Proc(const std::shared_ptr< T > &pb_msg) override
#define ADEBUG
Definition log.h:41
#define AERROR
Definition log.h:44
#define AINFO
Definition log.h:42
class register implement
Definition arena_queue.h:37