/src/Fast-DDS/include/fastdds/rtps/common/CDRMessage_t.hpp
Line | Count | Source (jump to first uncovered line) |
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 | | |
15 | | /** |
16 | | * @file CDRMessage_t.hpp |
17 | | */ |
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 | | |
33 | | //!Max size of RTPS message in bytes. |
34 | 0 | #define RTPSMESSAGE_DEFAULT_SIZE 10500 //max size of rtps message in bytes |
35 | 0 | #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 | 5.27k | #define RTPSMESSAGE_HEADER_SIZE 20 //header size in bytes |
38 | 0 | #define RTPSMESSAGE_SUBMESSAGEHEADER_SIZE 4 |
39 | 0 | #define RTPSMESSAGE_DATA_EXTRA_INLINEQOS_SIZE 4 |
40 | | #define RTPSMESSAGE_INFOTS_SIZE 12 |
41 | | |
42 | 0 | #define RTPSMESSAGE_OCTETSTOINLINEQOS_DATASUBMSG 16 //may change in future versions |
43 | 0 | #define RTPSMESSAGE_OCTETSTOINLINEQOS_DATAFRAGSUBMSG 28 //may change in future versions |
44 | 64 | #define RTPSMESSAGE_DATA_MIN_LENGTH 24 |
45 | | |
46 | | /** |
47 | | * @brief Structure CDRMessage_t, contains a serialized message. |
48 | | * @ingroup COMMON_MODULE |
49 | | */ |
50 | | struct FASTDDS_EXPORTED_API CDRMessage_t final |
51 | | { |
52 | | // TODO(Miguel C): Deprecate when not used in mocks |
53 | | CDRMessage_t() |
54 | 0 | : CDRMessage_t(RTPSMESSAGE_DEFAULT_SIZE) |
55 | 0 | { |
56 | 0 | } |
57 | | |
58 | | ~CDRMessage_t() |
59 | 1.74k | { |
60 | 1.74k | if (buffer != nullptr && !wraps) |
61 | 0 | { |
62 | 0 | free(buffer); |
63 | 0 | } |
64 | 1.74k | } |
65 | | |
66 | | /** |
67 | | * Constructor with maximum size |
68 | | * @param size Maximum size |
69 | | */ |
70 | | explicit CDRMessage_t( |
71 | | uint32_t size) |
72 | 1.74k | { |
73 | 1.74k | wraps = false; |
74 | 1.74k | pos = 0; |
75 | 1.74k | length = 0; |
76 | | |
77 | 1.74k | if (size != 0) |
78 | 0 | { |
79 | 0 | buffer = (octet*)malloc(size); |
80 | 0 | } |
81 | 1.74k | else |
82 | 1.74k | { |
83 | 1.74k | buffer = nullptr; |
84 | 1.74k | } |
85 | | |
86 | 1.74k | max_size = size; |
87 | 1.74k | reserved_size = size; |
88 | 1.74k | msg_endian = DEFAULT_ENDIAN; |
89 | 1.74k | } |
90 | | |
91 | | /** |
92 | | * Constructor to wrap a serialized payload |
93 | | * @param payload Payload to wrap |
94 | | */ |
95 | | explicit CDRMessage_t( |
96 | | const SerializedPayload_t& payload) |
97 | 0 | : wraps(true) |
98 | 0 | { |
99 | 0 | msg_endian = LITTLEEND; |
100 | 0 | if (payload.encapsulation == PL_CDR_BE || payload.encapsulation == CDR_BE) |
101 | 0 | { |
102 | 0 | msg_endian = BIGEND; |
103 | 0 | } |
104 | 0 | pos = payload.pos; |
105 | 0 | length = payload.length; |
106 | 0 | buffer = payload.data; |
107 | 0 | max_size = payload.max_size; |
108 | 0 | reserved_size = payload.max_size; |
109 | 0 | } |
110 | | |
111 | | CDRMessage_t( |
112 | | const CDRMessage_t& message) |
113 | 0 | { |
114 | 0 | wraps = false; |
115 | 0 | pos = 0; |
116 | 0 | length = message.length; |
117 | 0 | max_size = message.max_size; |
118 | 0 | msg_endian = message.msg_endian; |
119 | 0 |
|
120 | 0 | reserved_size = max_size; |
121 | 0 | if (max_size != 0) |
122 | 0 | { |
123 | 0 | buffer = (octet*)malloc(max_size); |
124 | 0 | memcpy(buffer, message.buffer, length); |
125 | 0 | } |
126 | 0 | else |
127 | 0 | { |
128 | 0 | buffer = nullptr; |
129 | 0 | } |
130 | 0 | } |
131 | | |
132 | | CDRMessage_t( |
133 | | CDRMessage_t&& message) |
134 | 0 | { |
135 | 0 | wraps = message.wraps; |
136 | 0 | message.wraps = false; |
137 | 0 | pos = message.pos; |
138 | 0 | message.pos = 0; |
139 | 0 | length = message.length; |
140 | 0 | message.length = 0; |
141 | 0 | max_size = message.max_size; |
142 | 0 | message.max_size = 0; |
143 | 0 | reserved_size = message.reserved_size; |
144 | 0 | message.reserved_size = 0; |
145 | 0 | msg_endian = message.msg_endian; |
146 | 0 | message.msg_endian = DEFAULT_ENDIAN; |
147 | 0 | buffer = message.buffer; |
148 | 0 | message.buffer = nullptr; |
149 | 0 | } |
150 | | |
151 | | CDRMessage_t& operator =( |
152 | | CDRMessage_t&& message) |
153 | 0 | { |
154 | 0 | wraps = message.wraps; |
155 | 0 | message.wraps = false; |
156 | 0 | pos = message.pos; |
157 | 0 | message.pos = 0; |
158 | 0 | length = message.length; |
159 | 0 | message.length = 0; |
160 | 0 | max_size = message.max_size; |
161 | 0 | message.max_size = 0; |
162 | 0 | reserved_size = message.reserved_size; |
163 | 0 | message.reserved_size = 0; |
164 | 0 | msg_endian = message.msg_endian; |
165 | 0 | message.msg_endian = DEFAULT_ENDIAN; |
166 | 0 | buffer = message.buffer; |
167 | 0 | message.buffer = nullptr; |
168 | 0 |
|
169 | 0 | return *(this); |
170 | 0 | } |
171 | | |
172 | | void init( |
173 | | octet* buffer_ptr, |
174 | | uint32_t size) |
175 | 0 | { |
176 | 0 | assert(buffer == nullptr); |
177 | 0 | wraps = true; |
178 | 0 | pos = 0; |
179 | 0 | length = 0; |
180 | 0 | buffer = buffer_ptr; |
181 | 0 | max_size = size; |
182 | 0 | reserved_size = size; |
183 | 0 | msg_endian = DEFAULT_ENDIAN; |
184 | 0 | } |
185 | | |
186 | | void reserve( |
187 | | uint32_t size) |
188 | 0 | { |
189 | 0 | assert(wraps == false); |
190 | 0 | if (size > reserved_size) |
191 | 0 | { |
192 | 0 | octet* new_buffer = (octet*) realloc(buffer, size); |
193 | 0 | if (new_buffer == nullptr) |
194 | 0 | { |
195 | | // TODO: Exception? Assertion? |
196 | 0 | } |
197 | 0 | else |
198 | 0 | { |
199 | 0 | buffer = new_buffer; |
200 | 0 | reserved_size = size; |
201 | 0 | } |
202 | 0 | } |
203 | |
|
204 | 0 | max_size = size; |
205 | 0 | } |
206 | | |
207 | | //!Pointer to the buffer where the data is stored. |
208 | | octet* buffer; |
209 | | //!Read or write position. |
210 | | uint32_t pos; |
211 | | //!Max size of the message. |
212 | | uint32_t max_size; |
213 | | //!Size allocated on buffer. May be higher than max_size. |
214 | | uint32_t reserved_size; |
215 | | //!Current length of the message. |
216 | | uint32_t length; |
217 | | //!Endianness of the message. |
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 |