CARLA
Message.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/ListView.h"
10 #include "carla/Buffer.h"
11 #include "carla/BufferView.h"
12 #include "carla/Debug.h"
13 #include "carla/NonCopyable.h"
15 
16 #include <boost/asio/buffer.hpp>
17 
18 #include <array>
19 #include <exception>
20 #include <limits>
21 #include <memory>
22 #include <string>
23 
24 namespace carla {
25 namespace streaming {
26 namespace detail {
27 namespace tcp {
28 
29  /// Serialization of a set of buffers to be sent over a TCP socket as a single
30  /// message. Template paramenter @a MaxNumberOfBuffers imposes a compile-time
31  /// limit on the maximum number of buffers that can be included in a single
32  /// message.
33  template <size_t MaxNumberOfBuffers>
35  : public std::enable_shared_from_this<MessageTmpl<MaxNumberOfBuffers>>,
36  private NonCopyable {
37  public:
38 
39  static constexpr size_t max_size() {
40  return MaxNumberOfBuffers;
41  }
42 
43  private:
44 
45  MessageTmpl(size_t) {}
46 
47  template <typename... Buffers>
48  MessageTmpl(size_t size, SharedBufferView buffer, Buffers... buffers)
49  : MessageTmpl(size, buffers...) {
51  _total_size += buffer->size();
52  _buffer_views[1u + size - _number_of_buffers] = buffer->cbuffer();
53  _buffers[size - _number_of_buffers] = buffer;
54  }
55 
56  public:
57 
58  template <typename... Buffers>
59  MessageTmpl(SharedBufferView buf, Buffers... buffers)
60  : MessageTmpl(sizeof...(Buffers) + 1u, buf, buffers...) {
61  static_assert(sizeof...(Buffers) < max_size(), "Too many buffers!");
62  _buffer_views[0u] = boost::asio::buffer(&_total_size, sizeof(_total_size));
63  }
64 
65  /// Size in bytes of the message excluding the header.
66  auto size() const noexcept {
67  return _total_size;
68  }
69 
70  bool empty() const noexcept {
71  return size() == 0u;
72  }
73 
74  auto GetBufferSequence() const {
75  auto begin = _buffer_views.begin();
76  return MakeListView(begin, begin + _number_of_buffers + 1u);
77  }
78 
79  private:
80 
82 
84 
85  std::array<SharedBufferView, MaxNumberOfBuffers> _buffers;
86 
87  std::array<boost::asio::const_buffer, MaxNumberOfBuffers + 1u> _buffer_views;
88  };
89 
90  /// A TCP message containing a maximum of 2 buffers. This is optimized for a
91  /// header and body sort of messages.
93 
94 } // namespace tcp
95 } // namespace detail
96 } // namespace streaming
97 } // namespace carla
static constexpr size_t max_size()
Definition: Message.h:39
auto size() const noexcept
Size in bytes of the message excluding the header.
Definition: Message.h:66
std::array< SharedBufferView, MaxNumberOfBuffers > _buffers
Definition: Message.h:85
std::array< boost::asio::const_buffer, MaxNumberOfBuffers+1u > _buffer_views
Definition: Message.h:87
This file contains definitions of common data structures used in traffic manager. ...
Definition: Carla.cpp:133
uint32_t message_size_type
Definition: Types.h:20
Serialization of a set of buffers to be sent over a TCP socket as a single message.
Definition: Message.h:34
MessageTmpl(size_t size, SharedBufferView buffer, Buffers... buffers)
Definition: Message.h:48
std::shared_ptr< BufferView > SharedBufferView
Definition: BufferView.h:151
Inherit (privately) to suppress copy/move construction and assignment.
MessageTmpl(SharedBufferView buf, Buffers... buffers)
Definition: Message.h:59
static auto MakeListView(Iterator begin, Iterator end)