Apollo 10.0
自动驾驶开放平台
underlay_message_type.cc
浏览该文件的文档.
1// Copyright 2016 Proyectos y Sistemas de Mantenimiento SL (eProsima).
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
23
24#include <fastcdr/CdrEncoding.hpp>
25
26#include "fastcdr/Cdr.h"
27#include "fastcdr/FastBuffer.h"
28
29#include "cyber/common/log.h"
30
31namespace apollo {
32namespace cyber {
33namespace transport {
34
36 setName("UnderlayMessage");
38 type_size += eprosima::fastcdr::Cdr::alignment(
39 type_size, 4); /* possible submessage alignment */
40 m_typeSize = static_cast<uint32_t>(type_size) + 4; /*encapsulation*/
41 m_isGetKeyDefined = UnderlayMessage::isKeyDefined();
42 size_t keyLength = UnderlayMessage::getKeyMaxCdrSerializedSize() > 16
44 : 16;
45 m_keyBuffer = (unsigned char*)malloc(keyLength);
46 memset(m_keyBuffer, 0, keyLength);
47}
48
50 if (m_keyBuffer != nullptr) {
51 free(m_keyBuffer);
52 }
53}
54
56 void* data, eprosima::fastrtps::rtps::SerializedPayload_t* payload) {
57 UnderlayMessage* p_type = reinterpret_cast<UnderlayMessage*>(data);
58 eprosima::fastcdr::FastBuffer fastbuffer(
59 reinterpret_cast<char*>(payload->data),
60 payload->max_size); // Object that manages the raw buffer.
61 eprosima::fastcdr::Cdr ser(
62 fastbuffer, eprosima::fastcdr::Cdr::DEFAULT_ENDIAN,
63 eprosima::fastcdr::CdrVersion::DDS_CDR); // Object that serializes the
64 // data.
65 payload->encapsulation =
66 ser.endianness() == eprosima::fastcdr::Cdr::BIG_ENDIANNESS ? CDR_BE
67 : CDR_LE;
68 try {
69 // Serialize encapsulation
70 ser.serialize_encapsulation();
71 // Serialize the object.
72 p_type->serialize(ser);
73 } catch (eprosima::fastcdr::exception::Exception& e) {
74 AERROR << "serialize exception: " << e.what();
75 return false;
76 }
77
78 payload->length =
79 (uint32_t)ser.get_serialized_data_length(); // Get the serialized length
80 return true;
81}
82
84 eprosima::fastrtps::rtps::SerializedPayload_t* payload, void* data) {
85 try {
86 // Convert DATA to pointer of your type
87 UnderlayMessage* p_type = static_cast<UnderlayMessage*>(data);
88
89 // Object that manages the raw buffer.
90 eprosima::fastcdr::FastBuffer fastbuffer(
91 reinterpret_cast<char*>(payload->data), payload->length);
92
93 // Object that deserializes the data.
94 eprosima::fastcdr::Cdr deser(fastbuffer,
95 eprosima::fastcdr::Cdr::DEFAULT_ENDIAN,
96 eprosima::fastcdr::CdrVersion::DDS_CDR);
97
98 // Deserialize encapsulation.
99 deser.read_encapsulation();
100 payload->encapsulation =
101 deser.endianness() == eprosima::fastcdr::Cdr::BIG_ENDIANNESS ? CDR_BE
102 : CDR_LE;
103
104 // Deserialize the object.
105 p_type->deserialize(deser);
106 } catch (eprosima::fastcdr::exception::Exception& e) {
107 AERROR << "deserialize exception: " << e.what();
108 return false;
109 }
110 return true;
111}
112
114 void* data) {
115 return [data]() -> uint32_t {
116 return (uint32_t)type::getCdrSerializedSize(
117 *static_cast<UnderlayMessage*>(data)) +
118 4u /*encapsulation*/;
119 };
120}
121
123 return reinterpret_cast<void*>(new UnderlayMessage());
124}
125
127 delete (reinterpret_cast<UnderlayMessage*>(data));
128}
129
131 void* data, eprosima::fastrtps::rtps::InstanceHandle_t* handle,
132 bool force_md5) {
133 RETURN_VAL_IF(!m_isGetKeyDefined, false);
134 UnderlayMessage* p_type = reinterpret_cast<UnderlayMessage*>(data);
135 eprosima::fastcdr::FastBuffer fastbuffer(
136 reinterpret_cast<char*>(m_keyBuffer),
137 UnderlayMessage::getKeyMaxCdrSerializedSize()); // Object that manages
138 // the raw buffer.
139 // Object that serializes the data.
140 eprosima::fastcdr::Cdr ser(fastbuffer,
141 eprosima::fastcdr::Cdr::BIG_ENDIANNESS);
142 p_type->serializeKey(ser);
143 if (force_md5 || UnderlayMessage::getKeyMaxCdrSerializedSize() > 16) {
144 m_md5.init();
145 m_md5.update(m_keyBuffer,
146 static_cast<unsigned int>(ser.get_serialized_data_length()));
147 m_md5.finalize();
148 for (uint8_t i = 0; i < 16; ++i) {
149 handle->value[i] = m_md5.digest[i];
150 }
151 } else {
152 for (uint8_t i = 0; i < 16; ++i) {
153 handle->value[i] = m_keyBuffer[i];
154 }
155 }
156 return true;
157}
158
159} // namespace transport
160} // namespace cyber
161} // namespace apollo
virtual bool serialize(void *data, eprosima::fastrtps::rtps::SerializedPayload_t *payload) override
virtual bool deserialize(eprosima::fastrtps::rtps::SerializedPayload_t *payload, void *data) override
virtual std::function< uint32_t()> getSerializedSizeProvider(void *data) override
virtual bool getKey(void *data, eprosima::fastrtps::rtps::InstanceHandle_t *ihandle, bool force_md5=false) override
This class represents the structure UnderlayMessage defined by the user in the IDL file.
void serializeKey(eprosima::fastcdr::Cdr &cdr) const
This function serializes the key members of an object using CDR serialization.
static bool isKeyDefined()
This function tells you if the Key has been defined for this type
static size_t getCdrSerializedSize(const UnderlayMessage &data, size_t current_alignment=0)
This function returns the serialized size of a data depending on the buffer alignment.
static size_t getMaxCdrSerializedSize(size_t current_alignment=0)
This function returns the maximum serialized size of an object depending on the buffer alignment.
void deserialize(eprosima::fastcdr::Cdr &cdr)
This function deserializes an object using CDR serialization.
static 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...
void serialize(eprosima::fastcdr::Cdr &cdr) const
This function serializes an object using CDR serialization.
#define RETURN_VAL_IF(condition, val)
Definition log.h:114
#define AERROR
Definition log.h:44
class register implement
Definition arena_queue.h:37