nng_proto: add comments. fix return statement in serialize_proto_to_nng()

This commit is contained in:
Florian Lüke 2024-12-25 18:29:22 +01:00
parent ba54b5ff19
commit d5f9b87896
2 changed files with 10 additions and 1 deletions

View file

@ -7,8 +7,17 @@
namespace mesytec::mnode::nng namespace mesytec::mnode::nng
{ {
// Format is: [u32 size][protobuf message]
// Appends size prefix and serialied message contents to the nng msg.
// Returns the number of bytes written.
size_t serialize_proto_to_nng(const google::protobuf::MessageLite &message, nng_msg *msg); size_t serialize_proto_to_nng(const google::protobuf::MessageLite &message, nng_msg *msg);
// Deserializes a protobuf message from an nng msg.
// Returns the number of bytes used (size prefix + message).
size_t deserialize_proto_from_nng(google::protobuf::MessageLite &message, nng_msg *msg); size_t deserialize_proto_from_nng(google::protobuf::MessageLite &message, nng_msg *msg);
// Same as above but also trims the message.
size_t deserialize_proto_from_nng_trim(google::protobuf::MessageLite &message, nng_msg *msg); size_t deserialize_proto_from_nng_trim(google::protobuf::MessageLite &message, nng_msg *msg);
} // namespace mesytec::mnode::nng } // namespace mesytec::mnode::nng

View file

@ -10,7 +10,7 @@ size_t serialize_proto_to_nng(const google::protobuf::MessageLite &message, nng_
auto messageSize = message.ByteSizeLong(); auto messageSize = message.ByteSizeLong();
if (auto res = nng_msg_realloc(msg, nng_msg_len(msg) + sizeof(u32) + messageSize)) if (auto res = nng_msg_realloc(msg, nng_msg_len(msg) + sizeof(u32) + messageSize))
return false; return 0;
*reinterpret_cast<u32 *>(nng_msg_body(msg)) = messageSize; *reinterpret_cast<u32 *>(nng_msg_body(msg)) = messageSize;
if (!message.SerializeToArray(reinterpret_cast<u8 *>(nng_msg_body(msg)) + sizeof(u32), if (!message.SerializeToArray(reinterpret_cast<u8 *>(nng_msg_body(msg)) + sizeof(u32),