CARLA
AsyncDataStream.h
Go to the documentation of this file.
1 // Copyright (c) 2020 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 
10 #include <carla/Buffer.h>
11 #include <carla/Logging.h>
14 #include <carla/streaming/Stream.h>
16 
17 template <typename T>
19 
20 // =============================================================================
21 // -- FAsyncDataStreamTmpl -----------------------------------------------------
22 // =============================================================================
23 
24 /// A streaming channel for sending sensor data to clients, supports sending
25 /// data asynchronously. Data sent by the "Send" functions is passed to the
26 /// serializer registered with the sensor at carla::sensor:SensorRegistry before
27 /// being sent down the stream.
28 ///
29 /// @warning This is a single-use object, a new one needs to be created for each
30 /// new message.
31 ///
32 /// FAsyncDataStream also has a pool of carla::Buffer that allows reusing the
33 /// allocated memory, use it whenever possible.
34 template <typename T>
36 {
37 public:
38 
39  using StreamType = T;
40 
42 
43  /// Return the token that allows subscribing to this stream.
44  auto GetToken() const
45  {
46  return Stream.GetToken();
47  }
48 
49  /// Pop a Buffer from the pool. Buffers in the pool can reuse the memory
50  /// allocated by previous messages, significantly improving performance for
51  /// big messages.
53  {
54  return Stream.MakeBuffer();
55  }
56 
57  /// Send some data down the stream.
58  template <typename SensorT, typename... ArgsT>
59  void Send(SensorT &Sensor, ArgsT &&... Args);
60 
61  /// allow to change the frame number of the header
62  void SetFrameNumber(uint64_t FrameNumber)
63  {
65  if (HeaderStr)
66  {
67  if (HeaderStr->frame != FrameNumber)
68  {
69  carla::log_info("Re-framing sensor type ", HeaderStr->sensor_type, " from ", HeaderStr->frame, " to ", FrameNumber);
70  HeaderStr->frame = FrameNumber;
71  }
72  }
73  }
74 
75 private:
76 
77  friend class FDataStreamTmpl<T>;
78 
79  /// @pre This functions needs to be called in the game-thread.
80  template <typename SensorT>
81  explicit FAsyncDataStreamTmpl(
82  const SensorT &InSensor,
83  double Timestamp,
84  StreamType InStream);
85 
87 
89 };
90 
91 // =============================================================================
92 // -- FAsyncDataStream and FAsyncDataMultiStream -------------------------------
93 // =============================================================================
94 
96 
98 
99 // =============================================================================
100 // -- FAsyncDataStreamTmpl implementation --------------------------------------
101 // =============================================================================
102 
103 template <typename T>
104 template <typename SensorT, typename... ArgsT>
105 inline void FAsyncDataStreamTmpl<T>::Send(SensorT &Sensor, ArgsT &&... Args)
106 {
107  Stream.Write(
108  std::move(Header),
109  carla::sensor::SensorRegistry::Serialize(Sensor, std::forward<ArgsT>(Args)...));
110 }
const value_type * data() const noexcept
Direct access to the allocated memory or nullptr if no memory is allocated.
Definition: carla/Buffer.h:155
FAsyncDataStreamTmpl(FAsyncDataStreamTmpl &&)=default
A streaming channel for sending sensor data to clients.
carla::Buffer Header
carla::Buffer PopBufferFromPool()
Pop a Buffer from the pool.
static Buffer Serialize(Sensor &sensor, Args &&... args)
Serialize the arguments provided into a Buffer by calling to the serializer registered for the given ...
static void log_info(Args &&... args)
Definition: Logging.h:82
A piece of raw data.
Definition: carla/Buffer.h:41
A streaming channel for sending sensor data to clients, supports sending data asynchronously.
auto GetToken() const
Return the token that allows subscribing to this stream.
void Send(SensorT &Sensor, ArgsT &&... Args)
Send some data down the stream.
void SetFrameNumber(uint64_t FrameNumber)
allow to change the frame number of the header