From 7e84b8a2e2c2322dd429813e61d465babb386584 Mon Sep 17 00:00:00 2001 From: mitchellgoffpc Date: Thu, 3 Aug 2023 13:37:16 -0700 Subject: [PATCH] Added MessageBuilder methods to serialize to an existing buffer --- messaging/messaging.h | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/messaging/messaging.h b/messaging/messaging.h index 483b2465c..d768ccb9b 100644 --- a/messaging/messaging.h +++ b/messaging/messaging.h @@ -116,6 +116,18 @@ class MessageBuilder : public capnp::MallocMessageBuilder { return heapArray_.asBytes(); } + size_t getSerializedSize() { + return capnp::computeSerializedSizeInWords(*this) * sizeof(capnp::word); + } + + int serializeToBuffer(unsigned char *buffer, size_t buffer_size) { + size_t serialized_size = getSerializedSize(); + if (serialized_size > buffer_size) { return -1; } + kj::ArrayOutputStream out(kj::ArrayPtr(buffer, buffer_size)); + capnp::writeMessage(out, *this); + return serialized_size; + } + private: kj::Array heapArray_; };