CARLA
Toggle main menu visibility
Loading...
Searching...
No Matches
LibCarla
source
carla
streaming
low_level
streaming/low_level/Client.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
9
#include "
carla/streaming/detail/Token.h
"
10
#include "
carla/streaming/detail/tcp/Client.h
"
11
12
#include <boost/asio/io_context.hpp>
13
14
#include <memory>
15
#include <unordered_map>
16
17
namespace
carla
{
18
namespace
streaming
{
19
namespace
low_level
{
20
21
/// A client able to subscribe to multiple streams. Accepts an external
22
/// io_context.
23
///
24
/// @warning The client should not be destroyed before the @a io_context is
25
/// stopped.
26
template
<
typename
T>
27
class
Client
{
28
public
:
29
30
using
underlying_client
= T;
31
using
protocol_type
=
typename
underlying_client::protocol_type
;
32
using
token_type
=
carla::streaming::detail::token_type
;
33
34
explicit
Client
(boost::asio::ip::address fallback_address)
35
:
_fallback_address
(
std
::move(fallback_address)) {}
36
37
explicit
Client
(
const
std::string &fallback_address)
38
:
Client
(
carla
::
streaming
::make_address(fallback_address)) {}
39
40
explicit
Client
()
41
:
Client
(
carla
::
streaming
::make_localhost_address()) {}
42
43
~Client
() {
44
for
(
auto
&pair :
_clients
) {
45
pair.second->Stop();
46
}
47
}
48
49
/// @warning cannot subscribe twice to the same stream (even if it's a
50
/// MultiStream).
51
template
<
typename
Functor>
52
void
Subscribe
(
53
boost::asio::io_context &io_context,
54
token_type
token,
55
Functor &&callback) {
56
DEBUG_ASSERT_EQ
(
_clients
.find(token.
get_stream_id
()),
_clients
.end());
57
if
(!token.
has_address
()) {
58
token.
set_address
(
_fallback_address
);
59
}
60
auto
client
= std::make_shared<underlying_client>(
61
io_context,
62
token,
63
std::forward<Functor>(callback));
64
client
->Connect();
65
_clients
.emplace(token.
get_stream_id
(), std::move(
client
));
66
}
67
68
void
UnSubscribe
(
token_type
token) {
69
log_debug(
"calling sensor UnSubscribe()"
);
70
auto
it =
_clients
.find(token.
get_stream_id
());
71
if
(it !=
_clients
.end()) {
72
it->second->Stop();
73
_clients
.erase(it);
74
}
75
}
76
77
private
:
78
79
boost::asio::ip::address
_fallback_address
;
80
81
std::unordered_map<
82
detail::stream_id_type
,
83
std::shared_ptr<underlying_client>>
_clients
;
84
};
85
86
}
// namespace low_level
87
}
// namespace streaming
88
}
// namespace carla
DEBUG_ASSERT_EQ
#define DEBUG_ASSERT_EQ(lhs, rhs)
Definition
Debug.h:76
carla::streaming::detail::tcp::Client::protocol_type
endpoint::protocol_type protocol_type
Definition
streaming/detail/tcp/Client.h:43
carla::streaming::detail::token_type
Serializes a stream endpoint.
Definition
detail/Token.h:61
carla::streaming::detail::token_type::get_stream_id
const auto & get_stream_id() const
Definition
detail/Token.h:116
carla::streaming::detail::token_type::set_address
void set_address(const boost::asio::ip::address &addr)
Definition
Token.cpp:18
carla::streaming::detail::token_type::has_address
bool has_address() const
Definition
detail/Token.h:124
carla::streaming::low_level::Client::Client
Client()
Definition
streaming/low_level/Client.h:40
carla::streaming::low_level::Client< detail::tcp::Client >::_clients
std::unordered_map< detail::stream_id_type, std::shared_ptr< underlying_client > > _clients
Definition
streaming/low_level/Client.h:83
carla::streaming::low_level::Client::Subscribe
void Subscribe(boost::asio::io_context &io_context, token_type token, Functor &&callback)
Definition
streaming/low_level/Client.h:52
carla::streaming::low_level::Client< detail::tcp::Client >::protocol_type
typename underlying_client::protocol_type protocol_type
Definition
streaming/low_level/Client.h:31
carla::streaming::low_level::Client< detail::tcp::Client >::token_type
carla::streaming::detail::token_type token_type
Definition
streaming/low_level/Client.h:32
carla::streaming::low_level::Client< detail::tcp::Client >::_fallback_address
boost::asio::ip::address _fallback_address
Definition
streaming/low_level/Client.h:79
carla::streaming::low_level::Client::~Client
~Client()
Definition
streaming/low_level/Client.h:43
carla::streaming::low_level::Client::Client
Client(boost::asio::ip::address fallback_address)
Definition
streaming/low_level/Client.h:34
carla::streaming::low_level::Client::Client
Client(const std::string &fallback_address)
Definition
streaming/low_level/Client.h:37
carla::streaming::low_level::Client::UnSubscribe
void UnSubscribe(token_type token)
Definition
streaming/low_level/Client.h:68
carla::streaming::low_level::Client< detail::tcp::Client >::underlying_client
detail::tcp::Client underlying_client
Definition
streaming/low_level/Client.h:30
Token.h
carla::client
Definition
BasicAgent.h:24
carla::streaming::detail::stream_id_type
uint32_t stream_id_type
Definition
streaming/detail/Types.h:18
carla::streaming::low_level
Definition
streaming/low_level/Client.h:19
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
std
Definition
CarlaRecorderCollision.h:31
Client.h
Generated by
1.18.0