Initial message_bridge client and server
These will forward data, and track what made it across and what didn't
when configured correctly. This should be off if nothing is requested
to be logged remotely.
It implements ttl, reconnects, and has a basic smoke test.
We still need to handle forwarding data for logging.
Change-Id: I7daebe8cef54029a5733b7f81ee6b68367c80d82
diff --git a/aos/flatbuffers.h b/aos/flatbuffers.h
index 4e4520c..4388687 100644
--- a/aos/flatbuffers.h
+++ b/aos/flatbuffers.h
@@ -5,6 +5,7 @@
#include <string_view>
#include "flatbuffers/flatbuffers.h"
+#include "glog/logging.h"
namespace aos {
@@ -31,15 +32,19 @@
// This class is a fixed memory allocator which holds the data for a flatbuffer
// in an array.
-template <size_t S>
class FixedAllocator : public FixedAllocatorBase {
public:
+ FixedAllocator(size_t size) : buffer_(size, 0) {}
+
uint8_t *data() override { return &buffer_[0]; }
const uint8_t *data() const override { return &buffer_[0]; }
size_t size() const override { return buffer_.size(); }
+ // Releases the data in the buffer.
+ std::vector<uint8_t> release() { return std::move(buffer_); }
+
private:
- std::array<uint8_t, S> buffer_;
+ std::vector<uint8_t> buffer_;
};
// This class adapts a preallocated memory region to an Allocator.
@@ -82,46 +87,6 @@
virtual size_t size() const = 0;
};
-// Array backed flatbuffer.
-template <typename T>
-class FlatbufferArray : public Flatbuffer<T> {
- public:
- // Builds a Flatbuffer by copying the data from the other flatbuffer.
- FlatbufferArray(const Flatbuffer<T> &other) {
- CHECK_LE(other.size(), data_.size());
-
- memcpy(data_.data(), other.data(), other.size());
- size_ = other.size();
- }
-
- // Coppies the data from the other flatbuffer.
- FlatbufferArray &operator=(const Flatbuffer<T> &other) {
- CHECK_LE(other.size(), data_.size());
-
- memcpy(data_.data(), other.data(), other.size());
- size_ = other.size();
- return *this;
- }
-
- virtual ~FlatbufferArray() override {}
-
- // Creates a builder wrapping the underlying data.
- flatbuffers::FlatBufferBuilder FlatBufferBuilder() {
- data_.deallocate(data_.data(), data_.size());
- flatbuffers::FlatBufferBuilder fbb(data_.size(), &data_);
- fbb.ForceDefaults(1);
- return fbb;
- }
-
- const uint8_t *data() const override { return data_.data(); }
- uint8_t *data() override { return data_.data(); }
- size_t size() const override { return size_; }
-
- private:
- FixedAllocator<8 * 1024> data_;
- size_t size_ = data_.size();
-};
-
// String backed flatbuffer.
template <typename T>
class FlatbufferString : public Flatbuffer<T> {
@@ -228,6 +193,48 @@
flatbuffers::DetachedBuffer buffer_;
};
+// This object associates the message type with the memory storing the
+// flatbuffer. This only stores root tables.
+//
+// From a usage point of view, pointers to the data are very different than
+// pointers to the tables.
+template <typename T>
+class SizePrefixedFlatbufferDetachedBuffer final : public Flatbuffer<T> {
+ public:
+ // Builds a Flatbuffer by taking ownership of the buffer.
+ SizePrefixedFlatbufferDetachedBuffer(flatbuffers::DetachedBuffer &&buffer)
+ : buffer_(::std::move(buffer)) {
+ CHECK_GE(buffer_.size(), sizeof(flatbuffers::uoffset_t));
+ }
+
+ // Builds a flatbuffer by taking ownership of the buffer from the other
+ // flatbuffer.
+ SizePrefixedFlatbufferDetachedBuffer(
+ SizePrefixedFlatbufferDetachedBuffer &&fb)
+ : buffer_(::std::move(fb.buffer_)) {}
+ SizePrefixedFlatbufferDetachedBuffer &operator=(
+ SizePrefixedFlatbufferDetachedBuffer &&fb) {
+ ::std::swap(buffer_, fb.buffer_);
+ return *this;
+ }
+
+ virtual ~SizePrefixedFlatbufferDetachedBuffer() override {}
+
+ // Returns references to the buffer, and the data.
+ const flatbuffers::DetachedBuffer &buffer() const { return buffer_; }
+ const uint8_t *data() const override {
+ return buffer_.data() + sizeof(flatbuffers::uoffset_t);
+ }
+ uint8_t *data() override {
+ return buffer_.data() + sizeof(flatbuffers::uoffset_t);
+ }
+ size_t size() const override {
+ return buffer_.size() - sizeof(flatbuffers::uoffset_t);
+ }
+
+ private:
+ flatbuffers::DetachedBuffer buffer_;
+};
// TODO(austin): Need a way to get our hands on the max size. Can start with
// "large" for now.