[Yunq] Add repeated field parsing and serialization.

This commit is contained in:
Drew Galbraith 2023-11-10 12:26:59 -08:00
parent 0e99189dba
commit 0dacbb87dc
7 changed files with 127 additions and 21 deletions

View file

@ -4,7 +4,7 @@ interface VFS {
message OpenFileRequest {
string path;
u64 options;
repeated u64 options;
}
message File {

View file

@ -34,7 +34,15 @@ void OpenFileRequest::ParseFromBytes(const glcr::ByteBuffer& bytes, uint64_t off
set_path(bytes.StringAt(offset + path_pointer.offset, path_pointer.length));
// Parse options.
set_options(bytes.At<uint64_t>(offset + header_size + (8 * 1)));
auto options_pointer = bytes.At<ExtPointer>(offset + header_size + (8 * 1))
options_.Resize(options_pointer.length);
for (uint64_t i = offset + options_pointer.offset;
i < offset + options_pointer.offset + (sizeof(uint64_t) * options_pointer.length);
i += sizeof(uint64_t)) {
options_.PushBack(bytes.At<uint64_t>(i));
}
}
void OpenFileRequest::ParseFromBytes(const glcr::ByteBuffer& bytes, uint64_t offset, const glcr::CapBuffer& caps) {
@ -44,7 +52,15 @@ void OpenFileRequest::ParseFromBytes(const glcr::ByteBuffer& bytes, uint64_t off
set_path(bytes.StringAt(offset + path_pointer.offset, path_pointer.length));
// Parse options.
set_options(bytes.At<uint64_t>(offset + header_size + (8 * 1)));
auto options_pointer = bytes.At<ExtPointer>(offset + header_size + (8 * 1))
options_.Resize(options_pointer.length);
for (uint64_t i = offset + options_pointer.offset;
i < offset + options_pointer.offset + (sizeof(uint64_t) * options_pointer.length);
i += sizeof(uint64_t)) {
options_.PushBack(bytes.At<uint64_t>(i));
}
}
uint64_t OpenFileRequest::SerializeToBytes(glcr::ByteBuffer& bytes, uint64_t offset) const {
@ -62,7 +78,18 @@ uint64_t OpenFileRequest::SerializeToBytes(glcr::ByteBuffer& bytes, uint64_t off
bytes.WriteAt<ExtPointer>(offset + header_size + (8 * 0), path_ptr);
// Write options.
bytes.WriteAt<uint64_t>(offset + header_size + (8 * 1), options());
ExtPointer options_ptr{
.offset = next_extension,
.length = (uint32_t)(options().size() * sizeof(uint64_t)),
};
next_extension += options_ptr.length;
bytes.WriteAt<ExtPointer>(offset + header_size + (8 * 1), options_ptr);
for (uint64_t i = 0; i < options().size(); i++) {
uint32_t ext_offset = offset + options_ptr.offset + (i * sizeof(uint64_t));
bytes.WriteAt<uint64_t>(ext_offset, options().at(i));
}
// The next extension pointer is the length of the message.
WriteHeader(bytes, offset, core_size, next_extension);
@ -86,7 +113,18 @@ uint64_t OpenFileRequest::SerializeToBytes(glcr::ByteBuffer& bytes, uint64_t off
bytes.WriteAt<ExtPointer>(offset + header_size + (8 * 0), path_ptr);
// Write options.
bytes.WriteAt<uint64_t>(offset + header_size + (8 * 1), options());
ExtPointer options_ptr{
.offset = next_extension,
.length = (uint32_t)(options().size() * sizeof(uint64_t)),
};
next_extension += options_ptr.length;
bytes.WriteAt<ExtPointer>(offset + header_size + (8 * 1), options_ptr);
for (uint64_t i = 0; i < options().size(); i++) {
uint32_t ext_offset = offset + options_ptr.offset + (i * sizeof(uint64_t));
bytes.WriteAt<uint64_t>(ext_offset, options().at(i));
}
// The next extension pointer is the length of the message.
WriteHeader(bytes, offset, core_size, next_extension);

View file

@ -3,6 +3,7 @@
#include <glacier/buffer/byte_buffer.h>
#include <glacier/buffer/cap_buffer.h>
#include <glacier/container/vector.h>
#include <glacier/string/string.h>
#include <ztypes.h>
class OpenFileRequest {
@ -15,15 +16,15 @@ class OpenFileRequest {
void ParseFromBytes(const glcr::ByteBuffer&, uint64_t offset);
void ParseFromBytes(const glcr::ByteBuffer&, uint64_t offset, const glcr::CapBuffer&);
uint64_t SerializeToBytes(glcr::ByteBuffer&, uint64_t offset) const;
uint64_t SerializeToBytes(glcr::ByteBuffer&, uint64_t offset, glcr::CapBuffer&) const;
uint64_t SerializeToBytes(glcr::ByteBuffer&, uint64_t offset, glcr::CapBuffer&) const;
glcr::String path() const { return path_; }
void set_path(const glcr::String& value) { path_ = value; }
uint64_t options() const { return options_; }
void set_options(const uint64_t& value) { options_ = value; }
const glcr::Vector<uint64_t>& options() const { return options_; }
void add_options(const uint64_t& value) { options_.PushBack(value); }
private:
glcr::String path_;
uint64_t options_;
glcr::Vector<uint64_t> options_;
};
class File {
@ -36,11 +37,11 @@ class File {
void ParseFromBytes(const glcr::ByteBuffer&, uint64_t offset);
void ParseFromBytes(const glcr::ByteBuffer&, uint64_t offset, const glcr::CapBuffer&);
uint64_t SerializeToBytes(glcr::ByteBuffer&, uint64_t offset) const;
uint64_t SerializeToBytes(glcr::ByteBuffer&, uint64_t offset, glcr::CapBuffer&) const;
uint64_t SerializeToBytes(glcr::ByteBuffer&, uint64_t offset, glcr::CapBuffer&) const;
glcr::String path() const { return path_; }
void set_path(const glcr::String& value) { path_ = value; }
void set_path(const glcr::String& value) { path_ = value; }
uint64_t attrs() const { return attrs_; }
void set_attrs(const uint64_t& value) { attrs_ = value; }
void set_attrs(const uint64_t& value) { attrs_ = value; }
z_cap_t mem_cap() const { return mem_cap_; }
void set_mem_cap(const z_cap_t& value) { mem_cap_ = value; }

View file

@ -31,8 +31,7 @@ void VFSServerBaseThreadBootstrap(void* server_base) {
glcr::ErrorOr<VFSClient> VFSServerBase::CreateClient() {
uint64_t client_cap;
// FIXME: Restrict permissions to send-only here.
RET_ERR(ZCapDuplicate(endpoint_, &client_cap));
RET_ERR(ZCapDuplicate(endpoint_, ~(kZionPerm_Read), &client_cap));
return VFSClient(client_cap);
}
@ -51,9 +50,9 @@ void VFSServerBase::ServerThread() {
uint64_t recv_cap_size = 0x10;
uint64_t recv_buf_size = 0x1000;
recv_cap.Reset();
glcr::ErrorCode recv_err = ZEndpointRecv(endpoint_, &recv_buf_size, recv_buffer.RawPtr(), &recv_cap_size, recv_cap.RawPtr(), &reply_port_cap);
glcr::ErrorCode recv_err = static_cast<glcr::ErrorCode>(ZEndpointRecv(endpoint_, &recv_buf_size, recv_buffer.RawPtr(), &recv_cap_size, recv_cap.RawPtr(), &reply_port_cap));
if (recv_err != glcr::OK) {
dbgln("Error in receive: %x", recv_err);
dbgln("Error in receive: {x}", recv_err);
continue;
}
@ -64,13 +63,13 @@ void VFSServerBase::ServerThread() {
glcr::ErrorCode err = HandleRequest(recv_buffer, recv_cap, resp_buffer, resp_length, resp_cap);
if (err != glcr::OK) {
WriteError(resp_buffer, err);
reply_err = ZReplyPortSend(reply_port_cap, kHeaderSize, resp_buffer.RawPtr(), 0, nullptr);
reply_err = static_cast<glcr::ErrorCode>(ZReplyPortSend(reply_port_cap, kHeaderSize, resp_buffer.RawPtr(), 0, nullptr));
} else {
WriteHeader(resp_buffer, resp_length);
reply_err = ZReplyPortSend(reply_port_cap, kHeaderSize + resp_length, resp_buffer.RawPtr(), resp_cap.UsedSlots(), resp_cap.RawPtr());
reply_err = static_cast<glcr::ErrorCode>(ZReplyPortSend(reply_port_cap, kHeaderSize + resp_length, resp_buffer.RawPtr(), resp_cap.UsedSlots(), resp_cap.RawPtr()));
}
if (reply_err != glcr::OK) {
dbgln("Error in reply: %x", reply_err);
dbgln("Error in reply: {x}", reply_err);
}
}