CARLA
 
载入中...
搜索中...
未找到
detail/Token.h
浏览该文件的文档.
1// Copyright (c) 2017 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"
13
14#include <boost/asio/ip/address.hpp>
15#include <boost/asio/ip/tcp.hpp>
16#include <boost/asio/ip/udp.hpp>
17
18namespace carla {
19namespace streaming {
20namespace detail {
21
22#pragma pack(push, 1)
23
24 struct token_data {
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
40
41 union {
42 boost::asio::ip::address_v4::bytes_type v4;
43 boost::asio::ip::address_v6::bytes_type v6;
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 ?
72 }
73
74 template <typename P>
75 boost::asio::ip::basic_endpoint<P> get_endpoint() const {
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,
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,
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
121 _token.stream_id = id;
122 }
123
124 bool has_address() const {
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
141
142 bool address_is_v4() const {
144 }
145
146 bool address_is_v6() const {
148 }
149
150 bool protocol_is_udp() const {
152 }
153
154 bool protocol_is_tcp() const {
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
176 };
177
178} // namespace detail
179} // namespace streaming
180} // namespace carla
#define DEBUG_ASSERT(predicate)
Definition Debug.h:66
A token that uniquely identify a stream.
Definition Token.h:17
std::array< unsigned char, 24u > data
Definition Token.h:20
Keeps the mapping between streams and sessions.
Definition Dispatcher.h:27
Serializes a stream endpoint.
boost::asio::ip::tcp::endpoint to_tcp_endpoint() const
boost::asio::ip::address get_address() const
Definition Token.cpp:40
static constexpr auto get_protocol()
boost::asio::ip::basic_endpoint< P > get_endpoint() const
token_type(stream_id_type stream_id, EndPoint< Protocol, PartiallyDefinedEndPoint > ep)
bool has_same_protocol(const boost::asio::ip::basic_endpoint< Protocol > &) const
boost::asio::ip::udp::endpoint to_udp_endpoint() const
void set_address(const boost::asio::ip::address &addr)
Definition Token.cpp:18
token_type(stream_id_type stream_id, const EndPoint< Protocol, FullyDefinedEndPoint > &ep)
token_type(const token_type &)=default
void set_stream_id(stream_id_type id)
uint32_t stream_id_type
Definition Types.h:18
This file contains definitions of common data structures used in traffic manager.
Definition Carla.cpp:133
enum carla::streaming::detail::token_data::address address_type
boost::asio::ip::address_v4::bytes_type v4
boost::asio::ip::address_v6::bytes_type v6