Skip to content

Commit

Permalink
Expose payload pool in DDS layer example (#3598)
Browse files Browse the repository at this point in the history
* Refs #18913: Custom Payload pool PoC

Signed-off-by: JesusPoderoso <[email protected]>

* Refs #19491: Apply rev suggestions

Signed-off-by: JesusPoderoso <[email protected]>

* Refs #19491: Apply latest rev suggestions

Signed-off-by: JesusPoderoso <[email protected]>

* Refs #19491: Please linters

Signed-off-by: JesusPoderoso <[email protected]>

* Refs #19491: Fix public to private heritance

* Set payload owner to be allocating PayloadPool

Signed-off-by: Juan Lopez Fernandez <[email protected]>

* Refs #19491: Revert partial change introduced in previous commit b59b76d to force payload owner to 'this' always

Signed-off-by: JesusPoderoso <[email protected]>

* Refs #19491: Fix Windows installer build

Signed-off-by: JesusPoderoso <[email protected]>

---------

Signed-off-by: JesusPoderoso <[email protected]>
Signed-off-by: Juan Lopez Fernandez <[email protected]>
Co-authored-by: Juan Lopez Fernandez <[email protected]>
  • Loading branch information
JesusPoderoso and juanlofer-eprosima committed Sep 27, 2023
1 parent 9ee22d9 commit ab56fa8
Show file tree
Hide file tree
Showing 16 changed files with 1,948 additions and 0 deletions.
1 change: 1 addition & 0 deletions examples/cpp/dds/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ add_subdirectory(BasicConfigurationExample)
add_subdirectory(Configurability)
add_subdirectory(ContentFilteredTopicExample)
add_subdirectory(CustomListenerExample)
add_subdirectory(CustomPayloadPoolExample)
add_subdirectory(DeadlineQoSExample)
add_subdirectory(DisablePositiveACKs)
add_subdirectory(DiscoveryServerExample)
Expand Down
48 changes: 48 additions & 0 deletions examples/cpp/dds/CustomPayloadPoolExample/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# Copyright 2023 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.

cmake_minimum_required(VERSION 3.16.3)

project(CustomPayloadPoolExample VERSION 1 LANGUAGES CXX)

# Find requirements
if(NOT fastcdr_FOUND)
find_package(fastcdr REQUIRED)
endif()

if(NOT fastrtps_FOUND)
find_package(fastrtps REQUIRED)
endif()

#Check C++11
include(CheckCXXCompilerFlag)
if(CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang")
check_cxx_compiler_flag(-std=c++11 SUPPORTS_CXX11)
if(NOT SUPPORTS_CXX11)
message(FATAL_ERROR "Compiler doesn't support C++11")
endif()
endif()

message(STATUS "Configuring custom payload pool example...")
file(GLOB CUSTOM_PAYLOAD_POOL_DATA_EXAMPLE_SOURCES_CXX "*.cxx")
file(GLOB CUSTOM_PAYLOAD_POOL_DATA_EXAMPLE_SOURCES_CPP "*.cpp")

add_executable(CustomPayloadPoolExample ${CUSTOM_PAYLOAD_POOL_DATA_EXAMPLE_SOURCES_CXX} ${CUSTOM_PAYLOAD_POOL_DATA_EXAMPLE_SOURCES_CPP})
target_compile_definitions(CustomPayloadPoolExample PRIVATE
$<$<AND:$<NOT:$<BOOL:${WIN32}>>,$<STREQUAL:"${CMAKE_BUILD_TYPE}","Debug">>:__DEBUG>
$<$<BOOL:${INTERNAL_DEBUG}>:__INTERNALDEBUG> # Internal debug activated.
)
target_link_libraries(CustomPayloadPoolExample fastrtps fastcdr fastdds::optionparser)
install(TARGETS CustomPayloadPoolExample
RUNTIME DESTINATION examples/cpp/dds/CustomPayloadPoolExample/${BIN_INSTALL_DIR})
101 changes: 101 additions & 0 deletions examples/cpp/dds/CustomPayloadPoolExample/CustomPayloadPool.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
// Copyright 2023 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 CustomPayloadPool.hpp
*/

#ifndef DDS_CUSTOM_PAYLOAD_POOL_DATA_HPP
#define DDS_CUSTOM_PAYLOAD_POOL_DATA_HPP

#include <assert.h>
#include <stdio.h>
#include <string.h>

#include <fastdds/rtps/history/IPayloadPool.h>
#include <fastdds/rtps/common/CacheChange.h>

class CustomPayloadPool : public eprosima::fastrtps::rtps::IPayloadPool
{
public:

~CustomPayloadPool() = default;

bool get_payload(
unsigned int size,
eprosima::fastrtps::rtps::CacheChange_t& cache_change)
{
// Reserve new memory for the payload buffer
unsigned char* payload = new unsigned char[size];

// Assign the payload buffer to the CacheChange and update sizes
cache_change.serializedPayload.data = payload;
cache_change.serializedPayload.length = size;
cache_change.serializedPayload.max_size = size;

// Tell the CacheChange who needs to release its payload
cache_change.payload_owner(this);

return true;
}

bool get_payload(
eprosima::fastrtps::rtps::SerializedPayload_t& data,
eprosima::fastrtps::rtps::IPayloadPool*& /*data_owner*/,
eprosima::fastrtps::rtps::CacheChange_t& cache_change)
{
// Reserve new memory for the payload buffer
unsigned char* payload = new unsigned char[data.length];

// Copy the data
memcpy(payload, data.data, data.length);

// Tell the CacheChange who needs to release its payload
cache_change.payload_owner(this);

// Assign the payload buffer to the CacheChange and update sizes
cache_change.serializedPayload.data = payload;
cache_change.serializedPayload.length = data.length;
cache_change.serializedPayload.max_size = data.length;

return true;
}

bool release_payload(
eprosima::fastrtps::rtps::CacheChange_t& cache_change)
{
// Ensure precondition
if (this != cache_change.payload_owner())
{
std::cerr << "Trying to release a payload buffer allocated by a different PayloadPool." << std::endl;
return false;
}

// Dealloc the buffer of the payload
delete[] cache_change.serializedPayload.data;

// Reset sizes and pointers
cache_change.serializedPayload.data = nullptr;
cache_change.serializedPayload.length = 0;
cache_change.serializedPayload.max_size = 0;

// Reset the owner of the payload
cache_change.payload_owner(nullptr);

return true;
}

};

#endif // DDS_CUSTOM_PAYLOAD_POOL_DATA_HPP
164 changes: 164 additions & 0 deletions examples/cpp/dds/CustomPayloadPoolExample/CustomPayloadPoolData.cxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
// Copyright 2016 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 CustomPayloadPoolData.cpp
* This source file contains the implementation of the described types in the IDL file.
*
* This file was generated by the tool fastddsgen.
*/

#ifdef _WIN32
// Remove linker warning LNK4221 on Visual Studio
namespace {
char dummy;
} // namespace
#endif // _WIN32

#include "CustomPayloadPoolData.h"
#include <fastcdr/Cdr.h>


#include <fastcdr/exceptions/BadParamException.h>
using namespace eprosima::fastcdr::exception;

#include <utility>


CustomPayloadPoolData::CustomPayloadPoolData()
{

}

CustomPayloadPoolData::~CustomPayloadPoolData()
{
}

CustomPayloadPoolData::CustomPayloadPoolData(
const CustomPayloadPoolData& x)
{
m_index = x.m_index;
m_message = x.m_message;
}

CustomPayloadPoolData::CustomPayloadPoolData(
CustomPayloadPoolData&& x) noexcept
{
m_index = x.m_index;
m_message = std::move(x.m_message);
}

CustomPayloadPoolData& CustomPayloadPoolData::operator =(
const CustomPayloadPoolData& x)
{

m_index = x.m_index;
m_message = x.m_message;

return *this;
}

CustomPayloadPoolData& CustomPayloadPoolData::operator =(
CustomPayloadPoolData&& x) noexcept
{

m_index = x.m_index;
m_message = std::move(x.m_message);

return *this;
}

bool CustomPayloadPoolData::operator ==(
const CustomPayloadPoolData& x) const
{
return (m_index == x.m_index &&
m_message == x.m_message);
}

bool CustomPayloadPoolData::operator !=(
const CustomPayloadPoolData& x) const
{
return !(*this == x);
}

/*!
* @brief This function sets a value in member index
* @param _index New value for member index
*/
void CustomPayloadPoolData::index(
uint32_t _index)
{
m_index = _index;
}

/*!
* @brief This function returns the value of member index
* @return Value of member index
*/
uint32_t CustomPayloadPoolData::index() const
{
return m_index;
}

/*!
* @brief This function returns a reference to member index
* @return Reference to member index
*/
uint32_t& CustomPayloadPoolData::index()
{
return m_index;
}


/*!
* @brief This function copies the value in member message
* @param _message New value to be copied in member message
*/
void CustomPayloadPoolData::message(
const std::string& _message)
{
m_message = _message;
}

/*!
* @brief This function moves the value in member message
* @param _message New value to be moved in member message
*/
void CustomPayloadPoolData::message(
std::string&& _message)
{
m_message = std::move(_message);
}

/*!
* @brief This function returns a constant reference to member message
* @return Constant reference to member message
*/
const std::string& CustomPayloadPoolData::message() const
{
return m_message;
}

/*!
* @brief This function returns a reference to member message
* @return Reference to member message
*/
std::string& CustomPayloadPoolData::message()
{
return m_message;
}


// Include auxiliary functions like for serializing/deserializing.
#include "CustomPayloadPoolDataCdrAux.ipp"
Loading

0 comments on commit ab56fa8

Please sign in to comment.