CARLA
 
载入中...
搜索中...
未找到
EndPoint.h
浏览该文件的文档.
1// Copyright (c) 2017 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::asio::ip::tcp::resolver::query query(boost::asio::ip::tcp::v4(), address, "", boost::asio::ip::tcp::resolver::query::canonical_name);
79 boost::asio::ip::tcp::resolver::iterator iter = resolver.resolve(query);
80 boost::asio::ip::tcp::resolver::iterator end;
81 while (iter != end)
82 {
83 boost::asio::ip::tcp::endpoint endpoint = *iter++;
84 return endpoint.address();
85 }
86 return boost::asio::ip::make_address(address);
87 }
88
89 template <typename Protocol>
90 static inline auto make_endpoint(boost::asio::ip::basic_endpoint<Protocol> ep) {
92 }
93
94 template <typename Protocol>
95 static inline auto make_endpoint(const char *address, uint16_t port) {
96 return make_endpoint<Protocol>({make_address(address), port});
97 }
98
99 template <typename Protocol>
100 static inline auto make_endpoint(const std::string &address, uint16_t port) {
101 return make_endpoint<Protocol>(address.c_str(), port);
102 }
103
104 template <typename Protocol>
105 static inline auto make_endpoint(uint16_t port) {
107 }
108
109} // namespace streaming
110} // 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:90
This file contains definitions of common data structures used in traffic manager.
Definition Carla.cpp:133