CARLA
 
载入中...
搜索中...
未找到
streaming/low_level/Client.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
11
12#include <boost/asio/io_context.hpp>
13
14#include <memory> // 引入C++标准库的内存管理头文件
15#include <unordered_map> // 引入C++标准库的无序映射容器头文件
16
17namespace carla {
18namespace streaming {
19namespace low_level {
20
21 /// A client able to subscribe to multiple streams. Accepts an external
22 /// io_context.
23 ///
24 /// @warning The client should not be destroyed before the @a io_context is
25 /// stopped.
26 template <typename T>
27 class Client {
28 public:
29
31 using protocol_type = typename underlying_client::protocol_type;
32 using token_type = carla::streaming::detail::token_type; // 使用类型别名来简化代码中对模板参数T及相关类型的引用
33
34 explicit Client(boost::asio::ip::address fallback_address)
35 : _fallback_address(std::move(fallback_address)) {} // 构造函数,接受一个boost::asio::ip::address类型的备用地址作为参数
36
37 explicit Client(const std::string &fallback_address)
38 : Client(carla::streaming::make_address(fallback_address)) {} // 构造函数,接受一个字符串形式的备用地址作为参数,会先将其转换为boost::asio::ip::address类型,再调用另一个构造函数进行初始化
39
40 explicit Client()
41 : Client(carla::streaming::make_localhost_address()) {} // 默认构造函数,会先获取本地主机地址作为备用地址,再调用另一个构造函数进行初始化
42
44 for (auto &pair : _clients) {
45 pair.second->Stop(); // 析构函数,用于在对象销毁时清理资源,会遍历所有已订阅的客户端并调用它们的停止方法
46 }
47 }
48
49 /// @warning cannot subscribe twice to the same stream (even if it's a
50 /// MultiStream).
51 template <typename Functor>
53 boost::asio::io_context &io_context,
54 token_type token, // 订阅流的方法,接受io_context、令牌以及回调函数作为参数
55 Functor &&callback) {
56 DEBUG_ASSERT_EQ(_clients.find(token.get_stream_id()), _clients.end()); // 断言确保当前要订阅的流ID在已订阅客户端的映射容器中不存在,即不能两次订阅同一个流
57 if (!token.has_address()) {
58 token.set_address(_fallback_address); // 如果传入的令牌没有地址,就将备用地址设置给令牌
59 }
60 auto client = std::make_shared<underlying_client>( // 创建一个底层客户端的智能指针,并通过底层客户端的构造函数进行初始化,传入io_context、令牌和回调函数
61 io_context,
62 token,
63 std::forward<Functor>(callback));
64 client->Connect(); // 让客户端尝试连接到对应的流
65 _clients.emplace(token.get_stream_id(), std::move(client)); // 将创建好的客户端智能指针以流ID为键存入到_clients映射容器中,以便后续管理和操作
66 }
67
68 void UnSubscribe(token_type token) { // 取消订阅流的方法,接受一个令牌作为参数
69 log_debug("calling sensor UnSubscribe()"); // 输出一条调试信息,表示正在调用取消订阅操作
70 auto it = _clients.find(token.get_stream_id()); // 在已订阅客户端的映射容器中查找与传入令牌的流ID对应的客户端指针
71 if (it != _clients.end()) { // 如果找到了对应的客户端指针,就调用它的停止方法停止相关操作,并从映射容器中删除该客户端的记录
72 it->second->Stop();
73 _clients.erase(it);
74 }
75 }
76
77 private:
78
79 boost::asio::ip::address _fallback_address; // 存储备用的IP地址,在构造函数中进行初始化,可能在流连接出现问题需要使用备用地址时发挥作用
80
81 std::unordered_map< // 一个无序映射容器,存储底层客户端的智能指针,用于管理和操作订阅的各个流对应的客户端
83 std::shared_ptr<underlying_client>> _clients;
84 };
85
86} // namespace low_level
87} // namespace streaming
88} // namespace carla
#define DEBUG_ASSERT_EQ(lhs, rhs)
Definition Debug.h:81
静态断言,用于确保token_data结构体的大小与Token::data的大小相同。
const auto & get_stream_id() const
获取流ID的引用。
void set_address(const boost::asio::ip::address &addr)
设置地址。
Definition Token.cpp:19
bool has_address() const
检查是否已设置地址。
A client able to subscribe to multiple streams.
std::unordered_map< detail::stream_id_type, std::shared_ptr< underlying_client > > _clients
void Subscribe(boost::asio::io_context &io_context, token_type token, Functor &&callback)
typename underlying_client::protocol_type protocol_type
Client(boost::asio::ip::address fallback_address)
Client(const std::string &fallback_address)
包含CARLA流处理相关头文件和Boost.Asio网络库头文件。 包含CARLA的调试功能相关定义。 包含CARLA流处理的端点(EndPoint)类定义。 包含CARLA流处理的令牌(Token)类...
uint32_t stream_id_type
流ID的类型定义。
Definition Types.h:33
static auto make_address(const std::string &address)
Definition EndPoint.h:85
static auto make_localhost_address()
Definition EndPoint.h:80
CARLA模拟器的主命名空间。
Definition Carla.cpp:139
static void log_debug(Args &&... args)
Definition Logging.h:71
包含CARLA客户端相关类和函数的命名空间。
本文件包含了网络通信相关类所需的头文件。