Skip to content

Commit

Permalink
Refs #21121: Create SerializedPayload.cpp
Browse files Browse the repository at this point in the history
Signed-off-by: cferreiragonz <[email protected]>
  • Loading branch information
cferreiragonz committed Jun 11, 2024
1 parent 4331af5 commit c7c210b
Show file tree
Hide file tree
Showing 22 changed files with 204 additions and 123 deletions.
145 changes: 22 additions & 123 deletions include/fastdds/rtps/common/SerializedPayload.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,6 @@ struct FASTDDS_EXPORTED_API SerializedPayload_t
{
}

//!Copy constructor
SerializedPayload_t(
const SerializedPayload_t& other) = delete;
//!Copy operator
SerializedPayload_t& operator = (
const SerializedPayload_t& other) = delete;

/**
* @param len Maximum size of the payload
*/
Expand All @@ -100,52 +93,12 @@ struct FASTDDS_EXPORTED_API SerializedPayload_t
this->reserve(len);
}

/*!
* Destructor
* It is expected to release the payload if the payload owner is not nullptr before destruction
*/
~SerializedPayload_t()
{
if (payload_owner != nullptr)
{
payload_owner->release_payload(*this);
}
this->empty();
}

bool operator == (
const SerializedPayload_t& other) const
{
return ((encapsulation == other.encapsulation) &&
(length == other.length) &&
(0 == memcmp(data, other.data, length)));
}

//!Move operator
//!Copy constructor
SerializedPayload_t(
const SerializedPayload_t& other) = delete;
//!Copy operator
SerializedPayload_t& operator = (
SerializedPayload_t&& other) noexcept
{
if (this == &other)
{
return *this;
}

encapsulation = other.encapsulation;
length = other.length;
data = other.data;
max_size = other.max_size;
pos = other.pos;
payload_owner = other.payload_owner;

other.encapsulation = CDR_BE;
other.length = 0;
other.data = nullptr;
other.max_size = 0;
other.pos = 0;
other.payload_owner = nullptr;

return *this;
}
const SerializedPayload_t& other) = delete;

//!Move constructor
SerializedPayload_t(
Expand All @@ -154,6 +107,19 @@ struct FASTDDS_EXPORTED_API SerializedPayload_t
*this = std::move(other);
}

//!Move operator
SerializedPayload_t& operator = (
SerializedPayload_t&& other) noexcept;

/*!
* Destructor
* It is expected to release the payload if the payload owner is not nullptr before destruction
*/
~SerializedPayload_t();

bool operator == (
const SerializedPayload_t& other) const;

/*!
* Copy another structure (including allocating new space for the data).
* @param[in] serData Pointer to the structure to copy
Expand All @@ -162,91 +128,24 @@ struct FASTDDS_EXPORTED_API SerializedPayload_t
*/
bool copy(
const SerializedPayload_t* serData,
bool with_limit = true)
{
length = serData->length;

if (serData->length > max_size)
{
if (with_limit)
{
return false;
}
else
{
this->reserve(serData->length);
}
}
encapsulation = serData->encapsulation;
if (length == 0)
{
return true;
}
memcpy(data, serData->data, length);
return true;
}
bool with_limit = true);

/*!
* Allocate new space for fragmented data
* @param[in] serData Pointer to the structure to copy
* @return True if correct
*/
bool reserve_fragmented(
SerializedPayload_t* serData)
{
length = serData->length;
max_size = serData->length;
encapsulation = serData->encapsulation;
data = (octet*)calloc(length, sizeof(octet));
return true;
}
SerializedPayload_t* serData);

/*!
* Empty the payload
* @pre payload_owner must be nullptr
*/
void empty()
{
assert(payload_owner == nullptr);

length = 0;
encapsulation = CDR_BE;
max_size = 0;
if (data != nullptr)
{
free(data);
}
data = nullptr;
}
void empty();

void reserve(
uint32_t new_size)
{
if (new_size <= this->max_size)
{
return;
}
if (data == nullptr)
{
data = (octet*)calloc(new_size, sizeof(octet));
if (!data)
{
throw std::bad_alloc();
}
}
else
{
void* old_data = data;
data = (octet*)realloc(data, new_size);
if (!data)
{
free(old_data);
throw std::bad_alloc();
}
memset(data + max_size, 0, (new_size - max_size) * sizeof(octet));
}
max_size = new_size;
}
uint32_t new_size);

};

