From 984d8b143ed35619ff30a41abcd78cc1078ac807 Mon Sep 17 00:00:00 2001 From: Drew Galbraith Date: Thu, 11 Jan 2024 20:47:39 -0800 Subject: [PATCH] [Yunq] Move message parser to shared internal function. --- sys/denali/lib/denali/denali.yunq.cpp | 45 +++------ sys/denali/lib/denali/denali.yunq.h | 7 ++ .../lib/victoriafalls/victoriafalls.yunq.cpp | 48 ++++------ .../lib/victoriafalls/victoriafalls.yunq.h | 9 ++ .../lib/voyageurs/voyageurs.yunq.cpp | 11 +-- sys/voyageurs/lib/voyageurs/voyageurs.yunq.h | 3 + .../lib/yellowstone/yellowstone.yunq.cpp | 96 ++++++------------- .../lib/yellowstone/yellowstone.yunq.h | 13 +++ yunq/message.cpp.jinja | 29 +----- yunq/message.h.jinja | 3 + 10 files changed, 109 insertions(+), 155 deletions(-) diff --git a/sys/denali/lib/denali/denali.yunq.cpp b/sys/denali/lib/denali/denali.yunq.cpp index 329db3d..5ba2a69 100644 --- a/sys/denali/lib/denali/denali.yunq.cpp +++ b/sys/denali/lib/denali/denali.yunq.cpp @@ -39,20 +39,15 @@ glcr::Status ReadRequest::ParseFromBytesInternal(const yunq::MessageView& messag uint64_t ReadRequest::SerializeToBytes(glcr::ByteBuffer& bytes, uint64_t offset) const { yunq::Serializer serializer(bytes, offset, 3); - // Write device_id. - serializer.WriteField(0, device_id_); - // Write lba. - serializer.WriteField(1, lba_); - // Write size. - serializer.WriteField(2, size_); - - serializer.WriteHeader(); - - return serializer.size(); + return SerializeInternal(serializer); } uint64_t ReadRequest::SerializeToBytes(glcr::ByteBuffer& bytes, uint64_t offset, glcr::CapBuffer& caps) const { yunq::Serializer serializer(bytes, offset, 3, caps); + return SerializeInternal(serializer); +} + +uint64_t ReadRequest::SerializeInternal(yunq::Serializer& serializer) const { // Write device_id. serializer.WriteField(0, device_id_); // Write lba. @@ -90,20 +85,15 @@ glcr::Status ReadManyRequest::ParseFromBytesInternal(const yunq::MessageView& me uint64_t ReadManyRequest::SerializeToBytes(glcr::ByteBuffer& bytes, uint64_t offset) const { yunq::Serializer serializer(bytes, offset, 3); - // Write device_id. - serializer.WriteField(0, device_id_); - // Write lba. - serializer.WriteRepeated(1, lba_); - // Write sector_cnt. - serializer.WriteRepeated(2, sector_cnt_); - - serializer.WriteHeader(); - - return serializer.size(); + return SerializeInternal(serializer); } uint64_t ReadManyRequest::SerializeToBytes(glcr::ByteBuffer& bytes, uint64_t offset, glcr::CapBuffer& caps) const { yunq::Serializer serializer(bytes, offset, 3, caps); + return SerializeInternal(serializer); +} + +uint64_t ReadManyRequest::SerializeInternal(yunq::Serializer& serializer) const { // Write device_id. serializer.WriteField(0, device_id_); // Write lba. @@ -142,20 +132,15 @@ glcr::Status ReadResponse::ParseFromBytesInternal(const yunq::MessageView& messa uint64_t ReadResponse::SerializeToBytes(glcr::ByteBuffer& bytes, uint64_t offset) const { yunq::Serializer serializer(bytes, offset, 3); - // Write device_id. - serializer.WriteField(0, device_id_); - // Write size. - serializer.WriteField(1, size_); - // Write memory. - serializer.WriteCapability(2, memory_); - - serializer.WriteHeader(); - - return serializer.size(); + return SerializeInternal(serializer); } uint64_t ReadResponse::SerializeToBytes(glcr::ByteBuffer& bytes, uint64_t offset, glcr::CapBuffer& caps) const { yunq::Serializer serializer(bytes, offset, 3, caps); + return SerializeInternal(serializer); +} + +uint64_t ReadResponse::SerializeInternal(yunq::Serializer& serializer) const { // Write device_id. serializer.WriteField(0, device_id_); // Write size. diff --git a/sys/denali/lib/denali/denali.yunq.h b/sys/denali/lib/denali/denali.yunq.h index a32d782..d4b093c 100644 --- a/sys/denali/lib/denali/denali.yunq.h +++ b/sys/denali/lib/denali/denali.yunq.h @@ -7,6 +7,7 @@ #include #include #include +#include #include @@ -35,6 +36,8 @@ class ReadRequest { // Parses everything except for caps. glcr::Status ParseFromBytesInternal(const yunq::MessageView& message); + + uint64_t SerializeInternal(yunq::Serializer& serializer) const; }; class ReadManyRequest { public: @@ -61,6 +64,8 @@ class ReadManyRequest { // Parses everything except for caps. glcr::Status ParseFromBytesInternal(const yunq::MessageView& message); + + uint64_t SerializeInternal(yunq::Serializer& serializer) const; }; class ReadResponse { public: @@ -87,5 +92,7 @@ class ReadResponse { // Parses everything except for caps. glcr::Status ParseFromBytesInternal(const yunq::MessageView& message); + + uint64_t SerializeInternal(yunq::Serializer& serializer) const; }; diff --git a/sys/victoriafalls/lib/victoriafalls/victoriafalls.yunq.cpp b/sys/victoriafalls/lib/victoriafalls/victoriafalls.yunq.cpp index a529810..21fea87 100644 --- a/sys/victoriafalls/lib/victoriafalls/victoriafalls.yunq.cpp +++ b/sys/victoriafalls/lib/victoriafalls/victoriafalls.yunq.cpp @@ -35,16 +35,15 @@ glcr::Status OpenFileRequest::ParseFromBytesInternal(const yunq::MessageView& me uint64_t OpenFileRequest::SerializeToBytes(glcr::ByteBuffer& bytes, uint64_t offset) const { yunq::Serializer serializer(bytes, offset, 1); - // Write path. - serializer.WriteField(0, path_); - - serializer.WriteHeader(); - - return serializer.size(); + return SerializeInternal(serializer); } uint64_t OpenFileRequest::SerializeToBytes(glcr::ByteBuffer& bytes, uint64_t offset, glcr::CapBuffer& caps) const { yunq::Serializer serializer(bytes, offset, 1, caps); + return SerializeInternal(serializer); +} + +uint64_t OpenFileRequest::SerializeInternal(yunq::Serializer& serializer) const { // Write path. serializer.WriteField(0, path_); @@ -79,20 +78,15 @@ glcr::Status OpenFileResponse::ParseFromBytesInternal(const yunq::MessageView& m uint64_t OpenFileResponse::SerializeToBytes(glcr::ByteBuffer& bytes, uint64_t offset) const { yunq::Serializer serializer(bytes, offset, 3); - // Write path. - serializer.WriteField(0, path_); - // Write size. - serializer.WriteField(1, size_); - // Write memory. - serializer.WriteCapability(2, memory_); - - serializer.WriteHeader(); - - return serializer.size(); + return SerializeInternal(serializer); } uint64_t OpenFileResponse::SerializeToBytes(glcr::ByteBuffer& bytes, uint64_t offset, glcr::CapBuffer& caps) const { yunq::Serializer serializer(bytes, offset, 3, caps); + return SerializeInternal(serializer); +} + +uint64_t OpenFileResponse::SerializeInternal(yunq::Serializer& serializer) const { // Write path. serializer.WriteField(0, path_); // Write size. @@ -124,16 +118,15 @@ glcr::Status GetDirectoryRequest::ParseFromBytesInternal(const yunq::MessageView uint64_t GetDirectoryRequest::SerializeToBytes(glcr::ByteBuffer& bytes, uint64_t offset) const { yunq::Serializer serializer(bytes, offset, 1); - // Write path. - serializer.WriteField(0, path_); - - serializer.WriteHeader(); - - return serializer.size(); + return SerializeInternal(serializer); } uint64_t GetDirectoryRequest::SerializeToBytes(glcr::ByteBuffer& bytes, uint64_t offset, glcr::CapBuffer& caps) const { yunq::Serializer serializer(bytes, offset, 1, caps); + return SerializeInternal(serializer); +} + +uint64_t GetDirectoryRequest::SerializeInternal(yunq::Serializer& serializer) const { // Write path. serializer.WriteField(0, path_); @@ -161,16 +154,15 @@ glcr::Status Directory::ParseFromBytesInternal(const yunq::MessageView& message) uint64_t Directory::SerializeToBytes(glcr::ByteBuffer& bytes, uint64_t offset) const { yunq::Serializer serializer(bytes, offset, 1); - // Write filenames. - serializer.WriteField(0, filenames_); - - serializer.WriteHeader(); - - return serializer.size(); + return SerializeInternal(serializer); } uint64_t Directory::SerializeToBytes(glcr::ByteBuffer& bytes, uint64_t offset, glcr::CapBuffer& caps) const { yunq::Serializer serializer(bytes, offset, 1, caps); + return SerializeInternal(serializer); +} + +uint64_t Directory::SerializeInternal(yunq::Serializer& serializer) const { // Write filenames. serializer.WriteField(0, filenames_); diff --git a/sys/victoriafalls/lib/victoriafalls/victoriafalls.yunq.h b/sys/victoriafalls/lib/victoriafalls/victoriafalls.yunq.h index 5a294fc..aaf87c9 100644 --- a/sys/victoriafalls/lib/victoriafalls/victoriafalls.yunq.h +++ b/sys/victoriafalls/lib/victoriafalls/victoriafalls.yunq.h @@ -7,6 +7,7 @@ #include #include #include +#include #include @@ -29,6 +30,8 @@ class OpenFileRequest { // Parses everything except for caps. glcr::Status ParseFromBytesInternal(const yunq::MessageView& message); + + uint64_t SerializeInternal(yunq::Serializer& serializer) const; }; class OpenFileResponse { public: @@ -55,6 +58,8 @@ class OpenFileResponse { // Parses everything except for caps. glcr::Status ParseFromBytesInternal(const yunq::MessageView& message); + + uint64_t SerializeInternal(yunq::Serializer& serializer) const; }; class GetDirectoryRequest { public: @@ -75,6 +80,8 @@ class GetDirectoryRequest { // Parses everything except for caps. glcr::Status ParseFromBytesInternal(const yunq::MessageView& message); + + uint64_t SerializeInternal(yunq::Serializer& serializer) const; }; class Directory { public: @@ -95,5 +102,7 @@ class Directory { // Parses everything except for caps. glcr::Status ParseFromBytesInternal(const yunq::MessageView& message); + + uint64_t SerializeInternal(yunq::Serializer& serializer) const; }; diff --git a/sys/voyageurs/lib/voyageurs/voyageurs.yunq.cpp b/sys/voyageurs/lib/voyageurs/voyageurs.yunq.cpp index 9a69f66..1ad84f2 100644 --- a/sys/voyageurs/lib/voyageurs/voyageurs.yunq.cpp +++ b/sys/voyageurs/lib/voyageurs/voyageurs.yunq.cpp @@ -38,16 +38,15 @@ glcr::Status KeyboardListener::ParseFromBytesInternal(const yunq::MessageView& m uint64_t KeyboardListener::SerializeToBytes(glcr::ByteBuffer& bytes, uint64_t offset) const { yunq::Serializer serializer(bytes, offset, 1); - // Write port_capability. - serializer.WriteCapability(0, port_capability_); - - serializer.WriteHeader(); - - return serializer.size(); + return SerializeInternal(serializer); } uint64_t KeyboardListener::SerializeToBytes(glcr::ByteBuffer& bytes, uint64_t offset, glcr::CapBuffer& caps) const { yunq::Serializer serializer(bytes, offset, 1, caps); + return SerializeInternal(serializer); +} + +uint64_t KeyboardListener::SerializeInternal(yunq::Serializer& serializer) const { // Write port_capability. serializer.WriteCapability(0, port_capability_); diff --git a/sys/voyageurs/lib/voyageurs/voyageurs.yunq.h b/sys/voyageurs/lib/voyageurs/voyageurs.yunq.h index 727b31b..2b2c618 100644 --- a/sys/voyageurs/lib/voyageurs/voyageurs.yunq.h +++ b/sys/voyageurs/lib/voyageurs/voyageurs.yunq.h @@ -7,6 +7,7 @@ #include #include #include +#include #include @@ -29,5 +30,7 @@ class KeyboardListener { // Parses everything except for caps. glcr::Status ParseFromBytesInternal(const yunq::MessageView& message); + + uint64_t SerializeInternal(yunq::Serializer& serializer) const; }; diff --git a/sys/yellowstone/lib/yellowstone/yellowstone.yunq.cpp b/sys/yellowstone/lib/yellowstone/yellowstone.yunq.cpp index 33a5f0d..78827d7 100644 --- a/sys/yellowstone/lib/yellowstone/yellowstone.yunq.cpp +++ b/sys/yellowstone/lib/yellowstone/yellowstone.yunq.cpp @@ -42,18 +42,15 @@ glcr::Status RegisterEndpointRequest::ParseFromBytesInternal(const yunq::Message uint64_t RegisterEndpointRequest::SerializeToBytes(glcr::ByteBuffer& bytes, uint64_t offset) const { yunq::Serializer serializer(bytes, offset, 2); - // Write endpoint_name. - serializer.WriteField(0, endpoint_name_); - // Write endpoint_capability. - serializer.WriteCapability(1, endpoint_capability_); - - serializer.WriteHeader(); - - return serializer.size(); + return SerializeInternal(serializer); } uint64_t RegisterEndpointRequest::SerializeToBytes(glcr::ByteBuffer& bytes, uint64_t offset, glcr::CapBuffer& caps) const { yunq::Serializer serializer(bytes, offset, 2, caps); + return SerializeInternal(serializer); +} + +uint64_t RegisterEndpointRequest::SerializeInternal(yunq::Serializer& serializer) const { // Write endpoint_name. serializer.WriteField(0, endpoint_name_); // Write endpoint_capability. @@ -83,16 +80,15 @@ glcr::Status GetEndpointRequest::ParseFromBytesInternal(const yunq::MessageView& uint64_t GetEndpointRequest::SerializeToBytes(glcr::ByteBuffer& bytes, uint64_t offset) const { yunq::Serializer serializer(bytes, offset, 1); - // Write endpoint_name. - serializer.WriteField(0, endpoint_name_); - - serializer.WriteHeader(); - - return serializer.size(); + return SerializeInternal(serializer); } uint64_t GetEndpointRequest::SerializeToBytes(glcr::ByteBuffer& bytes, uint64_t offset, glcr::CapBuffer& caps) const { yunq::Serializer serializer(bytes, offset, 1, caps); + return SerializeInternal(serializer); +} + +uint64_t GetEndpointRequest::SerializeInternal(yunq::Serializer& serializer) const { // Write endpoint_name. serializer.WriteField(0, endpoint_name_); @@ -123,16 +119,15 @@ glcr::Status Endpoint::ParseFromBytesInternal(const yunq::MessageView& message) uint64_t Endpoint::SerializeToBytes(glcr::ByteBuffer& bytes, uint64_t offset) const { yunq::Serializer serializer(bytes, offset, 1); - // Write endpoint. - serializer.WriteCapability(0, endpoint_); - - serializer.WriteHeader(); - - return serializer.size(); + return SerializeInternal(serializer); } uint64_t Endpoint::SerializeToBytes(glcr::ByteBuffer& bytes, uint64_t offset, glcr::CapBuffer& caps) const { yunq::Serializer serializer(bytes, offset, 1, caps); + return SerializeInternal(serializer); +} + +uint64_t Endpoint::SerializeInternal(yunq::Serializer& serializer) const { // Write endpoint. serializer.WriteCapability(0, endpoint_); @@ -165,18 +160,15 @@ glcr::Status AhciInfo::ParseFromBytesInternal(const yunq::MessageView& message) uint64_t AhciInfo::SerializeToBytes(glcr::ByteBuffer& bytes, uint64_t offset) const { yunq::Serializer serializer(bytes, offset, 2); - // Write ahci_region. - serializer.WriteCapability(0, ahci_region_); - // Write region_length. - serializer.WriteField(1, region_length_); - - serializer.WriteHeader(); - - return serializer.size(); + return SerializeInternal(serializer); } uint64_t AhciInfo::SerializeToBytes(glcr::ByteBuffer& bytes, uint64_t offset, glcr::CapBuffer& caps) const { yunq::Serializer serializer(bytes, offset, 2, caps); + return SerializeInternal(serializer); +} + +uint64_t AhciInfo::SerializeInternal(yunq::Serializer& serializer) const { // Write ahci_region. serializer.WriteCapability(0, ahci_region_); // Write region_length. @@ -228,38 +220,15 @@ glcr::Status FramebufferInfo::ParseFromBytesInternal(const yunq::MessageView& me uint64_t FramebufferInfo::SerializeToBytes(glcr::ByteBuffer& bytes, uint64_t offset) const { yunq::Serializer serializer(bytes, offset, 12); - // Write address_phys. - serializer.WriteField(0, address_phys_); - // Write width. - serializer.WriteField(1, width_); - // Write height. - serializer.WriteField(2, height_); - // Write pitch. - serializer.WriteField(3, pitch_); - // Write bpp. - serializer.WriteField(4, bpp_); - // Write memory_model. - serializer.WriteField(5, memory_model_); - // Write red_mask_size. - serializer.WriteField(6, red_mask_size_); - // Write red_mask_shift. - serializer.WriteField(7, red_mask_shift_); - // Write green_mask_size. - serializer.WriteField(8, green_mask_size_); - // Write green_mask_shift. - serializer.WriteField(9, green_mask_shift_); - // Write blue_mask_size. - serializer.WriteField(10, blue_mask_size_); - // Write blue_mask_shift. - serializer.WriteField(11, blue_mask_shift_); - - serializer.WriteHeader(); - - return serializer.size(); + return SerializeInternal(serializer); } uint64_t FramebufferInfo::SerializeToBytes(glcr::ByteBuffer& bytes, uint64_t offset, glcr::CapBuffer& caps) const { yunq::Serializer serializer(bytes, offset, 12, caps); + return SerializeInternal(serializer); +} + +uint64_t FramebufferInfo::SerializeInternal(yunq::Serializer& serializer) const { // Write address_phys. serializer.WriteField(0, address_phys_); // Write width. @@ -316,20 +285,15 @@ glcr::Status DenaliInfo::ParseFromBytesInternal(const yunq::MessageView& message uint64_t DenaliInfo::SerializeToBytes(glcr::ByteBuffer& bytes, uint64_t offset) const { yunq::Serializer serializer(bytes, offset, 3); - // Write denali_endpoint. - serializer.WriteCapability(0, denali_endpoint_); - // Write device_id. - serializer.WriteField(1, device_id_); - // Write lba_offset. - serializer.WriteField(2, lba_offset_); - - serializer.WriteHeader(); - - return serializer.size(); + return SerializeInternal(serializer); } uint64_t DenaliInfo::SerializeToBytes(glcr::ByteBuffer& bytes, uint64_t offset, glcr::CapBuffer& caps) const { yunq::Serializer serializer(bytes, offset, 3, caps); + return SerializeInternal(serializer); +} + +uint64_t DenaliInfo::SerializeInternal(yunq::Serializer& serializer) const { // Write denali_endpoint. serializer.WriteCapability(0, denali_endpoint_); // Write device_id. diff --git a/sys/yellowstone/lib/yellowstone/yellowstone.yunq.h b/sys/yellowstone/lib/yellowstone/yellowstone.yunq.h index c69b5a2..93f05e8 100644 --- a/sys/yellowstone/lib/yellowstone/yellowstone.yunq.h +++ b/sys/yellowstone/lib/yellowstone/yellowstone.yunq.h @@ -7,6 +7,7 @@ #include #include #include +#include #include @@ -34,6 +35,8 @@ class RegisterEndpointRequest { // Parses everything except for caps. glcr::Status ParseFromBytesInternal(const yunq::MessageView& message); + + uint64_t SerializeInternal(yunq::Serializer& serializer) const; }; class GetEndpointRequest { public: @@ -54,6 +57,8 @@ class GetEndpointRequest { // Parses everything except for caps. glcr::Status ParseFromBytesInternal(const yunq::MessageView& message); + + uint64_t SerializeInternal(yunq::Serializer& serializer) const; }; class Endpoint { public: @@ -74,6 +79,8 @@ class Endpoint { // Parses everything except for caps. glcr::Status ParseFromBytesInternal(const yunq::MessageView& message); + + uint64_t SerializeInternal(yunq::Serializer& serializer) const; }; class AhciInfo { public: @@ -97,6 +104,8 @@ class AhciInfo { // Parses everything except for caps. glcr::Status ParseFromBytesInternal(const yunq::MessageView& message); + + uint64_t SerializeInternal(yunq::Serializer& serializer) const; }; class FramebufferInfo { public: @@ -150,6 +159,8 @@ class FramebufferInfo { // Parses everything except for caps. glcr::Status ParseFromBytesInternal(const yunq::MessageView& message); + + uint64_t SerializeInternal(yunq::Serializer& serializer) const; }; class DenaliInfo { public: @@ -176,6 +187,8 @@ class DenaliInfo { // Parses everything except for caps. glcr::Status ParseFromBytesInternal(const yunq::MessageView& message); + + uint64_t SerializeInternal(yunq::Serializer& serializer) const; }; diff --git a/yunq/message.cpp.jinja b/yunq/message.cpp.jinja index 7666bf4..dba366a 100644 --- a/yunq/message.cpp.jinja +++ b/yunq/message.cpp.jinja @@ -74,36 +74,15 @@ glcr::Status {{message.name}}::ParseFromBytesInternal(const yunq::MessageView& m uint64_t {{message.name}}::SerializeToBytes(glcr::ByteBuffer& bytes, uint64_t offset) const { yunq::Serializer serializer(bytes, offset, {{ message.fields | length }}); - -{%- for field in message.fields %} - // Write {{field.name}}. -{%- if not field.repeated %} - -{%- if field.type != Type.CAPABILITY %} - serializer.WriteField<{{field.cpp_type()}}>({{loop.index0}}, {{field.name}}_); -{%- else %} - serializer.WriteCapability({{loop.index0}}, {{field.name}}_); -{%- endif %} - -{%- else %} - -{%- if field.type != Type.CAPABILITY %} - serializer.WriteRepeated<{{field.cpp_type()}}>({{loop.index0}}, {{field.name}}_); -{%- else %} - serializer.WriteRepeatedCapability({{loop.index0}}, {{field.name}}_); -{%- endif %} - -{%- endif %} -{%- endfor %} - - serializer.WriteHeader(); - - return serializer.size(); + return SerializeInternal(serializer); } uint64_t {{message.name}}::SerializeToBytes(glcr::ByteBuffer& bytes, uint64_t offset, glcr::CapBuffer& caps) const { yunq::Serializer serializer(bytes, offset, {{ message.fields | length }}, caps); + return SerializeInternal(serializer); +} +uint64_t {{message.name}}::SerializeInternal(yunq::Serializer& serializer) const { {%- for field in message.fields %} // Write {{field.name}}. {%- if not field.repeated %} diff --git a/yunq/message.h.jinja b/yunq/message.h.jinja index 79d47cd..814ce81 100644 --- a/yunq/message.h.jinja +++ b/yunq/message.h.jinja @@ -7,6 +7,7 @@ #include #include #include +#include #include {% if package != None %} @@ -47,6 +48,8 @@ class {{message.name}} { // Parses everything except for caps. glcr::Status ParseFromBytesInternal(const yunq::MessageView& message); + + uint64_t SerializeInternal(yunq::Serializer& serializer) const; }; {%- endfor %}