CARLA
 
载入中...
搜索中...
未找到
Server.cpp
浏览该文件的文档.
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#include "carla/streaming/detail/tcp/Server.h" // 包含服务器类的定义,该类负责TCP流的接收
8
9#include <boost/asio/post.hpp> // 包含boost库中用于在io_context上安排函数执行的函数
10
11#include "carla/Logging.h" // 包含carla库的日志记录功能,用于记录错误和其他信息
12
13#include <memory> // 包含C++标准库中的智能指针和内存管理工具
14
15namespace carla {
16namespace streaming {
17namespace detail {
18namespace tcp {
19
20 Server::Server(boost::asio::io_context &io_context, endpoint ep)
21 : _io_context(io_context), // 初始化io_context,用于处理异步操作
22 _acceptor(_io_context, std::move(ep)), // 初始化acceptor,绑定到提供的端点
23 _timeout(time_duration::seconds(10u)), // 设置接受连接的超时时间为10秒
24 _synchronous(false) {} // 初始化同步标志为false,表示异步操作
25
26 void Server::OpenSession( // 定义一个函数,用于开启新的会话
27 time_duration timeout, // 会话的超时时间
28 ServerSession::callback_function_type on_opened, // 会话开启时的回调函数
29 ServerSession::callback_function_type on_closed) { // 会话关闭时的回调函数
30 using boost::system::error_code; // 使用boost库中的错误代码类型
31
32 auto session = std::make_shared<ServerSession>(_io_context, timeout, *this); // 创建一个新的会话实例,与io_context和超时时间相关联
33
34 auto handle_query = [on_opened, on_closed, session](const error_code &ec) { // 定义一个lambda函数,用于处理异步接受连接的结果
35 if (!ec) {
36 session->Open(std::move(on_opened), std::move(on_closed)); // 如果没有错误,使用提供的回调函数打开会话
37 } else {
38 log_error("tcp accept stream error:", ec.message()); // 如果有错误,记录错误信息
39 }
40 };
41
42 _acceptor.async_accept(session->_socket, [=](error_code ec) { // 异步接受连接,当新的连接到达时,会调用提供的回调函数
43 // 立即处理查询并打开一个新的会话
44 boost::asio::post(_io_context, [=]() { handle_query(ec); }); // 在io_context上安排处理查询,确保在正确的线程上执行
45 OpenSession(timeout, on_opened, on_closed); // 递归调用OpenSession,以接受下一个连接
46 });
47 }
48
49} // namespace tcp
50} // namespace detail
51} // namespace streaming
52} // namespace carla
std::function< void(std::shared_ptr< ServerSession >)> callback_function_type
回调函数类型别名。
void OpenSession(time_duration timeout, ServerSession::callback_function_type on_session_opened, ServerSession::callback_function_type on_session_closed)
Definition Server.cpp:26
Server(boost::asio::io_context &io_context, endpoint ep)
Definition Server.cpp:20
Positive time duration up to milliseconds resolution.
Definition Time.h:19
CARLA模拟器的主命名空间。
Definition Carla.cpp:139
static void log_error(Args &&... args)
Definition Logging.h:115