CARLA
Loading...
Searching...
No Matches
EndPoint.h
Go to the documentation of this file.
1// Copyright (c) 2026 Computer Vision Center (CVC) at the Universitat Autonoma
2// de Barcelona (UAB).
3//
4// This work is licensed under the terms of the MIT license.
5// For a copy, see <https://opensource.org/licenses/MIT>.
6
7#pragma once
8
9#include <boost/asio/io_context.hpp>
10#include <boost/asio/ip/address.hpp>
11#include <boost/asio/ip/tcp.hpp>
12#include <boost/asio/ip/udp.hpp>
13
14namespace carla {
15namespace streaming {
16namespace detail {
17
18 // When in doubt, V4 addresses are returned.
19
22
23 template <typename Protocol, typename EndPointType>
24 class EndPoint;
25
26 template <typename Protocol>
27 class EndPoint<Protocol, FullyDefinedEndPoint> {
28 public:
29
30 explicit EndPoint(boost::asio::ip::basic_endpoint<Protocol> ep)
31 : _endpoint(std::move(ep)) {}
32
33 auto address() const {
34 return _endpoint.address();
35 }
36
37 uint16_t port() const {
38 return _endpoint.port();
39 }
40
41 operator boost::asio::ip::basic_endpoint<Protocol>() const {
42 return _endpoint;
43 }
44
45 private:
46
47 boost::asio::ip::basic_endpoint<Protocol> _endpoint;
48 };
49
50 template <typename Protocol>
52 public:
53
54 explicit EndPoint(uint16_t port) : _port(port) {}
55
56 uint16_t port() const {
57 return _port;
58 }
59
60 operator boost::asio::ip::basic_endpoint<Protocol>() const {
61 return {Protocol::v4(), _port};
62 }
63
64 private:
65
66 uint16_t _port;
67 };
68
69} // namespace detail
70
71 static inline auto make_localhost_address() {
72 return boost::asio::ip::make_address("127.0.0.1");
73 }
74
75 static inline auto make_address(const std::string &address) {
76 boost::asio::io_context io_context;
77 boost::asio::ip::tcp::resolver resolver(io_context);
78 boost::system::error_code ec;
79 auto results = resolver.resolve(boost::asio::ip::tcp::v4(), address, "", ec);
80 if (!ec) {
81 for (const auto &entry : results) {
82 return entry.endpoint().address();
83 }
84 }
85 return boost::asio::ip::make_address(address);
86 }
87
88 template <typename Protocol>
89 static inline auto make_endpoint(boost::asio::ip::basic_endpoint<Protocol> ep) {
91 }
92
93 template <typename Protocol>
94 static inline auto make_endpoint(const char *address, uint16_t port) {
95 return make_endpoint<Protocol>({make_address(address), port});
96 }
97
98 template <typename Protocol>
99 static inline auto make_endpoint(const std::string &address, uint16_t port) {
100 return make_endpoint<Protocol>(address.c_str(), port);
101 }
102
103 template <typename Protocol>
104 static inline auto make_endpoint(uint16_t port) {
106 }
107
108} // namespace streaming
109} // namespace carla
EndPoint(boost::asio::ip::basic_endpoint< Protocol > ep)
Definition EndPoint.h:30
boost::asio::ip::basic_endpoint< Protocol > _endpoint
Definition EndPoint.h:47
static auto make_address(const std::string &address)
Definition EndPoint.h:75
static auto make_localhost_address()
Definition EndPoint.h:71
static auto make_endpoint(boost::asio::ip::basic_endpoint< Protocol > ep)
Definition EndPoint.h:89
This file contains definitions of common data structures used in traffic manager.