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/Time.h"
11#include "carla/Buffer.h"
12
13#include <boost/asio/io_context.hpp>
14#include <boost/asio/ip/tcp.hpp>
15#include <boost/asio/post.hpp>
16
17#include <atomic>
18
19namespace carla {
20namespace multigpu {
21
22 class Primary;
23
24 /// @warning This server cannot be destructed before its @a io_context is
25 /// stopped.
26 class Listener : public std::enable_shared_from_this<Listener>, private NonCopyable {
27 public:
28
29 using endpoint = boost::asio::ip::tcp::endpoint;
30 using protocol_type = endpoint::protocol_type;
31 using Session = std::shared_ptr<Primary>;
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);
36 ~Listener();
37
39 return _acceptor.local_endpoint();
40 }
41
42 /// Set session time-out. Applies only to newly created sessions. By default
43 /// the time-out is set to 10 seconds.
44 void SetTimeout(time_duration timeout) {
45 _timeout = timeout;
46 }
47
48 /// Start listening for connections. On each new connection, @a
49 /// on_session_opened is called, and @a on_session_closed when the session
50 /// is closed, also @a on_response is called when an answer is received.
51 void Listen(callback_function_type on_session_opened,
52 callback_function_type on_session_closed,
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();
64
65 private:
66
67 void OpenSession(
68 time_duration timeout,
69 callback_function_type on_session_opened,
70 callback_function_type on_session_closed,
72
73 boost::asio::io_context &_io_context;
74 boost::asio::ip::tcp::acceptor _acceptor;
75 std::atomic<time_duration> _timeout;
76 };
77
78} // namespace multigpu
79} // namespace carla
A piece of raw data.
Inherit (privately) to suppress copy/move construction and assignment.
boost::asio::io_context & _io_context
Definition listener.h:73
std::atomic< time_duration > _timeout
Definition listener.h:75
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.h:74
void Listen(callback_function_type on_session_opened, callback_function_type on_session_closed, callback_function_type_response on_response)
Start listening for connections.
Definition listener.h:51
void SetTimeout(time_duration timeout)
Set session time-out.
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)
Definition listener.cpp:37
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)
Definition listener.cpp:19
Positive time duration up to milliseconds resolution.
Definition Time.h:19
This file contains definitions of common data structures used in traffic manager.
Definition Carla.cpp:133