/src/Fast-DDS/include/fastdds/rtps/common/SerializedPayload.h
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 SerializedPayload.h |
17 | | */ |
18 | | |
19 | | #ifndef _FASTDDS_RTPS_SERIALIZEDPAYLOAD_H_ |
20 | | #define _FASTDDS_RTPS_SERIALIZEDPAYLOAD_H_ |
21 | | #include <fastrtps/fastrtps_dll.h> |
22 | | #include <fastdds/rtps/common/Types.h> |
23 | | #include <cstring> |
24 | | #include <new> |
25 | | #include <stdexcept> |
26 | | #include <stdint.h> |
27 | | #include <stdlib.h> |
28 | | |
29 | | /*! |
30 | | * @brief Maximum payload is maximum of UDP packet size minus 536bytes (RTPSMESSAGE_COMMON_RTPS_PAYLOAD_SIZE) |
31 | | * With those 536 bytes (RTPSMESSAGE_COMMON_RTPS_PAYLOAD_SIZE) bytes is posible to send RTPS Header plus RTPS Data submessage plus RTPS Heartbeat submessage. |
32 | | */ |
33 | | |
34 | | namespace eprosima { |
35 | | namespace fastrtps { |
36 | | namespace rtps { |
37 | | |
38 | | //Pre define data encapsulation schemes |
39 | 80.6k | #define CDR_BE 0x0000 |
40 | 56 | #define CDR_LE 0x0001 |
41 | 0 | #define PL_CDR_BE 0x0002 |
42 | 0 | #define PL_CDR_LE 0x0003 |
43 | | |
44 | | #if FASTDDS_IS_BIG_ENDIAN_TARGET |
45 | | #define DEFAULT_ENCAPSULATION CDR_LE |
46 | | #define PL_DEFAULT_ENCAPSULATION PL_CDR_BE |
47 | | #else |
48 | 0 | #define DEFAULT_ENCAPSULATION CDR_LE |
49 | 0 | #define PL_DEFAULT_ENCAPSULATION PL_CDR_LE |
50 | | #endif // FASTDDS_IS_BIG_ENDIAN_TARGET |
51 | | |
52 | | //!@brief Structure SerializedPayload_t. |
53 | | //!@ingroup COMMON_MODULE |
54 | | struct RTPS_DllAPI SerializedPayload_t |
55 | | { |
56 | | //!Size in bytes of the representation header as specified in the RTPS 2.3 specification chapter 10. |
57 | | static constexpr size_t representation_header_size = 4u; |
58 | | |
59 | | //!Encapsulation of the data as suggested in the RTPS 2.1 specification chapter 10. |
60 | | uint16_t encapsulation; |
61 | | //!Actual length of the data |
62 | | uint32_t length; |
63 | | //!Pointer to the data. |
64 | | octet* data; |
65 | | //!Maximum size of the payload |
66 | | uint32_t max_size; |
67 | | //!Position when reading |
68 | | uint32_t pos; |
69 | | |
70 | | //!Default constructor |
71 | | SerializedPayload_t() |
72 | | : encapsulation(CDR_BE) |
73 | | , length(0) |
74 | | , data(nullptr) |
75 | | , max_size(0) |
76 | | , pos(0) |
77 | 84.1k | { |
78 | 84.1k | } |
79 | | |
80 | | /** |
81 | | * @param len Maximum size of the payload |
82 | | */ |
83 | | explicit SerializedPayload_t( |
84 | | uint32_t len) |
85 | | : SerializedPayload_t() |
86 | 56 | { |
87 | 56 | this->reserve(len); |
88 | 56 | } |
89 | | |
90 | | ~SerializedPayload_t() |
91 | 80.6k | { |
92 | 80.6k | this->empty(); |
93 | 80.6k | } |
94 | | |
95 | | bool operator == ( |
96 | | const SerializedPayload_t& other) const |
97 | 0 | { |
98 | 0 | return ((encapsulation == other.encapsulation) && |
99 | 0 | (length == other.length) && |
100 | 0 | (0 == memcmp(data, other.data, length))); |
101 | 0 | } |
102 | | |
103 | | /*! |
104 | | * Copy another structure (including allocating new space for the data.) |
105 | | * @param[in] serData Pointer to the structure to copy |
106 | | * @param with_limit if true, the function will fail when providing a payload too big |
107 | | * @return True if correct |
108 | | */ |
109 | | bool copy( |
110 | | const SerializedPayload_t* serData, |
111 | | bool with_limit = true) |
112 | 165 | { |
113 | 165 | length = serData->length; |
114 | | |
115 | 165 | if (serData->length > max_size) |
116 | 0 | { |
117 | 0 | if (with_limit) |
118 | 0 | { |
119 | 0 | return false; |
120 | 0 | } |
121 | 0 | else |
122 | 0 | { |
123 | 0 | this->reserve(serData->length); |
124 | 0 | } |
125 | 0 | } |
126 | 165 | encapsulation = serData->encapsulation; |
127 | 165 | if (length == 0) |
128 | 165 | { |
129 | 165 | return true; |
130 | 165 | } |
131 | 0 | memcpy(data, serData->data, length); |
132 | 0 | return true; |
133 | 165 | } |
134 | | |
135 | | /*! |
136 | | * Allocate new space for fragmented data |
137 | | * @param[in] serData Pointer to the structure to copy |
138 | | * @return True if correct |
139 | | */ |
140 | | bool reserve_fragmented( |
141 | | SerializedPayload_t* serData) |
142 | 0 | { |
143 | 0 | length = serData->length; |
144 | 0 | max_size = serData->length; |
145 | 0 | encapsulation = serData->encapsulation; |
146 | 0 | data = (octet*)calloc(length, sizeof(octet)); |
147 | 0 | return true; |
148 | 0 | } |
149 | | |
150 | | //! Empty the payload |
151 | | void empty() |
152 | 80.6k | { |
153 | 80.6k | length = 0; |
154 | 80.6k | encapsulation = CDR_BE; |
155 | 80.6k | max_size = 0; |
156 | 80.6k | if (data != nullptr) |
157 | 56 | { |
158 | 56 | free(data); |
159 | 56 | } |
160 | 80.6k | data = nullptr; |
161 | 80.6k | } |
162 | | |
163 | | void reserve( |
164 | | uint32_t new_size) |
165 | 221 | { |
166 | 221 | if (new_size <= this->max_size) |
167 | 165 | { |
168 | 165 | return; |
169 | 165 | } |
170 | 56 | if (data == nullptr) |
171 | 56 | { |
172 | 56 | data = (octet*)calloc(new_size, sizeof(octet)); |
173 | 56 | if (!data) |
174 | 0 | { |
175 | 0 | throw std::bad_alloc(); |
176 | 0 | } |
177 | 56 | } |
178 | 0 | else |
179 | 0 | { |
180 | 0 | void* old_data = data; |
181 | 0 | data = (octet*)realloc(data, new_size); |
182 | 0 | if (!data) |
183 | 0 | { |
184 | 0 | free(old_data); |
185 | 0 | throw std::bad_alloc(); |
186 | 0 | } |
187 | 0 | memset(data + max_size, 0, (new_size - max_size) * sizeof(octet)); |
188 | 0 | } |
189 | 56 | max_size = new_size; |
190 | 56 | } |
191 | | |
192 | | }; |
193 | | |
194 | | } /* namespace rtps */ |
195 | | } /* namespace fastrtps */ |
196 | | } /* namespace eprosima */ |
197 | | |
198 | | #endif /* _FASTDDS_RTPS_SERIALIZEDPAYLOAD_H_ */ |