[Yunq] Move MessageView higher in call stack.
This commit is contained in:
parent
30b220b2fb
commit
0e6aa532a1
14 changed files with 181 additions and 160 deletions
|
|
@ -2,14 +2,32 @@
|
|||
|
||||
namespace yunq {
|
||||
|
||||
namespace {
|
||||
|
||||
const uint64_t kIdentByte = 0x33441122;
|
||||
|
||||
} // namespace
|
||||
|
||||
glcr::Status MessageView::CheckHeader() const {
|
||||
if (buffer_.At<uint32_t>(offset_ + 0) != kIdentByte) {
|
||||
return glcr::InvalidArgument("Trying to parse an invalid yunq message.");
|
||||
}
|
||||
// TODO: Parse core size.
|
||||
// TODO: Parse extension size.
|
||||
// TODO: Check CRC32
|
||||
// TODO: Parse options.
|
||||
return glcr::Status::Ok();
|
||||
}
|
||||
|
||||
template <>
|
||||
glcr::ErrorOr<uint64_t> MessageView::ReadField<uint64_t>(uint64_t field_index) {
|
||||
glcr::ErrorOr<uint64_t> MessageView::ReadField<uint64_t>(
|
||||
uint64_t field_index) const {
|
||||
return buffer_.At<uint64_t>(field_offset(field_index));
|
||||
}
|
||||
|
||||
template <>
|
||||
glcr::ErrorOr<glcr::String> MessageView::ReadField<glcr::String>(
|
||||
uint64_t field_index) {
|
||||
uint64_t field_index) const {
|
||||
ExtensionPointer ptr =
|
||||
buffer_.At<ExtensionPointer>(field_offset(field_index));
|
||||
|
||||
|
|
@ -18,7 +36,7 @@ glcr::ErrorOr<glcr::String> MessageView::ReadField<glcr::String>(
|
|||
|
||||
template <>
|
||||
glcr::ErrorOr<glcr::Vector<uint64_t>> MessageView::ReadRepeated<uint64_t>(
|
||||
uint64_t field_index) {
|
||||
uint64_t field_index) const {
|
||||
ExtensionPointer pointer =
|
||||
buffer_.At<ExtensionPointer>(field_offset(field_index));
|
||||
|
||||
|
|
|
|||
|
|
@ -3,11 +3,20 @@
|
|||
#include <glacier/buffer/byte_buffer.h>
|
||||
#include <glacier/container/vector.h>
|
||||
#include <glacier/status/error_or.h>
|
||||
#include <glacier/status/status.h>
|
||||
|
||||
namespace yunq {
|
||||
|
||||
const uint64_t kHeaderSize = 24; // 4x uint32, 1x uint64
|
||||
|
||||
struct MessageHeader {
|
||||
uint32_t ident;
|
||||
uint32_t core_length;
|
||||
uint32_t length;
|
||||
uint32_t crc32;
|
||||
uint64_t options;
|
||||
} __attribute__((packed));
|
||||
|
||||
struct ExtensionPointer {
|
||||
uint32_t offset;
|
||||
uint32_t length;
|
||||
|
|
@ -18,31 +27,34 @@ class MessageView {
|
|||
MessageView(const glcr::ByteBuffer& buffer, uint64_t offset)
|
||||
: buffer_(buffer), offset_(offset) {}
|
||||
|
||||
[[nodiscard]] glcr::Status CheckHeader() const;
|
||||
|
||||
// TODO: Implement glcr::StatusOr
|
||||
template <typename T>
|
||||
glcr::ErrorOr<T> ReadField(uint64_t field_index);
|
||||
glcr::ErrorOr<T> ReadField(uint64_t field_index) const;
|
||||
|
||||
template <typename T>
|
||||
glcr::ErrorOr<glcr::Vector<T>> ReadRepeated(uint64_t field_index);
|
||||
glcr::ErrorOr<glcr::Vector<T>> ReadRepeated(uint64_t field_index) const;
|
||||
|
||||
private:
|
||||
const glcr::ByteBuffer& buffer_;
|
||||
uint64_t offset_;
|
||||
|
||||
uint64_t field_offset(uint64_t field_index) {
|
||||
uint64_t field_offset(uint64_t field_index) const {
|
||||
return offset_ + kHeaderSize + (8 * field_index);
|
||||
}
|
||||
};
|
||||
|
||||
template <>
|
||||
glcr::ErrorOr<uint64_t> MessageView::ReadField<uint64_t>(uint64_t field_index);
|
||||
glcr::ErrorOr<uint64_t> MessageView::ReadField<uint64_t>(
|
||||
uint64_t field_index) const;
|
||||
|
||||
template <>
|
||||
glcr::ErrorOr<glcr::String> MessageView::ReadField<glcr::String>(
|
||||
uint64_t field_index);
|
||||
uint64_t field_index) const;
|
||||
|
||||
template <>
|
||||
glcr::ErrorOr<glcr::Vector<uint64_t>> MessageView::ReadRepeated<uint64_t>(
|
||||
uint64_t field_index);
|
||||
uint64_t field_index) const;
|
||||
|
||||
} // namespace yunq
|
||||
|
|
|
|||
|
|
@ -7,17 +7,6 @@ const uint64_t kIdentByte = 0x33441122;
|
|||
|
||||
} // namespace
|
||||
|
||||
glcr::Status CheckHeader(const glcr::ByteBuffer& buffer, uint64_t offset) {
|
||||
if (buffer.At<uint32_t>(offset + 0) != kIdentByte) {
|
||||
return glcr::InvalidArgument("Trying to parse an invalid yunq message.");
|
||||
}
|
||||
// TODO: Parse core size.
|
||||
// TODO: Parse extension size.
|
||||
// TODO: Check CRC32
|
||||
// TODO: Parse options.
|
||||
return glcr::Status::Ok();
|
||||
}
|
||||
|
||||
void WriteHeader(glcr::ByteBuffer& bytes, uint64_t offset, uint32_t core_size,
|
||||
uint32_t extension_size) {
|
||||
bytes.WriteAt<uint32_t>(offset + 0, kIdentByte);
|
||||
|
|
|
|||
|
|
@ -5,9 +5,6 @@
|
|||
|
||||
namespace yunq {
|
||||
|
||||
[[nodiscard]] glcr::Status CheckHeader(const glcr::ByteBuffer& buffer,
|
||||
uint64_t offset);
|
||||
|
||||
void WriteHeader(glcr::ByteBuffer& bytes, uint64_t offset, uint32_t core_size,
|
||||
uint32_t extension_size);
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue