CARLA
 
载入中...
搜索中...
未找到
AsyncDataStream.h
浏览该文件的文档.
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>
16
17template <typename T>
18class FDataStreamTmpl;
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.
34template <typename T>
36{
37public:
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 template <typename SensorT, typename... ArgsT>
62 void SerializeAndSend(SensorT &Sensor, ArgsT &&... Args);
63
64 /// allow to change the frame number of the header
65 void SetFrameNumber(uint64_t FrameNumber)
66 {
69 if (HeaderStr)
70 {
71 if (HeaderStr->frame != FrameNumber)
72 {
73 carla::log_info("Re-framing sensor type ", HeaderStr->sensor_type, " from ", HeaderStr->frame, " to ", FrameNumber);
74 HeaderStr->frame = FrameNumber;
75 }
76 }
77 }
78
79 /// return the type of sensor of this stream
80 uint64_t GetSensorType()
81 {
84 if (HeaderStr)
85 {
86 return HeaderStr->sensor_type;
87 }
88 return 0u;
89 }
90
91 /// return the transform of the sensor
92 FTransform GetSensorTransform()
93 {
96 if (HeaderStr)
97 {
98 return FTransform(HeaderStr->sensor_transform);
99 }
100 return FTransform();
101 }
102
103 /// return the timestamp of the sensor
105 {
108 if (HeaderStr)
109 {
110 return HeaderStr->timestamp;
111 }
112 return 0.0;
113 }
114
115private:
116
117 friend class FDataStreamTmpl<T>;
118
119 /// @pre This functions needs to be called in the game-thread.
120 template <typename SensorT>
121 explicit FAsyncDataStreamTmpl(
122 const SensorT &InSensor,
123 double Timestamp,
124 StreamType InStream);
125
127
129};
130
131// =============================================================================
132// -- FAsyncDataStream and FAsyncDataMultiStream -------------------------------
133// =============================================================================
134
136
138
139// =============================================================================
140// -- FAsyncDataStreamTmpl implementation --------------------------------------
141// =============================================================================
142
143template <typename T>
144template <typename SensorT, typename... ArgsT>
145inline void FAsyncDataStreamTmpl<T>::SerializeAndSend(SensorT &Sensor, ArgsT &&... Args)
146{
147 // serialize data
148 carla::Buffer Data(carla::sensor::SensorRegistry::Serialize(Sensor, std::forward<ArgsT>(Args)...));
149
150 // create views of buffers
151 auto ViewHeader = carla::BufferView::CreateFrom(std::move(Header));
152 auto ViewData = carla::BufferView::CreateFrom(std::move(Data));
153
154 // send views
155 Stream.Write(ViewHeader, ViewData);
156}
157
158template <typename T>
159template <typename SensorT, typename... ArgsT>
160inline void FAsyncDataStreamTmpl<T>::Send(SensorT &Sensor, ArgsT &&... Args)
161{
162 // create views of buffers
163 auto ViewHeader = carla::BufferView::CreateFrom(std::move(Header));
164
165 // send views
166 Stream.Write(ViewHeader, std::forward<ArgsT>(Args)...);
167}
A streaming channel for sending sensor data to clients, supports sending data asynchronously.
FAsyncDataStreamTmpl(FAsyncDataStreamTmpl &&)=default
FTransform GetSensorTransform()
return the transform of the sensor
void SetFrameNumber(uint64_t FrameNumber)
allow to change the frame number of the header
void SerializeAndSend(SensorT &Sensor, ArgsT &&... Args)
carla::Buffer PopBufferFromPool()
Pop a Buffer from the pool.
uint64_t GetSensorType()
return the type of sensor of this stream
double GetSensorTimestamp()
return the timestamp of the sensor
auto GetToken() const
Return the token that allows subscribing to this stream.
void Send(SensorT &Sensor, ArgsT &&... Args)
Send some data down the stream.
A streaming channel for sending sensor data to clients.
Definition DataStream.h:27
boost::optional< StreamType > Stream
Definition DataStream.h:72
static std::shared_ptr< BufferView > CreateFrom(Buffer &&buffer)
Definition BufferView.h:56
A piece of raw data.
const value_type * data() const noexcept
Direct access to the allocated memory or nullptr if no memory is allocated.
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