CARLA
Server.cpp
Go to the documentation of this file.
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 
8 
9 #include <boost/asio/post.hpp>
10 
11 #include "carla/Logging.h"
12 
13 #include <memory>
14 
15 namespace carla {
16 namespace streaming {
17 namespace detail {
18 namespace tcp {
19 
20  Server::Server(boost::asio::io_context &io_context, endpoint ep)
21  : _io_context(io_context),
22  _acceptor(_io_context, std::move(ep)),
23  _timeout(time_duration::seconds(10u)),
24  _synchronous(false) {}
25 
27  time_duration timeout,
30  using boost::system::error_code;
31 
32  auto session = std::make_shared<ServerSession>(_io_context, timeout, *this);
33 
34  auto handle_query = [on_opened, on_closed, session](const error_code &ec) {
35  if (!ec) {
36  session->Open(std::move(on_opened), std::move(on_closed));
37  } else {
38  log_error("tcp accept stream error:", ec.message());
39  }
40  };
41 
42  _acceptor.async_accept(session->_socket, [=](error_code ec) {
43  // Handle query and open a new session immediately.
44  boost::asio::post(_io_context, [=]() { handle_query(ec); });
45  OpenSession(timeout, on_opened, on_closed);
46  });
47  }
48 
49 } // namespace tcp
50 } // namespace detail
51 } // namespace streaming
52 } // namespace carla
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
std::function< void(std::shared_ptr< ServerSession >)> callback_function_type
Definition: ServerSession.h:48
Positive time duration up to milliseconds resolution.
Definition: Time.h:19
Server(boost::asio::io_context &io_context, endpoint ep)
Definition: Server.cpp:20
void OpenSession(time_duration timeout, ServerSession::callback_function_type on_session_opened, ServerSession::callback_function_type on_session_closed)
Definition: Server.cpp:26