CARLA
Toggle main menu visibility
Loading...
Searching...
No Matches
LibCarla
source
carla
streaming
detail
tcp
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
7
#include "
carla/streaming/detail/tcp/Server.h
"
8
9
#include "
carla/Logging.h
"
10
#include "
carla/Sockets.h
"
11
12
#include <boost/asio/post.hpp>
13
14
#include <memory>
15
16
namespace
carla
{
17
namespace
streaming
{
18
namespace
detail
{
19
namespace
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.
30
carla::SetSocketCloseOnExec
(
_acceptor
.native_handle());
31
}
32
33
void
Server::OpenSession
(
34
time_duration
timeout,
35
ServerSession::callback_function_type
on_opened,
36
ServerSession::callback_function_type
on_closed) {
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
Logging.h
Sockets.h
carla::streaming::detail::tcp::ServerSession::callback_function_type
std::function< void(std::shared_ptr< ServerSession >)> callback_function_type
Definition
ServerSession.h:49
carla::streaming::detail::tcp::Server::_io_context
boost::asio::io_context & _io_context
Definition
streaming/detail/tcp/Server.h:72
carla::streaming::detail::tcp::Server::endpoint
boost::asio::ip::tcp::endpoint endpoint
Definition
streaming/detail/tcp/Server.h:29
carla::streaming::detail::tcp::Server::_synchronous
bool _synchronous
Definition
streaming/detail/tcp/Server.h:78
carla::streaming::detail::tcp::Server::OpenSession
void OpenSession(time_duration timeout, ServerSession::callback_function_type on_session_opened, ServerSession::callback_function_type on_session_closed)
Definition
Server.cpp:33
carla::streaming::detail::tcp::Server::Server
Server(boost::asio::io_context &io_context, endpoint ep)
Definition
Server.cpp:21
carla::streaming::detail::tcp::Server::_timeout
std::atomic< time_duration > _timeout
Definition
streaming/detail/tcp/Server.h:76
carla::streaming::detail::tcp::Server::_acceptor
boost::asio::ip::tcp::acceptor _acceptor
Definition
streaming/detail/tcp/Server.h:74
carla::time_duration
Positive time duration up to milliseconds resolution.
Definition
Time.h:20
carla::streaming::detail::tcp
Definition
streaming/detail/tcp/Client.cpp:26
carla::streaming::detail
Definition
Dispatcher.cpp:17
carla::streaming
Definition
client/detail/Client.h:61
carla
This file contains definitions of common data structures used in traffic manager.
Definition
SignComponent.h:21
carla::SetSocketCloseOnExec
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
std
Definition
CarlaRecorderCollision.h:31
Server.h
Generated by
1.18.0