Coverage Report

Created: 2026-07-25 06:47

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/Fast-DDS/include/fastdds/rtps/common/SerializedPayload.hpp
Line
Count
Source
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.hpp
17
 */
18
19
#ifndef FASTDDS_RTPS_COMMON__SERIALIZEDPAYLOAD_HPP
20
#define FASTDDS_RTPS_COMMON__SERIALIZEDPAYLOAD_HPP
21
22
#include <cstring>
23
#include <new>
24
#include <stdexcept>
25
#include <cassert>
26
#include <stdint.h>
27
#include <stdlib.h>
28
29
#include <fastdds/fastdds_dll.hpp>
30
#include <fastdds/rtps/common/Types.hpp>
31
#include <fastdds/rtps/history/IPayloadPool.hpp>
32
33
/*!
34
 * @brief Maximum payload is maximum of UDP packet size minus 536bytes (RTPSMESSAGE_COMMON_RTPS_PAYLOAD_SIZE)
35
 * With those 536 bytes (RTPSMESSAGE_COMMON_RTPS_PAYLOAD_SIZE) bytes is posible to send RTPS Header plus RTPS Data submessage plus RTPS Heartbeat submessage.
36
 */
37
38
namespace eprosima {
39
namespace fastdds {
40
namespace rtps {
41
42
//Pre define data encapsulation schemes
43
395k
#define CDR_BE 0x0000
44
0
#define CDR_LE 0x0001
45
7.33k
#define PL_CDR_BE 0x0002
46
6.95k
#define PL_CDR_LE 0x0003
47
48
#if FASTDDS_IS_BIG_ENDIAN_TARGET
49
#define DEFAULT_ENCAPSULATION CDR_BE
50
#define PL_DEFAULT_ENCAPSULATION PL_CDR_BE
51
#else
52
0
#define DEFAULT_ENCAPSULATION CDR_LE
53
#define PL_DEFAULT_ENCAPSULATION PL_CDR_LE
54
#endif  // FASTDDS_IS_BIG_ENDIAN_TARGET
55
56
//!@brief Structure SerializedPayload_t.
57
//!@ingroup COMMON_MODULE
58
struct FASTDDS_EXPORTED_API SerializedPayload_t
59
{
60
    //!Size in bytes of the representation header as specified in the RTPS 2.3 specification chapter 10.
61
    static constexpr size_t representation_header_size = 4u;
62
63
    //!Encapsulation of the data as suggested in the RTPS 2.1 specification chapter 10.
64
    uint16_t encapsulation;
65
    //!Actual length of the data
66
    uint32_t length;
67
    //!Pointer to the data.
68
    octet* data;
69
    //!Maximum size of the payload
70
    uint32_t max_size;
71
    //!Position when reading
72
    uint32_t pos;
73
    //!Pool that created the payload
74
    IPayloadPool* payload_owner = nullptr;
75
    //!Whether the payload contains a serialized key, or the whole data
76
    bool is_serialized_key = false;
77
78
    //!Default constructor
79
    SerializedPayload_t()
80
199k
        : encapsulation(CDR_BE)
81
199k
        , length(0)
82
199k
        , data(nullptr)
83
199k
        , max_size(0)
84
199k
        , pos(0)
85
199k
    {
86
199k
    }
87
88
    /**
89
     * @param len Maximum size of the payload
90
     */
91
    explicit SerializedPayload_t(
92
            uint32_t len)
93
25.6k
        : SerializedPayload_t()
94
25.6k
    {
95
25.6k
        this->reserve(len);
96
25.6k
    }
97
98
    //!Copy constructor
99
    SerializedPayload_t(
100
            const SerializedPayload_t& other) = delete;
101
    //!Copy operator
102
    SerializedPayload_t& operator = (
103
            const SerializedPayload_t& other) = delete;
104
105
    //!Move constructor
106
    //!Directly moves the fields from \c other instead of calling the move assignment operator,
107
    //!since the latter assumes the destination is already constructed (it may release the
108
    //!currently owned payload). Using the move assignment operator here would read
109
    //!uninitialized members of the newly constructed object.
110
    SerializedPayload_t(
111
            SerializedPayload_t&& other) noexcept
112
0
        : encapsulation(other.encapsulation)
113
0
        , length(other.length)
114
0
        , data(other.data)
115
0
        , max_size(other.max_size)
116
0
        , pos(other.pos)
117
0
        , payload_owner(other.payload_owner)
118
0
        , is_serialized_key(other.is_serialized_key)
119
0
    {
120
0
        other.encapsulation = CDR_BE;
121
0
        other.length = 0;
122
0
        other.data = nullptr;
123
0
        other.max_size = 0;
124
0
        other.pos = 0;
125
0
        other.payload_owner = nullptr;
126
0
        other.is_serialized_key = false;
127
0
    }
128
129
    //!Move operator
130
    SerializedPayload_t& operator = (
131
            SerializedPayload_t&& other) noexcept;
132
133
    /*!
134
     * Destructor
135
     * It is expected to release the payload if the payload owner is not nullptr before destruction
136
     */
137
    ~SerializedPayload_t();
138
139
    bool operator == (
140
            const SerializedPayload_t& other) const;
141
142
    /*!
143
     * Copy another structure (including allocating new space for the data).
144
     * @param [in] serData Pointer to the structure to copy
145
     * @param with_limit if true, the function will fail when providing a payload too big
146
     * @return True if correct
147
     */
148
    bool copy(
149
            const SerializedPayload_t* serData,
150
            bool with_limit = true);
151
152
    /*!
153
     * Allocate new space for fragmented data
154
     * @param [in] serData Pointer to the structure to copy
155
     * @return True if correct
156
     */
157
    bool reserve_fragmented(
158
            SerializedPayload_t* serData);
159
160
    /*!
161
     * Empty the payload
162
     * @pre payload_owner must be nullptr
163
     */
164
    void empty();
165
166
    void reserve(
167
            uint32_t new_size);
168
169
};
170
171
} // namespace rtps
172
} // namespace fastdds
173
} // namespace eprosima
174
175
#endif // FASTDDS_RTPS_COMMON__SERIALIZEDPAYLOAD_HPP