Apollo 11.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/Cdr.h"
25#include "fastcdr/FastBuffer.h"
26
27#include "cyber/common/log.h"
28
29namespace apollo {
30namespace cyber {
31namespace transport {
32
34 setName("UnderlayMessage");
35 m_typeSize = (uint32_t)UnderlayMessage::getMaxCdrSerializedSize() +
36 4 /*encapsulation*/;
37 m_isGetKeyDefined = UnderlayMessage::isKeyDefined();
39 (unsigned char*)malloc(UnderlayMessage::getKeyMaxCdrSerializedSize() > 16
41 : 16);
42}
43
45 if (m_keyBuffer != nullptr) {
46 free(m_keyBuffer);
47 }
48}
49
50bool UnderlayMessageType::serialize(void* data, SerializedPayload_t* payload) {
51 UnderlayMessage* p_type = reinterpret_cast<UnderlayMessage*>(data);
52 eprosima::fastcdr::FastBuffer fastbuffer(
53 reinterpret_cast<char*>(payload->data),
54 payload->max_size); // Object that manages the raw buffer.
55 eprosima::fastcdr::Cdr ser(
56 fastbuffer, eprosima::fastcdr::Cdr::DEFAULT_ENDIAN,
57 eprosima::fastcdr::Cdr::DDS_CDR); // Object that serializes the data.
58 payload->encapsulation =
59 ser.endianness() == eprosima::fastcdr::Cdr::BIG_ENDIANNESS ? CDR_BE
60 : CDR_LE;
61 // Serialize encapsulation
62 ser.serialize_encapsulation();
63 p_type->serialize(ser); // Serialize the object:
64 payload->length =
65 (uint32_t)ser.getSerializedDataLength(); // Get the serialized length
66 return true;
67}
68
69bool UnderlayMessageType::deserialize(SerializedPayload_t* payload,
70 void* data) {
71 UnderlayMessage* p_type = reinterpret_cast<UnderlayMessage*>(
72 data); // Convert DATA to pointer of your type
73 eprosima::fastcdr::FastBuffer fastbuffer(
74 reinterpret_cast<char*>(payload->data),
75 payload->length); // Object that manages the raw buffer.
76 eprosima::fastcdr::Cdr deser(
77 fastbuffer, eprosima::fastcdr::Cdr::DEFAULT_ENDIAN,
78 eprosima::fastcdr::Cdr::DDS_CDR); // Object that deserializes the data.
79 // Deserialize encapsulation.
80 deser.read_encapsulation();
81 payload->encapsulation =
82 deser.endianness() == eprosima::fastcdr::Cdr::BIG_ENDIANNESS ? CDR_BE
83 : CDR_LE;
84 p_type->deserialize(deser); // Deserialize the object:
85 return true;
86}
87
89 void* data) {
90 return [data]() -> uint32_t {
91 return (uint32_t)type::getCdrSerializedSize(
92 *static_cast<UnderlayMessage*>(data)) +
93 4 /*encapsulation*/;
94 };
95}
96
98 return reinterpret_cast<void*>(new UnderlayMessage());
99}
100
102 delete (reinterpret_cast<UnderlayMessage*>(data));
103}
104
105bool UnderlayMessageType::getKey(void* data, InstanceHandle_t* handle) {
106 RETURN_VAL_IF((!m_isGetKeyDefined), false);
107 UnderlayMessage* p_type = reinterpret_cast<UnderlayMessage*>(data);
108 eprosima::fastcdr::FastBuffer fastbuffer(
109 reinterpret_cast<char*>(m_keyBuffer),
110 UnderlayMessage::getKeyMaxCdrSerializedSize()); // Object that manages
111 // the raw buffer.
112 eprosima::fastcdr::Cdr ser(
113 fastbuffer, eprosima::fastcdr::Cdr::BIG_ENDIANNESS); // Object that
114 // serializes the
115 // data.
116 p_type->serializeKey(ser);
118 m_md5.init();
119 m_md5.update(m_keyBuffer, (unsigned int)ser.getSerializedDataLength());
120 m_md5.finalize();
121 for (uint8_t i = 0; i < 16; ++i) {
122 handle->value[i] = m_md5.digest[i];
123 }
124 } else {
125 for (uint8_t i = 0; i < 16; ++i) {
126 handle->value[i] = m_keyBuffer[i];
127 }
128 }
129 return true;
130}
131
132} // namespace transport
133} // namespace cyber
134} // namespace apollo
std::function< uint32_t()> getSerializedSizeProvider(void *data)
bool getKey(void *data, InstanceHandle_t *ihandle)
bool serialize(void *data, SerializedPayload_t *payload)
bool deserialize(SerializedPayload_t *payload, void *data)
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
class register implement
Definition arena_queue.h:37