Branch data Line data Source code
1 : : // Copyright 2020 Silexica GmbH, Lichtstr. 25, Cologne, Germany. All rights reserved. 2 : : // 3 : : // Licensed under the Apache License, Version 2.0 (the "License"); 4 : : // you may not use this file except in compliance with the License. 5 : : // You may obtain a copy of the License at 6 : : // 7 : : // http://www.apache.org/licenses/LICENSE-2.0 8 : : // 9 : : // Unless required by applicable law or agreed to in writing, software 10 : : // distributed under the License is distributed on an "AS IS" BASIS, 11 : : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 : : // See the License for the specific language governing permissions and 13 : : // limitations under the License. 14 : : // 15 : : // Co-developed by Tier IV, Inc. and Apex.AI, Inc. 16 : : 17 : : #include <lidar_integration/udp_sender.hpp> 18 : : #include <common/types.hpp> 19 : : 20 : : #include <string.h> 21 : : #include <arpa/inet.h> 22 : : 23 : : #include <stdexcept> 24 : : #include <sstream> 25 : : 26 : : using autoware::common::types::bool8_t; 27 : : using autoware::common::types::char8_t; 28 : : 29 : 22 : UdpSenderBase::UdpSenderBase( 30 : : const char8_t * const ip, const uint16_t port, 31 : 22 : const bool8_t ipv6) 32 : 22 : : m_socket(-1) 33 : : { 34 : : // Create socket 35 [ + - ]: 44 : m_socket = socket(ipv6 ? AF_INET6 : AF_INET, SOCK_DGRAM, 0); 36 [ - + ]: 22 : if (m_socket < 0) { 37 [ # # ]: 0 : throw std::runtime_error("Failed to create socket"); 38 : : } 39 : : 40 : : // Setup address information for connecting 41 [ - + ]: 22 : memset(&m_addr, 0, sizeof(m_addr)); 42 [ - + ]: 22 : if (ipv6) { 43 : 0 : m_addr.sin6_family = AF_INET6; 44 : 0 : m_addr.sin6_port = htons(port); 45 : 0 : inet_pton(AF_INET6, ip, &m_addr.sin6_addr.s6_addr); 46 : : } else { 47 : : sockaddr_in * addr = reinterpret_cast<sockaddr_in *>(&m_addr); 48 : 22 : addr->sin_family = AF_INET; 49 : 22 : addr->sin_port = htons(port); 50 : 22 : inet_pton(AF_INET, ip, &addr->sin_addr); 51 : : } 52 : : 53 : : // Connect to address 54 [ + - ]: 44 : int ret = connect( 55 : : m_socket, reinterpret_cast<sockaddr *>(&m_addr), 56 : : static_cast<socklen_t>(ipv6 ? sizeof(sockaddr_in6) : sizeof(sockaddr_in))); 57 [ - + ]: 22 : if (ret != 0) { 58 [ # # ]: 0 : throw std::runtime_error("Could not connect"); 59 : : } 60 : 22 : } 61 : : 62 : 53091 : void UdpSenderBase::send(const void * const data, const size_t length) const 63 : : { 64 : 53091 : ::send(m_socket, data, length, 0); 65 : 53091 : }