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> // 引入ASIO库的io_context
10#include <boost/asio/ip/address.hpp> // 引入IP地址相关的功能
11#include <boost/asio/ip/tcp.hpp> // 引入TCP协议相关的功能
12#include <boost/asio/ip/udp.hpp> // 引入UDP协议相关的功能
13
14namespace carla {
15namespace streaming {
16namespace detail {
17
18// 当不确定时,返回V4地址。
19struct FullyDefinedEndPoint {}; // 完全定义的端点结构体
20struct PartiallyDefinedEndPoint {}; // 部分定义的端点结构体
21
22template <typename Protocol, typename EndPointType>
23class EndPoint; // 声明模板类EndPoint
24
25// 完全定义的端点类
26template <typename Protocol>
28public:
29
30 // 构造函数,接受一个基本端点并进行移动
31 explicit EndPoint(boost::asio::ip::basic_endpoint<Protocol> ep)
32 : _endpoint(std::move(ep)) {}
33
34 // 返回地址
35 auto address() const {
36 return _endpoint.address();
37 }
38
39 // 返回端口
40 uint16_t port() const {
41 return _endpoint.port();
42 }
43
44 // 类型转换操作符,转换为基本端点类型
45 operator boost::asio::ip::basic_endpoint<Protocol>() const {
46 return _endpoint;
47 }
48
49private:
50 // 存储基本端点
51 boost::asio::ip::basic_endpoint<Protocol> _endpoint;
52};
53
54// 部分定义的端点类
55template <typename Protocol>
57public:
58
59 // 构造函数,接受端口号
60 explicit EndPoint(uint16_t port) : _port(port) {}
61
62 // 返回端口
63 uint16_t port() const {
64 return _port;
65 }
66
67 // 类型转换操作符,转换为基本端点类型,使用默认地址
68 operator boost::asio::ip::basic_endpoint<Protocol>() const {
69 return {Protocol::v4(), _port}; // 默认使用IPv4地址
70 }
71
72private:
73 // 存储端口号
74 uint16_t _port;
75};
76
77} // namespace detail
78
79// 创建本地主机地址的静态方法
80static inline auto make_localhost_address() {
81 return boost::asio::ip::make_address("127.0.0.1"); // 返回127.0.0.1地址
82}
83
84// 根据字符串地址创建IP地址的静态方法
85static inline auto make_address(const std::string &address) {
86 boost::asio::io_context io_context; // 创建io_context对象
87 boost::asio::ip::tcp::resolver resolver(io_context); // 创建解析器
88 // 查询IPv4地址对应的主机名
89 boost::asio::ip::tcp::resolver::query query(boost::asio::ip::tcp::v4(), address, "", boost::asio::ip::tcp::resolver::query::canonical_name);
90 boost::asio::ip::tcp::resolver::iterator iter = resolver.resolve(query); // 执行解析
91 boost::asio::ip::tcp::resolver::iterator end; // 结束迭代器
92 while (iter != end) // 遍历解析结果
93 {
94 boost::asio::ip::tcp::endpoint endpoint = *iter++; // 获取当前端点
95 return endpoint.address(); // 返回地址
96 }
97 return boost::asio::ip::make_address(address); // 如果没有找到,则返回给定地址
98}
99
100// 创建完全定义的端点的静态方法
101template <typename Protocol>
102static inline auto make_endpoint(boost::asio::ip::basic_endpoint<Protocol> ep) {
103 return detail::EndPoint<Protocol, detail::FullyDefinedEndPoint>{std::move(ep)}; // 使用给定的端点创建EndPoint
104}
105
106// 根据字符地址和端口号创建端点的静态方法
107template <typename Protocol>
108static inline auto make_endpoint(const char *address, uint16_t port) {
109 return make_endpoint<Protocol>({make_address(address), port}); // 创建端点
110}
111
112// 根据字符串地址和端口号创建端点的静态方法
113template <typename Protocol>
114static inline auto make_endpoint(const std::string &address, uint16_t port) {
115 return make_endpoint<Protocol>(address.c_str(), port); // 调用重载方法
116}
117
118// 根据端口号创建部分定义的端点的静态方法
119template <typename Protocol>
120static inline auto make_endpoint(uint16_t port) {
121 return detail::EndPoint<Protocol, detail::PartiallyDefinedEndPoint>{port}; // 创建部分定义的端点
122}
123
124} // namespace streaming
125} // namespace carla
auto end() const noexcept
EndPoint(boost::asio::ip::basic_endpoint< Protocol > ep)
Definition EndPoint.h:31
boost::asio::ip::basic_endpoint< Protocol > _endpoint
Definition EndPoint.h:51
static auto make_address(const std::string &address)
Definition EndPoint.h:85
static auto make_localhost_address()
Definition EndPoint.h:80
static auto make_endpoint(boost::asio::ip::basic_endpoint< Protocol > ep)
Definition EndPoint.h:102
CARLA模拟器的主命名空间。
Definition Carla.cpp:139