Expand Down
1 change: 1 addition & 0 deletions src/cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ set(${PROJECT_NAME}_source_files
rtps/builtin/liveliness/WLP.cpp
rtps/builtin/liveliness/WLPListener.cpp
rtps/common/GuidPrefix_t.cpp
rtps/common/SerializedPayload.cpp
rtps/common/LocatorWithMask.cpp
rtps/common/Time_t.cpp
rtps/common/Token.cpp
Expand Down
148 changes: 148 additions & 0 deletions src/cpp/rtps/common/SerializedPayload.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
// Copyright 2024 Proyectos y Sistemas de Mantenimiento SL (eProsima).
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

/*!
* @file SerializedPayload.cpp
*/

#include <fastdds/rtps/common/SerializedPayload.h>

namespace eprosima {
namespace fastrtps {
namespace rtps {

SerializedPayload_t& SerializedPayload_t::operator = (
SerializedPayload_t&& other) noexcept
{
if (this == &other)
{
return *this;
}

encapsulation = other.encapsulation;
length = other.length;
data = other.data;
max_size = other.max_size;
pos = other.pos;
payload_owner = other.payload_owner;

other.encapsulation = CDR_BE;
other.length = 0;
other.data = nullptr;
other.max_size = 0;
other.pos = 0;
other.payload_owner = nullptr;

return *this;
}

SerializedPayload_t::~SerializedPayload_t()
{
if (payload_owner != nullptr)
{
payload_owner->release_payload(*this);
}
this->empty();
}

bool SerializedPayload_t::operator == (
const SerializedPayload_t& other) const
{
return ((encapsulation == other.encapsulation) &&
(length == other.length) &&
(0 == memcmp(data, other.data, length)));
}

bool SerializedPayload_t::copy(
const SerializedPayload_t* serData,
bool with_limit)
{
length = serData->length;

if (serData->length > max_size)
{
if (with_limit)
{
return false;
}
else
{
this->reserve(serData->length);
}
}
encapsulation = serData->encapsulation;
if (length == 0)
{
return true;
}
memcpy(data, serData->data, length);
return true;
}

bool SerializedPayload_t::reserve_fragmented(
SerializedPayload_t* serData)
{
length = serData->length;
max_size = serData->length;
encapsulation = serData->encapsulation;
data = (octet*)calloc(length, sizeof(octet));
return true;
}

void SerializedPayload_t::empty()
{
assert(payload_owner == nullptr);

length = 0;
encapsulation = CDR_BE;
max_size = 0;
if (data != nullptr)
{
free(data);
}
data = nullptr;
}

void SerializedPayload_t::reserve(
uint32_t new_size)
{
if (new_size <= this->max_size)
{
return;
}
if (data == nullptr)
{
data = (octet*)calloc(new_size, sizeof(octet));
if (!data)
{
throw std::bad_alloc();
}
}
else
{
void* old_data = data;
data = (octet*)realloc(data, new_size);
if (!data)
{
free(old_data);
throw std::bad_alloc();
}
memset(data + max_size, 0, (new_size - max_size) * sizeof(octet));
}
max_size = new_size;
}

} /* namespace rtps */
} /* namespace fastrtps */
} /* namespace eprosima */
1 change: 1 addition & 0 deletions test/unittest/dds/publisher/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ set(DATAWRITERTESTS_SOURCE DataWriterTests.cpp
${PROJECT_SOURCE_DIR}/src/cpp/rtps/builtin/liveliness/WLPListener.cpp
${PROJECT_SOURCE_DIR}/src/cpp/rtps/common/GuidPrefix_t.cpp
${PROJECT_SOURCE_DIR}/src/cpp/rtps/common/LocatorWithMask.cpp
${PROJECT_SOURCE_DIR}/src/cpp/rtps/common/SerializedPayload.cpp
${PROJECT_SOURCE_DIR}/src/cpp/rtps/common/Time_t.cpp
${PROJECT_SOURCE_DIR}/src/cpp/rtps/DataSharing/DataSharingListener.cpp
${PROJECT_SOURCE_DIR}/src/cpp/rtps/DataSharing/DataSharingNotification.cpp
Expand Down
1 change: 1 addition & 0 deletions test/unittest/dds/status/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ set(LISTENERTESTS_SOURCE ListenerTests.cpp
${PROJECT_SOURCE_DIR}/src/cpp/rtps/attributes/PropertyPolicy.cpp
${PROJECT_SOURCE_DIR}/src/cpp/rtps/attributes/ThreadSettings.cpp
${PROJECT_SOURCE_DIR}/src/cpp/rtps/common/LocatorWithMask.cpp
${PROJECT_SOURCE_DIR}/src/cpp/rtps/common/SerializedPayload.cpp
${PROJECT_SOURCE_DIR}/src/cpp/rtps/common/Time_t.cpp
${PROJECT_SOURCE_DIR}/src/cpp/rtps/flowcontrol/FlowControllerConsts.cpp
${PROJECT_SOURCE_DIR}/src/cpp/rtps/history/CacheChangePool.cpp
Expand Down
1 change: 1 addition & 0 deletions test/unittest/dds/subscriber/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ set(DATAREADERHISTORYTESTS_SOURCE DataReaderHistoryTests.cpp
${PROJECT_SOURCE_DIR}/src/cpp/fastdds/subscriber/history/DataReaderHistory.cpp
${PROJECT_SOURCE_DIR}/src/cpp/fastdds/topic/TopicDataType.cpp
${PROJECT_SOURCE_DIR}/src/cpp/rtps/common/LocatorWithMask.cpp
${PROJECT_SOURCE_DIR}/src/cpp/rtps/common/SerializedPayload.cpp
${PROJECT_SOURCE_DIR}/src/cpp/rtps/common/Time_t.cpp
${PROJECT_SOURCE_DIR}/src/cpp/rtps/history/History.cpp
${PROJECT_SOURCE_DIR}/src/cpp/rtps/history/ReaderHistory.cpp
Expand Down
1 change: 1 addition & 0 deletions test/unittest/dds/topic/DDSSQLFilter/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ file(GLOB DDSSQLFILTER_LIB_SOURCES
${PROJECT_SOURCE_DIR}/src/cpp/fastdds/log/*.cpp
${PROJECT_SOURCE_DIR}/src/cpp/fastdds/topic/TopicDataType.cpp
${PROJECT_SOURCE_DIR}/src/cpp/rtps/common/LocatorWithMask.cpp
${PROJECT_SOURCE_DIR}/src/cpp/rtps/common/SerializedPayload.cpp
${PROJECT_SOURCE_DIR}/src/cpp/rtps/common/Time_t.cpp
${PROJECT_SOURCE_DIR}/src/cpp/rtps/network/utils/netmask_filter.cpp
${PROJECT_SOURCE_DIR}/src/cpp/rtps/network/utils/network.cpp
Expand Down
1 change: 1 addition & 0 deletions test/unittest/rtps/builtin/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ set(BUILTIN_DATA_SERIALIZATION_TESTS_SOURCE BuiltinDataSerializationTests.cpp
${PROJECT_SOURCE_DIR}/src/cpp/rtps/builtin/data/WriterProxyData.cpp
${PROJECT_SOURCE_DIR}/src/cpp/rtps/common/GuidPrefix_t.cpp
${PROJECT_SOURCE_DIR}/src/cpp/rtps/common/LocatorWithMask.cpp
${PROJECT_SOURCE_DIR}/src/cpp/rtps/common/SerializedPayload.cpp
${PROJECT_SOURCE_DIR}/src/cpp/rtps/common/Time_t.cpp
${PROJECT_SOURCE_DIR}/src/cpp/rtps/flowcontrol/FlowControllerConsts.cpp
${PROJECT_SOURCE_DIR}/src/cpp/rtps/messages/CDRMessage.cpp
Expand Down
1 change: 1 addition & 0 deletions test/unittest/rtps/common/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# limitations under the License.

set(CACHECHANGETESTS_SOURCE CacheChangeTests.cpp
${PROJECT_SOURCE_DIR}/src/cpp/rtps/common/SerializedPayload.cpp
${PROJECT_SOURCE_DIR}/src/cpp/rtps/common/Time_t.cpp)
set(GUID_UTILS_TESTS_SOURCE GuidUtilsTests.cpp
${PROJECT_SOURCE_DIR}/src/cpp/rtps/common/GuidPrefix_t.cpp
Expand Down
Loading

0 comments on commit c7c210b

Please sign in to comment.