CARLA
Loading...
Searching...
No Matches
Server.cpp
Go to the documentation of this file.
1// Copyright (c) 2026 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 "carla/Logging.h"
10#include "carla/Sockets.h"
11
12#include <boost/asio/post.hpp>
13
14#include <memory>
15
16namespace carla {
17namespace streaming {
18namespace detail {
19namespace tcp {
20
21 Server::Server(boost::asio::io_context &io_context, endpoint ep)
22 : _io_context(io_context),
23 _acceptor(_io_context, std::move(ep)),
24 _timeout(time_duration::seconds(10u)),
25 _synchronous(false) {
26 // Prevent this listening socket from leaking into child processes
27 // started via fork+exec while the server is alive (e.g. RecastBuilder,
28 // launched by UCarlaEpisode::LoadNewOpendriveEpisode). See
29 // carla/Sockets.h for details.
31 }
32
34 time_duration timeout,
37 using boost::system::error_code;
38
39 auto session = std::make_shared<ServerSession>(_io_context, timeout, *this);
40
41 auto handle_query = [on_opened, on_closed, session](const error_code &ec) {
42 if (!ec) {
43 session->Open(std::move(on_opened), std::move(on_closed));
44 } else {
45 log_error("tcp accept stream error:", ec.message());
46 }
47 };
48
49 _acceptor.async_accept(session->_socket, [=](error_code ec) {
50 // Handle query and open a new session immediately.
51 boost::asio::post(_io_context, [=, this]() { handle_query(ec); });
52 OpenSession(timeout, on_opened, on_closed);
53 });
54 }
55
56} // namespace tcp
57} // namespace detail
58} // namespace streaming
59} // namespace carla
std::function< void(std::shared_ptr< ServerSession >)> callback_function_type
void OpenSession(time_duration timeout, ServerSession::callback_function_type on_session_opened, ServerSession::callback_function_type on_session_closed)
Definition Server.cpp:33
Server(boost::asio::io_context &io_context, endpoint ep)
Definition Server.cpp:21
Positive time duration up to milliseconds resolution.
Definition Time.h:20
This file contains definitions of common data structures used in traffic manager.
void SetSocketCloseOnExec(NativeHandle fd)
Marks a socket file descriptor close-on-exec, so it is not inherited by child processes started via f...
Definition Sockets.h:37
static void log_error(Args &&... args)
Definition Logging.h:110