CARLA
 
载入中...
搜索中...
未找到
streaming/detail/tcp/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/NonCopyable.h" // 引入carla命名空间下的NonCopyable类,该类用于防止对象被复制
10#include "carla/Time.h"
12
13#include <boost/asio/io_context.hpp> // 引入Boost库的asio模块中的io_context类,用于事件处理和I/O操作
14#include <boost/asio/ip/tcp.hpp> // 引入Boost库的asio模块中的ip/tcp协议支持类
15#include <boost/asio/post.hpp> // 引入Boost库的asio模块中的post函数,用于在io_context上安排函数执行
16
17#include <atomic> // 引入C++标准库中的原子操作模板,用于线程安全的共享变量操作
18
19namespace carla {
20namespace streaming {
21namespace detail {
22namespace tcp {
23
24 ///警告:在io_context停止之前,不能销毁这个服务器实例
25 class Server : private NonCopyable {
26 public:
27 // 使用using声明简化类型名称,方便后续代码中使用
28 using endpoint = boost::asio::ip::tcp::endpoint;
29 using protocol_type = endpoint::protocol_type;
30
31 explicit Server(boost::asio::io_context &io_context, endpoint ep); // 构造函数,接受一个io_context引用和一个endpoint作为参数
32
33 endpoint GetLocalEndpoint() const { // 获取服务器监听的本地端点
34 return _acceptor.local_endpoint();
35 }
36
37 /// 设置会话超时时间,仅对新创建的会话有效,默认为10秒
38 ///
39 void SetTimeout(time_duration timeout) {
40 _timeout = timeout;
41 }
42
43 // 开始监听连接,为每个新连接设置打开和关闭时的回调函数
44 //
45 template <typename FunctorT1, typename FunctorT2>
46 void Listen(FunctorT1 on_session_opened, FunctorT2 on_session_closed) {
47 boost::asio::post(_io_context, [=]() {
50 std::move(on_session_opened),
51 std::move(on_session_closed));
52 });
53 }
54
55 void SetSynchronousMode(bool is_synchro) { // 设置服务器是否运行在同步模式
56 _synchronous = is_synchro;
57 }
58
59 bool IsSynchronousMode() const { // 获取服务器是否运行在同步模式
60 return _synchronous;
61 }
62
63 private:
64
65 void OpenSession( // 私有方法,用于打开新的会话
66 time_duration timeout,
69
70 boost::asio::io_context &_io_context; // I/O上下文引用,用于事件处理和I/O操作
71
72 boost::asio::ip::tcp::acceptor _acceptor; // TCP协议的acceptor,用于接受客户端连接
73
74 std::atomic<time_duration> _timeout; // 原子操作的超时时间,用于线程安全的超时时间设置
75
76 bool _synchronous; // 布尔值,表示服务器是否运行在同步模式
77 };
78
79} // namespace tcp
80} // namespace detail
81} // namespace streaming
82} // namespace carla
包含Carla流处理模块中TCP通信相关类的头文件依赖。
这个类用于禁止拷贝和移动构造函数及赋值操作
std::function< void(std::shared_ptr< ServerSession >)> callback_function_type
回调函数类型别名。
警告:在io_context停止之前,不能销毁这个服务器实例
void SetTimeout(time_duration timeout)
设置会话超时时间,仅对新创建的会话有效,默认为10秒
void OpenSession(time_duration timeout, ServerSession::callback_function_type on_session_opened, ServerSession::callback_function_type on_session_closed)
Definition Server.cpp:26
void Listen(FunctorT1 on_session_opened, FunctorT2 on_session_closed)
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