mirror of
https://github.com/qicosmos/rest_rpc.git
synced 2026-08-29 08:34:47 +08:00
remove string_view.h
This commit is contained in:
@@ -1,17 +1,11 @@
|
||||
#pragma once
|
||||
#include <tuple>
|
||||
#if __cplusplus > 201402L
|
||||
#include <string_view>
|
||||
using string_view = std::string_view;
|
||||
#else
|
||||
#include "string_view.hpp"
|
||||
using namespace nonstd;
|
||||
#endif
|
||||
#include <tuple>
|
||||
|
||||
#include "codec.h"
|
||||
|
||||
namespace rest_rpc {
|
||||
inline bool has_error(string_view result) {
|
||||
inline bool has_error(std::string_view result) {
|
||||
if (result.empty()) {
|
||||
return true;
|
||||
}
|
||||
@@ -22,20 +16,20 @@ inline bool has_error(string_view result) {
|
||||
return std::get<0>(tp) != 0;
|
||||
}
|
||||
|
||||
template <typename T> inline T get_result(string_view result) {
|
||||
template <typename T> inline T get_result(std::string_view result) {
|
||||
rpc_service::msgpack_codec codec;
|
||||
auto tp = codec.unpack<std::tuple<int, T>>(result.data(), result.size());
|
||||
return std::get<1>(tp);
|
||||
}
|
||||
|
||||
inline std::string get_error_msg(string_view result) {
|
||||
inline std::string get_error_msg(std::string_view result) {
|
||||
rpc_service::msgpack_codec codec;
|
||||
auto tp =
|
||||
codec.unpack<std::tuple<int, std::string>>(result.data(), result.size());
|
||||
return std::get<1>(tp);
|
||||
}
|
||||
|
||||
template <typename T> inline T as(string_view result) {
|
||||
template <typename T> inline T as(std::string_view result) {
|
||||
if (has_error(result)) {
|
||||
throw std::logic_error(get_error_msg(result));
|
||||
}
|
||||
|
||||
@@ -209,7 +209,7 @@ private:
|
||||
read_head();
|
||||
if (req_type_ == request_type::req_res) {
|
||||
route_result_t ret = router_.route<connection>(
|
||||
func_id, nonstd::string_view{body_.data(), length},
|
||||
func_id, std::string_view{body_.data(), length},
|
||||
this->shared_from_this());
|
||||
if (delay_) {
|
||||
delay_ = false;
|
||||
|
||||
@@ -5,10 +5,10 @@
|
||||
#include "function_name.h"
|
||||
#include "md5.hpp"
|
||||
#include "meta_util.hpp"
|
||||
#include "string_view.hpp"
|
||||
#include "use_asio.hpp"
|
||||
#include <functional>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <unordered_map>
|
||||
|
||||
namespace rest_rpc {
|
||||
@@ -81,7 +81,7 @@ public:
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
route_result_t route(uint32_t key, nonstd::string_view data,
|
||||
route_result_t route(uint32_t key, std::string_view data,
|
||||
std::weak_ptr<T> conn) {
|
||||
route_result_t route_result{};
|
||||
std::string result;
|
||||
@@ -198,8 +198,7 @@ private:
|
||||
template <bool is_pub, typename Function>
|
||||
void register_nonmember_func(uint32_t key, Function f) {
|
||||
this->map_invokers_[key] = [f](std::weak_ptr<connection> conn,
|
||||
nonstd::string_view str,
|
||||
std::string &result) {
|
||||
std::string_view str, std::string &result) {
|
||||
using args_tuple = typename function_traits<Function>::bare_tuple_type;
|
||||
msgpack_codec codec;
|
||||
try {
|
||||
@@ -217,7 +216,7 @@ private:
|
||||
template <bool is_pub, typename Function, typename Self>
|
||||
void register_member_func(uint32_t key, const Function &f, Self *self) {
|
||||
this->map_invokers_[key] = [f, self](std::weak_ptr<connection> conn,
|
||||
nonstd::string_view str,
|
||||
std::string_view str,
|
||||
std::string &result) {
|
||||
using args_tuple = typename function_traits<Function>::bare_tuple_type;
|
||||
msgpack_codec codec;
|
||||
@@ -235,7 +234,7 @@ private:
|
||||
|
||||
std::unordered_map<uint32_t,
|
||||
std::function<void(std::weak_ptr<connection>,
|
||||
nonstd::string_view, std::string &)>>
|
||||
std::string_view, std::string &)>>
|
||||
map_invokers_;
|
||||
std::unordered_map<uint32_t, std::string> key2func_name_;
|
||||
};
|
||||
|
||||
@@ -26,7 +26,7 @@ enum class client_language_t {
|
||||
class req_result {
|
||||
public:
|
||||
req_result() = default;
|
||||
req_result(string_view data) : data_(data.data(), data.length()) {}
|
||||
req_result(std::string_view data) : data_(data.data(), data.length()) {}
|
||||
bool success() const { return !has_error(data_); }
|
||||
|
||||
template <typename T> T as() {
|
||||
@@ -310,7 +310,7 @@ public:
|
||||
|
||||
template <size_t TIMEOUT = DEFAULT_TIMEOUT, typename... Args>
|
||||
void async_call(std::string_view rpc_name,
|
||||
std::function<void(asio::error_code, string_view)> cb,
|
||||
std::function<void(asio::error_code, std::string_view)> cb,
|
||||
Args &&...args) {
|
||||
if (!has_connected_) {
|
||||
if (cb)
|
||||
@@ -623,7 +623,7 @@ private:
|
||||
}
|
||||
|
||||
void call_back(uint64_t req_id, const asio::error_code &ec,
|
||||
string_view data) {
|
||||
std::string_view data) {
|
||||
if (client_language_ == client_language_t::JAVA) {
|
||||
// For Java client.
|
||||
// TODO(qwang): Call java callback.
|
||||
@@ -678,7 +678,7 @@ private:
|
||||
}
|
||||
}
|
||||
|
||||
void callback_sub(const asio::error_code &ec, string_view result) {
|
||||
void callback_sub(const asio::error_code &ec, std::string_view result) {
|
||||
rpc_service::msgpack_codec codec;
|
||||
try {
|
||||
auto tp = codec.unpack<std::tuple<int, std::string, std::string>>(
|
||||
@@ -728,7 +728,7 @@ private:
|
||||
public std::enable_shared_from_this<call_t> {
|
||||
public:
|
||||
call_t(asio::io_context &ios,
|
||||
std::function<void(asio::error_code, string_view)> cb,
|
||||
std::function<void(asio::error_code, std::string_view)> cb,
|
||||
size_t timeout)
|
||||
: timer_(ios), cb_(std::move(cb)), timeout_(timeout) {}
|
||||
|
||||
@@ -748,7 +748,7 @@ private:
|
||||
});
|
||||
}
|
||||
|
||||
void callback(asio::error_code ec, string_view data) { cb_(ec, data); }
|
||||
void callback(asio::error_code ec, std::string_view data) { cb_(ec, data); }
|
||||
|
||||
bool has_timeout() const { return has_timeout_; }
|
||||
|
||||
@@ -763,7 +763,7 @@ private:
|
||||
|
||||
private:
|
||||
asio::steady_timer timer_;
|
||||
std::function<void(asio::error_code, string_view)> cb_;
|
||||
std::function<void(asio::error_code, std::string_view)> cb_;
|
||||
size_t timeout_;
|
||||
bool has_timeout_ = false;
|
||||
};
|
||||
@@ -889,7 +889,7 @@ private:
|
||||
struct client_message_type {
|
||||
std::uint64_t req_id;
|
||||
request_type req_type;
|
||||
string_view content;
|
||||
std::string_view content;
|
||||
uint32_t func_id;
|
||||
};
|
||||
std::deque<client_message_type> outbox_;
|
||||
@@ -912,7 +912,8 @@ private:
|
||||
|
||||
rpc_header header_;
|
||||
|
||||
std::unordered_map<std::string, std::function<void(string_view)>> sub_map_;
|
||||
std::unordered_map<std::string, std::function<void(std::string_view)>>
|
||||
sub_map_;
|
||||
std::set<std::pair<std::string, std::string>> key_token_set_;
|
||||
|
||||
client_language_t client_language_ = client_language_t::CPP;
|
||||
|
||||
@@ -61,8 +61,8 @@ public:
|
||||
router_.register_handler<is_pub>(name, f, self);
|
||||
}
|
||||
|
||||
void
|
||||
set_error_callback(std::function<void(asio::error_code, string_view)> f) {
|
||||
void set_error_callback(
|
||||
std::function<void(asio::error_code, std::string_view)> f) {
|
||||
err_cb_ = std::move(f);
|
||||
}
|
||||
|
||||
@@ -174,7 +174,7 @@ private:
|
||||
}
|
||||
}
|
||||
}
|
||||
void error_callback(const asio::error_code &ec, string_view msg) {
|
||||
void error_callback(const asio::error_code &ec, std::string_view msg) {
|
||||
if (err_cb_) {
|
||||
err_cb_(ec, msg);
|
||||
}
|
||||
@@ -269,7 +269,7 @@ private:
|
||||
bool stop_check_ = false;
|
||||
std::condition_variable cv_;
|
||||
|
||||
std::function<void(asio::error_code, string_view)> err_cb_;
|
||||
std::function<void(asio::error_code, std::string_view)> err_cb_;
|
||||
std::function<void(int64_t)> conn_timeout_callback_;
|
||||
std::function<void(std::shared_ptr<connection>, std::string)>
|
||||
on_net_err_callback_ = nullptr;
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -14,13 +14,7 @@ using tcp_socket = asio::ip::tcp::socket;
|
||||
using ssl_socket = asio::ssl::stream<asio::ip::tcp::socket>;
|
||||
#endif
|
||||
|
||||
#if __cplusplus > 201402L
|
||||
#include <string_view>
|
||||
using string_view = std::string_view;
|
||||
#else
|
||||
#include "string_view.hpp"
|
||||
using namespace nonstd;
|
||||
#endif
|
||||
|
||||
#ifdef CINATRA_ENABLE_SSL
|
||||
#if __cplusplus > 201402L
|
||||
|
||||
Reference in New Issue
Block a user