Skip to content

Commit

Permalink
Add in a class to paper over fastcdr v1 to v2 differences.
Browse files Browse the repository at this point in the history
Signed-off-by: Chris Lalancette <[email protected]>
  • Loading branch information
clalancette committed Apr 4, 2024
1 parent ac85307 commit 8dee384
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 41 deletions.
1 change: 1 addition & 0 deletions rmw_zenoh_cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ find_package(zenohc REQUIRED)

add_library(rmw_zenoh_cpp SHARED
src/detail/attachment_helpers.cpp
src/detail/cdr.cpp
src/detail/event.cpp
src/detail/identifier.cpp
src/detail/graph_cache.cpp
Expand Down
42 changes: 42 additions & 0 deletions rmw_zenoh_cpp/src/detail/cdr.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright 2024 Open Source Robotics Foundation, Inc.
//
// 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.

#include "fastcdr/Cdr.h"
#include "fastcdr/FastBuffer.h"
#include "fastcdr/config.h"

#include "cdr.hpp"

rmw_zenoh_cpp::Cdr::Cdr(eprosima::fastcdr::FastBuffer & fastbuffer)
#if FASTCDR_VERSION_MAJOR == 1
: cdr_(fastbuffer, eprosima::fastcdr::Cdr::DEFAULT_ENDIAN, eprosima::fastcdr::Cdr::DDS_CDR)
#else
: cdr_(fastbuffer, eprosima::fastcdr::Cdr::DEFAULT_ENDIAN, eprosima::fastcdr::CdrVersion::DDS_CDR)
#endif
{
}

size_t rmw_zenoh_cpp::Cdr::get_serialized_data_length() const
{
#if FASTCDR_VERSION_MAJOR == 1
return cdr_.getSerializedDataLength();
#else
return cdr_.get_serialized_data_length();
#endif
}

eprosima::fastcdr::Cdr & rmw_zenoh_cpp::Cdr::get_cdr()
{
return cdr_;
}
39 changes: 39 additions & 0 deletions rmw_zenoh_cpp/src/detail/cdr.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright 2024 Open Source Robotics Foundation, Inc.
//
// 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.

#ifndef DETAIL__CDR_HPP_
#define DETAIL__CDR_HPP_

#include "fastcdr/Cdr.h"
#include "fastcdr/FastBuffer.h"

// A wrapper class to paper over the differences between Fast-CDR v1 and Fast-CDR v2
namespace rmw_zenoh_cpp
{
class Cdr final
{
public:
explicit Cdr(eprosima::fastcdr::FastBuffer & fastbuffer);

eprosima::fastcdr::Cdr & get_cdr();

size_t get_serialized_data_length() const;

private:
eprosima::fastcdr::Cdr cdr_;
};

} // namespace rmw_zenoh_cpp

#endif // DETAIL__CDR_HPP_
60 changes: 19 additions & 41 deletions rmw_zenoh_cpp/src/rmw_zenoh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@
// limitations under the License.

#include <fastcdr/FastBuffer.h>
#include <fastcdr/CdrEncoding.hpp>
#include <fastcdr/Cdr.h>

#include <zenoh.h>

Expand All @@ -30,6 +28,7 @@
#include <utility>

#include "detail/attachment_helpers.hpp"
#include "detail/cdr.hpp"
#include "detail/guard_condition.hpp"
#include "detail/graph_cache.hpp"
#include "detail/identifier.hpp"
Expand Down Expand Up @@ -913,13 +912,10 @@ rmw_publish(
eprosima::fastcdr::FastBuffer fastbuffer(msg_bytes, max_data_length);

// Object that serializes the data
eprosima::fastcdr::Cdr ser(
fastbuffer,
eprosima::fastcdr::Cdr::DEFAULT_ENDIAN,
eprosima::fastcdr::CdrVersion::DDS_CDR);
rmw_zenoh_cpp::Cdr ser(fastbuffer);
if (!publisher_data->type_support->serialize_ros_message(
ros_message,
ser,
ser.get_cdr(),
publisher_data->type_support_impl))
{
RMW_SET_ERROR_MSG("could not serialize ROS message");
Expand Down Expand Up @@ -1057,9 +1053,8 @@ rmw_publish_serialized_message(

eprosima::fastcdr::FastBuffer buffer(
reinterpret_cast<char *>(serialized_message->buffer), serialized_message->buffer_length);
eprosima::fastcdr::Cdr ser(
buffer, eprosima::fastcdr::Cdr::DEFAULT_ENDIAN, eprosima::fastcdr::CdrVersion::DDS_CDR);
if (!ser.jump(serialized_message->buffer_length)) {
rmw_zenoh_cpp::Cdr ser(buffer);
if (!ser.get_cdr().jump(serialized_message->buffer_length)) {
RMW_SET_ERROR_MSG("cannot correctly set serialized buffer");
return RMW_RET_ERROR;
}
Expand Down Expand Up @@ -1162,10 +1157,9 @@ rmw_serialize(

eprosima::fastcdr::FastBuffer buffer(
reinterpret_cast<char *>(serialized_message->buffer), data_length);
eprosima::fastcdr::Cdr ser(
buffer, eprosima::fastcdr::Cdr::DEFAULT_ENDIAN, eprosima::fastcdr::CdrVersion::DDS_CDR);
rmw_zenoh_cpp::Cdr ser(buffer);

auto ret = tss.serialize_ros_message(ros_message, ser, callbacks);
auto ret = tss.serialize_ros_message(ros_message, ser.get_cdr(), callbacks);
serialized_message->buffer_length = data_length;
serialized_message->buffer_capacity = data_length;
return ret == true ? RMW_RET_OK : RMW_RET_ERROR;
Expand All @@ -1189,10 +1183,9 @@ rmw_deserialize(
auto tss = MessageTypeSupport(callbacks);
eprosima::fastcdr::FastBuffer buffer(
reinterpret_cast<char *>(serialized_message->buffer), serialized_message->buffer_length);
eprosima::fastcdr::Cdr deser(buffer, eprosima::fastcdr::Cdr::DEFAULT_ENDIAN,
eprosima::fastcdr::CdrVersion::DDS_CDR);
rmw_zenoh_cpp::Cdr deser(buffer);

auto ret = tss.deserialize_ros_message(deser, ros_message, callbacks);
auto ret = tss.deserialize_ros_message(deser.get_cdr(), ros_message, callbacks);
return ret == true ? RMW_RET_OK : RMW_RET_ERROR;
}

Expand Down Expand Up @@ -1663,12 +1656,9 @@ static rmw_ret_t __rmw_take(
msg_data->payload.payload.len);

// Object that serializes the data
eprosima::fastcdr::Cdr deser(
fastbuffer,
eprosima::fastcdr::Cdr::DEFAULT_ENDIAN,
eprosima::fastcdr::CdrVersion::DDS_CDR);
rmw_zenoh_cpp::Cdr deser(fastbuffer);
if (!sub_data->type_support->deserialize_ros_message(
deser,
deser.get_cdr(),
ros_message,
sub_data->type_support_impl))
{
Expand Down Expand Up @@ -2244,13 +2234,10 @@ rmw_send_request(
eprosima::fastcdr::FastBuffer fastbuffer(request_bytes, max_data_length);

// Object that serializes the data
eprosima::fastcdr::Cdr ser(
fastbuffer,
eprosima::fastcdr::Cdr::DEFAULT_ENDIAN,
eprosima::fastcdr::CdrVersion::DDS_CDR);
rmw_zenoh_cpp::Cdr ser(fastbuffer);
if (!client_data->request_type_support->serialize_ros_message(
ros_request,
ser,
ser.get_cdr(),
client_data->request_type_support_impl))
{
return RMW_RET_ERROR;
Expand Down Expand Up @@ -2340,12 +2327,9 @@ rmw_take_response(
sample->payload.len);

// Object that serializes the data
eprosima::fastcdr::Cdr deser(
fastbuffer,
eprosima::fastcdr::Cdr::DEFAULT_ENDIAN,
eprosima::fastcdr::CdrVersion::DDS_CDR);
rmw_zenoh_cpp::Cdr deser(fastbuffer);
if (!client_data->response_type_support->deserialize_ros_message(
deser,
deser.get_cdr(),
ros_response,
client_data->response_type_support_impl))
{
Expand Down Expand Up @@ -2789,12 +2773,9 @@ rmw_take_request(
payload_value.payload.len);

// Object that serializes the data
eprosima::fastcdr::Cdr deser(
fastbuffer,
eprosima::fastcdr::Cdr::DEFAULT_ENDIAN,
eprosima::fastcdr::CdrVersion::DDS_CDR);
rmw_zenoh_cpp::Cdr deser(fastbuffer);
if (!service_data->request_type_support->deserialize_ros_message(
deser,
deser.get_cdr(),
ros_request,
service_data->request_type_support_impl))
{
Expand Down Expand Up @@ -2891,13 +2872,10 @@ rmw_send_response(
eprosima::fastcdr::FastBuffer fastbuffer(response_bytes, max_data_length);

// Object that serializes the data
eprosima::fastcdr::Cdr ser(
fastbuffer,
eprosima::fastcdr::Cdr::DEFAULT_ENDIAN,
eprosima::fastcdr::CdrVersion::DDS_CDR);
rmw_zenoh_cpp::Cdr ser(fastbuffer);
if (!service_data->response_type_support->serialize_ros_message(
ros_response,
ser,
ser.get_cdr(),
service_data->response_type_support_impl))
{
return RMW_RET_ERROR;
Expand Down

0 comments on commit 8dee384

Please sign in to comment.