Skip to content

Commit

Permalink
Return copy of protobuf prototype.
Browse files Browse the repository at this point in the history
  • Loading branch information
KerstinKeller committed May 14, 2024
1 parent c050e9e commit 0b411e5
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,7 @@ void PluginWidget::onProtoMessageCallback(const std::shared_ptr<google::protobuf
std::lock_guard<std::mutex> lock(proto_message_mutex_);

// Create a copy of the new message as member variable. We cannot use a reference here, as this may cause a deadlock with the GUI thread
last_proto_message_.reset(message->New());
last_proto_message_->CopyFrom(*message);
last_proto_message_ = message;

last_message_publish_timestamp_ = eCAL::Time::ecal_clock::time_point(std::chrono::duration_cast<eCAL::Time::ecal_clock::duration>(std::chrono::microseconds(send_time_usecs)));

Expand Down
2 changes: 1 addition & 1 deletion ecal/core/include/ecal/msg/dynamic.h
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ namespace eCAL
{
return(m_deserializer.Deserialize(rec_buf.c_str(), rec_buf.size(), m_datatype_info_received.value()));
}
catch (const DynamicReflectionException& e)
catch (const DynamicReflectionException& /*e*/)
{
return std::nullopt;
}
Expand Down
26 changes: 11 additions & 15 deletions ecal/core/include/ecal/msg/protobuf/dynamic_subscriber.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,15 @@ namespace eCAL
public:
std::shared_ptr<google::protobuf::Message> Deserialize(const void* buffer_, size_t size_, const SDataTypeInformation& datatype_info_)
{
auto message_prototype = GetMessagePointer(datatype_info_);
// for some reason cannot use std::make_shared, however should be ok in this context.
std::shared_ptr<google::protobuf::Message> message_with_content(message_prototype->New());
message_with_content->CopyFrom(*message_prototype);

try
{
auto msg_ = GetMessagePointer(datatype_info_);
msg_->ParseFromArray(buffer_, (int)size_);
return msg_;
message_with_content->ParseFromArray(buffer_, (int)size_);
return message_with_content;
}
catch (...)
{
Expand All @@ -71,33 +75,25 @@ namespace eCAL
auto schema = m_message_map.find(datatype_info_);
if (schema == m_message_map.end())
{
try
{
m_message_map[datatype_info_] = CreateMessagePointer(datatype_info_);
}
catch (DynamicReflectionException e)
{
m_message_map[datatype_info_] = nullptr;
}
m_message_map[datatype_info_] = CreateMessagePointer(datatype_info_);
}
return m_message_map[datatype_info_];
}


std::shared_ptr<google::protobuf::Message> CreateMessagePointer(const SDataTypeInformation& topic_info_)
{
// get topic type
std::string topic_type{ topic_info_.name };
topic_type = topic_type.substr(topic_type.find_last_of('.') + 1, topic_type.size());
if (StrEmptyOrNull(topic_type))
{
throw DynamicReflectionException("CDynamicSubscriber: Could not get type");
throw DynamicReflectionException("ProtobufDynamicDeserializer: Could not get type");
}

std::string topic_desc = topic_info_.descriptor;
if (StrEmptyOrNull(topic_desc))
{
throw DynamicReflectionException("CDynamicSubscriber: Could not get description for type" + std::string(topic_type));
throw DynamicReflectionException("ProtobufDynamicDeserializer: Could not get description for type" + std::string(topic_type));
}

google::protobuf::FileDescriptorSet proto_desc;
Expand All @@ -107,7 +103,7 @@ namespace eCAL
if (proto_msg_ptr == nullptr)
{
std::stringstream s;
s << "CDynamicSubscriber: Message of type " + std::string(topic_type) << " could not be decoded" << std::endl;
s << "ProtobufDynamicDeserializer: Message of type " + std::string(topic_type) << " could not be decoded" << std::endl;
s << error_s;
throw DynamicReflectionException(s.str());
}
Expand Down

0 comments on commit 0b411e5

Please sign in to comment.