diff --git a/lib/yunq/message_view.h b/lib/yunq/message_view.h index 85c5fb6..996ba8f 100644 --- a/lib/yunq/message_view.h +++ b/lib/yunq/message_view.h @@ -6,23 +6,10 @@ #include #include +#include "yunq/yunq.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; -}; - class MessageView { public: MessageView(const glcr::ByteBuffer& buffer, uint64_t offset) diff --git a/lib/yunq/serialize.cpp b/lib/yunq/serialize.cpp index 749063b..6522ab3 100644 --- a/lib/yunq/serialize.cpp +++ b/lib/yunq/serialize.cpp @@ -7,12 +7,38 @@ const uint64_t kIdentByte = 0x33441122; } // namespace -void WriteHeader(glcr::ByteBuffer& bytes, uint64_t offset, uint32_t core_size, - uint32_t extension_size) { - bytes.WriteAt(offset + 0, kIdentByte); - bytes.WriteAt(offset + 4, core_size); - bytes.WriteAt(offset + 8, extension_size); - bytes.WriteAt(offset + 12, 0); // TODO: Calculate CRC32. +void Serializer::WriteHeader() { + buffer_.WriteAt(offset_ + 0, kIdentByte); + buffer_.WriteAt(offset_ + 4, core_size_); + buffer_.WriteAt(offset_ + 8, next_extension_); + buffer_.WriteAt(offset_ + 12, 0); // TODO: Calculate CRC32. } +template <> +void Serializer::WriteField(uint64_t field_index, + const glcr::String& value) { + ExtensionPointer ptr{ + .offset = (uint32_t)next_extension_, + // FIXME: Check downcast of str length. + .length = (uint32_t)value.length(), + }; + + buffer_.WriteStringAt(offset_ + next_extension_, value); + next_extension_ += ptr.length; + + buffer_.WriteAt(field_offset(field_index), ptr); +} + +void Serializer::WriteCapability(uint64_t field_index, uint64_t value) { + if (caps_) { + buffer_.WriteAt(field_offset(field_index), next_cap_); + caps_.value().get().WriteAt(next_cap_++, value); + } else { + WriteField(field_index, value); + } +} + +void Serializer::WriteRepeatedCapability(uint64_t field_index, + const glcr::Vector& value) {} + } // namespace yunq diff --git a/lib/yunq/serialize.h b/lib/yunq/serialize.h index cdb76c1..ae6a77b 100644 --- a/lib/yunq/serialize.h +++ b/lib/yunq/serialize.h @@ -1,11 +1,84 @@ #pragma once #include +#include +#include +#include +#include #include +#include "yunq/yunq.h" + namespace yunq { -void WriteHeader(glcr::ByteBuffer& bytes, uint64_t offset, uint32_t core_size, - uint32_t extension_size); +class Serializer { + public: + Serializer(glcr::ByteBuffer& bytes, uint64_t offset, uint64_t num_fields) + : buffer_(bytes), + offset_(offset), + next_extension_(kHeaderSize + (8 * num_fields)), + core_size_(next_extension_), + caps_() {} + Serializer(glcr::ByteBuffer& bytes, uint64_t offset, uint64_t num_fields, + glcr::CapBuffer& caps) + : buffer_(bytes), + offset_(offset), + next_extension_(kHeaderSize + (8 * num_fields)), + core_size_(next_extension_), + caps_(caps) {} + + template + void WriteField(uint64_t field_index, const T& value); + + template + void WriteRepeated(uint64_t field_index, const glcr::Vector& value); + + void WriteCapability(uint64_t field_index, uint64_t value); + + void WriteRepeatedCapability(uint64_t field_index, + const glcr::Vector& value); + + void WriteHeader(); + + uint64_t size() const { return next_extension_; } + + private: + glcr::ByteBuffer& buffer_; + uint64_t offset_; + uint64_t next_extension_; + uint64_t core_size_; + uint64_t next_cap_ = 0; + glcr::Optional> caps_; + + uint64_t field_offset(uint64_t field_index) const { + return offset_ + kHeaderSize + (8 * field_index); + } +}; + +template +void Serializer::WriteField(uint64_t field_index, const T& value) { + buffer_.WriteAt(field_offset(field_index), value); +} + +template <> +void Serializer::WriteField(uint64_t field_index, + const glcr::String& value); + +template +void Serializer::WriteRepeated(uint64_t field_index, + const glcr::Vector& value) { + ExtensionPointer ptr{ + .offset = (uint32_t)next_extension_, + .length = (uint32_t)(value.size() * sizeof(T)), + }; + + next_extension_ += ptr.length; + buffer_.WriteAt(field_offset(field_index), ptr); + + for (uint64_t i = 0; i < value.size(); i++) { + uint32_t ext_offset = offset_ + ptr.offset + (i * sizeof(T)); + buffer_.WriteAt(ext_offset, value.at(i)); + } +} } // namespace yunq diff --git a/lib/yunq/yunq.h b/lib/yunq/yunq.h new file mode 100644 index 0000000..3a33a8c --- /dev/null +++ b/lib/yunq/yunq.h @@ -0,0 +1,22 @@ +#pragma once + +#include + +namespace yunq { + +struct MessageHeader { + uint32_t ident; + uint32_t core_length; + uint32_t length; + uint32_t crc32; + uint64_t options; +} __attribute__((packed)); + +const uint64_t kHeaderSize = 24; // 4x uint32, 1x uint64 + +struct ExtensionPointer { + uint32_t offset; + uint32_t length; +}; + +} // namespace yunq diff --git a/sys/denali/lib/denali/denali.yunq.cpp b/sys/denali/lib/denali/denali.yunq.cpp index ffdc39a..329db3d 100644 --- a/sys/denali/lib/denali/denali.yunq.cpp +++ b/sys/denali/lib/denali/denali.yunq.cpp @@ -38,36 +38,31 @@ glcr::Status ReadRequest::ParseFromBytesInternal(const yunq::MessageView& messag } uint64_t ReadRequest::SerializeToBytes(glcr::ByteBuffer& bytes, uint64_t offset) const { - uint32_t next_extension = header_size + 8 * 3; - const uint32_t core_size = next_extension; + yunq::Serializer serializer(bytes, offset, 3); // Write device_id. - bytes.WriteAt(offset + header_size + (8 * 0), device_id()); + serializer.WriteField(0, device_id_); // Write lba. - bytes.WriteAt(offset + header_size + (8 * 1), lba()); + serializer.WriteField(1, lba_); // Write size. - bytes.WriteAt(offset + header_size + (8 * 2), size()); + serializer.WriteField(2, size_); - // The next extension pointer is the length of the message. - yunq::WriteHeader(bytes, offset, core_size, next_extension); + serializer.WriteHeader(); - return next_extension; + return serializer.size(); } uint64_t ReadRequest::SerializeToBytes(glcr::ByteBuffer& bytes, uint64_t offset, glcr::CapBuffer& caps) const { - uint32_t next_extension = header_size + 8 * 3; - const uint32_t core_size = next_extension; - uint64_t next_cap = 0; + yunq::Serializer serializer(bytes, offset, 3, caps); // Write device_id. - bytes.WriteAt(offset + header_size + (8 * 0), device_id()); + serializer.WriteField(0, device_id_); // Write lba. - bytes.WriteAt(offset + header_size + (8 * 1), lba()); + serializer.WriteField(1, lba_); // Write size. - bytes.WriteAt(offset + header_size + (8 * 2), size()); + serializer.WriteField(2, size_); - // The next extension pointer is the length of the message. - yunq::WriteHeader(bytes, offset, core_size, next_extension); + serializer.WriteHeader(); - return next_extension; + return serializer.size(); } glcr::Status ReadManyRequest::ParseFromBytes(const yunq::MessageView& message) { RETURN_ERROR(ParseFromBytesInternal(message)); @@ -94,80 +89,31 @@ glcr::Status ReadManyRequest::ParseFromBytesInternal(const yunq::MessageView& me } uint64_t ReadManyRequest::SerializeToBytes(glcr::ByteBuffer& bytes, uint64_t offset) const { - uint32_t next_extension = header_size + 8 * 3; - const uint32_t core_size = next_extension; + yunq::Serializer serializer(bytes, offset, 3); // Write device_id. - bytes.WriteAt(offset + header_size + (8 * 0), device_id()); + serializer.WriteField(0, device_id_); // Write lba. - ExtPointer lba_ptr{ - .offset = next_extension, - .length = (uint32_t)(lba().size() * sizeof(uint64_t)), - }; - - next_extension += lba_ptr.length; - bytes.WriteAt(offset + header_size + (8 * 1), lba_ptr); - - for (uint64_t i = 0; i < lba().size(); i++) { - uint32_t ext_offset = offset + lba_ptr.offset + (i * sizeof(uint64_t)); - bytes.WriteAt(ext_offset, lba().at(i)); - } + serializer.WriteRepeated(1, lba_); // Write sector_cnt. - ExtPointer sector_cnt_ptr{ - .offset = next_extension, - .length = (uint32_t)(sector_cnt().size() * sizeof(uint64_t)), - }; + serializer.WriteRepeated(2, sector_cnt_); - next_extension += sector_cnt_ptr.length; - bytes.WriteAt(offset + header_size + (8 * 2), sector_cnt_ptr); + serializer.WriteHeader(); - for (uint64_t i = 0; i < sector_cnt().size(); i++) { - uint32_t ext_offset = offset + sector_cnt_ptr.offset + (i * sizeof(uint64_t)); - bytes.WriteAt(ext_offset, sector_cnt().at(i)); - } - - // The next extension pointer is the length of the message. - yunq::WriteHeader(bytes, offset, core_size, next_extension); - - return next_extension; + return serializer.size(); } uint64_t ReadManyRequest::SerializeToBytes(glcr::ByteBuffer& bytes, uint64_t offset, glcr::CapBuffer& caps) const { - uint32_t next_extension = header_size + 8 * 3; - const uint32_t core_size = next_extension; - uint64_t next_cap = 0; + yunq::Serializer serializer(bytes, offset, 3, caps); // Write device_id. - bytes.WriteAt(offset + header_size + (8 * 0), device_id()); + serializer.WriteField(0, device_id_); // Write lba. - ExtPointer lba_ptr{ - .offset = next_extension, - .length = (uint32_t)(lba().size() * sizeof(uint64_t)), - }; - - next_extension += lba_ptr.length; - bytes.WriteAt(offset + header_size + (8 * 1), lba_ptr); - - for (uint64_t i = 0; i < lba().size(); i++) { - uint32_t ext_offset = offset + lba_ptr.offset + (i * sizeof(uint64_t)); - bytes.WriteAt(ext_offset, lba().at(i)); - } + serializer.WriteRepeated(1, lba_); // Write sector_cnt. - ExtPointer sector_cnt_ptr{ - .offset = next_extension, - .length = (uint32_t)(sector_cnt().size() * sizeof(uint64_t)), - }; + serializer.WriteRepeated(2, sector_cnt_); - next_extension += sector_cnt_ptr.length; - bytes.WriteAt(offset + header_size + (8 * 2), sector_cnt_ptr); + serializer.WriteHeader(); - for (uint64_t i = 0; i < sector_cnt().size(); i++) { - uint32_t ext_offset = offset + sector_cnt_ptr.offset + (i * sizeof(uint64_t)); - bytes.WriteAt(ext_offset, sector_cnt().at(i)); - } - - // The next extension pointer is the length of the message. - yunq::WriteHeader(bytes, offset, core_size, next_extension); - - return next_extension; + return serializer.size(); } glcr::Status ReadResponse::ParseFromBytes(const yunq::MessageView& message) { RETURN_ERROR(ParseFromBytesInternal(message)); @@ -195,37 +141,30 @@ glcr::Status ReadResponse::ParseFromBytesInternal(const yunq::MessageView& messa } uint64_t ReadResponse::SerializeToBytes(glcr::ByteBuffer& bytes, uint64_t offset) const { - uint32_t next_extension = header_size + 8 * 3; - const uint32_t core_size = next_extension; + yunq::Serializer serializer(bytes, offset, 3); // Write device_id. - bytes.WriteAt(offset + header_size + (8 * 0), device_id()); + serializer.WriteField(0, device_id_); // Write size. - bytes.WriteAt(offset + header_size + (8 * 1), size()); + serializer.WriteField(1, size_); // Write memory. - // FIXME: Implement inbuffer capabilities. - bytes.WriteAt(offset + header_size + (8 * 2), 0); + serializer.WriteCapability(2, memory_); - // The next extension pointer is the length of the message. - yunq::WriteHeader(bytes, offset, core_size, next_extension); + serializer.WriteHeader(); - return next_extension; + return serializer.size(); } uint64_t ReadResponse::SerializeToBytes(glcr::ByteBuffer& bytes, uint64_t offset, glcr::CapBuffer& caps) const { - uint32_t next_extension = header_size + 8 * 3; - const uint32_t core_size = next_extension; - uint64_t next_cap = 0; + yunq::Serializer serializer(bytes, offset, 3, caps); // Write device_id. - bytes.WriteAt(offset + header_size + (8 * 0), device_id()); + serializer.WriteField(0, device_id_); // Write size. - bytes.WriteAt(offset + header_size + (8 * 1), size()); + serializer.WriteField(1, size_); // Write memory. - caps.WriteAt(next_cap, memory()); - bytes.WriteAt(offset + header_size + (8 * 2), next_cap++); + serializer.WriteCapability(2, memory_); - // The next extension pointer is the length of the message. - yunq::WriteHeader(bytes, offset, core_size, next_extension); + serializer.WriteHeader(); - return next_extension; + return serializer.size(); } diff --git a/sys/victoriafalls/lib/victoriafalls/victoriafalls.yunq.cpp b/sys/victoriafalls/lib/victoriafalls/victoriafalls.yunq.cpp index 0c0d70b..a529810 100644 --- a/sys/victoriafalls/lib/victoriafalls/victoriafalls.yunq.cpp +++ b/sys/victoriafalls/lib/victoriafalls/victoriafalls.yunq.cpp @@ -34,46 +34,23 @@ glcr::Status OpenFileRequest::ParseFromBytesInternal(const yunq::MessageView& me } uint64_t OpenFileRequest::SerializeToBytes(glcr::ByteBuffer& bytes, uint64_t offset) const { - uint32_t next_extension = header_size + 8 * 1; - const uint32_t core_size = next_extension; + yunq::Serializer serializer(bytes, offset, 1); // Write path. - ExtPointer path_ptr{ - .offset = next_extension, - // FIXME: Check downcast of str length. - .length = (uint32_t)path().length(), - }; + serializer.WriteField(0, path_); - bytes.WriteStringAt(offset + next_extension, path()); - next_extension += path_ptr.length; + serializer.WriteHeader(); - bytes.WriteAt(offset + header_size + (8 * 0), path_ptr); - - // The next extension pointer is the length of the message. - yunq::WriteHeader(bytes, offset, core_size, next_extension); - - return next_extension; + return serializer.size(); } uint64_t OpenFileRequest::SerializeToBytes(glcr::ByteBuffer& bytes, uint64_t offset, glcr::CapBuffer& caps) const { - uint32_t next_extension = header_size + 8 * 1; - const uint32_t core_size = next_extension; - uint64_t next_cap = 0; + yunq::Serializer serializer(bytes, offset, 1, caps); // Write path. - ExtPointer path_ptr{ - .offset = next_extension, - // FIXME: Check downcast of str length. - .length = (uint32_t)path().length(), - }; + serializer.WriteField(0, path_); - bytes.WriteStringAt(offset + next_extension, path()); - next_extension += path_ptr.length; + serializer.WriteHeader(); - bytes.WriteAt(offset + header_size + (8 * 0), path_ptr); - - // The next extension pointer is the length of the message. - yunq::WriteHeader(bytes, offset, core_size, next_extension); - - return next_extension; + return serializer.size(); } glcr::Status OpenFileResponse::ParseFromBytes(const yunq::MessageView& message) { RETURN_ERROR(ParseFromBytesInternal(message)); @@ -101,56 +78,31 @@ glcr::Status OpenFileResponse::ParseFromBytesInternal(const yunq::MessageView& m } uint64_t OpenFileResponse::SerializeToBytes(glcr::ByteBuffer& bytes, uint64_t offset) const { - uint32_t next_extension = header_size + 8 * 3; - const uint32_t core_size = next_extension; + yunq::Serializer serializer(bytes, offset, 3); // Write path. - ExtPointer path_ptr{ - .offset = next_extension, - // FIXME: Check downcast of str length. - .length = (uint32_t)path().length(), - }; - - bytes.WriteStringAt(offset + next_extension, path()); - next_extension += path_ptr.length; - - bytes.WriteAt(offset + header_size + (8 * 0), path_ptr); + serializer.WriteField(0, path_); // Write size. - bytes.WriteAt(offset + header_size + (8 * 1), size()); + serializer.WriteField(1, size_); // Write memory. - // FIXME: Implement inbuffer capabilities. - bytes.WriteAt(offset + header_size + (8 * 2), 0); + serializer.WriteCapability(2, memory_); - // The next extension pointer is the length of the message. - yunq::WriteHeader(bytes, offset, core_size, next_extension); + serializer.WriteHeader(); - return next_extension; + return serializer.size(); } uint64_t OpenFileResponse::SerializeToBytes(glcr::ByteBuffer& bytes, uint64_t offset, glcr::CapBuffer& caps) const { - uint32_t next_extension = header_size + 8 * 3; - const uint32_t core_size = next_extension; - uint64_t next_cap = 0; + yunq::Serializer serializer(bytes, offset, 3, caps); // Write path. - ExtPointer path_ptr{ - .offset = next_extension, - // FIXME: Check downcast of str length. - .length = (uint32_t)path().length(), - }; - - bytes.WriteStringAt(offset + next_extension, path()); - next_extension += path_ptr.length; - - bytes.WriteAt(offset + header_size + (8 * 0), path_ptr); + serializer.WriteField(0, path_); // Write size. - bytes.WriteAt(offset + header_size + (8 * 1), size()); + serializer.WriteField(1, size_); // Write memory. - caps.WriteAt(next_cap, memory()); - bytes.WriteAt(offset + header_size + (8 * 2), next_cap++); + serializer.WriteCapability(2, memory_); - // The next extension pointer is the length of the message. - yunq::WriteHeader(bytes, offset, core_size, next_extension); + serializer.WriteHeader(); - return next_extension; + return serializer.size(); } glcr::Status GetDirectoryRequest::ParseFromBytes(const yunq::MessageView& message) { RETURN_ERROR(ParseFromBytesInternal(message)); @@ -171,46 +123,23 @@ glcr::Status GetDirectoryRequest::ParseFromBytesInternal(const yunq::MessageView } uint64_t GetDirectoryRequest::SerializeToBytes(glcr::ByteBuffer& bytes, uint64_t offset) const { - uint32_t next_extension = header_size + 8 * 1; - const uint32_t core_size = next_extension; + yunq::Serializer serializer(bytes, offset, 1); // Write path. - ExtPointer path_ptr{ - .offset = next_extension, - // FIXME: Check downcast of str length. - .length = (uint32_t)path().length(), - }; + serializer.WriteField(0, path_); - bytes.WriteStringAt(offset + next_extension, path()); - next_extension += path_ptr.length; + serializer.WriteHeader(); - bytes.WriteAt(offset + header_size + (8 * 0), path_ptr); - - // The next extension pointer is the length of the message. - yunq::WriteHeader(bytes, offset, core_size, next_extension); - - return next_extension; + return serializer.size(); } uint64_t GetDirectoryRequest::SerializeToBytes(glcr::ByteBuffer& bytes, uint64_t offset, glcr::CapBuffer& caps) const { - uint32_t next_extension = header_size + 8 * 1; - const uint32_t core_size = next_extension; - uint64_t next_cap = 0; + yunq::Serializer serializer(bytes, offset, 1, caps); // Write path. - ExtPointer path_ptr{ - .offset = next_extension, - // FIXME: Check downcast of str length. - .length = (uint32_t)path().length(), - }; + serializer.WriteField(0, path_); - bytes.WriteStringAt(offset + next_extension, path()); - next_extension += path_ptr.length; + serializer.WriteHeader(); - bytes.WriteAt(offset + header_size + (8 * 0), path_ptr); - - // The next extension pointer is the length of the message. - yunq::WriteHeader(bytes, offset, core_size, next_extension); - - return next_extension; + return serializer.size(); } glcr::Status Directory::ParseFromBytes(const yunq::MessageView& message) { RETURN_ERROR(ParseFromBytesInternal(message)); @@ -231,45 +160,22 @@ glcr::Status Directory::ParseFromBytesInternal(const yunq::MessageView& message) } uint64_t Directory::SerializeToBytes(glcr::ByteBuffer& bytes, uint64_t offset) const { - uint32_t next_extension = header_size + 8 * 1; - const uint32_t core_size = next_extension; + yunq::Serializer serializer(bytes, offset, 1); // Write filenames. - ExtPointer filenames_ptr{ - .offset = next_extension, - // FIXME: Check downcast of str length. - .length = (uint32_t)filenames().length(), - }; + serializer.WriteField(0, filenames_); - bytes.WriteStringAt(offset + next_extension, filenames()); - next_extension += filenames_ptr.length; + serializer.WriteHeader(); - bytes.WriteAt(offset + header_size + (8 * 0), filenames_ptr); - - // The next extension pointer is the length of the message. - yunq::WriteHeader(bytes, offset, core_size, next_extension); - - return next_extension; + return serializer.size(); } uint64_t Directory::SerializeToBytes(glcr::ByteBuffer& bytes, uint64_t offset, glcr::CapBuffer& caps) const { - uint32_t next_extension = header_size + 8 * 1; - const uint32_t core_size = next_extension; - uint64_t next_cap = 0; + yunq::Serializer serializer(bytes, offset, 1, caps); // Write filenames. - ExtPointer filenames_ptr{ - .offset = next_extension, - // FIXME: Check downcast of str length. - .length = (uint32_t)filenames().length(), - }; + serializer.WriteField(0, filenames_); - bytes.WriteStringAt(offset + next_extension, filenames()); - next_extension += filenames_ptr.length; + serializer.WriteHeader(); - bytes.WriteAt(offset + header_size + (8 * 0), filenames_ptr); - - // The next extension pointer is the length of the message. - yunq::WriteHeader(bytes, offset, core_size, next_extension); - - return next_extension; + return serializer.size(); } diff --git a/sys/voyageurs/lib/voyageurs/voyageurs.yunq.cpp b/sys/voyageurs/lib/voyageurs/voyageurs.yunq.cpp index 863a0d9..9a69f66 100644 --- a/sys/voyageurs/lib/voyageurs/voyageurs.yunq.cpp +++ b/sys/voyageurs/lib/voyageurs/voyageurs.yunq.cpp @@ -37,29 +37,22 @@ glcr::Status KeyboardListener::ParseFromBytesInternal(const yunq::MessageView& m } uint64_t KeyboardListener::SerializeToBytes(glcr::ByteBuffer& bytes, uint64_t offset) const { - uint32_t next_extension = header_size + 8 * 1; - const uint32_t core_size = next_extension; + yunq::Serializer serializer(bytes, offset, 1); // Write port_capability. - // FIXME: Implement inbuffer capabilities. - bytes.WriteAt(offset + header_size + (8 * 0), 0); + serializer.WriteCapability(0, port_capability_); - // The next extension pointer is the length of the message. - yunq::WriteHeader(bytes, offset, core_size, next_extension); + serializer.WriteHeader(); - return next_extension; + return serializer.size(); } uint64_t KeyboardListener::SerializeToBytes(glcr::ByteBuffer& bytes, uint64_t offset, glcr::CapBuffer& caps) const { - uint32_t next_extension = header_size + 8 * 1; - const uint32_t core_size = next_extension; - uint64_t next_cap = 0; + yunq::Serializer serializer(bytes, offset, 1, caps); // Write port_capability. - caps.WriteAt(next_cap, port_capability()); - bytes.WriteAt(offset + header_size + (8 * 0), next_cap++); + serializer.WriteCapability(0, port_capability_); - // The next extension pointer is the length of the message. - yunq::WriteHeader(bytes, offset, core_size, next_extension); + serializer.WriteHeader(); - return next_extension; + return serializer.size(); } diff --git a/sys/yellowstone/lib/yellowstone/yellowstone.yunq.cpp b/sys/yellowstone/lib/yellowstone/yellowstone.yunq.cpp index 0b21546..33a5f0d 100644 --- a/sys/yellowstone/lib/yellowstone/yellowstone.yunq.cpp +++ b/sys/yellowstone/lib/yellowstone/yellowstone.yunq.cpp @@ -41,52 +41,27 @@ glcr::Status RegisterEndpointRequest::ParseFromBytesInternal(const yunq::Message } uint64_t RegisterEndpointRequest::SerializeToBytes(glcr::ByteBuffer& bytes, uint64_t offset) const { - uint32_t next_extension = header_size + 8 * 2; - const uint32_t core_size = next_extension; + yunq::Serializer serializer(bytes, offset, 2); // Write endpoint_name. - ExtPointer endpoint_name_ptr{ - .offset = next_extension, - // FIXME: Check downcast of str length. - .length = (uint32_t)endpoint_name().length(), - }; - - bytes.WriteStringAt(offset + next_extension, endpoint_name()); - next_extension += endpoint_name_ptr.length; - - bytes.WriteAt(offset + header_size + (8 * 0), endpoint_name_ptr); + serializer.WriteField(0, endpoint_name_); // Write endpoint_capability. - // FIXME: Implement inbuffer capabilities. - bytes.WriteAt(offset + header_size + (8 * 1), 0); + serializer.WriteCapability(1, endpoint_capability_); - // The next extension pointer is the length of the message. - yunq::WriteHeader(bytes, offset, core_size, next_extension); + serializer.WriteHeader(); - return next_extension; + return serializer.size(); } uint64_t RegisterEndpointRequest::SerializeToBytes(glcr::ByteBuffer& bytes, uint64_t offset, glcr::CapBuffer& caps) const { - uint32_t next_extension = header_size + 8 * 2; - const uint32_t core_size = next_extension; - uint64_t next_cap = 0; + yunq::Serializer serializer(bytes, offset, 2, caps); // Write endpoint_name. - ExtPointer endpoint_name_ptr{ - .offset = next_extension, - // FIXME: Check downcast of str length. - .length = (uint32_t)endpoint_name().length(), - }; - - bytes.WriteStringAt(offset + next_extension, endpoint_name()); - next_extension += endpoint_name_ptr.length; - - bytes.WriteAt(offset + header_size + (8 * 0), endpoint_name_ptr); + serializer.WriteField(0, endpoint_name_); // Write endpoint_capability. - caps.WriteAt(next_cap, endpoint_capability()); - bytes.WriteAt(offset + header_size + (8 * 1), next_cap++); + serializer.WriteCapability(1, endpoint_capability_); - // The next extension pointer is the length of the message. - yunq::WriteHeader(bytes, offset, core_size, next_extension); + serializer.WriteHeader(); - return next_extension; + return serializer.size(); } glcr::Status GetEndpointRequest::ParseFromBytes(const yunq::MessageView& message) { RETURN_ERROR(ParseFromBytesInternal(message)); @@ -107,46 +82,23 @@ glcr::Status GetEndpointRequest::ParseFromBytesInternal(const yunq::MessageView& } uint64_t GetEndpointRequest::SerializeToBytes(glcr::ByteBuffer& bytes, uint64_t offset) const { - uint32_t next_extension = header_size + 8 * 1; - const uint32_t core_size = next_extension; + yunq::Serializer serializer(bytes, offset, 1); // Write endpoint_name. - ExtPointer endpoint_name_ptr{ - .offset = next_extension, - // FIXME: Check downcast of str length. - .length = (uint32_t)endpoint_name().length(), - }; + serializer.WriteField(0, endpoint_name_); - bytes.WriteStringAt(offset + next_extension, endpoint_name()); - next_extension += endpoint_name_ptr.length; + serializer.WriteHeader(); - bytes.WriteAt(offset + header_size + (8 * 0), endpoint_name_ptr); - - // The next extension pointer is the length of the message. - yunq::WriteHeader(bytes, offset, core_size, next_extension); - - return next_extension; + return serializer.size(); } uint64_t GetEndpointRequest::SerializeToBytes(glcr::ByteBuffer& bytes, uint64_t offset, glcr::CapBuffer& caps) const { - uint32_t next_extension = header_size + 8 * 1; - const uint32_t core_size = next_extension; - uint64_t next_cap = 0; + yunq::Serializer serializer(bytes, offset, 1, caps); // Write endpoint_name. - ExtPointer endpoint_name_ptr{ - .offset = next_extension, - // FIXME: Check downcast of str length. - .length = (uint32_t)endpoint_name().length(), - }; + serializer.WriteField(0, endpoint_name_); - bytes.WriteStringAt(offset + next_extension, endpoint_name()); - next_extension += endpoint_name_ptr.length; + serializer.WriteHeader(); - bytes.WriteAt(offset + header_size + (8 * 0), endpoint_name_ptr); - - // The next extension pointer is the length of the message. - yunq::WriteHeader(bytes, offset, core_size, next_extension); - - return next_extension; + return serializer.size(); } glcr::Status Endpoint::ParseFromBytes(const yunq::MessageView& message) { RETURN_ERROR(ParseFromBytesInternal(message)); @@ -170,30 +122,23 @@ glcr::Status Endpoint::ParseFromBytesInternal(const yunq::MessageView& message) } uint64_t Endpoint::SerializeToBytes(glcr::ByteBuffer& bytes, uint64_t offset) const { - uint32_t next_extension = header_size + 8 * 1; - const uint32_t core_size = next_extension; + yunq::Serializer serializer(bytes, offset, 1); // Write endpoint. - // FIXME: Implement inbuffer capabilities. - bytes.WriteAt(offset + header_size + (8 * 0), 0); + serializer.WriteCapability(0, endpoint_); - // The next extension pointer is the length of the message. - yunq::WriteHeader(bytes, offset, core_size, next_extension); + serializer.WriteHeader(); - return next_extension; + return serializer.size(); } uint64_t Endpoint::SerializeToBytes(glcr::ByteBuffer& bytes, uint64_t offset, glcr::CapBuffer& caps) const { - uint32_t next_extension = header_size + 8 * 1; - const uint32_t core_size = next_extension; - uint64_t next_cap = 0; + yunq::Serializer serializer(bytes, offset, 1, caps); // Write endpoint. - caps.WriteAt(next_cap, endpoint()); - bytes.WriteAt(offset + header_size + (8 * 0), next_cap++); + serializer.WriteCapability(0, endpoint_); - // The next extension pointer is the length of the message. - yunq::WriteHeader(bytes, offset, core_size, next_extension); + serializer.WriteHeader(); - return next_extension; + return serializer.size(); } glcr::Status AhciInfo::ParseFromBytes(const yunq::MessageView& message) { RETURN_ERROR(ParseFromBytesInternal(message)); @@ -219,34 +164,27 @@ glcr::Status AhciInfo::ParseFromBytesInternal(const yunq::MessageView& message) } uint64_t AhciInfo::SerializeToBytes(glcr::ByteBuffer& bytes, uint64_t offset) const { - uint32_t next_extension = header_size + 8 * 2; - const uint32_t core_size = next_extension; + yunq::Serializer serializer(bytes, offset, 2); // Write ahci_region. - // FIXME: Implement inbuffer capabilities. - bytes.WriteAt(offset + header_size + (8 * 0), 0); + serializer.WriteCapability(0, ahci_region_); // Write region_length. - bytes.WriteAt(offset + header_size + (8 * 1), region_length()); + serializer.WriteField(1, region_length_); - // The next extension pointer is the length of the message. - yunq::WriteHeader(bytes, offset, core_size, next_extension); + serializer.WriteHeader(); - return next_extension; + return serializer.size(); } uint64_t AhciInfo::SerializeToBytes(glcr::ByteBuffer& bytes, uint64_t offset, glcr::CapBuffer& caps) const { - uint32_t next_extension = header_size + 8 * 2; - const uint32_t core_size = next_extension; - uint64_t next_cap = 0; + yunq::Serializer serializer(bytes, offset, 2, caps); // Write ahci_region. - caps.WriteAt(next_cap, ahci_region()); - bytes.WriteAt(offset + header_size + (8 * 0), next_cap++); + serializer.WriteCapability(0, ahci_region_); // Write region_length. - bytes.WriteAt(offset + header_size + (8 * 1), region_length()); + serializer.WriteField(1, region_length_); - // The next extension pointer is the length of the message. - yunq::WriteHeader(bytes, offset, core_size, next_extension); + serializer.WriteHeader(); - return next_extension; + return serializer.size(); } glcr::Status FramebufferInfo::ParseFromBytes(const yunq::MessageView& message) { RETURN_ERROR(ParseFromBytesInternal(message)); @@ -289,72 +227,67 @@ glcr::Status FramebufferInfo::ParseFromBytesInternal(const yunq::MessageView& me } uint64_t FramebufferInfo::SerializeToBytes(glcr::ByteBuffer& bytes, uint64_t offset) const { - uint32_t next_extension = header_size + 8 * 12; - const uint32_t core_size = next_extension; + yunq::Serializer serializer(bytes, offset, 12); // Write address_phys. - bytes.WriteAt(offset + header_size + (8 * 0), address_phys()); + serializer.WriteField(0, address_phys_); // Write width. - bytes.WriteAt(offset + header_size + (8 * 1), width()); + serializer.WriteField(1, width_); // Write height. - bytes.WriteAt(offset + header_size + (8 * 2), height()); + serializer.WriteField(2, height_); // Write pitch. - bytes.WriteAt(offset + header_size + (8 * 3), pitch()); + serializer.WriteField(3, pitch_); // Write bpp. - bytes.WriteAt(offset + header_size + (8 * 4), bpp()); + serializer.WriteField(4, bpp_); // Write memory_model. - bytes.WriteAt(offset + header_size + (8 * 5), memory_model()); + serializer.WriteField(5, memory_model_); // Write red_mask_size. - bytes.WriteAt(offset + header_size + (8 * 6), red_mask_size()); + serializer.WriteField(6, red_mask_size_); // Write red_mask_shift. - bytes.WriteAt(offset + header_size + (8 * 7), red_mask_shift()); + serializer.WriteField(7, red_mask_shift_); // Write green_mask_size. - bytes.WriteAt(offset + header_size + (8 * 8), green_mask_size()); + serializer.WriteField(8, green_mask_size_); // Write green_mask_shift. - bytes.WriteAt(offset + header_size + (8 * 9), green_mask_shift()); + serializer.WriteField(9, green_mask_shift_); // Write blue_mask_size. - bytes.WriteAt(offset + header_size + (8 * 10), blue_mask_size()); + serializer.WriteField(10, blue_mask_size_); // Write blue_mask_shift. - bytes.WriteAt(offset + header_size + (8 * 11), blue_mask_shift()); + serializer.WriteField(11, blue_mask_shift_); - // The next extension pointer is the length of the message. - yunq::WriteHeader(bytes, offset, core_size, next_extension); + serializer.WriteHeader(); - return next_extension; + return serializer.size(); } uint64_t FramebufferInfo::SerializeToBytes(glcr::ByteBuffer& bytes, uint64_t offset, glcr::CapBuffer& caps) const { - uint32_t next_extension = header_size + 8 * 12; - const uint32_t core_size = next_extension; - uint64_t next_cap = 0; + yunq::Serializer serializer(bytes, offset, 12, caps); // Write address_phys. - bytes.WriteAt(offset + header_size + (8 * 0), address_phys()); + serializer.WriteField(0, address_phys_); // Write width. - bytes.WriteAt(offset + header_size + (8 * 1), width()); + serializer.WriteField(1, width_); // Write height. - bytes.WriteAt(offset + header_size + (8 * 2), height()); + serializer.WriteField(2, height_); // Write pitch. - bytes.WriteAt(offset + header_size + (8 * 3), pitch()); + serializer.WriteField(3, pitch_); // Write bpp. - bytes.WriteAt(offset + header_size + (8 * 4), bpp()); + serializer.WriteField(4, bpp_); // Write memory_model. - bytes.WriteAt(offset + header_size + (8 * 5), memory_model()); + serializer.WriteField(5, memory_model_); // Write red_mask_size. - bytes.WriteAt(offset + header_size + (8 * 6), red_mask_size()); + serializer.WriteField(6, red_mask_size_); // Write red_mask_shift. - bytes.WriteAt(offset + header_size + (8 * 7), red_mask_shift()); + serializer.WriteField(7, red_mask_shift_); // Write green_mask_size. - bytes.WriteAt(offset + header_size + (8 * 8), green_mask_size()); + serializer.WriteField(8, green_mask_size_); // Write green_mask_shift. - bytes.WriteAt(offset + header_size + (8 * 9), green_mask_shift()); + serializer.WriteField(9, green_mask_shift_); // Write blue_mask_size. - bytes.WriteAt(offset + header_size + (8 * 10), blue_mask_size()); + serializer.WriteField(10, blue_mask_size_); // Write blue_mask_shift. - bytes.WriteAt(offset + header_size + (8 * 11), blue_mask_shift()); + serializer.WriteField(11, blue_mask_shift_); - // The next extension pointer is the length of the message. - yunq::WriteHeader(bytes, offset, core_size, next_extension); + serializer.WriteHeader(); - return next_extension; + return serializer.size(); } glcr::Status DenaliInfo::ParseFromBytes(const yunq::MessageView& message) { RETURN_ERROR(ParseFromBytesInternal(message)); @@ -382,38 +315,31 @@ glcr::Status DenaliInfo::ParseFromBytesInternal(const yunq::MessageView& message } uint64_t DenaliInfo::SerializeToBytes(glcr::ByteBuffer& bytes, uint64_t offset) const { - uint32_t next_extension = header_size + 8 * 3; - const uint32_t core_size = next_extension; + yunq::Serializer serializer(bytes, offset, 3); // Write denali_endpoint. - // FIXME: Implement inbuffer capabilities. - bytes.WriteAt(offset + header_size + (8 * 0), 0); + serializer.WriteCapability(0, denali_endpoint_); // Write device_id. - bytes.WriteAt(offset + header_size + (8 * 1), device_id()); + serializer.WriteField(1, device_id_); // Write lba_offset. - bytes.WriteAt(offset + header_size + (8 * 2), lba_offset()); + serializer.WriteField(2, lba_offset_); - // The next extension pointer is the length of the message. - yunq::WriteHeader(bytes, offset, core_size, next_extension); + serializer.WriteHeader(); - return next_extension; + return serializer.size(); } uint64_t DenaliInfo::SerializeToBytes(glcr::ByteBuffer& bytes, uint64_t offset, glcr::CapBuffer& caps) const { - uint32_t next_extension = header_size + 8 * 3; - const uint32_t core_size = next_extension; - uint64_t next_cap = 0; + yunq::Serializer serializer(bytes, offset, 3, caps); // Write denali_endpoint. - caps.WriteAt(next_cap, denali_endpoint()); - bytes.WriteAt(offset + header_size + (8 * 0), next_cap++); + serializer.WriteCapability(0, denali_endpoint_); // Write device_id. - bytes.WriteAt(offset + header_size + (8 * 1), device_id()); + serializer.WriteField(1, device_id_); // Write lba_offset. - bytes.WriteAt(offset + header_size + (8 * 2), lba_offset()); + serializer.WriteField(2, lba_offset_); - // The next extension pointer is the length of the message. - yunq::WriteHeader(bytes, offset, core_size, next_extension); + serializer.WriteHeader(); - return next_extension; + return serializer.size(); } diff --git a/yunq/message.cpp.jinja b/yunq/message.cpp.jinja index 403188e..7666bf4 100644 --- a/yunq/message.cpp.jinja +++ b/yunq/message.cpp.jinja @@ -73,105 +73,63 @@ glcr::Status {{message.name}}::ParseFromBytesInternal(const yunq::MessageView& m } uint64_t {{message.name}}::SerializeToBytes(glcr::ByteBuffer& bytes, uint64_t offset) const { - uint32_t next_extension = header_size + 8 * {{ message.fields | length }}; - const uint32_t core_size = next_extension; - + yunq::Serializer serializer(bytes, offset, {{ message.fields | length }}); + {%- for field in message.fields %} // Write {{field.name}}. {%- if not field.repeated %} -{%- if field.type == Type.U64 %} - bytes.WriteAt(offset + header_size + (8 * {{loop.index0}}), {{field.name}}()); -{%- elif field.type == Type.I64 %} - bytes.WriteAt(offset + header_size + (8 * {{loop.index0}}), {{field.name}}()); -{%- elif field.type == Type.STRING %} - ExtPointer {{field.name}}_ptr{ - .offset = next_extension, - // FIXME: Check downcast of str length. - .length = (uint32_t){{field.name}}().length(), - }; - bytes.WriteStringAt(offset + next_extension, {{field.name}}()); - next_extension += {{field.name}}_ptr.length; - - bytes.WriteAt(offset + header_size + (8 * {{loop.index0}}), {{field.name}}_ptr); -{%- elif field.type == Type.CAPABILITY %} - // FIXME: Implement inbuffer capabilities. - bytes.WriteAt(offset + header_size + (8 * {{loop.index0}}), 0); +{%- if field.type != Type.CAPABILITY %} + serializer.WriteField<{{field.cpp_type()}}>({{loop.index0}}, {{field.name}}_); {%- else %} - // TODO: Unimplemented serialization {{field.type}} + serializer.WriteCapability({{loop.index0}}, {{field.name}}_); {%- endif %} + {%- else %} - ExtPointer {{field.name}}_ptr{ - .offset = next_extension, - .length = (uint32_t)({{field.name}}().size() * sizeof({{field.cpp_type()}})), - }; - next_extension += {{field.name}}_ptr.length; - bytes.WriteAt(offset + header_size + (8 * {{loop.index0}}), {{field.name}}_ptr); +{%- if field.type != Type.CAPABILITY %} + serializer.WriteRepeated<{{field.cpp_type()}}>({{loop.index0}}, {{field.name}}_); +{%- else %} + serializer.WriteRepeatedCapability({{loop.index0}}, {{field.name}}_); +{%- endif %} - for (uint64_t i = 0; i < {{field.name}}().size(); i++) { - uint32_t ext_offset = offset + {{field.name}}_ptr.offset + (i * sizeof({{field.cpp_type()}})); - bytes.WriteAt<{{field.cpp_type()}}>(ext_offset, {{field.name}}().at(i)); - } {%- endif %} {%- endfor %} - // The next extension pointer is the length of the message. - yunq::WriteHeader(bytes, offset, core_size, next_extension); + serializer.WriteHeader(); - return next_extension; + return serializer.size(); } uint64_t {{message.name}}::SerializeToBytes(glcr::ByteBuffer& bytes, uint64_t offset, glcr::CapBuffer& caps) const { - uint32_t next_extension = header_size + 8 * {{ message.fields | length}}; - const uint32_t core_size = next_extension; - uint64_t next_cap = 0; - + yunq::Serializer serializer(bytes, offset, {{ message.fields | length }}, caps); + {%- for field in message.fields %} // Write {{field.name}}. {%- if not field.repeated %} -{%- if field.type == Type.U64 %} - bytes.WriteAt(offset + header_size + (8 * {{loop.index0}}), {{field.name}}()); -{%- elif field.type == Type.I64 %} - bytes.WriteAt(offset + header_size + (8 * {{loop.index0}}), {{field.name}}()); -{%- elif field.type == Type.STRING %} - ExtPointer {{field.name}}_ptr{ - .offset = next_extension, - // FIXME: Check downcast of str length. - .length = (uint32_t){{field.name}}().length(), - }; - bytes.WriteStringAt(offset + next_extension, {{field.name}}()); - next_extension += {{field.name}}_ptr.length; - - bytes.WriteAt(offset + header_size + (8 * {{loop.index0}}), {{field.name}}_ptr); -{%- elif field.type == Type.CAPABILITY %} - caps.WriteAt(next_cap, {{field.name}}()); - bytes.WriteAt(offset + header_size + (8 * {{loop.index0}}), next_cap++); +{%- if field.type != Type.CAPABILITY %} + serializer.WriteField<{{field.cpp_type()}}>({{loop.index0}}, {{field.name}}_); {%- else %} - // TODO: Unimplemented serialization {{field.type}} + serializer.WriteCapability({{loop.index0}}, {{field.name}}_); {%- endif %} + {%- else %} - ExtPointer {{field.name}}_ptr{ - .offset = next_extension, - .length = (uint32_t)({{field.name}}().size() * sizeof({{field.cpp_type()}})), - }; - next_extension += {{field.name}}_ptr.length; - bytes.WriteAt(offset + header_size + (8 * {{loop.index0}}), {{field.name}}_ptr); +{%- if field.type != Type.CAPABILITY %} + serializer.WriteRepeated<{{field.cpp_type()}}>({{loop.index0}}, {{field.name}}_); +{%- else %} + serializer.WriteRepeatedCapability({{loop.index0}}, {{field.name}}_); +{%- endif %} - for (uint64_t i = 0; i < {{field.name}}().size(); i++) { - uint32_t ext_offset = offset + {{field.name}}_ptr.offset + (i * sizeof({{field.cpp_type()}})); - bytes.WriteAt<{{field.cpp_type()}}>(ext_offset, {{field.name}}().at(i)); - } {%- endif %} {%- endfor %} - // The next extension pointer is the length of the message. - yunq::WriteHeader(bytes, offset, core_size, next_extension); + serializer.WriteHeader(); - return next_extension; + return serializer.size(); } + {%- endfor %} {% if package != None %}