CARLA
listener.cpp
Go to the documentation of this file.
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 
16 namespace carla {
17 namespace 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 
30  void Listener::Stop() {
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,
41  callback_function_type_response on_response) {
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
std::function< void(std::shared_ptr< Primary >, carla::Buffer)> callback_function_type_response
Definition: listener.h:33
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
boost::asio::io_context & _io_context
Definition: listener.h:73
std::function< void(std::shared_ptr< Primary >)> callback_function_type
Definition: listener.h:32
boost::asio::ip::tcp::acceptor _acceptor
Definition: listener.h:74
static void log_error(Args &&... args)
Definition: Logging.h:110
This file contains definitions of common data structures used in traffic manager. ...
Definition: Carla.cpp:133
boost::asio::ip::tcp::endpoint endpoint
Definition: listener.h:29
Listener(boost::asio::io_context &io_context, endpoint ep)
Definition: listener.cpp:19
Positive time duration up to milliseconds resolution.
Definition: Time.h:19