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  template <typename Protocol>
82  explicit token_type(
85  _token.stream_id = stream_id;
86  _token.port = ep.port();
87  _token.protocol = get_protocol<Protocol>();
88  set_address(ep.address());
89  }
90 
91  template <typename Protocol>
92  explicit token_type(
95  _token.stream_id = stream_id;
96  _token.port = ep.port();
97  _token.protocol = get_protocol<Protocol>();
98  }
99 
100  public:
101 
102  token_type() = default;
103  token_type(const token_type &) = default;
104 
105  token_type(const Token &rhs);
106 
107  operator Token() const;
108 
109  // We need to return a reference here so we can use the address of the
110  // stream id to send it as buffer.
111  const auto &get_stream_id() const {
112  return _token.stream_id;
113  }
114 
115  bool has_address() const {
116  return _token.address_type != token_data::address::not_set;
117  }
118 
119  void set_address(const boost::asio::ip::address &addr);
120 
121  boost::asio::ip::address get_address() const;
122 
123  auto get_port() const {
124  return _token.port;
125  }
126 
127  bool is_valid() const {
128  return has_address() &&
129  ((_token.protocol != token_data::protocol::not_set) &&
130  (_token.address_type != token_data::address::not_set));
131  }
132 
133  bool address_is_v4() const {
134  return _token.address_type == token_data::address::ip_v4;
135  }
136 
137  bool address_is_v6() const {
138  return _token.address_type == token_data::address::ip_v6;
139  }
140 
141  bool protocol_is_udp() const {
142  return _token.protocol == token_data::protocol::udp;
143  }
144 
145  bool protocol_is_tcp() const {
146  return _token.protocol == token_data::protocol::tcp;
147  }
148 
149  template <typename Protocol>
150  bool has_same_protocol(const boost::asio::ip::basic_endpoint<Protocol> &) const {
151  return _token.protocol == get_protocol<Protocol>();
152  }
153 
154  boost::asio::ip::udp::endpoint to_udp_endpoint() const {
155  return get_endpoint<boost::asio::ip::udp>();
156  }
157 
158  boost::asio::ip::tcp::endpoint to_tcp_endpoint() const {
159  return get_endpoint<boost::asio::ip::tcp>();
160  }
161 
162  private:
163 
164  friend class Dispatcher;
165 
167  };
168 
169 } // namespace detail
170 } // namespace streaming
171 } // namespace carla
boost::asio::ip::tcp::endpoint to_tcp_endpoint() const
Definition: detail/Token.h:158
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:92
This file contains definitions of common data structures used in traffic manager. ...
Definition: Carla.cpp:99
boost::asio::ip::udp::endpoint to_udp_endpoint() const
Definition: detail/Token.h:154
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:150
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:82
enum carla::streaming::detail::token_data::address address_type
Keeps the mapping between streams and sessions.
Definition: Dispatcher.h:25
boost::asio::ip::address_v4::bytes_type v4
Definition: detail/Token.h:42
const auto & get_stream_id() const
Definition: detail/Token.h:111
boost::asio::ip::basic_endpoint< P > get_endpoint() const
Definition: detail/Token.h:75
static constexpr auto get_protocol()
Definition: detail/Token.h:65