CARLA
 
载入中...
搜索中...
未找到
ServerSession.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 * @file
10 * @brief 包含Carla流处理模块中TCP通信相关类的头文件依赖。
11 *
12 * 此文件汇集了Carla流处理模块中TCP通信功能所需的头文件,包括不可复制类、时间处理、类型特征、
13 * 性能分析、流处理细节类型、TCP消息类以及Boost.Asio的网络编程库。
14 */
15
16 /**
17 * @brief 引入Carla的不可复制类定义。
18 *
19 * 此类用于确保对象不能被复制,通常用于管理唯一资源或状态的对象。
20 */
21#include "carla/NonCopyable.h"
22 /**
23 * @brief 引入Carla的时间处理类定义。
24 *
25 * 此类提供了时间点的表示、时间间隔的计算以及时间相关的功能。
26 */
27#include "carla/Time.h"
28 /**
29 * @brief 引入Carla的类型特征库。
30 *
31 * 该库提供了类型检查和转换的功能,用于在编译时进行类型安全性和一致性的检查。
32 */
33#include "carla/TypeTraits.h"
34 /**
35 * @brief 引入Carla的性能分析类定义。
36 *
37 * 此类用于分析对象的生命周期性能,帮助开发者识别性能瓶颈。
38 */
40 /**
41 * @brief 引入Carla流处理模块中的底层细节类型定义。
42 *
43 * 此文件定义了流处理模块中使用的底层类型,如流ID和消息大小类型。
44 */
46 /**
47 * @brief 引入Carla流处理模块中TCP消息类的定义。
48 *
49 * 此类用于表示TCP通信中传输的消息,包括消息头和消息体。
50 */
52 /**
53 * @brief Clang编译器的警告控制区域开始。
54 *
55 * 如果使用Clang编译器,则忽略"-Wshadow"警告,该警告会在变量名遮蔽时触发。
56 */
57#if defined(__clang__)
58# pragma clang diagnostic push
59# pragma clang diagnostic ignored "-Wshadow"
60#endif
61 /**
62 * @brief 引入Boost.Asio的deadline_timer类。
63 *
64 * 该类用于在指定的时间点或经过指定的时间间隔后触发一个回调。
65 */
66#include <boost/asio/deadline_timer.hpp>
67 /**
68 * @brief 引入Boost.Asio的io_context类。
69 *
70 * 该类提供了I/O服务的核心功能,包括异步操作的调度和执行。
71 */
72#include <boost/asio/io_context.hpp>
73 /**
74 * @brief 引入Boost.Asio的TCP协议类。
75 *
76 * 该类提供了TCP协议的网络编程接口,包括套接字和端点的管理。
77 */
78#include <boost/asio/ip/tcp.hpp>
79 /**
80 * @brief 引入Boost.Asio的strand类。
81 *
82 * 该类用于确保在单个I/O上下文(io_context)中异步操作的顺序执行。
83 */
84#include <boost/asio/strand.hpp>
85 /**
86 * @brief Clang编译器的警告控制区域结束。
87 *
88 * 如果使用Clang编译器,则恢复之前的警告设置。
89 */
90#if defined(__clang__)
91# pragma clang diagnostic pop
92#endif
93 /**
94 * @brief 引入C++标准库中的functional头文件。
95 *
96 * 该头文件提供了函数对象、函数包装器以及标准函数适配器等功能。
97 */
98#include <functional>
99 /**
100 * @brief 引入C++标准库中的memory头文件。
101 *
102 * 该头文件提供了智能指针、动态内存分配和对象生命周期管理等功能。
103 */
104#include <memory>
105 /**
106 * @namespace carla::streaming::detail::tcp
107 * @brief 包含Carla流处理模块中TCP通信的详细实现。
108 */
109namespace carla {
110namespace streaming {
111namespace detail {
112namespace tcp {
113 /**
114 * @class Server
115 * @brief TCP服务器类的前向声明。
116 *
117 * 此类是TCP服务器类的前向声明,用于在ServerSession类中引用。
118 */
119 class Server;
120
121 /**
122 * @class ServerSession
123 * @brief TCP服务器会话类。
124 *
125 * 当会话打开时,它会从套接字读取一个流ID对象,并将自身传递给回调函数。如果在指定的不活动超时后没有活动,会话将自行关闭。
126 *
127 * 该类继承自std::enable_shared_from_this<ServerSession>,以便能够安全地生成自身的shared_ptr。同时,它私有继承自
128 * profiler::LifetimeProfiled用于性能分析,以及NonCopyable类以防止复制。
129 */
131 : public std::enable_shared_from_this<ServerSession>,
133 private NonCopyable {
134 public:
135 /**
136 * @brief 套接字类型别名。
137 */
138 using socket_type = boost::asio::ip::tcp::socket;
139 /**
140 * @brief 回调函数类型别名。
141 *
142 * 回调函数接受一个ServerSession的shared_ptr作为参数。
143 */
144 using callback_function_type = std::function<void(std::shared_ptr<ServerSession>)>;
145 /**
146 * @brief 构造函数。
147 *
148 * @param io_context I/O上下文,用于异步操作。
149 * @param timeout 不活动超时时间。
150 * @param server 对Server对象的引用。
151 */
152 explicit ServerSession(
153 boost::asio::io_context &io_context,
154 time_duration timeout,
155 Server &server);
156
157 /**
158 * @brief 启动会话。
159 *
160 * 成功读取流ID后调用@a on_opened回调,会话关闭时调用@a on_closed回调。
161 *
162 * @param on_opened 会话打开时的回调函数。
163 * @param on_closed 会话关闭时的回调函数。
164 */
165 void Open(
166 callback_function_type on_opened,
167 callback_function_type on_closed);
168
169 /**
170 * @warning 此函数只能在会话打开后调用。从回调函数中调用此函数是安全的。
171 *
172 * @brief 获取流ID。
173 *
174 * @return 流ID。
175 */
177 return _stream_id;
178 }
179 /**
180 * @brief 创建消息。
181 *
182 * 静态模板函数,接受一个或多个BufferView类型的参数,并创建一个Message对象的shared_ptr。
183 *
184 * @param buffers 一个或多个BufferView类型的参数。
185 * @return Message对象的shared_ptr。
186 *
187 * @note 此函数仅接受BufferView类型的参数。
188 */
189 template <typename... Buffers>
190 static auto MakeMessage(Buffers... buffers) {
191 static_assert(
192 are_same<SharedBufferView, Buffers...>::value,
193 "This function only accepts arguments of type BufferView.");
194 return std::make_shared<const Message>(buffers...);
195 }
196
197 /// @brief 向套接字写入一些数据。
198///
199/// 该函数将一个包含数据的消息对象写入到套接字中。
200 void Write(std::shared_ptr<const Message> message);
201
202 /// @brief 向套接字写入一些数据(模板函数)。
203 ///
204 /// 该模板函数接受任意数量的缓冲区参数,并将它们组合成一个消息对象,然后写入到套接字中。
205 /// @param buffers 要写入的缓冲区,类型应为 BufferView 或其兼容类型。
206 template <typename... Buffers>
207 void Write(Buffers... buffers) {
208 /// @details 内部调用 Write(std::shared_ptr<const Message> message) 函数,
209 /// 通过 MakeMessage 函数将缓冲区参数转换为消息对象。
210 Write(MakeMessage(buffers...));
211 }
212
213 /// @brief 发布一个关闭会话的任务。
214///
215/// 该函数安排一个任务来关闭当前会话,但不会立即关闭。
216 void Close();
217
218 private:
219 /// @brief 启动定时器。
220///
221/// 该函数用于启动一个定时器,该定时器在会话空闲时间超过指定时长后触发关闭操作。
222 void StartTimer();
223 /// @brief 立即关闭会话。
224///
225/// 该函数用于立即关闭会话,可选地接受一个错误代码参数来表示关闭的原因。
226/// @param ec 关闭会话时的错误代码,默认为无错误。
227 void CloseNow(boost::system::error_code ec = boost::system::error_code());
228 /// @brief 允许 Server 类访问私有成员。
229 friend class Server;
230 /// @brief 对 Server 对象的引用。
232 /// @brief 会话的唯一标识符。
233 const size_t _session_id;
234 /// @brief 流标识符,用于标识会话中传输的数据流。
236 /// @brief 套接字类型,用于网络通信。
238 /// @brief 会话超时时长,表示会话在多长时间内无活动将被关闭。
240 /// @brief 定时器,用于在会话超时后触发关闭操作。
241 boost::asio::deadline_timer _deadline;
242 /// @brief 用于保证异步操作顺序的 strand 对象。
243 boost::asio::io_context::strand _strand;
244 /// @brief 会话关闭时的回调函数。
246 /// @brief 表示当前是否正在进行写入操作的标志。
247 bool _is_writing = false;
248 };
249
250} // namespace tcp
251} // namespace detail
252} // namespace streaming
253} // namespace carla
包含 Carla 框架中与网络流相关的类和函数的声明。
包含与Carla流处理相关的底层细节的头文件。
这个类用于禁止拷贝和移动构造函数及赋值操作
stream_id_type get_stream_id() const
获取流ID。
boost::asio::deadline_timer _deadline
定时器,用于在会话超时后触发关闭操作。
void CloseNow(boost::system::error_code ec=boost::system::error_code())
立即关闭会话。
void Close()
发布一个关闭会话的任务。
boost::asio::ip::tcp::socket socket_type
套接字类型别名。
Server & _server
对 Server 对象的引用。
ServerSession(boost::asio::io_context &io_context, time_duration timeout, Server &server)
构造函数。
void Open(callback_function_type on_opened, callback_function_type on_closed)
启动会话。
callback_function_type _on_closed
会话关闭时的回调函数。
boost::asio::io_context::strand _strand
用于保证异步操作顺序的 strand 对象。
socket_type _socket
套接字类型,用于网络通信。
stream_id_type _stream_id
流标识符,用于标识会话中传输的数据流。
static auto MakeMessage(Buffers... buffers)
创建消息。
const size_t _session_id
会话的唯一标识符。
std::function< void(std::shared_ptr< ServerSession >)> callback_function_type
回调函数类型别名。
void Write(Buffers... buffers)
向套接字写入一些数据(模板函数)。
time_duration _timeout
会话超时时长,表示会话在多长时间内无活动将被关闭。
bool _is_writing
表示当前是否正在进行写入操作的标志。
void Write(std::shared_ptr< const Message > message)
向套接字写入一些数据。
警告:在io_context停止之前,不能销毁这个服务器实例
Positive time duration up to milliseconds resolution.
Definition Time.h:19
uint32_t stream_id_type
流ID的类型定义。
Definition Types.h:33
CARLA模拟器的主命名空间。
Definition Carla.cpp:139
std::shared_ptr< BufferView > SharedBufferView
Definition BufferView.h:163