| Fast DDS Version 3.3.0 Fast DDS

Loading...
Searching...
No Matches
CDRMessage_t.hpp
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
18
19#ifndef FASTDDS_RTPS_COMMON__CDRMESSAGE_T_HPP
20#define FASTDDS_RTPS_COMMON__CDRMESSAGE_T_HPP
21#ifndef DOXYGEN_SHOULD_SKIP_THIS_PUBLIC
22
23#include <fastdds/rtps/common/SerializedPayload.hpp>
24#include <fastdds/rtps/common/Types.hpp>
25#include <cassert>
26#include <cstdlib>
27#include <cstring>
28
29 namespace eprosima {
30 namespace fastdds {
31 namespace rtps {
32
34#define RTPSMESSAGE_DEFAULT_SIZE 10500 //max size of rtps message in bytes
35#define RTPSMESSAGE_COMMON_RTPS_PAYLOAD_SIZE 536 //common payload a rtps message has TODO(Ricardo) It is necessary?
36#define RTPSMESSAGE_COMMON_DATA_PAYLOAD_SIZE 10000 //common data size
37#define RTPSMESSAGE_HEADER_SIZE 20 //header size in bytes
38#define RTPSMESSAGE_SUBMESSAGEHEADER_SIZE 4
39#define RTPSMESSAGE_DATA_EXTRA_INLINEQOS_SIZE 4
40#define RTPSMESSAGE_INFOTS_SIZE 12
41
42#define RTPSMESSAGE_OCTETSTOINLINEQOS_DATASUBMSG 16 //may change in future versions
43#define RTPSMESSAGE_OCTETSTOINLINEQOS_DATAFRAGSUBMSG 28 //may change in future versions
44#define RTPSMESSAGE_DATA_MIN_LENGTH 24
45
50 struct FASTDDS_EXPORTED_API CDRMessage_t final
51{
52 // TODO(Miguel C): Deprecate when not used in mocks
53 CDRMessage_t()
54 : CDRMessage_t(RTPSMESSAGE_DEFAULT_SIZE)
55 {
56 }
57
58 ~CDRMessage_t()
59 {
60 if (buffer != nullptr && !wraps)
61 {
62 free(buffer);
63 }
64 }
65
70 explicit CDRMessage_t(
71 uint32_t size)
72 {
73 wraps = false;
74 pos = 0;
75 length = 0;
76
77 if (size != 0)
78 {
79 buffer = (octet*)malloc(size);
80 }
81 else
82 {
83 buffer = nullptr;
84 }
85
86 max_size = size;
87 reserved_size = size;
88 msg_endian = DEFAULT_ENDIAN;
89 }
90
95 explicit CDRMessage_t(
96 const SerializedPayload_t& payload)
97 : wraps(true)
98 {
99 msg_endian = LITTLEEND;
100 if (payload.encapsulation == PL_CDR_BE || payload.encapsulation == CDR_BE)
101 {
102 msg_endian = BIGEND;
103 }
104 pos = payload.pos;
105 length = payload.length;
106 buffer = payload.data;
107 max_size = payload.max_size;
108 reserved_size = payload.max_size;
109 }
110
111 CDRMessage_t(
112 const CDRMessage_t& message)
113 {
114 wraps = false;
115 pos = 0;
116 length = message.length;
117 max_size = message.max_size;
118 msg_endian = message.msg_endian;
119
120 reserved_size = max_size;
121 if (max_size != 0)
122 {
123 buffer = (octet*)malloc(max_size);
124 memcpy(buffer, message.buffer, length);
125 }
126 else
127 {
128 buffer = nullptr;
129 }
130 }
131
132 CDRMessage_t(
133 CDRMessage_t&& message)
134 {
135 wraps = message.wraps;
136 message.wraps = false;
137 pos = message.pos;
138 message.pos = 0;
139 length = message.length;
140 message.length = 0;
141 max_size = message.max_size;
142 message.max_size = 0;
143 reserved_size = message.reserved_size;
144 message.reserved_size = 0;
145 msg_endian = message.msg_endian;
146 message.msg_endian = DEFAULT_ENDIAN;
147 buffer = message.buffer;
148 message.buffer = nullptr;
149 }
150
151 CDRMessage_t& operator =(
152 CDRMessage_t&& message)
153 {
154 wraps = message.wraps;
155 message.wraps = false;
156 pos = message.pos;
157 message.pos = 0;
158 length = message.length;
159 message.length = 0;
160 max_size = message.max_size;
161 message.max_size = 0;
162 reserved_size = message.reserved_size;
163 message.reserved_size = 0;
164 msg_endian = message.msg_endian;
165 message.msg_endian = DEFAULT_ENDIAN;
166 buffer = message.buffer;
167 message.buffer = nullptr;
168
169 return *(this);
170 }
171
172 void init(
173 octet* buffer_ptr,
174 uint32_t size)
175 {
176 assert(buffer == nullptr);
177 wraps = true;
178 pos = 0;
179 length = 0;
180 buffer = buffer_ptr;
181 max_size = size;
182 reserved_size = size;
183 msg_endian = DEFAULT_ENDIAN;
184 }
185
186 void reserve(
187 uint32_t size)
188 {
189 assert(wraps == false);
190 if (size > reserved_size)
191 {
192 octet* new_buffer = (octet*) realloc(buffer, size);
193 if (new_buffer == nullptr)
194 {
195 // TODO: Exception? Assertion?
196 }
197 else
198 {
199 buffer = new_buffer;
200 reserved_size = size;
201 }
202 }
203
204 max_size = size;
205 }
206
208 octet* buffer;
210 uint32_t pos;
212 uint32_t max_size;
214 uint32_t reserved_size;
216 uint32_t length;
218 Endianness_t msg_endian;
219 //Whether this message is wrapping a buffer managed elsewhere.
220 bool wraps;
221};
222
223} // namespace rtps
224} // namespace fastdds
225} // namespace eprosima
226
227#endif // DOXYGEN_SHOULD_SKIP_THIS_PUBLIC
228#endif // FASTDDS_RTPS_COMMON__CDRMESSAGE_T_HPP
eprosima::fastdds::rtps
Contains the RTPS protocol implementation.
eprosima::fastdds::rtps::Endianness_t
Endianness_t
This enumeration represents endianness types.
Definition Types.hpp:40
eprosima::fastdds::rtps::LITTLEEND
@ LITTLEEND
Little endianness.
Definition Types.hpp:44
eprosima::fastdds::rtps::octet
unsigned char octet
Definition Types.hpp:83
eprosima::fastdds::rtps::DEFAULT_ENDIAN
constexpr Endianness_t DEFAULT_ENDIAN
Definition Types.hpp:80
eprosima::fastdds
eprosima
eProsima namespace.
eprosima::fastdds::rtps::CDRMessage_t::init
void init(octet *buffer_ptr, uint32_t size)
Definition CDRMessage_t.hpp:172
eprosima::fastdds::rtps::CDRMessage_t::buffer
octet * buffer
Pointer to the buffer where the data is stored.
Definition CDRMessage_t.hpp:208
eprosima::fastdds::rtps::CDRMessage_t::max_size
uint32_t max_size
Max size of the message.
Definition CDRMessage_t.hpp:212
eprosima::fastdds::rtps::CDRMessage_t::msg_endian
Endianness_t msg_endian
Endianness of the message.
Definition CDRMessage_t.hpp:218
eprosima::fastdds::rtps::CDRMessage_t::~CDRMessage_t
~CDRMessage_t()
Definition CDRMessage_t.hpp:58
eprosima::fastdds::rtps::CDRMessage_t::CDRMessage_t
CDRMessage_t()
Definition CDRMessage_t.hpp:53
eprosima::fastdds::rtps::CDRMessage_t::reserve
void reserve(uint32_t size)
Definition CDRMessage_t.hpp:186
eprosima::fastdds::rtps::CDRMessage_t::wraps
bool wraps
Definition CDRMessage_t.hpp:220
eprosima::fastdds::rtps::CDRMessage_t::CDRMessage_t
CDRMessage_t(const CDRMessage_t &message)
Definition CDRMessage_t.hpp:111
eprosima::fastdds::rtps::CDRMessage_t::reserved_size
uint32_t reserved_size
Size allocated on buffer. May be higher than max_size.
Definition CDRMessage_t.hpp:214
eprosima::fastdds::rtps::CDRMessage_t::length
uint32_t length
Current length of the message.
Definition CDRMessage_t.hpp:216
eprosima::fastdds::rtps::CDRMessage_t::CDRMessage_t
CDRMessage_t(CDRMessage_t &&message)
Definition CDRMessage_t.hpp:132
eprosima::fastdds::rtps::CDRMessage_t::pos
uint32_t pos
Read or write position.
Definition CDRMessage_t.hpp:210
eprosima::fastdds::rtps::CDRMessage_t::CDRMessage_t
CDRMessage_t(uint32_t size)
Constructor with maximum size.
Definition CDRMessage_t.hpp:70
eprosima::fastdds::rtps::CDRMessage_t::CDRMessage_t
CDRMessage_t(const SerializedPayload_t &payload)
Constructor to wrap a serialized payload.
Definition CDRMessage_t.hpp:95
eprosima::fastdds::rtps::SerializedPayload_t
Structure SerializedPayload_t.
Definition SerializedPayload.hpp:59
eprosima::fastdds::rtps::SerializedPayload_t::data
octet * data
Pointer to the data.
Definition SerializedPayload.hpp:68
eprosima::fastdds::rtps::SerializedPayload_t::encapsulation
uint16_t encapsulation
Encapsulation of the data as suggested in the RTPS 2.1 specification chapter 10.
Definition SerializedPayload.hpp:64
eprosima::fastdds::rtps::SerializedPayload_t::max_size
uint32_t max_size
Maximum size of the payload.
Definition SerializedPayload.hpp:70
eprosima::fastdds::rtps::SerializedPayload_t::length
uint32_t length
Actual length of the data.
Definition SerializedPayload.hpp:66
eprosima::fastdds::rtps::SerializedPayload_t::pos
uint32_t pos
Position when reading.
Definition SerializedPayload.hpp:72