Skip to content

Commit

Permalink
Merge branch 'main' into update_coro_http
Browse files Browse the repository at this point in the history
  • Loading branch information
qicosmos authored Jan 17, 2024
2 parents 6285e36 + 63bfdbe commit 5d24dfb
Show file tree
Hide file tree
Showing 34 changed files with 527 additions and 448 deletions.
2 changes: 1 addition & 1 deletion include/ylt/coro_rpc/impl/context.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ class context_base {
error_code, error_msg, self_->req_head_, self_->is_delay_);
}
void response_error(coro_rpc::err_code error_code) {
response_error(error_code, make_error_message(error_code));
response_error(error_code, error_code.message());
}
/*!
* Send response message
Expand Down
10 changes: 5 additions & 5 deletions include/ylt/coro_rpc/impl/coro_rpc_client.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -744,12 +744,12 @@ class coro_rpc_client {
uint8_t rpc_errc,
bool &error_happen) {
rpc_return_type_t<T> ret;
struct_pack::errc ec;
struct_pack::err_code ec;
coro_rpc_protocol::rpc_error err;
if (rpc_errc == 0)
AS_LIKELY {
ec = struct_pack::deserialize_to(ret, buffer);
if (ec == struct_pack::errc::ok) {
if SP_LIKELY (!ec) {
if constexpr (std::is_same_v<T, void>) {
return {};
}
Expand All @@ -759,17 +759,17 @@ class coro_rpc_client {
}
}
else {
err.code = rpc_errc;
err.val() = rpc_errc;
if (rpc_errc != UINT8_MAX) {
ec = struct_pack::deserialize_to(err.msg, buffer);
if (ec == struct_pack::errc::ok) {
if SP_LIKELY (!ec) {
error_happen = true;
return rpc_result<T, coro_rpc_protocol>{unexpect_t{}, std::move(err)};
}
}
else {
ec = struct_pack::deserialize_to(err, buffer);
if (ec == struct_pack::errc::ok) {
if SP_LIKELY (!ec) {
return rpc_result<T, coro_rpc_protocol>{unexpect_t{}, std::move(err)};
}
}
Expand Down
51 changes: 27 additions & 24 deletions include/ylt/coro_rpc/impl/errno.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,30 +33,7 @@ enum class errc : uint16_t {
message_too_large,
server_has_ran,
};
struct err_code {
public:
errc ec;
err_code() : ec(errc::ok) {}
explicit err_code(uint16_t ec) : ec{ec} {};
err_code(errc ec) : ec(ec){};
err_code& operator=(errc ec) {
this->ec = ec;
return *this;
}
err_code& operator=(uint16_t ec) {
this->ec = errc{ec};
return *this;
}
err_code(const err_code& err_code) = default;
err_code& operator=(const err_code& o) = default;
bool operator!() const { return ec == errc::ok; }
operator errc() const { return ec; }
operator bool() const { return static_cast<uint16_t>(ec); }
explicit operator uint16_t() const { return static_cast<uint16_t>(ec); }
uint16_t val() const { return static_cast<uint16_t>(ec); }
};
inline bool operator!(errc ec) { return ec == errc::ok; }
inline std::string_view make_error_message(errc ec) {
inline constexpr std::string_view make_error_message(errc ec) noexcept {
switch (ec) {
case errc::ok:
return "ok";
Expand Down Expand Up @@ -86,4 +63,30 @@ inline std::string_view make_error_message(errc ec) {
return "unknown_user-defined_error";
}
}
struct err_code {
public:
errc ec;
constexpr err_code() noexcept : ec(errc::ok) {}
explicit constexpr err_code(uint16_t ec) noexcept : ec{ec} {};
constexpr err_code(errc ec) noexcept : ec(ec){};
constexpr err_code& operator=(errc ec) noexcept {
this->ec = ec;
return *this;
}
constexpr err_code(const err_code& err_code) noexcept = default;
constexpr err_code& operator=(const err_code& o) noexcept = default;
constexpr operator errc() const noexcept { return ec; }
constexpr operator bool() const noexcept { return static_cast<uint16_t>(ec); }
constexpr explicit operator uint16_t() const noexcept {
return static_cast<uint16_t>(ec);
}
constexpr uint16_t val() const noexcept { return static_cast<uint16_t>(ec); }
constexpr std::string_view message() const noexcept {
return make_error_message(ec);
}
};

inline bool operator!(err_code ec) noexcept { return ec == errc::ok; }
inline bool operator!(errc ec) noexcept { return ec == errc::ok; }

}; // namespace coro_rpc
7 changes: 2 additions & 5 deletions include/ylt/coro_rpc/impl/protocol/struct_pack_protocol.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,12 @@ namespace coro_rpc::protocol {
struct struct_pack_protocol {
template <typename T>
static bool deserialize_to(T& t, std::string_view buffer) {
struct_pack::errc ok{};
if constexpr (std::tuple_size_v<T> == 1) {
ok = struct_pack::deserialize_to(std::get<0>(t), buffer);
return !struct_pack::deserialize_to(std::get<0>(t), buffer);
}
else {
ok = struct_pack::deserialize_to(t, buffer);
return !struct_pack::deserialize_to(t, buffer);
}

return ok == struct_pack::errc::ok;
}
template <typename T>
static std::string serialize(const T& t) {
Expand Down
Loading

0 comments on commit 5d24dfb

Please sign in to comment.