CARLA
 
载入中...
搜索中...
未找到
listener.cpp
浏览该文件的文档.
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
9
10#include <boost/asio/post.hpp>
11
12#include "carla/Logging.h"
13
14#include <memory>
15
16namespace carla {
17namespace multigpu {
18
19 Listener::Listener(boost::asio::io_context &io_context, endpoint ep)
20 : _io_context(io_context),
21 _acceptor(_io_context, std::move(ep)),
22 _timeout(time_duration::seconds(1u)) {
23 _acceptor.listen();
24 }
25
27 Stop();
28 }
29
31 _acceptor.cancel();
32 _acceptor.close();
33 _io_context.stop();
34 _io_context.reset();
35 }
36
38 time_duration timeout,
39 callback_function_type on_opened,
40 callback_function_type on_closed,
42
43 using boost::system::error_code;
44
45 auto session = std::make_shared<Primary>(_io_context, timeout, *this);
46 auto self = shared_from_this();
47
48 auto handle_query = [on_opened, on_closed, on_response, session, self](const error_code &ec) {
49 if (!ec) {
50 session->Open(std::move(on_opened), std::move(on_closed), std::move(on_response));
51 } else {
52 log_error("Secondary server:", ec.message());
53 }
54 };
55
56 _acceptor.async_accept(session->_socket, [=](error_code ec) {
57 // Handle query and open a new session immediately.
58 boost::asio::post(_io_context, [=]() { handle_query(ec); });
59 OpenSession(timeout, on_opened, on_closed, on_response);
60 });
61 }
62
63} // namespace multigpu
64} // namespace carla
boost::asio::io_context & _io_context
Definition listener.h:73
boost::asio::ip::tcp::endpoint endpoint
Definition listener.h:29
boost::asio::ip::tcp::acceptor _acceptor
Definition listener.h:74
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
static void log_error(Args &&... args)
Definition Logging.h:110