CARLA
 
载入中...
搜索中...
未找到
streaming/low_level/Server.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 "carla/streaming/detail/Dispatcher.h" // 引入 Dispatcher 头文件
10#include "carla/streaming/detail/Types.h" // 引入类型定义头文件
11#include "carla/streaming/Stream.h" // 引入 Stream 头文件
12
13#include <boost/asio/io_context.hpp> // 引入 Boost.Asio 的 IO 上下文头文件
14
15namespace carla {
16namespace streaming {
17namespace low_level {
18
19 /// 一个低级的流媒体服务器。每个新流都有一个关联的令牌,
20 /// 客户端可以使用这个令牌来订阅流。此服务器需要外部 io_context 运行。
21 ///
22 /// @warning 在停止 @a io_context 之前,无法析构此服务器。
23 template <typename T>
24 class Server {
25 public:
26
27 using underlying_server = T; // 定义底层服务器类型
28
29 using protocol_type = typename underlying_server::protocol_type; // 协议类型
30
32
34
35 // 构造函数,接受内部和外部端点类型
36 template <typename InternalEPType, typename ExternalEPType>
37 explicit Server(
38 boost::asio::io_context &io_context, // 输入 IO 上下文引用
39 detail::EndPoint<protocol_type, InternalEPType> internal_ep, // 内部端点
40 detail::EndPoint<protocol_type, ExternalEPType> external_ep) // 外部端点
41 : _server(io_context, std::move(internal_ep)), // 初始化底层服务器
42 _dispatcher(std::move(external_ep)) { // 初始化调度器
43 StartServer(); // 启动服务器
44 }
45
46 // 构造函数,仅接受内部端点类型
47 template <typename InternalEPType>
48 explicit Server(
49 boost::asio::io_context &io_context, // 输入 IO 上下文引用
50 detail::EndPoint<protocol_type, InternalEPType> internal_ep) // 内部端点
51 : _server(io_context, std::move(internal_ep)), // 初始化底层服务器
52 _dispatcher(make_endpoint<protocol_type>(_server.GetLocalEndpoint().port())) { // 创建调度器
53 StartServer(); // 启动服务器
54 }
55
56 // 通用构造函数,接受可变参数的端点
57 template <typename... EPArgs>
58 explicit Server(boost::asio::io_context &io_context, EPArgs &&... args)
59 : Server(io_context, make_endpoint<protocol_type>(std::forward<EPArgs>(args)...)) {} // 调用其他构造函数
60
61 // 获取本地端点
62 typename underlying_server::endpoint GetLocalEndpoint() const {
63 return _server.GetLocalEndpoint(); // 返回底层服务器的本地端点
64 }
65
66 // 设置超时时间
67 void SetTimeout(time_duration timeout) {
68 _server.SetTimeout(timeout); // 设置底层服务器的超时时间
69 }
70
71 // 创建流
73 return _dispatcher.MakeStream(); // 调用调度器创建流
74 }
75
76 // 关闭指定的流
78 return _dispatcher.CloseStream(id); // 调用调度器关闭流
79 }
80
81 // 设置同步模式
82 void SetSynchronousMode(bool is_synchro) {
83 _server.SetSynchronousMode(is_synchro); // 设置底层服务器的同步模式
84 }
85
86 // 获取流的令牌
88 return _dispatcher.GetToken(sensor_id); // 从调度器获取流的令牌
89 }
90
91 // 为 ROS 启用指定流
92 void EnableForROS(stream_id sensor_id) {
93 _dispatcher.EnableForROS(sensor_id); // 调用调度器启用流
94 }
95
96 // 为 ROS 禁用指定流
97 void DisableForROS(stream_id sensor_id) {
98 _dispatcher.DisableForROS(sensor_id); // 调用调度器禁用流
99 }
100
101 // 检查指定流是否为 ROS 启用
102 bool IsEnabledForROS(stream_id sensor_id) {
103 return _dispatcher.IsEnabledForROS(sensor_id); // 调用调度器检查流状态
104 }
105
106 private:
107
108 // 启动服务器的方法
109 void StartServer() {
110 // 会话打开时的回调
111 auto on_session_opened = [this](auto session) {
112 if (!_dispatcher.RegisterSession(session)) { // 注册会话
113 session->Close(); // 如果注册失败,关闭会话
114 }
115 };
116 // 会话关闭时的回调
117 auto on_session_closed = [this](auto session) {
118 log_debug("on_session_closed called"); // 日志记录会话关闭
119 _dispatcher.DeregisterSession(session); // 注销会话
120 };
121 _server.Listen(on_session_opened, on_session_closed); // 开始监听会话
122 }
123
124 underlying_server _server; // 底层服务器实例
125
126 detail::Dispatcher _dispatcher; // 调度器实例
127 };
128
129} // namespace low_level
130} // namespace streaming
131} // namespace carla
包含与Carla流处理相关的底层细节的头文件。
Keeps the mapping between streams and sessions.
Definition Dispatcher.h:30
void DeregisterSession(std::shared_ptr< Session > session)
carla::streaming::Stream MakeStream()
bool RegisterSession(std::shared_ptr< Session > session)
void DisableForROS(stream_id_type sensor_id)
Definition Dispatcher.h:56
bool IsEnabledForROS(stream_id_type sensor_id)
Definition Dispatcher.h:63
void CloseStream(carla::streaming::detail::stream_id_type id)
void EnableForROS(stream_id_type sensor_id)
Definition Dispatcher.h:49
token_type GetToken(stream_id_type sensor_id)
静态断言,用于确保token_data结构体的大小与Token::data的大小相同。
一个低级的流媒体服务器。每个新流都有一个关联的令牌, 客户端可以使用这个令牌来订阅流。此服务器需要外部 io_context 运行。
Server(boost::asio::io_context &io_context, detail::EndPoint< protocol_type, InternalEPType > internal_ep, detail::EndPoint< protocol_type, ExternalEPType > external_ep)
void CloseStream(carla::streaming::detail::stream_id_type id)
carla::streaming::detail::stream_id_type stream_id
typename underlying_server::protocol_type protocol_type
token_type GetToken(stream_id sensor_id)
Server(boost::asio::io_context &io_context, EPArgs &&... args)
Server(boost::asio::io_context &io_context, detail::EndPoint< protocol_type, InternalEPType > internal_ep)
underlying_server::endpoint GetLocalEndpoint() const
Positive time duration up to milliseconds resolution.
Definition Time.h:19
uint32_t stream_id_type
流ID的类型定义。
Definition Types.h:33
static auto make_endpoint(boost::asio::ip::basic_endpoint< Protocol > ep)
Definition EndPoint.h:102
CARLA模拟器的主命名空间。
Definition Carla.cpp:139
static void log_debug(Args &&... args)
Definition Logging.h:71