From 700f3f94cb20189d82bcd9b1bfd20e3b139d499a Mon Sep 17 00:00:00 2001 From: Drew Galbraith Date: Fri, 1 Dec 2023 10:23:54 -0800 Subject: [PATCH] [Yunq] Return status in server code. --- lib/glacier/string/str_format.cpp | 6 +++ lib/glacier/string/str_format.h | 3 ++ sys/denali/denali_server.cpp | 14 +++---- sys/denali/denali_server.h | 7 ++-- sys/denali/lib/denali/denali.yunq.server.cpp | 35 +++++++--------- sys/denali/lib/denali/denali.yunq.server.h | 11 ++--- .../victoriafalls.yunq.server.cpp | 34 +++++++--------- .../victoriafalls/victoriafalls.yunq.server.h | 11 ++--- sys/victoriafalls/victoriafalls_server.cpp | 31 +++++++------- sys/victoriafalls/victoriafalls_server.h | 8 ++-- .../lib/voyageurs/voyageurs.yunq.server.cpp | 27 ++++++------- .../lib/voyageurs/voyageurs.yunq.server.h | 9 +++-- sys/voyageurs/voyageurs_server.cpp | 4 +- sys/voyageurs/voyageurs_server.h | 2 +- .../yellowstone/yellowstone.yunq.server.cpp | 40 ++++++++----------- .../lib/yellowstone/yellowstone.yunq.server.h | 17 ++++---- sys/yellowstone/yellowstone_server.cpp | 27 +++++++------ sys/yellowstone/yellowstone_server.h | 12 +++--- yunq/server.cpp.jinja | 33 +++++++-------- yunq/server.h.jinja | 13 +++--- 20 files changed, 166 insertions(+), 178 deletions(-) diff --git a/lib/glacier/string/str_format.cpp b/lib/glacier/string/str_format.cpp index 68e5927..51239c7 100644 --- a/lib/glacier/string/str_format.cpp +++ b/lib/glacier/string/str_format.cpp @@ -72,6 +72,12 @@ void StrFormatValue(StringBuilder& builder, char const* const& value, StrFormatInternal(builder, StringView(value)); } +template <> +void StrFormatValue(StringBuilder& builder, const String& value, + StringView opts) { + StrFormatInternal(builder, value); +} + template <> void StrFormatValue(StringBuilder& builder, const StringView& value, StringView opts) { diff --git a/lib/glacier/string/str_format.h b/lib/glacier/string/str_format.h index 1d0c907..6d1ab90 100644 --- a/lib/glacier/string/str_format.h +++ b/lib/glacier/string/str_format.h @@ -44,6 +44,9 @@ void StrFormatValue(StringBuilder& builder, char const* const& value, StringView opts); template <> +void StrFormatValue(StringBuilder& builder, const String& value, + StringView opts); +template <> void StrFormatValue(StringBuilder& builder, const StringView& value, StringView opts); diff --git a/sys/denali/denali_server.cpp b/sys/denali/denali_server.cpp index 330fdc7..0c397c4 100644 --- a/sys/denali/denali_server.cpp +++ b/sys/denali/denali_server.cpp @@ -12,8 +12,8 @@ glcr::ErrorOr> DenaliServer::Create( return glcr::UniquePtr(new DenaliServer(cap, driver)); } -glcr::ErrorCode DenaliServer::HandleRead(const ReadRequest& req, - ReadResponse& resp) { +glcr::Status DenaliServer::HandleRead(const ReadRequest& req, + ReadResponse& resp) { ASSIGN_OR_RETURN(AhciDevice * device, driver_.GetDevice(req.device_id())); uint64_t paddr; @@ -28,15 +28,15 @@ glcr::ErrorCode DenaliServer::HandleRead(const ReadRequest& req, resp.set_device_id(req.device_id()); resp.set_size(req.size()); resp.set_memory(region.DuplicateCap()); - return glcr::OK; + return glcr::Status::Ok(); } -glcr::ErrorCode DenaliServer::HandleReadMany(const ReadManyRequest& req, - ReadResponse& resp) { +glcr::Status DenaliServer::HandleReadMany(const ReadManyRequest& req, + ReadResponse& resp) { ASSIGN_OR_RETURN(AhciDevice * device, driver_.GetDevice(req.device_id())); if (req.lba().size() != req.sector_cnt().size()) { - return glcr::INVALID_ARGUMENT; + return glcr::InvalidArgument("LBA and Sector Cnt must be the same length."); } uint64_t sector_cnt = 0; @@ -60,5 +60,5 @@ glcr::ErrorCode DenaliServer::HandleReadMany(const ReadManyRequest& req, resp.set_device_id(req.device_id()); resp.set_size(sector_cnt); resp.set_memory(region.DuplicateCap()); - return glcr::OK; + return glcr::Status::Ok(); } diff --git a/sys/denali/denali_server.h b/sys/denali/denali_server.h index 26f63b7..483fabe 100644 --- a/sys/denali/denali_server.h +++ b/sys/denali/denali_server.h @@ -10,10 +10,9 @@ class DenaliServer : public DenaliServerBase { static glcr::ErrorOr> Create( AhciDriver& driver); - glcr::ErrorCode HandleRead(const ReadRequest& req, - ReadResponse& resp) override; - glcr::ErrorCode HandleReadMany(const ReadManyRequest& req, - ReadResponse& resp) override; + glcr::Status HandleRead(const ReadRequest& req, ReadResponse& resp) override; + glcr::Status HandleReadMany(const ReadManyRequest& req, + ReadResponse& resp) override; private: static const uint64_t kBuffSize = 1024; diff --git a/sys/denali/lib/denali/denali.yunq.server.cpp b/sys/denali/lib/denali/denali.yunq.server.cpp index 88c9fa1..30323e3 100644 --- a/sys/denali/lib/denali/denali.yunq.server.cpp +++ b/sys/denali/lib/denali/denali.yunq.server.cpp @@ -73,9 +73,10 @@ void DenaliServerBase::ServerThread() { glcr::ErrorCode reply_err = glcr::OK; resp_cap.Reset(); - glcr::ErrorCode err = HandleRequest(recv_buffer, recv_cap, resp_buffer, resp_length, resp_cap); - if (err != glcr::OK) { - WriteError(resp_buffer, err); + glcr::Status err = HandleRequest(recv_buffer, recv_cap, resp_buffer, resp_length, resp_cap); + if (!err) { + WriteError(resp_buffer, err.code()); + dbgln("Responding Error {}", err.message()); reply_err = static_cast(ZReplyPortSend(reply_port_cap, kHeaderSize, resp_buffer.RawPtr(), 0, nullptr)); } else { WriteHeader(resp_buffer, resp_length); @@ -88,12 +89,12 @@ void DenaliServerBase::ServerThread() { } -glcr::ErrorCode DenaliServerBase::HandleRequest(const glcr::ByteBuffer& request, - const glcr::CapBuffer& req_caps, - glcr::ByteBuffer& response, uint64_t& resp_length, - glcr::CapBuffer& resp_caps) { +glcr::Status DenaliServerBase::HandleRequest(const glcr::ByteBuffer& request, + const glcr::CapBuffer& req_caps, + glcr::ByteBuffer& response, uint64_t& resp_length, + glcr::CapBuffer& resp_caps) { if (request.At(0) != kSentinel) { - return glcr::INVALID_ARGUMENT; + return glcr::InvalidArgument("Request Not Valid"); } uint64_t method_select = request.At(8); @@ -104,10 +105,7 @@ glcr::ErrorCode DenaliServerBase::HandleRequest(const glcr::ByteBuffer& request, ReadRequest yunq_request; // TODO: Return status. - auto status = yunq_request.ParseFromBytes(request, kHeaderSize, req_caps); - if (!status) { - return status.code(); - } + RETURN_ERROR(yunq_request.ParseFromBytes(request, kHeaderSize, req_caps)); @@ -115,7 +113,7 @@ glcr::ErrorCode DenaliServerBase::HandleRequest(const glcr::ByteBuffer& request, - RET_ERR(HandleRead(yunq_request, yunq_response)); + RETURN_ERROR(HandleRead(yunq_request, yunq_response)); @@ -128,10 +126,7 @@ glcr::ErrorCode DenaliServerBase::HandleRequest(const glcr::ByteBuffer& request, ReadManyRequest yunq_request; // TODO: Return status. - auto status = yunq_request.ParseFromBytes(request, kHeaderSize, req_caps); - if (!status) { - return status.code(); - } + RETURN_ERROR(yunq_request.ParseFromBytes(request, kHeaderSize, req_caps)); @@ -139,7 +134,7 @@ glcr::ErrorCode DenaliServerBase::HandleRequest(const glcr::ByteBuffer& request, - RET_ERR(HandleReadMany(yunq_request, yunq_response)); + RETURN_ERROR(HandleReadMany(yunq_request, yunq_response)); @@ -148,10 +143,10 @@ glcr::ErrorCode DenaliServerBase::HandleRequest(const glcr::ByteBuffer& request, break; } default: { - return glcr::UNIMPLEMENTED; + return glcr::Unimplemented("Method unimplemented by server."); } } - return glcr::OK; + return glcr::Status::Ok(); } diff --git a/sys/denali/lib/denali/denali.yunq.server.h b/sys/denali/lib/denali/denali.yunq.server.h index 241ec99..5dc12e3 100644 --- a/sys/denali/lib/denali/denali.yunq.server.h +++ b/sys/denali/lib/denali/denali.yunq.server.h @@ -2,6 +2,7 @@ #pragma once #include +#include #include #include @@ -26,11 +27,11 @@ class DenaliServerBase { - [[nodiscard]] virtual glcr::ErrorCode HandleRead(const ReadRequest&, ReadResponse&) = 0; + [[nodiscard]] virtual glcr::Status HandleRead(const ReadRequest&, ReadResponse&) = 0; - [[nodiscard]] virtual glcr::ErrorCode HandleReadMany(const ReadManyRequest&, ReadResponse&) = 0; + [[nodiscard]] virtual glcr::Status HandleReadMany(const ReadManyRequest&, ReadResponse&) = 0; @@ -40,9 +41,9 @@ class DenaliServerBase { friend void DenaliServerBaseThreadBootstrap(void*); void ServerThread(); - [[nodiscard]] glcr::ErrorCode HandleRequest(const glcr::ByteBuffer& request, const glcr::CapBuffer& req_caps, - glcr::ByteBuffer& response, uint64_t& resp_length, - glcr::CapBuffer& resp_caps); + [[nodiscard]] glcr::Status HandleRequest(const glcr::ByteBuffer& request, const glcr::CapBuffer& req_caps, + glcr::ByteBuffer& response, uint64_t& resp_length, + glcr::CapBuffer& resp_caps); }; diff --git a/sys/victoriafalls/lib/victoriafalls/victoriafalls.yunq.server.cpp b/sys/victoriafalls/lib/victoriafalls/victoriafalls.yunq.server.cpp index a948032..13df75a 100644 --- a/sys/victoriafalls/lib/victoriafalls/victoriafalls.yunq.server.cpp +++ b/sys/victoriafalls/lib/victoriafalls/victoriafalls.yunq.server.cpp @@ -73,9 +73,9 @@ void VFSServerBase::ServerThread() { glcr::ErrorCode reply_err = glcr::OK; resp_cap.Reset(); - glcr::ErrorCode err = HandleRequest(recv_buffer, recv_cap, resp_buffer, resp_length, resp_cap); - if (err != glcr::OK) { - WriteError(resp_buffer, err); + glcr::Status err = HandleRequest(recv_buffer, recv_cap, resp_buffer, resp_length, resp_cap); + if (!err) { + WriteError(resp_buffer, err.code()); reply_err = static_cast(ZReplyPortSend(reply_port_cap, kHeaderSize, resp_buffer.RawPtr(), 0, nullptr)); } else { WriteHeader(resp_buffer, resp_length); @@ -88,12 +88,12 @@ void VFSServerBase::ServerThread() { } -glcr::ErrorCode VFSServerBase::HandleRequest(const glcr::ByteBuffer& request, - const glcr::CapBuffer& req_caps, - glcr::ByteBuffer& response, uint64_t& resp_length, - glcr::CapBuffer& resp_caps) { +glcr::Status VFSServerBase::HandleRequest(const glcr::ByteBuffer& request, + const glcr::CapBuffer& req_caps, + glcr::ByteBuffer& response, uint64_t& resp_length, + glcr::CapBuffer& resp_caps) { if (request.At(0) != kSentinel) { - return glcr::INVALID_ARGUMENT; + return glcr::InvalidArgument("Request Not Valid"); } uint64_t method_select = request.At(8); @@ -104,10 +104,7 @@ glcr::ErrorCode VFSServerBase::HandleRequest(const glcr::ByteBuffer& request, OpenFileRequest yunq_request; // TODO: Return status. - auto status = yunq_request.ParseFromBytes(request, kHeaderSize, req_caps); - if (!status) { - return status.code(); - } + RETURN_ERROR(yunq_request.ParseFromBytes(request, kHeaderSize, req_caps)); @@ -115,7 +112,7 @@ glcr::ErrorCode VFSServerBase::HandleRequest(const glcr::ByteBuffer& request, - RET_ERR(HandleOpenFile(yunq_request, yunq_response)); + RETURN_ERROR(HandleOpenFile(yunq_request, yunq_response)); @@ -128,10 +125,7 @@ glcr::ErrorCode VFSServerBase::HandleRequest(const glcr::ByteBuffer& request, GetDirectoryRequest yunq_request; // TODO: Return status. - auto status = yunq_request.ParseFromBytes(request, kHeaderSize, req_caps); - if (!status) { - return status.code(); - } + RETURN_ERROR(yunq_request.ParseFromBytes(request, kHeaderSize, req_caps)); @@ -139,7 +133,7 @@ glcr::ErrorCode VFSServerBase::HandleRequest(const glcr::ByteBuffer& request, - RET_ERR(HandleGetDirectory(yunq_request, yunq_response)); + RETURN_ERROR(HandleGetDirectory(yunq_request, yunq_response)); @@ -148,10 +142,10 @@ glcr::ErrorCode VFSServerBase::HandleRequest(const glcr::ByteBuffer& request, break; } default: { - return glcr::UNIMPLEMENTED; + return glcr::Unimplemented("Method unimplemented by server."); } } - return glcr::OK; + return glcr::Status::Ok(); } diff --git a/sys/victoriafalls/lib/victoriafalls/victoriafalls.yunq.server.h b/sys/victoriafalls/lib/victoriafalls/victoriafalls.yunq.server.h index b1a3085..bf91f66 100644 --- a/sys/victoriafalls/lib/victoriafalls/victoriafalls.yunq.server.h +++ b/sys/victoriafalls/lib/victoriafalls/victoriafalls.yunq.server.h @@ -2,6 +2,7 @@ #pragma once #include +#include #include #include @@ -26,11 +27,11 @@ class VFSServerBase { - [[nodiscard]] virtual glcr::ErrorCode HandleOpenFile(const OpenFileRequest&, OpenFileResponse&) = 0; + [[nodiscard]] virtual glcr::Status HandleOpenFile(const OpenFileRequest&, OpenFileResponse&) = 0; - [[nodiscard]] virtual glcr::ErrorCode HandleGetDirectory(const GetDirectoryRequest&, Directory&) = 0; + [[nodiscard]] virtual glcr::Status HandleGetDirectory(const GetDirectoryRequest&, Directory&) = 0; @@ -40,9 +41,9 @@ class VFSServerBase { friend void VFSServerBaseThreadBootstrap(void*); void ServerThread(); - [[nodiscard]] glcr::ErrorCode HandleRequest(const glcr::ByteBuffer& request, const glcr::CapBuffer& req_caps, - glcr::ByteBuffer& response, uint64_t& resp_length, - glcr::CapBuffer& resp_caps); + [[nodiscard]] glcr::Status HandleRequest(const glcr::ByteBuffer& request, const glcr::CapBuffer& req_caps, + glcr::ByteBuffer& response, uint64_t& resp_length, + glcr::CapBuffer& resp_caps); }; diff --git a/sys/victoriafalls/victoriafalls_server.cpp b/sys/victoriafalls/victoriafalls_server.cpp index 3f54d38..fbc5e42 100644 --- a/sys/victoriafalls/victoriafalls_server.cpp +++ b/sys/victoriafalls/victoriafalls_server.cpp @@ -11,13 +11,13 @@ glcr::ErrorOr> VFSServer::Create( return glcr::UniquePtr(new VFSServer(endpoint_cap, driver)); } -glcr::ErrorCode VFSServer::HandleOpenFile(const OpenFileRequest& request, - OpenFileResponse& response) { +glcr::Status VFSServer::HandleOpenFile(const OpenFileRequest& request, + OpenFileResponse& response) { auto path_tokens = glcr::StrSplit(request.path(), '/'); // Require all paths to be absolute rather than relative. // If the path starts with '/' then the first token will be empty. if (path_tokens.at(0) != "") { - return glcr::INVALID_ARGUMENT; + return glcr::InvalidArgument("Open file supports only absolute paths."); } ASSIGN_OR_RETURN(auto files, driver_.ReadDirectory(2)); @@ -31,9 +31,8 @@ glcr::ErrorCode VFSServer::HandleOpenFile(const OpenFileRequest& request, } } if (!found_token) { - dbgln("Directory '{}' not found.", - glcr::String(path_tokens.at(i)).cstr()); - return glcr::NOT_FOUND; + return glcr::NotFound(glcr::StrFormat("Directory '{}' not found.", + glcr::String(path_tokens.at(i)))); } } @@ -48,8 +47,9 @@ glcr::ErrorCode VFSServer::HandleOpenFile(const OpenFileRequest& request, } } if (!region) { - dbgln("File '{}' not found.", - glcr::String(path_tokens.at(path_tokens.size() - 1)).cstr()); + return glcr::NotFound( + glcr::StrFormat("File '{}' not found.", + glcr::String(path_tokens.at(path_tokens.size() - 1)))); } response.set_path(request.path()); @@ -61,15 +61,15 @@ glcr::ErrorCode VFSServer::HandleOpenFile(const OpenFileRequest& request, ASSIGN_OR_RETURN(Inode * inode, driver_.GetInode(inode_num)); // FIXME: This technically only sets the lower 32 bits. response.set_size(inode->size); - return glcr::OK; + return glcr::Status::Ok(); } -glcr::ErrorCode VFSServer::HandleGetDirectory( - const GetDirectoryRequest& request, Directory& response) { +glcr::Status VFSServer::HandleGetDirectory(const GetDirectoryRequest& request, + Directory& response) { auto path_tokens = glcr::StrSplit(request.path(), '/'); if (path_tokens.at(0) != "") { - return glcr::INVALID_ARGUMENT; + return glcr::InvalidArgument("Get Directory only supports absolute path."); } // If there is a trailing slash we can get rid of the empty string. @@ -88,9 +88,8 @@ glcr::ErrorCode VFSServer::HandleGetDirectory( } } if (!found_token) { - dbgln("Directory '{}' not found.", - glcr::String(path_tokens.at(i)).cstr()); - return glcr::NOT_FOUND; + return glcr::NotFound(glcr::StrFormat("Directory '{}' not found.", + glcr::String(path_tokens.at(i)))); } } @@ -106,5 +105,5 @@ glcr::ErrorCode VFSServer::HandleGetDirectory( response.set_filenames(filelist.ToString()); - return glcr::OK; + return glcr::Status::Ok(); } diff --git a/sys/victoriafalls/victoriafalls_server.h b/sys/victoriafalls/victoriafalls_server.h index d94cef0..8716093 100644 --- a/sys/victoriafalls/victoriafalls_server.h +++ b/sys/victoriafalls/victoriafalls_server.h @@ -9,11 +9,11 @@ class VFSServer : public VFSServerBase { public: static glcr::ErrorOr> Create(Ext2Driver& driver); - glcr::ErrorCode HandleOpenFile(const OpenFileRequest&, - OpenFileResponse&) override; + glcr::Status HandleOpenFile(const OpenFileRequest&, + OpenFileResponse&) override; - glcr::ErrorCode HandleGetDirectory(const GetDirectoryRequest&, - Directory&) override; + glcr::Status HandleGetDirectory(const GetDirectoryRequest&, + Directory&) override; private: // FIXME: Don't store this as a reference. diff --git a/sys/voyageurs/lib/voyageurs/voyageurs.yunq.server.cpp b/sys/voyageurs/lib/voyageurs/voyageurs.yunq.server.cpp index b442937..e7e7d9b 100644 --- a/sys/voyageurs/lib/voyageurs/voyageurs.yunq.server.cpp +++ b/sys/voyageurs/lib/voyageurs/voyageurs.yunq.server.cpp @@ -73,9 +73,9 @@ void VoyageursServerBase::ServerThread() { glcr::ErrorCode reply_err = glcr::OK; resp_cap.Reset(); - glcr::ErrorCode err = HandleRequest(recv_buffer, recv_cap, resp_buffer, resp_length, resp_cap); - if (err != glcr::OK) { - WriteError(resp_buffer, err); + glcr::Status err = HandleRequest(recv_buffer, recv_cap, resp_buffer, resp_length, resp_cap); + if (!err) { + WriteError(resp_buffer, err.code()); reply_err = static_cast(ZReplyPortSend(reply_port_cap, kHeaderSize, resp_buffer.RawPtr(), 0, nullptr)); } else { WriteHeader(resp_buffer, resp_length); @@ -88,12 +88,12 @@ void VoyageursServerBase::ServerThread() { } -glcr::ErrorCode VoyageursServerBase::HandleRequest(const glcr::ByteBuffer& request, - const glcr::CapBuffer& req_caps, - glcr::ByteBuffer& response, uint64_t& resp_length, - glcr::CapBuffer& resp_caps) { +glcr::Status VoyageursServerBase::HandleRequest(const glcr::ByteBuffer& request, + const glcr::CapBuffer& req_caps, + glcr::ByteBuffer& response, uint64_t& resp_length, + glcr::CapBuffer& resp_caps) { if (request.At(0) != kSentinel) { - return glcr::INVALID_ARGUMENT; + return glcr::InvalidArgument("Request Not Valid"); } uint64_t method_select = request.At(8); @@ -104,16 +104,13 @@ glcr::ErrorCode VoyageursServerBase::HandleRequest(const glcr::ByteBuffer& reque KeyboardListener yunq_request; // TODO: Return status. - auto status = yunq_request.ParseFromBytes(request, kHeaderSize, req_caps); - if (!status) { - return status.code(); - } + RETURN_ERROR(yunq_request.ParseFromBytes(request, kHeaderSize, req_caps)); - RET_ERR(HandleRegisterKeyboardListener(yunq_request)); + RETURN_ERROR(HandleRegisterKeyboardListener(yunq_request)); @@ -122,10 +119,10 @@ glcr::ErrorCode VoyageursServerBase::HandleRequest(const glcr::ByteBuffer& reque break; } default: { - return glcr::UNIMPLEMENTED; + return glcr::Unimplemented("Method unimplemented by server."); } } - return glcr::OK; + return glcr::Status::Ok(); } diff --git a/sys/voyageurs/lib/voyageurs/voyageurs.yunq.server.h b/sys/voyageurs/lib/voyageurs/voyageurs.yunq.server.h index faa60df..d594368 100644 --- a/sys/voyageurs/lib/voyageurs/voyageurs.yunq.server.h +++ b/sys/voyageurs/lib/voyageurs/voyageurs.yunq.server.h @@ -2,6 +2,7 @@ #pragma once #include +#include #include #include @@ -26,7 +27,7 @@ class VoyageursServerBase { - [[nodiscard]] virtual glcr::ErrorCode HandleRegisterKeyboardListener(const KeyboardListener&) = 0; + [[nodiscard]] virtual glcr::Status HandleRegisterKeyboardListener(const KeyboardListener&) = 0; @@ -36,9 +37,9 @@ class VoyageursServerBase { friend void VoyageursServerBaseThreadBootstrap(void*); void ServerThread(); - [[nodiscard]] glcr::ErrorCode HandleRequest(const glcr::ByteBuffer& request, const glcr::CapBuffer& req_caps, - glcr::ByteBuffer& response, uint64_t& resp_length, - glcr::CapBuffer& resp_caps); + [[nodiscard]] glcr::Status HandleRequest(const glcr::ByteBuffer& request, const glcr::CapBuffer& req_caps, + glcr::ByteBuffer& response, uint64_t& resp_length, + glcr::CapBuffer& resp_caps); }; diff --git a/sys/voyageurs/voyageurs_server.cpp b/sys/voyageurs/voyageurs_server.cpp index 1404479..c80cda2 100644 --- a/sys/voyageurs/voyageurs_server.cpp +++ b/sys/voyageurs/voyageurs_server.cpp @@ -8,10 +8,10 @@ glcr::ErrorOr> VoyageursServer::Create( new VoyageursServer(cap, keyboard_driver)); } -glcr::ErrorCode VoyageursServer::HandleRegisterKeyboardListener( +glcr::Status VoyageursServer::HandleRegisterKeyboardListener( const KeyboardListener& listener) { keyboard_driver_.RegisterListener(listener.port_capability()); - return glcr::OK; + return glcr::Status::Ok(); } VoyageursServer::VoyageursServer(z_cap_t voyageurs_cap, diff --git a/sys/voyageurs/voyageurs_server.h b/sys/voyageurs/voyageurs_server.h index d597093..852a09e 100644 --- a/sys/voyageurs/voyageurs_server.h +++ b/sys/voyageurs/voyageurs_server.h @@ -11,7 +11,7 @@ class VoyageursServer : public VoyageursServerBase { static glcr::ErrorOr> Create( KeyboardDriver& keyboard_driver); - virtual glcr::ErrorCode HandleRegisterKeyboardListener( + virtual glcr::Status HandleRegisterKeyboardListener( const KeyboardListener& listener) override; private: diff --git a/sys/yellowstone/lib/yellowstone/yellowstone.yunq.server.cpp b/sys/yellowstone/lib/yellowstone/yellowstone.yunq.server.cpp index be4a716..9030940 100644 --- a/sys/yellowstone/lib/yellowstone/yellowstone.yunq.server.cpp +++ b/sys/yellowstone/lib/yellowstone/yellowstone.yunq.server.cpp @@ -75,9 +75,9 @@ void YellowstoneServerBase::ServerThread() { glcr::ErrorCode reply_err = glcr::OK; resp_cap.Reset(); - glcr::ErrorCode err = HandleRequest(recv_buffer, recv_cap, resp_buffer, resp_length, resp_cap); - if (err != glcr::OK) { - WriteError(resp_buffer, err); + glcr::Status err = HandleRequest(recv_buffer, recv_cap, resp_buffer, resp_length, resp_cap); + if (!err) { + WriteError(resp_buffer, err.code()); reply_err = static_cast(ZReplyPortSend(reply_port_cap, kHeaderSize, resp_buffer.RawPtr(), 0, nullptr)); } else { WriteHeader(resp_buffer, resp_length); @@ -90,12 +90,12 @@ void YellowstoneServerBase::ServerThread() { } -glcr::ErrorCode YellowstoneServerBase::HandleRequest(const glcr::ByteBuffer& request, - const glcr::CapBuffer& req_caps, - glcr::ByteBuffer& response, uint64_t& resp_length, - glcr::CapBuffer& resp_caps) { +glcr::Status YellowstoneServerBase::HandleRequest(const glcr::ByteBuffer& request, + const glcr::CapBuffer& req_caps, + glcr::ByteBuffer& response, uint64_t& resp_length, + glcr::CapBuffer& resp_caps) { if (request.At(0) != kSentinel) { - return glcr::INVALID_ARGUMENT; + return glcr::InvalidArgument("Request Not Valid"); } uint64_t method_select = request.At(8); @@ -106,16 +106,13 @@ glcr::ErrorCode YellowstoneServerBase::HandleRequest(const glcr::ByteBuffer& req RegisterEndpointRequest yunq_request; // TODO: Return status. - auto status = yunq_request.ParseFromBytes(request, kHeaderSize, req_caps); - if (!status) { - return status.code(); - } + RETURN_ERROR(yunq_request.ParseFromBytes(request, kHeaderSize, req_caps)); - RET_ERR(HandleRegisterEndpoint(yunq_request)); + RETURN_ERROR(HandleRegisterEndpoint(yunq_request)); @@ -128,10 +125,7 @@ glcr::ErrorCode YellowstoneServerBase::HandleRequest(const glcr::ByteBuffer& req GetEndpointRequest yunq_request; // TODO: Return status. - auto status = yunq_request.ParseFromBytes(request, kHeaderSize, req_caps); - if (!status) { - return status.code(); - } + RETURN_ERROR(yunq_request.ParseFromBytes(request, kHeaderSize, req_caps)); @@ -139,7 +133,7 @@ glcr::ErrorCode YellowstoneServerBase::HandleRequest(const glcr::ByteBuffer& req - RET_ERR(HandleGetEndpoint(yunq_request, yunq_response)); + RETURN_ERROR(HandleGetEndpoint(yunq_request, yunq_response)); @@ -156,7 +150,7 @@ glcr::ErrorCode YellowstoneServerBase::HandleRequest(const glcr::ByteBuffer& req - RET_ERR(HandleGetAhciInfo(yunq_response)); + RETURN_ERROR(HandleGetAhciInfo(yunq_response)); @@ -173,7 +167,7 @@ glcr::ErrorCode YellowstoneServerBase::HandleRequest(const glcr::ByteBuffer& req - RET_ERR(HandleGetFramebufferInfo(yunq_response)); + RETURN_ERROR(HandleGetFramebufferInfo(yunq_response)); @@ -190,7 +184,7 @@ glcr::ErrorCode YellowstoneServerBase::HandleRequest(const glcr::ByteBuffer& req - RET_ERR(HandleGetDenali(yunq_response)); + RETURN_ERROR(HandleGetDenali(yunq_response)); @@ -199,10 +193,10 @@ glcr::ErrorCode YellowstoneServerBase::HandleRequest(const glcr::ByteBuffer& req break; } default: { - return glcr::UNIMPLEMENTED; + return glcr::Unimplemented("Method unimplemented by server."); } } - return glcr::OK; + return glcr::Status::Ok(); } diff --git a/sys/yellowstone/lib/yellowstone/yellowstone.yunq.server.h b/sys/yellowstone/lib/yellowstone/yellowstone.yunq.server.h index af962fc..b725140 100644 --- a/sys/yellowstone/lib/yellowstone/yellowstone.yunq.server.h +++ b/sys/yellowstone/lib/yellowstone/yellowstone.yunq.server.h @@ -2,6 +2,7 @@ #pragma once #include +#include #include #include @@ -28,23 +29,23 @@ class YellowstoneServerBase { - [[nodiscard]] virtual glcr::ErrorCode HandleRegisterEndpoint(const RegisterEndpointRequest&) = 0; + [[nodiscard]] virtual glcr::Status HandleRegisterEndpoint(const RegisterEndpointRequest&) = 0; - [[nodiscard]] virtual glcr::ErrorCode HandleGetEndpoint(const GetEndpointRequest&, Endpoint&) = 0; + [[nodiscard]] virtual glcr::Status HandleGetEndpoint(const GetEndpointRequest&, Endpoint&) = 0; - [[nodiscard]] virtual glcr::ErrorCode HandleGetAhciInfo(AhciInfo&) = 0; + [[nodiscard]] virtual glcr::Status HandleGetAhciInfo(AhciInfo&) = 0; - [[nodiscard]] virtual glcr::ErrorCode HandleGetFramebufferInfo(FramebufferInfo&) = 0; + [[nodiscard]] virtual glcr::Status HandleGetFramebufferInfo(FramebufferInfo&) = 0; - [[nodiscard]] virtual glcr::ErrorCode HandleGetDenali(DenaliInfo&) = 0; + [[nodiscard]] virtual glcr::Status HandleGetDenali(DenaliInfo&) = 0; @@ -54,9 +55,9 @@ class YellowstoneServerBase { friend void YellowstoneServerBaseThreadBootstrap(void*); void ServerThread(); - [[nodiscard]] glcr::ErrorCode HandleRequest(const glcr::ByteBuffer& request, const glcr::CapBuffer& req_caps, - glcr::ByteBuffer& response, uint64_t& resp_length, - glcr::CapBuffer& resp_caps); + [[nodiscard]] glcr::Status HandleRequest(const glcr::ByteBuffer& request, const glcr::CapBuffer& req_caps, + glcr::ByteBuffer& response, uint64_t& resp_length, + glcr::CapBuffer& resp_caps); }; diff --git a/sys/yellowstone/yellowstone_server.cpp b/sys/yellowstone/yellowstone_server.cpp index ea8aca1..bac6795 100644 --- a/sys/yellowstone/yellowstone_server.cpp +++ b/sys/yellowstone/yellowstone_server.cpp @@ -42,13 +42,13 @@ glcr::ErrorOr> YellowstoneServer::Create() { YellowstoneServer::YellowstoneServer(z_cap_t endpoint_cap) : YellowstoneServerBase(endpoint_cap) {} -glcr::ErrorCode YellowstoneServer::HandleGetAhciInfo(AhciInfo& info) { +glcr::Status YellowstoneServer::HandleGetAhciInfo(AhciInfo& info) { info.set_ahci_region(pci_reader_.GetAhciVmmo()); info.set_region_length(kPcieConfigurationSize); - return glcr::OK; + return glcr::Status::Ok(); } -glcr::ErrorCode YellowstoneServer::HandleGetFramebufferInfo( +glcr::Status YellowstoneServer::HandleGetFramebufferInfo( FramebufferInfo& info) { // FIXME: Don't do this for each request. mmth::OwnedMemoryRegion region = @@ -68,22 +68,22 @@ glcr::ErrorCode YellowstoneServer::HandleGetFramebufferInfo( info.set_blue_mask_size(fb->blue_mask_size); info.set_blue_mask_shift(fb->blue_mask_shift); - return glcr::OK; + return glcr::Status::Ok(); } -glcr::ErrorCode YellowstoneServer::HandleGetDenali(DenaliInfo& info) { +glcr::Status YellowstoneServer::HandleGetDenali(DenaliInfo& info) { if (!endpoint_map_.Contains("denali")) { - return glcr::NOT_FOUND; + return glcr::NotFound("Denali Capability Not registered"); } z_cap_t new_denali; check(ZCapDuplicate(endpoint_map_.at("denali"), kZionPerm_All, &new_denali)); info.set_denali_endpoint(new_denali); info.set_device_id(device_id_); info.set_lba_offset(lba_offset_); - return glcr::OK; + return glcr::Status::Ok(); } -glcr::ErrorCode YellowstoneServer::HandleRegisterEndpoint( +glcr::Status YellowstoneServer::HandleRegisterEndpoint( const RegisterEndpointRequest& req) { dbgln("Registering {}.", req.endpoint_name().view()); check(endpoint_map_.Insert(req.endpoint_name(), req.endpoint_capability())); @@ -106,19 +106,20 @@ glcr::ErrorCode YellowstoneServer::HandleRegisterEndpoint( } else { dbgln("[WARN] Got endpoint cap type: {}", req.endpoint_name().cstr()); } - return glcr::OK; + return glcr::Status::Ok(); } -glcr::ErrorCode YellowstoneServer::HandleGetEndpoint( - const GetEndpointRequest& req, Endpoint& resp) { +glcr::Status YellowstoneServer::HandleGetEndpoint(const GetEndpointRequest& req, + Endpoint& resp) { if (!endpoint_map_.Contains(req.endpoint_name())) { - return glcr::NOT_FOUND; + return glcr::NotFound( + glcr::StrFormat("Endpoint '{}' not found.", req.endpoint_name())); } z_cap_t cap = endpoint_map_.at(req.endpoint_name()); z_cap_t new_cap; check(ZCapDuplicate(cap, kZionPerm_All, &new_cap)); resp.set_endpoint(new_cap); - return glcr::OK; + return glcr::Status::Ok(); } void YellowstoneServer::WaitDenaliRegistered() { has_denali_semaphore_.Wait(); } diff --git a/sys/yellowstone/yellowstone_server.h b/sys/yellowstone/yellowstone_server.h index 03982a8..6866001 100644 --- a/sys/yellowstone/yellowstone_server.h +++ b/sys/yellowstone/yellowstone_server.h @@ -16,13 +16,11 @@ class YellowstoneServer : public YellowstoneServerBase { public: static glcr::ErrorOr> Create(); - glcr::ErrorCode HandleGetAhciInfo(AhciInfo&) override; - glcr::ErrorCode HandleGetFramebufferInfo(FramebufferInfo&) override; - glcr::ErrorCode HandleGetDenali(DenaliInfo&) override; - glcr::ErrorCode HandleRegisterEndpoint( - const RegisterEndpointRequest&) override; - glcr::ErrorCode HandleGetEndpoint(const GetEndpointRequest&, - Endpoint&) override; + glcr::Status HandleGetAhciInfo(AhciInfo&) override; + glcr::Status HandleGetFramebufferInfo(FramebufferInfo&) override; + glcr::Status HandleGetDenali(DenaliInfo&) override; + glcr::Status HandleRegisterEndpoint(const RegisterEndpointRequest&) override; + glcr::Status HandleGetEndpoint(const GetEndpointRequest&, Endpoint&) override; void WaitDenaliRegistered(); void WaitVictoriaFallsRegistered(); diff --git a/yunq/server.cpp.jinja b/yunq/server.cpp.jinja index c964065..6f8a712 100644 --- a/yunq/server.cpp.jinja +++ b/yunq/server.cpp.jinja @@ -75,9 +75,10 @@ void {{interface.name}}ServerBase::ServerThread() { glcr::ErrorCode reply_err = glcr::OK; resp_cap.Reset(); - glcr::ErrorCode err = HandleRequest(recv_buffer, recv_cap, resp_buffer, resp_length, resp_cap); - if (err != glcr::OK) { - WriteError(resp_buffer, err); + glcr::Status err = HandleRequest(recv_buffer, recv_cap, resp_buffer, resp_length, resp_cap); + if (!err) { + WriteError(resp_buffer, err.code()); + dbgln("Responding Error {}", err.message()); reply_err = static_cast(ZReplyPortSend(reply_port_cap, kHeaderSize, resp_buffer.RawPtr(), 0, nullptr)); } else { WriteHeader(resp_buffer, resp_length); @@ -90,12 +91,12 @@ void {{interface.name}}ServerBase::ServerThread() { } -glcr::ErrorCode {{interface.name}}ServerBase::HandleRequest(const glcr::ByteBuffer& request, - const glcr::CapBuffer& req_caps, - glcr::ByteBuffer& response, uint64_t& resp_length, - glcr::CapBuffer& resp_caps) { +glcr::Status {{interface.name}}ServerBase::HandleRequest(const glcr::ByteBuffer& request, + const glcr::CapBuffer& req_caps, + glcr::ByteBuffer& response, uint64_t& resp_length, + glcr::CapBuffer& resp_caps) { if (request.At(0) != kSentinel) { - return glcr::INVALID_ARGUMENT; + return glcr::InvalidArgument("Request Not Valid"); } uint64_t method_select = request.At(8); @@ -106,11 +107,7 @@ glcr::ErrorCode {{interface.name}}ServerBase::HandleRequest(const glcr::ByteBuff {% if method.request != None %} {{method.request}} yunq_request; - // TODO: Return status. - auto status = yunq_request.ParseFromBytes(request, kHeaderSize, req_caps); - if (!status) { - return status.code(); - } + RETURN_ERROR(yunq_request.ParseFromBytes(request, kHeaderSize, req_caps)); {% endif %} {% if method.response != None %} @@ -118,11 +115,11 @@ glcr::ErrorCode {{interface.name}}ServerBase::HandleRequest(const glcr::ByteBuff {% endif %} {% if method.request == None %} - RET_ERR(Handle{{method.name}}(yunq_response)); + RETURN_ERROR(Handle{{method.name}}(yunq_response)); {% elif method.response == None %} - RET_ERR(Handle{{method.name}}(yunq_request)); + RETURN_ERROR(Handle{{method.name}}(yunq_request)); {% else %} - RET_ERR(Handle{{method.name}}(yunq_request, yunq_response)); + RETURN_ERROR(Handle{{method.name}}(yunq_request, yunq_response)); {% endif %} {% if method.response != None %} @@ -134,10 +131,10 @@ glcr::ErrorCode {{interface.name}}ServerBase::HandleRequest(const glcr::ByteBuff } {%- endfor %} default: { - return glcr::UNIMPLEMENTED; + return glcr::Unimplemented("Method unimplemented by server."); } } - return glcr::OK; + return glcr::Status::Ok(); } {% endfor %} diff --git a/yunq/server.h.jinja b/yunq/server.h.jinja index 8856900..e2b510d 100644 --- a/yunq/server.h.jinja +++ b/yunq/server.h.jinja @@ -2,6 +2,7 @@ #pragma once #include +#include #include #include @@ -28,11 +29,11 @@ class {{interface.name}}ServerBase { {% for method in interface.methods %} {% if method.request == None %} - [[nodiscard]] virtual glcr::ErrorCode Handle{{method.name}}({{method.response}}&) = 0; + [[nodiscard]] virtual glcr::Status Handle{{method.name}}({{method.response}}&) = 0; {% elif method.response == None %} - [[nodiscard]] virtual glcr::ErrorCode Handle{{method.name}}(const {{method.request}}&) = 0; + [[nodiscard]] virtual glcr::Status Handle{{method.name}}(const {{method.request}}&) = 0; {% else %} - [[nodiscard]] virtual glcr::ErrorCode Handle{{method.name}}(const {{method.request}}&, {{method.response}}&) = 0; + [[nodiscard]] virtual glcr::Status Handle{{method.name}}(const {{method.request}}&, {{method.response}}&) = 0; {% endif %} {% endfor %} @@ -42,9 +43,9 @@ class {{interface.name}}ServerBase { friend void {{interface.name}}ServerBaseThreadBootstrap(void*); void ServerThread(); - [[nodiscard]] glcr::ErrorCode HandleRequest(const glcr::ByteBuffer& request, const glcr::CapBuffer& req_caps, - glcr::ByteBuffer& response, uint64_t& resp_length, - glcr::CapBuffer& resp_caps); + [[nodiscard]] glcr::Status HandleRequest(const glcr::ByteBuffer& request, const glcr::CapBuffer& req_caps, + glcr::ByteBuffer& response, uint64_t& resp_length, + glcr::CapBuffer& resp_caps); }; {% endfor %}