Skip to content

Commit

Permalink
fixed issue #397, in case of empty protobuf messages (defaults only) …
Browse files Browse the repository at this point in the history
…transport will be forced with a minimal message of 1 byte (#398)

please cherry pick to at least 5.9.1
  • Loading branch information
rex-schilasky authored and FlorianReimold committed Sep 15, 2021
1 parent 9e4b34d commit 1f501a9
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
17 changes: 15 additions & 2 deletions ecal/core/include/ecal/msg/protobuf/publisher.h
Expand Up @@ -136,11 +136,24 @@ namespace eCAL
**/
size_t GetSize(const T& msg_) const
{
size_t size(0);
#if GOOGLE_PROTOBUF_VERSION >= 3001000
return((size_t)msg_.ByteSizeLong());
size = (size_t)msg_.ByteSizeLong();
#else
return((size_t)msg_.ByteSize());
size = (size_t)msg_.ByteSize();
#endif
// In case we have an empty protocol buffer message object
// (containing default values only) the serialized size
// will be zero and the message will not be transported by
// any layer.
// We increase message size to 1 byte to force transport.
// protobuf::CSubscriber::Deserialize will check this and
// return an empty message object as expected.
if (size == 0)
{
size = 1;
}
return(size);
}

/**
Expand Down
21 changes: 20 additions & 1 deletion ecal/core/include/ecal/msg/protobuf/subscriber.h
Expand Up @@ -139,7 +139,26 @@ namespace eCAL
**/
bool Deserialize(T& msg_, const void* buffer_, size_t size_) const
{
return(msg_.ParseFromArray(buffer_, static_cast<int>(size_)));
// we try to parse the message from the received buffer
if (msg_.ParseFromArray(buffer_, static_cast<int>(size_)))
{
return(true);
}
else
{
// If deserialization failed we check for message size.
// Empty messages will set to a minimum size of 1 byte
// by the protobuf::CPublisher::GetSize function to force
// the transport through all layers.
// In this case we clear the message object and
// return success.
if (size_ == 1)
{
msg_.Clear();
return(true);
}
}
return(false);
}

};
Expand Down

0 comments on commit 1f501a9

Please sign in to comment.