CARLA
Toggle main menu visibility
Loading...
Searching...
No Matches
LibCarla
source
carla
streaming
detail
detail/Token.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/Debug.h
"
10
#include "
carla/streaming/EndPoint.h
"
11
#include "
carla/streaming/Token.h
"
12
#include "
carla/streaming/detail/Types.h
"
13
14
#include <boost/asio/ip/address.hpp>
15
#include <boost/asio/ip/tcp.hpp>
16
#include <boost/asio/ip/udp.hpp>
17
18
namespace
carla
{
19
namespace
streaming
{
20
namespace
detail
{
21
22
#pragma pack(push, 1)
23
24
struct
token_data
{
25
stream_id_type
stream_id
= 0u;
26
27
uint16_t
port
= 0u;
28
29
enum class
protocol
: uint8_t {
30
not_set,
31
tcp
,
32
udp
33
} protocol =
protocol::not_set
;
34
35
enum class
address
: uint8_t {
36
not_set,
37
ip_v4,
38
ip_v6
39
}
address_type
=
address::not_set
;
40
41
union
{
42
boost::asio::ip::address_v4::bytes_type
v4
;
43
boost::asio::ip::address_v6::bytes_type
v6
;
44
} address;
45
};
46
47
#pragma pack(pop)
48
49
static_assert
(
50
sizeof
(token_data) ==
sizeof
(
Token::data
),
51
"Size shouldn't be more than"
52
" v6 address : 128"
53
" + state : 16"
54
" + port : 16"
55
" + stream id : 32"
56
" -----------------"
57
" 192"
);
58
59
/// Serializes a stream endpoint. Contains all the necessary information for a
60
/// client to subscribe to a stream.
61
class
token_type
{
62
private
:
63
64
template
<
typename
P>
65
static
constexpr
auto
get_protocol
() {
66
static_assert
(
67
std::is_same<P, boost::asio::ip::tcp>::value ||
68
std::is_same<P, boost::asio::ip::udp>::value,
"Invalid protocol."
);
69
return
std::is_same<P, boost::asio::ip::tcp>::value ?
70
token_data::protocol::tcp
:
71
token_data::protocol::udp
;
72
}
73
74
template
<
typename
P>
75
boost::asio::ip::basic_endpoint<P>
get_endpoint
()
const
{
76
DEBUG_ASSERT
(
is_valid
());
77
DEBUG_ASSERT
(
get_protocol<P>
() ==
_token
.
protocol
);
78
return
{
get_address
(),
_token
.
port
};
79
}
80
81
public
:
82
83
template
<
typename
Protocol>
84
explicit
token_type
(
85
stream_id_type
stream_id,
86
const
EndPoint<Protocol, FullyDefinedEndPoint>
&ep) {
87
_token
.
stream_id
= stream_id;
88
_token
.
port
= ep.port();
89
_token
.
protocol
=
get_protocol<Protocol>
();
90
set_address
(ep.address());
91
}
92
93
template
<
typename
Protocol>
94
explicit
token_type
(
95
stream_id_type
stream_id,
96
EndPoint<Protocol, PartiallyDefinedEndPoint>
ep) {
97
_token
.
stream_id
= stream_id;
98
_token
.
port
= ep.port();
99
_token
.
protocol
=
get_protocol<Protocol>
();
100
}
101
102
103
token_type
() =
default
;
104
token_type
(
const
token_type
&) =
default
;
105
106
token_type
(
const
Token
&rhs);
107
108
explicit
token_type
(
token_data
data) {
109
_token
= data;
110
}
111
112
operator
Token
()
const
;
113
114
// We need to return a reference here so we can use the address of the
115
// stream id to send it as buffer.
116
const
auto
&
get_stream_id
()
const
{
117
return
_token
.
stream_id
;
118
}
119
120
void
set_stream_id
(
stream_id_type
id
) {
121
_token
.
stream_id
= id;
122
}
123
124
bool
has_address
()
const
{
125
return
_token
.
address_type
!=
token_data::address::not_set
;
126
}
127
128
void
set_address
(
const
boost::asio::ip::address &addr);
129
130
boost::asio::ip::address
get_address
()
const
;
131
132
auto
get_port
()
const
{
133
return
_token
.
port
;
134
}
135
136
bool
is_valid
()
const
{
137
return
has_address
() &&
138
((
_token
.
protocol
!=
token_data::protocol::not_set
) &&
139
(
_token
.
address_type
!=
token_data::address::not_set
));
140
}
141
142
bool
address_is_v4
()
const
{
143
return
_token
.
address_type
==
token_data::address::ip_v4
;
144
}
145
146
bool
address_is_v6
()
const
{
147
return
_token
.
address_type
==
token_data::address::ip_v6
;
148
}
149
150
bool
protocol_is_udp
()
const
{
151
return
_token
.
protocol
==
token_data::protocol::udp
;
152
}
153
154
bool
protocol_is_tcp
()
const
{
155
return
_token
.
protocol
==
token_data::protocol::tcp
;
156
}
157
158
template
<
typename
Protocol>
159
bool
has_same_protocol
(
const
boost::asio::ip::basic_endpoint<Protocol> &)
const
{
160
return
_token
.
protocol
==
get_protocol<Protocol>
();
161
}
162
163
boost::asio::ip::udp::endpoint
to_udp_endpoint
()
const
{
164
return
get_endpoint<boost::asio::ip::udp>
();
165
}
166
167
boost::asio::ip::tcp::endpoint
to_tcp_endpoint
()
const
{
168
return
get_endpoint<boost::asio::ip::tcp>
();
169
}
170
171
private
:
172
173
friend
class
Dispatcher
;
174
175
token_data
_token
;
176
};
177
178
}
// namespace detail
179
}
// namespace streaming
180
}
// namespace carla
Debug.h
DEBUG_ASSERT
#define DEBUG_ASSERT(predicate)
Definition
Debug.h:66
EndPoint.h
Token.h
carla::streaming::Token
A token that uniquely identify a stream.
Definition
Token.h:17
carla::streaming::Token::data
std::array< unsigned char, 24u > data
Definition
Token.h:20
carla::streaming::detail::EndPoint
Definition
EndPoint.h:24
carla::streaming::detail::token_type::is_valid
bool is_valid() const
Definition
detail/Token.h:136
carla::streaming::detail::token_type::to_tcp_endpoint
boost::asio::ip::tcp::endpoint to_tcp_endpoint() const
Definition
detail/Token.h:167
carla::streaming::detail::token_type::get_address
boost::asio::ip::address get_address() const
Definition
Token.cpp:40
carla::streaming::detail::token_type::address_is_v6
bool address_is_v6() const
Definition
detail/Token.h:146
carla::streaming::detail::token_type::get_stream_id
const auto & get_stream_id() const
Definition
detail/Token.h:116
carla::streaming::detail::token_type::get_protocol
static constexpr auto get_protocol()
Definition
detail/Token.h:65
carla::streaming::detail::token_type::get_endpoint
boost::asio::ip::basic_endpoint< P > get_endpoint() const
Definition
detail/Token.h:75
carla::streaming::detail::token_type::token_type
token_type(stream_id_type stream_id, EndPoint< Protocol, PartiallyDefinedEndPoint > ep)
Definition
detail/Token.h:94
carla::streaming::detail::token_type::has_same_protocol
bool has_same_protocol(const boost::asio::ip::basic_endpoint< Protocol > &) const
Definition
detail/Token.h:159
carla::streaming::detail::token_type::token_type
token_type()=default
carla::streaming::detail::token_type::to_udp_endpoint
boost::asio::ip::udp::endpoint to_udp_endpoint() const
Definition
detail/Token.h:163
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::get_port
auto get_port() const
Definition
detail/Token.h:132
carla::streaming::detail::token_type::protocol_is_tcp
bool protocol_is_tcp() const
Definition
detail/Token.h:154
carla::streaming::detail::token_type::token_type
token_type(stream_id_type stream_id, const EndPoint< Protocol, FullyDefinedEndPoint > &ep)
Definition
detail/Token.h:84
carla::streaming::detail::token_type::has_address
bool has_address() const
Definition
detail/Token.h:124
carla::streaming::detail::token_type::_token
token_data _token
Definition
detail/Token.h:175
carla::streaming::detail::token_type::protocol_is_udp
bool protocol_is_udp() const
Definition
detail/Token.h:150
carla::streaming::detail::token_type::Dispatcher
friend class Dispatcher
Definition
detail/Token.h:173
carla::streaming::detail::token_type::address_is_v4
bool address_is_v4() const
Definition
detail/Token.h:142
carla::streaming::detail::token_type::token_type
token_type(token_data data)
Definition
detail/Token.h:108
carla::streaming::detail::token_type::token_type
token_type(const token_type &)=default
carla::streaming::detail::token_type::set_stream_id
void set_stream_id(stream_id_type id)
Definition
detail/Token.h:120
carla::streaming::detail::tcp
Definition
streaming/detail/tcp/Client.cpp:26
carla::streaming::detail
Definition
Dispatcher.cpp:17
carla::streaming::detail::stream_id_type
uint32_t stream_id_type
Definition
streaming/detail/Types.h:18
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
Types.h
carla::streaming::detail::token_data
Definition
detail/Token.h:24
carla::streaming::detail::token_data::port
uint16_t port
Definition
detail/Token.h:27
carla::streaming::detail::token_data::address_type
enum carla::streaming::detail::token_data::address address_type
carla::streaming::detail::token_data::stream_id
stream_id_type stream_id
Definition
detail/Token.h:25
carla::streaming::detail::token_data::address
address
Definition
detail/Token.h:35
carla::streaming::detail::token_data::address::not_set
@ not_set
Definition
detail/Token.h:36
carla::streaming::detail::token_data::address::ip_v6
@ ip_v6
Definition
detail/Token.h:38
carla::streaming::detail::token_data::address::ip_v4
@ ip_v4
Definition
detail/Token.h:37
carla::streaming::detail::token_data::protocol
protocol
Definition
detail/Token.h:29
carla::streaming::detail::token_data::protocol::not_set
@ not_set
Definition
detail/Token.h:30
carla::streaming::detail::token_data::protocol::udp
@ udp
Definition
detail/Token.h:32
carla::streaming::detail::token_data::protocol::tcp
@ tcp
Definition
detail/Token.h:31
carla::streaming::detail::token_data::[union].address::v4
boost::asio::ip::address_v4::bytes_type v4
carla::streaming::detail::token_data::[union].address::v6
boost::asio::ip::address_v6::bytes_type v6
Generated by
1.18.0