CARLA
detail/Token.h
Go to the documentation of this file.
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"
11 #include "carla/streaming/Token.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 {
26 
27  uint16_t port = 0u;
28 
29  enum class protocol : uint8_t {
30  not_set,
31  tcp,
32  udp
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;
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 ?
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(
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(
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 {
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 
176  };
177 
178 } // namespace detail
179 } // namespace streaming
180 } // namespace carla
boost::asio::ip::tcp::endpoint to_tcp_endpoint() const
Definition: detail/Token.h:167
std::array< unsigned char, 24u > data
Definition: Token.h:20
token_type(stream_id_type stream_id, EndPoint< Protocol, PartiallyDefinedEndPoint > ep)
Definition: detail/Token.h:94
This file contains definitions of common data structures used in traffic manager. ...
Definition: Carla.cpp:133
boost::asio::ip::udp::endpoint to_udp_endpoint() const
Definition: detail/Token.h:163
uint32_t stream_id_type
Definition: Types.h:18
boost::asio::ip::address_v6::bytes_type v6
Definition: detail/Token.h:43
#define DEBUG_ASSERT(predicate)
Definition: Debug.h:66
bool has_same_protocol(const boost::asio::ip::basic_endpoint< Protocol > &) const
Definition: detail/Token.h:159
Serializes a stream endpoint.
Definition: detail/Token.h:61
A token that uniquely identify a stream.
Definition: Token.h:17
token_type(stream_id_type stream_id, const EndPoint< Protocol, FullyDefinedEndPoint > &ep)
Definition: detail/Token.h:84
enum carla::streaming::detail::token_data::address address_type
Keeps the mapping between streams and sessions.
Definition: Dispatcher.h:27
boost::asio::ip::address_v4::bytes_type v4
Definition: detail/Token.h:42
const auto & get_stream_id() const
Definition: detail/Token.h:116
boost::asio::ip::basic_endpoint< P > get_endpoint() const
Definition: detail/Token.h:75
static constexpr auto get_protocol()
Definition: detail/Token.h:65
carla::streaming::detail::token_type token_type
void set_stream_id(stream_id_type id)
Definition: detail/Token.h:120