CARLA
Loading...
Searching...
No Matches
streaming/low_level/Server.h
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#pragma once
8
12
13#include <boost/asio/io_context.hpp>
14
15#include <optional>
16
17namespace carla {
18namespace streaming {
19namespace low_level {
20
21 /// A low-level streaming server. Each new stream has a token associated, this
22 /// token can be used by a client to subscribe to the stream. This server
23 /// requires an external io_context running.
24 ///
25 /// @warning This server cannot be destructed before its @a io_context is
26 /// stopped.
27 template <typename T>
28 class Server {
29 public:
30
32
33 using protocol_type = typename underlying_server::protocol_type;
34
36
38
39 template <typename InternalEPType, typename ExternalEPType>
40 explicit Server(
41 boost::asio::io_context &io_context,
44 : _server(io_context, std::move(internal_ep)),
45 _dispatcher(std::move(external_ep)) {
47 }
48
49 template <typename InternalEPType>
50 explicit Server(
51 boost::asio::io_context &io_context,
53 : _server(io_context, std::move(internal_ep)),
56 }
57
58 template <typename... EPArgs>
59 explicit Server(boost::asio::io_context &io_context, EPArgs &&... args)
60 : Server(io_context, make_endpoint<protocol_type>(std::forward<EPArgs>(args)...)) {}
61
62 typename underlying_server::endpoint GetLocalEndpoint() const {
63 return _server.GetLocalEndpoint();
64 }
65
66 void SetTimeout(time_duration timeout) {
67 _server.SetTimeout(timeout);
68 }
69
71 return _dispatcher.MakeStream();
72 }
73
77
78 void SetSynchronousMode(bool is_synchro) {
79 _server.SetSynchronousMode(is_synchro);
80 }
81
83 return _dispatcher.GetToken(sensor_id);
84 }
85
86 [[nodiscard]]
87 std::optional<token_type> FindToken(stream_id id) {
88 return _dispatcher.FindToken(id);
89 }
90
94
95 void SetStreamAlias(stream_id alias, stream_id target) {
96 _dispatcher.SetStreamAlias(alias, target);
97 }
98
102
103 [[nodiscard]]
104 std::optional<stream_id> FindStreamAlias(stream_id alias) {
105 return _dispatcher.FindStreamAlias(alias);
106 }
107
111
112 void EnableForROS(stream_id sensor_id) {
113 _dispatcher.EnableForROS(sensor_id);
114 }
115
116 void DisableForROS(stream_id sensor_id) {
117 _dispatcher.DisableForROS(sensor_id);
118 }
119
120 bool IsEnabledForROS(stream_id sensor_id) {
121 return _dispatcher.IsEnabledForROS(sensor_id);
122 }
123
124 private:
125
126 void StartServer() {
127 auto on_session_opened = [this](auto session) {
128 if (!_dispatcher.RegisterSession(session)) {
129 session->Close();
130 }
131 };
132 auto on_session_closed = [this](auto session) {
133 log_debug("on_session_closed called");
135 };
136 _server.Listen(on_session_opened, on_session_closed);
137 }
138
140
142 };
143
144} // namespace low_level
145} // namespace streaming
146} // namespace carla
Keeps the mapping between streams and sessions.
Definition Dispatcher.h:28
void SetSessionsUseStreamAliases(bool enabled)
When enabled, a session subscribes through an alias (see SetStreamAlias) and a stream id without an a...
void SetStreamAlias(stream_id_type alias, stream_id_type stream_id)
Routes sessions subscribing to alias to the local stream stream_id.
void DeregisterSession(std::shared_ptr< Session > session)
carla::streaming::Stream MakeStream()
std::optional< token_type > FindToken(stream_id_type stream_id)
Like GetToken(), but returns std::nullopt for an unknown stream_id instead of creating an empty strea...
bool RegisterSession(std::shared_ptr< Session > session)
void DisableForROS(stream_id_type sensor_id)
Definition Dispatcher.h:78
void SetROS2TopicVisibilityDefaultEnabled(bool enabled)
Definition Dispatcher.h:65
bool IsEnabledForROS(stream_id_type sensor_id)
Definition Dispatcher.h:86
std::optional< stream_id_type > FindStreamAlias(stream_id_type alias)
void CloseStream(carla::streaming::detail::stream_id_type id)
void RemoveStreamAlias(stream_id_type alias)
void EnableForROS(stream_id_type sensor_id)
Definition Dispatcher.h:70
token_type GetToken(stream_id_type sensor_id)
Serializes a stream endpoint.
Server(boost::asio::io_context &io_context, detail::EndPoint< protocol_type, InternalEPType > internal_ep, detail::EndPoint< protocol_type, ExternalEPType > external_ep)
void CloseStream(carla::streaming::detail::stream_id_type id)
carla::streaming::detail::stream_id_type stream_id
typename underlying_server::protocol_type protocol_type
token_type GetToken(stream_id sensor_id)
Server(boost::asio::io_context &io_context, EPArgs &&... args)
Server(boost::asio::io_context &io_context, detail::EndPoint< protocol_type, InternalEPType > internal_ep)
underlying_server::endpoint GetLocalEndpoint() const
std::optional< stream_id > FindStreamAlias(stream_id alias)
std::optional< token_type > FindToken(stream_id id)
void SetStreamAlias(stream_id alias, stream_id target)
Positive time duration up to milliseconds resolution.
Definition Time.h:20
static auto make_endpoint(boost::asio::ip::basic_endpoint< Protocol > ep)
Definition EndPoint.h:89
This file contains definitions of common data structures used in traffic manager.
static void log_debug(Args &&... args)
Definition Logging.h:68