Skip to content

Commit

Permalink
Added MessageBuilder methods to serialize to an existing buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
mitchellgoffpc committed Aug 3, 2023
1 parent a5378db commit 7e84b8a
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions messaging/messaging.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<capnp::byte>(buffer, buffer_size));
capnp::writeMessage(out, *this);
return serialized_size;
}

private:
kj::Array<capnp::word> heapArray_;
};
Expand Down

0 comments on commit 7e84b8a

Please sign in to comment.