CARLA
 
载入中...
搜索中...
未找到
listener.h
浏览该文件的文档.
1// Copyright (c) 2022 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/NonCopyable.h"
10#include "carla/NonCopyable.h" // 包含禁止拷贝操作的NonCopyable基类定义,用于防止派生类被意外复制
11#include "carla/Time.h" // 包含时间相关的类定义
12#include "carla/Buffer.h" // 包含缓冲区类的定义
13// 引入Boost库中asio模块的io_context类,用于管理异步I/O操作的上下文环境
14#include <boost/asio/io_context.hpp>
15#include <boost/asio/ip/tcp.hpp>
16#include <boost/asio/post.hpp>
17// 包含原子操作类,用于实现多线程环境下对变量的原子性操作,保证数据的一致性和并发安全
18#include <atomic> // 包含原子操作类
19
20namespace carla {
21namespace multigpu {
22 // 前置声明一个名为Primary的类,这个类在其他地方定义,意味着此处只是告知编译器有这么一个类存在,后续会在别处完整定义它,并且它与Listener类存在交互关系
23 class Primary; // 声明一个名为Primary的类,这个类在其他地方定义,与Listener类有交互
24
25 /// 警告:在它的io_context停止之前,这个服务器不能被销毁。
26 class Listener : public std::enable_shared_from_this<Listener>, private NonCopyable {
27 public:
28
29 using endpoint = boost::asio::ip::tcp::endpoint; // 使用typedef定义endpoint类型
30 using protocol_type = endpoint::protocol_type; // 使用typedef定义protocol_type类型
31 using Session = std::shared_ptr<Primary>; // 使用typedef定义Session类型为Primary的shared_ptr
32 using callback_function_type = std::function<void(std::shared_ptr<Primary>)>; // 定义回调函数类型
33 using callback_function_type_response = std::function<void(std::shared_ptr<Primary>, carla::Buffer)>;// 定义带响应的回调函数类型
34
35 explicit Listener(boost::asio::io_context &io_context, endpoint ep); // 构造函数,接受一个io_context引用和一个endpoint对象
36 ~Listener(); // 析构函数,用于清理资源
37
38 endpoint GetLocalEndpoint() const { // 获取当前Listener绑定的本地端点
39 return _acceptor.local_endpoint();
40 }
41
42 /// 设置会话超时时间。这只适用于新创建的会话。默认情况下,超时时间设置为10秒。
43 ///
44 void SetTimeout(time_duration timeout) { // 设置会话超时时间,用于控制连接的超时行为
45 _timeout = timeout;
46 }
47
48 /// 开始监听连接。在每个新连接时,会调用on_session_opened,
49 /// 在会话关闭时调用on_session_closed,同时在收到响应时调用on_response。
50 ///
51 void Listen(callback_function_type on_session_opened,
52 callback_function_type on_session_closed,
53 callback_function_type_response on_response) { // 开始监听连接
54 boost::asio::post(_io_context, [=]() {
57 std::move(on_session_opened),
58 std::move(on_session_closed),
59 std::move(on_response));
60 });
61 }
62
63 void Stop(); // 停止监听TCP连接
64
65 private:
66
68 time_duration timeout,// time_duration 是一个表示时间长度的类型,可能是自定义的结构体或类
69// timeout 参数指定了某个操作(如打开或保持会话)的最大允许时间
70 callback_function_type on_session_opened,// callback_function_type 是一个函数类型别名,用于指定回调函数的签名
71// on_session_opened 是一个回调函数,当会话成功打开时被调用
72 callback_function_type on_session_closed,// on_session_closed 是一个回调函数,当会话关闭时被调用
73// 它允许开发者执行清理操作或处理会话结束后的逻辑
74 callback_function_type_response on_response); // 私有成员函数,用于处理新会话的打开
75
76 boost::asio::io_context &_io_context; // 引用io_context
77 boost::asio::ip::tcp::acceptor _acceptor; // TCP acceptor对象,用于接受新的TCP连接
78 std::atomic<time_duration> _timeout; // 原子变量,用于存储会话超时时间
79 };
80
81} // namespace multigpu
82} // namespace carla
一块原始数据。 请注意,如果需要更多容量,则会分配一个新的内存块,并 删除旧的内存块。这意味着默认情况下,缓冲区只能增长。要释放内存,使用 clear 或 pop。
boost::asio::io_context & _io_context
Definition listener.cpp:39
std::atomic< time_duration > _timeout
Definition listener.h:78
endpoint GetLocalEndpoint() const
Definition listener.h:38
std::shared_ptr< Primary > Session
Definition listener.h:31
boost::asio::ip::tcp::endpoint endpoint
Definition listener.h:29
boost::asio::ip::tcp::acceptor _acceptor
Definition listener.cpp:40
void Listen(callback_function_type on_session_opened, callback_function_type on_session_closed, callback_function_type_response on_response)
开始监听连接。在每个新连接时,会调用on_session_opened, 在会话关闭时调用on_session_closed,同时在收到响应时调用on_response。
Definition listener.h:51
void SetTimeout(time_duration timeout)
设置会话超时时间。这只适用于新创建的会话。默认情况下,超时时间设置为10秒。
Definition listener.h:44
endpoint::protocol_type protocol_type
Definition listener.h:30
std::function< void(std::shared_ptr< Primary >)> callback_function_type
Definition listener.h:32
void OpenSession(time_duration timeout, callback_function_type on_session_opened, callback_function_type on_session_closed, callback_function_type_response on_response)
std::function< void(std::shared_ptr< Primary >, carla::Buffer)> callback_function_type_response
Definition listener.h:33
Listener(boost::asio::io_context &io_context, endpoint ep)
void OpenSession(time_duration timeout, callback_function_type on_opened, callback_function_type on_closed, callback_function_type_response on_response)
Definition listener.cpp:66
Positive time duration up to milliseconds resolution.
Definition Time.h:19
CARLA模拟器的主命名空间。
Definition Carla.cpp:139