blobPubSubTypes.cpp
Go to the documentation of this file.
1 // License: Apache 2.0. See LICENSE file in root directory.
2 // Copyright(c) 2023 Intel Corporation. All Rights Reserved.
3 
12 #include <fastcdr/FastBuffer.h>
13 #include <fastcdr/Cdr.h>
14 
16 
19 
20 namespace udds {
22  {
23  setName("udds::blob");
24  auto type_size = blob::getMaxCdrSerializedSize();
25  type_size += eprosima::fastcdr::Cdr::alignment(type_size, 4); /* possible submessage alignment */
26  m_typeSize = static_cast<uint32_t>(type_size) + 4; /*encapsulation*/
27  m_isGetKeyDefined = blob::isKeyDefined();
28  size_t keyLength = blob::getKeyMaxCdrSerializedSize() > 16 ?
30  m_keyBuffer = reinterpret_cast<unsigned char*>(malloc(keyLength));
31  memset(m_keyBuffer, 0, keyLength);
32  }
33 
35  {
36  if (m_keyBuffer != nullptr)
37  {
38  free(m_keyBuffer);
39  }
40  }
41 
43  void* data,
44  SerializedPayload_t* payload)
45  {
46  blob* p_type = static_cast<blob*>(data);
47 
48  // Object that manages the raw buffer.
49  eprosima::fastcdr::FastBuffer fastbuffer(reinterpret_cast<char*>(payload->data), payload->max_size);
50  // Object that serializes the data.
51  eprosima::fastcdr::Cdr ser(fastbuffer, eprosima::fastcdr::Cdr::DEFAULT_ENDIAN, eprosima::fastcdr::Cdr::DDS_CDR);
52  payload->encapsulation = ser.endianness() == eprosima::fastcdr::Cdr::BIG_ENDIANNESS ? CDR_BE : CDR_LE;
53  // Serialize encapsulation
54  ser.serialize_encapsulation();
55 
56  try
57  {
58  // Serialize the object.
59  p_type->serialize(ser);
60  }
61  catch (eprosima::fastcdr::exception::NotEnoughMemoryException& /*exception*/)
62  {
63  return false;
64  }
65 
66  // Get the serialized length
67  payload->length = static_cast<uint32_t>(ser.getSerializedDataLength());
68  return true;
69  }
70 
72  SerializedPayload_t* payload,
73  void* data)
74  {
75  try
76  {
77  //Convert DATA to pointer of your type
78  blob* p_type = static_cast<blob*>(data);
79 
80  // Object that manages the raw buffer.
81  eprosima::fastcdr::FastBuffer fastbuffer(reinterpret_cast<char*>(payload->data), payload->length);
82 
83  // Object that deserializes the data.
84  eprosima::fastcdr::Cdr deser(fastbuffer, eprosima::fastcdr::Cdr::DEFAULT_ENDIAN, eprosima::fastcdr::Cdr::DDS_CDR);
85 
86  // Deserialize encapsulation.
87  deser.read_encapsulation();
88  payload->encapsulation = deser.endianness() == eprosima::fastcdr::Cdr::BIG_ENDIANNESS ? CDR_BE : CDR_LE;
89 
90  // Deserialize the object.
91  p_type->deserialize(deser);
92  }
93  catch (eprosima::fastcdr::exception::NotEnoughMemoryException& /*exception*/)
94  {
95  return false;
96  }
97 
98  return true;
99  }
100 
102  void* data)
103  {
104  return [data]() -> uint32_t
105  {
106  return static_cast<uint32_t>(type::getCdrSerializedSize(*static_cast<blob*>(data))) +
107  4u /*encapsulation*/;
108  };
109  }
110 
112  {
113  return reinterpret_cast<void*>(new blob());
114  }
115 
117  void* data)
118  {
119  delete(reinterpret_cast<blob*>(data));
120  }
121 
123  void* data,
125  bool force_md5)
126  {
127  if (!m_isGetKeyDefined)
128  {
129  return false;
130  }
131 
132  blob* p_type = static_cast<blob*>(data);
133 
134  // Object that manages the raw buffer.
135  eprosima::fastcdr::FastBuffer fastbuffer(reinterpret_cast<char*>(m_keyBuffer),
137 
138  // Object that serializes the data.
139  eprosima::fastcdr::Cdr ser(fastbuffer, eprosima::fastcdr::Cdr::BIG_ENDIANNESS);
140  p_type->serializeKey(ser);
141  if (force_md5 || blob::getKeyMaxCdrSerializedSize() > 16)
142  {
143  m_md5.init();
144  m_md5.update(m_keyBuffer, static_cast<unsigned int>(ser.getSerializedDataLength()));
145  m_md5.finalize();
146  for (uint8_t i = 0; i < 16; ++i)
147  {
148  handle->value[i] = m_md5.digest[i];
149  }
150  }
151  else
152  {
153  for (uint8_t i = 0; i < 16; ++i)
154  {
155  handle->value[i] = m_keyBuffer[i];
156  }
157  }
158  return true;
159  }
160 
161 
162 } //End of namespace udds
udds::blobPubSubType::blobPubSubType
eProsima_user_DllExport blobPubSubType()
Definition: blobPubSubTypes.cpp:21
uint8_t
unsigned char uint8_t
Definition: stdint.h:78
data
Definition: parser.hpp:153
udds::blobPubSubType::m_keyBuffer
unsigned char * m_keyBuffer
Definition: blobPubSubTypes.h:89
udds::blobPubSubType::deleteData
virtual eProsima_user_DllExport void deleteData(void *data) override
Definition: blobPubSubTypes.cpp:116
udds::blobPubSubType::m_md5
MD5 m_md5
Definition: blobPubSubTypes.h:88
udds::blobPubSubType::~blobPubSubType
virtual eProsima_user_DllExport ~blobPubSubType() override
Definition: blobPubSubTypes.cpp:34
udds::blob::getKeyMaxCdrSerializedSize
static eProsima_user_DllExport size_t getKeyMaxCdrSerializedSize(size_t current_alignment=0)
This function returns the maximum serialized size of the Key of an object depending on the buffer ali...
Definition: blob.cpp:166
data
GLboolean * data
Definition: glad/glad/glad.h:1481
uint32_t
unsigned int uint32_t
Definition: stdint.h:80
udds::blob::getMaxCdrSerializedSize
static eProsima_user_DllExport size_t getMaxCdrSerializedSize(size_t current_alignment=0)
This function returns the maximum serialized size of an object depending on the buffer alignment.
Definition: blob.cpp:80
i
int i
Definition: rs-pcl-color.cpp:54
topic-send.blob
blob
Definition: topic-send.py:64
udds::blobPubSubType::deserialize
virtual eProsima_user_DllExport bool deserialize(eprosima::fastrtps::rtps::SerializedPayload_t *payload, void *data) override
Definition: blobPubSubTypes.cpp:71
udds::blob::deserialize
eProsima_user_DllExport void deserialize(eprosima::fastcdr::Cdr &cdr)
This function deserializes an object using CDR serialization.
Definition: blob.cpp:122
udds::blobPubSubType::createData
virtual eProsima_user_DllExport void * createData() override
Definition: blobPubSubTypes.cpp:111
udds::blob::isKeyDefined
static eProsima_user_DllExport bool isKeyDefined()
This function tells you if the Key has been defined for this type.
Definition: blob.cpp:176
udds::blob::serializeKey
eProsima_user_DllExport void serializeKey(eprosima::fastcdr::Cdr &cdr) const
This function serializes the key members of an object using CDR serialization.
Definition: blob.cpp:181
udds
Definition: blob.h:55
udds::blobPubSubType::serialize
virtual eProsima_user_DllExport bool serialize(void *data, eprosima::fastrtps::rtps::SerializedPayload_t *payload) override
Definition: blobPubSubTypes.cpp:42
udds::blob
This class represents the structure blob defined by the user in the IDL file.
Definition: blob.h:60
SerializedPayload_t
eprosima::fastrtps::rtps::SerializedPayload_t SerializedPayload_t
Definition: blobPubSubTypes.cpp:17
blobPubSubTypes.h
unit-test-config.handle
handle
Definition: unit-test-config.py:371
udds::blobPubSubType::getKey
virtual eProsima_user_DllExport bool getKey(void *data, eprosima::fastrtps::rtps::InstanceHandle_t *ihandle, bool force_md5=false) override
Definition: blobPubSubTypes.cpp:122
udds::blob::getCdrSerializedSize
static eProsima_user_DllExport size_t getCdrSerializedSize(const udds::blob &data, size_t current_alignment=0)
This function returns the serialized size of a data depending on the buffer alignment.
Definition: blob.cpp:95
InstanceHandle_t
eprosima::fastrtps::rtps::InstanceHandle_t InstanceHandle_t
Definition: blobPubSubTypes.cpp:18
udds::blob::serialize
eProsima_user_DllExport void serialize(eprosima::fastcdr::Cdr &cdr) const
This function serializes an object using CDR serialization.
Definition: blob.cpp:115
udds::blobPubSubType::getSerializedSizeProvider
virtual eProsima_user_DllExport std::function< uint32_t()> getSerializedSizeProvider(void *data) override
Definition: blobPubSubTypes.cpp:101


librealsense2
Author(s): LibRealSense ROS Team
autogenerated on Fri Aug 2 2024 08:30:01