remove string_view.h

This commit is contained in:
qicosmos
2025-09-07 14:06:06 +08:00
parent 8f2c0a4fa0
commit 0737fefead
10 changed files with 53 additions and 1465 deletions
+1 -1
View File
@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.1) cmake_minimum_required(VERSION 3.5)
project(rest_rpc) project(rest_rpc)
include_directories(include) include_directories(include)
+16 -15
View File
@@ -374,7 +374,7 @@ void test_callback() {
// set timeout 100ms // set timeout 100ms
client.async_call<10000>( client.async_call<10000>(
"delay_echo", "delay_echo",
[](const asio::error_code &ec, string_view data) { [](const asio::error_code &ec, std::string_view data) {
if (ec) { if (ec) {
std::cout << ec.value() << " timeout" << std::endl; std::cout << ec.value() << " timeout" << std::endl;
return; return;
@@ -389,7 +389,7 @@ void test_callback() {
// zero means no timeout check, no param means using default timeout(5s) // zero means no timeout check, no param means using default timeout(5s)
client.async_call<0>( client.async_call<0>(
"echo", "echo",
[](const asio::error_code &ec, string_view data) { [](const asio::error_code &ec, std::string_view data) {
auto str = as<std::string>(data); auto str = as<std::string>(data);
std::cout << "echo " << str << '\n'; std::cout << "echo " << str << '\n';
}, },
@@ -400,13 +400,13 @@ void test_callback() {
} }
void wait_for_notification(rpc_client &client) { void wait_for_notification(rpc_client &client) {
client.async_call<0>("sub", client.async_call<0>(
[&client](const asio::error_code &ec, string_view data) { "sub", [&client](const asio::error_code &ec, std::string_view data) {
auto str = as<std::string>(data); auto str = as<std::string>(data);
std::cout << str << '\n'; std::cout << str << '\n';
wait_for_notification(client); wait_for_notification(client);
}); });
} }
void test_sub1() { void test_sub1() {
@@ -418,11 +418,12 @@ void test_sub1() {
return; return;
} }
client.subscribe("key", [](string_view data) { std::cout << data << "\n"; }); client.subscribe("key",
[](std::string_view data) { std::cout << data << "\n"; });
client.subscribe( client.subscribe(
"key", "048a796c8a3c6a6b7bd1223bf2c8cee05232e927b521984ba417cb2fca6df9d1", "key", "048a796c8a3c6a6b7bd1223bf2c8cee05232e927b521984ba417cb2fca6df9d1",
[](string_view data) { [](std::string_view data) {
msgpack_codec codec; msgpack_codec codec;
person p = codec.unpack<person>(data.data(), data.size()); person p = codec.unpack<person>(data.data(), data.size());
std::cout << p.name << "\n"; std::cout << p.name << "\n";
@@ -431,7 +432,7 @@ void test_sub1() {
client.subscribe( client.subscribe(
"key1", "key1",
"048a796c8a3c6a6b7bd1223bf2c8cee05232e927b521984ba417cb2fca6df9d1", "048a796c8a3c6a6b7bd1223bf2c8cee05232e927b521984ba417cb2fca6df9d1",
[](string_view data) { std::cout << data << "\n"; }); [](std::string_view data) { std::cout << data << "\n"; });
bool stop = false; bool stop = false;
std::thread thd1([&client, &stop] { std::thread thd1([&client, &stop] {
@@ -494,7 +495,7 @@ void test_multiple_thread() {
for (size_t i = 0; i < 1000000; i++) { for (size_t i = 0; i < 1000000; i++) {
client->async_call<0>( client->async_call<0>(
"get_name", "get_name",
[](const asio::error_code &ec, string_view data) { [](const asio::error_code &ec, std::string_view data) {
if (ec) { if (ec) {
std::cout << ec.message() << '\n'; std::cout << ec.message() << '\n';
} }
@@ -548,7 +549,7 @@ void test_threads() {
for (size_t i = 1000000; i < 2 * 1000000; i++) { for (size_t i = 1000000; i < 2 * 1000000; i++) {
client.async_call( client.async_call(
"get_int", "get_int",
[i](asio::error_code ec, string_view data) { [i](asio::error_code ec, std::string_view data) {
if (ec) { if (ec) {
std::cout << ec.message() << '\n'; std::cout << ec.message() << '\n';
return; return;
@@ -618,7 +619,7 @@ void test_ssl() {
client.async_call( client.async_call(
"echo", "echo",
[](asio::error_code ec, string_view data) { [](asio::error_code ec, std::string_view data) {
if (ec) { if (ec) {
std::cout << ec.message() << " " << data << "\n"; std::cout << ec.message() << " " << data << "\n";
return; return;
@@ -643,7 +644,7 @@ void benchmark_test() {
for (size_t i = 0; i < 1000000; i++) { for (size_t i = 0; i < 1000000; i++) {
client.async_call( client.async_call(
"echo", "echo",
[i](asio::error_code ec, string_view data) { [i](asio::error_code ec, std::string_view data) {
if (ec) { if (ec) {
return; return;
} }
+5 -11
View File
@@ -1,17 +1,11 @@
#pragma once #pragma once
#include <tuple>
#if __cplusplus > 201402L
#include <string_view> #include <string_view>
using string_view = std::string_view; #include <tuple>
#else
#include "string_view.hpp"
using namespace nonstd;
#endif
#include "codec.h" #include "codec.h"
namespace rest_rpc { namespace rest_rpc {
inline bool has_error(string_view result) { inline bool has_error(std::string_view result) {
if (result.empty()) { if (result.empty()) {
return true; return true;
} }
@@ -22,20 +16,20 @@ inline bool has_error(string_view result) {
return std::get<0>(tp) != 0; 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; rpc_service::msgpack_codec codec;
auto tp = codec.unpack<std::tuple<int, T>>(result.data(), result.size()); auto tp = codec.unpack<std::tuple<int, T>>(result.data(), result.size());
return std::get<1>(tp); 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; rpc_service::msgpack_codec codec;
auto tp = auto tp =
codec.unpack<std::tuple<int, std::string>>(result.data(), result.size()); codec.unpack<std::tuple<int, std::string>>(result.data(), result.size());
return std::get<1>(tp); 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)) { if (has_error(result)) {
throw std::logic_error(get_error_msg(result)); throw std::logic_error(get_error_msg(result));
} }
+1 -1
View File
@@ -209,7 +209,7 @@ private:
read_head(); read_head();
if (req_type_ == request_type::req_res) { if (req_type_ == request_type::req_res) {
route_result_t ret = router_.route<connection>( 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()); this->shared_from_this());
if (delay_) { if (delay_) {
delay_ = false; delay_ = false;
+5 -6
View File
@@ -5,10 +5,10 @@
#include "function_name.h" #include "function_name.h"
#include "md5.hpp" #include "md5.hpp"
#include "meta_util.hpp" #include "meta_util.hpp"
#include "string_view.hpp"
#include "use_asio.hpp" #include "use_asio.hpp"
#include <functional> #include <functional>
#include <string> #include <string>
#include <string_view>
#include <unordered_map> #include <unordered_map>
namespace rest_rpc { namespace rest_rpc {
@@ -81,7 +81,7 @@ public:
} }
template <typename T> 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) { std::weak_ptr<T> conn) {
route_result_t route_result{}; route_result_t route_result{};
std::string result; std::string result;
@@ -198,8 +198,7 @@ private:
template <bool is_pub, typename Function> template <bool is_pub, typename Function>
void register_nonmember_func(uint32_t key, Function f) { void register_nonmember_func(uint32_t key, Function f) {
this->map_invokers_[key] = [f](std::weak_ptr<connection> conn, this->map_invokers_[key] = [f](std::weak_ptr<connection> conn,
nonstd::string_view str, std::string_view str, std::string &result) {
std::string &result) {
using args_tuple = typename function_traits<Function>::bare_tuple_type; using args_tuple = typename function_traits<Function>::bare_tuple_type;
msgpack_codec codec; msgpack_codec codec;
try { try {
@@ -217,7 +216,7 @@ private:
template <bool is_pub, typename Function, typename Self> template <bool is_pub, typename Function, typename Self>
void register_member_func(uint32_t key, const Function &f, Self *self) { void register_member_func(uint32_t key, const Function &f, Self *self) {
this->map_invokers_[key] = [f, self](std::weak_ptr<connection> conn, this->map_invokers_[key] = [f, self](std::weak_ptr<connection> conn,
nonstd::string_view str, std::string_view str,
std::string &result) { std::string &result) {
using args_tuple = typename function_traits<Function>::bare_tuple_type; using args_tuple = typename function_traits<Function>::bare_tuple_type;
msgpack_codec codec; msgpack_codec codec;
@@ -235,7 +234,7 @@ private:
std::unordered_map<uint32_t, std::unordered_map<uint32_t,
std::function<void(std::weak_ptr<connection>, std::function<void(std::weak_ptr<connection>,
nonstd::string_view, std::string &)>> std::string_view, std::string &)>>
map_invokers_; map_invokers_;
std::unordered_map<uint32_t, std::string> key2func_name_; std::unordered_map<uint32_t, std::string> key2func_name_;
}; };
+10 -9
View File
@@ -26,7 +26,7 @@ enum class client_language_t {
class req_result { class req_result {
public: public:
req_result() = default; 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_); } bool success() const { return !has_error(data_); }
template <typename T> T as() { template <typename T> T as() {
@@ -310,7 +310,7 @@ public:
template <size_t TIMEOUT = DEFAULT_TIMEOUT, typename... Args> template <size_t TIMEOUT = DEFAULT_TIMEOUT, typename... Args>
void async_call(std::string_view rpc_name, 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) { Args &&...args) {
if (!has_connected_) { if (!has_connected_) {
if (cb) if (cb)
@@ -623,7 +623,7 @@ private:
} }
void call_back(uint64_t req_id, const asio::error_code &ec, 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) { if (client_language_ == client_language_t::JAVA) {
// For Java client. // For Java client.
// TODO(qwang): Call java callback. // 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; rpc_service::msgpack_codec codec;
try { try {
auto tp = codec.unpack<std::tuple<int, std::string, std::string>>( 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 std::enable_shared_from_this<call_t> {
public: public:
call_t(asio::io_context &ios, 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) size_t timeout)
: timer_(ios), cb_(std::move(cb)), timeout_(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_; } bool has_timeout() const { return has_timeout_; }
@@ -763,7 +763,7 @@ private:
private: private:
asio::steady_timer timer_; 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_; size_t timeout_;
bool has_timeout_ = false; bool has_timeout_ = false;
}; };
@@ -889,7 +889,7 @@ private:
struct client_message_type { struct client_message_type {
std::uint64_t req_id; std::uint64_t req_id;
request_type req_type; request_type req_type;
string_view content; std::string_view content;
uint32_t func_id; uint32_t func_id;
}; };
std::deque<client_message_type> outbox_; std::deque<client_message_type> outbox_;
@@ -912,7 +912,8 @@ private:
rpc_header header_; 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_; std::set<std::pair<std::string, std::string>> key_token_set_;
client_language_t client_language_ = client_language_t::CPP; client_language_t client_language_ = client_language_t::CPP;
+4 -4
View File
@@ -61,8 +61,8 @@ public:
router_.register_handler<is_pub>(name, f, self); router_.register_handler<is_pub>(name, f, self);
} }
void void set_error_callback(
set_error_callback(std::function<void(asio::error_code, string_view)> f) { std::function<void(asio::error_code, std::string_view)> f) {
err_cb_ = std::move(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_) { if (err_cb_) {
err_cb_(ec, msg); err_cb_(ec, msg);
} }
@@ -269,7 +269,7 @@ private:
bool stop_check_ = false; bool stop_check_ = false;
std::condition_variable cv_; 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(int64_t)> conn_timeout_callback_;
std::function<void(std::shared_ptr<connection>, std::string)> std::function<void(std::shared_ptr<connection>, std::string)>
on_net_err_callback_ = nullptr; on_net_err_callback_ = nullptr;
File diff suppressed because it is too large Load Diff
-6
View File
@@ -14,13 +14,7 @@ using tcp_socket = asio::ip::tcp::socket;
using ssl_socket = asio::ssl::stream<asio::ip::tcp::socket>; using ssl_socket = asio::ssl::stream<asio::ip::tcp::socket>;
#endif #endif
#if __cplusplus > 201402L
#include <string_view> #include <string_view>
using string_view = std::string_view;
#else
#include "string_view.hpp"
using namespace nonstd;
#endif
#ifdef CINATRA_ENABLE_SSL #ifdef CINATRA_ENABLE_SSL
#if __cplusplus > 201402L #if __cplusplus > 201402L
+11 -11
View File
@@ -226,7 +226,7 @@ TEST_CASE("test_client_async_call") {
TEST_CASE("test_client_async_call_not_connect") { TEST_CASE("test_client_async_call_not_connect") {
rpc_client client("127.0.0.1", 9001); rpc_client client("127.0.0.1", 9001);
client.async_call<>("get_person", client.async_call<>("get_person",
[](const asio::error_code &ec, string_view data) { [](const asio::error_code &ec, std::string_view data) {
CHECK_EQ(ec, asio::error::not_connected); CHECK_EQ(ec, asio::error::not_connected);
CHECK_EQ(data, "not connected"); CHECK_EQ(data, "not connected");
}); });
@@ -246,14 +246,14 @@ TEST_CASE("test_client_async_call_with_timeout") {
// zero means no timeout check, no param means using default timeout(5s) // zero means no timeout check, no param means using default timeout(5s)
client.async_call<0>( client.async_call<0>(
"echo", "echo",
[](const asio::error_code &ec, string_view data) { [](const asio::error_code &ec, std::string_view data) {
if (ec) if (ec)
std::cout << "error code: " << ec << ", err msg: " << data << '\n'; std::cout << "error code: " << ec << ", err msg: " << data << '\n';
}, },
test); test);
client.async_call<>( client.async_call<>(
"echo", "echo",
[&client](const asio::error_code &ec, string_view data) { [&client](const asio::error_code &ec, std::string_view data) {
std::cout << "req id : " << client.reqest_id() << '\n'; std::cout << "req id : " << client.reqest_id() << '\n';
if (ec) if (ec)
std::cout << "error code: " << ec << ", err msg: " << data << '\n'; std::cout << "error code: " << ec << ", err msg: " << data << '\n';
@@ -261,7 +261,7 @@ TEST_CASE("test_client_async_call_with_timeout") {
test); test);
client.async_call<>( client.async_call<>(
"get_person", "get_person",
[&client](const asio::error_code &ec, string_view data) { [&client](const asio::error_code &ec, std::string_view data) {
if (ec) { if (ec) {
std::cout << "error code: " << ec << ", err msg: " << data << '\n'; std::cout << "error code: " << ec << ", err msg: " << data << '\n';
return; return;
@@ -306,7 +306,7 @@ TEST_CASE("test_client_subscribe") {
CHECK(r); CHECK(r);
client.publish("key", "hello subscriber"); client.publish("key", "hello subscriber");
client.subscribe("key", [&stop](string_view data) { client.subscribe("key", [&stop](std::string_view data) {
std::cout << data << "\n"; std::cout << data << "\n";
CHECK_EQ(data, "hello subscriber"); CHECK_EQ(data, "hello subscriber");
stop = true; stop = true;
@@ -322,7 +322,7 @@ TEST_CASE("test_client_subscribe_not_exist_key") {
server.publish(std::move(key), std::move(val)); server.publish(std::move(key), std::move(val));
}); });
bool stop = false; bool stop = false;
server.set_error_callback([&stop](asio::error_code ec, string_view msg) { server.set_error_callback([&stop](asio::error_code ec, std::string_view msg) {
std::cout << "line: " << __LINE__ << ", msg: " << ec.message() << " -- " std::cout << "line: " << __LINE__ << ", msg: " << ec.message() << " -- "
<< msg << std::endl; << msg << std::endl;
CHECK_EQ(ec, asio::error::invalid_argument); CHECK_EQ(ec, asio::error::invalid_argument);
@@ -346,7 +346,7 @@ TEST_CASE("test_client_subscribe_not_exist_key") {
std::cout << "line: " << __LINE__ << ", msg: " << ec.value() << " -- " std::cout << "line: " << __LINE__ << ", msg: " << ec.value() << " -- "
<< ec.message() << std::endl; << ec.message() << std::endl;
}); });
client.subscribe("key1", [&stop](string_view data) { client.subscribe("key1", [&stop](std::string_view data) {
CHECK(data != "hello subscriber"); CHECK(data != "hello subscriber");
stop = true; stop = true;
}); });
@@ -373,7 +373,7 @@ TEST_CASE("test_server_publish_encode_msg") {
rpc_client client; rpc_client client;
bool r = client.connect("127.0.0.1", 9000); bool r = client.connect("127.0.0.1", 9000);
CHECK(r); CHECK(r);
client.subscribe("person", [&stop](string_view data) { client.subscribe("person", [&stop](std::string_view data) {
try { try {
msgpack_codec codec; msgpack_codec codec;
person p = codec.unpack<person>(data.data(), data.size()); person p = codec.unpack<person>(data.data(), data.size());
@@ -407,7 +407,7 @@ TEST_CASE("test_client_subscribe_by_token") {
CHECK(r); CHECK(r);
client.subscribe( client.subscribe(
"key", "048a796c8a3c6a6b7bd1223bf2c8cee05232e927b521984ba417cb2fca6df9d1", "key", "048a796c8a3c6a6b7bd1223bf2c8cee05232e927b521984ba417cb2fca6df9d1",
[&stop](string_view data) { [&stop](std::string_view data) {
std::cout << data << "\n"; std::cout << data << "\n";
CHECK_EQ(data, "hello token subscriber"); CHECK_EQ(data, "hello token subscriber");
stop = true; stop = true;
@@ -441,7 +441,7 @@ TEST_CASE("test_client_publish_and_subscribe_by_token") {
bool r = client.connect("127.0.0.1", 9000); bool r = client.connect("127.0.0.1", 9000);
CHECK(r); CHECK(r);
client.publish_by_token("key", client_token, "hello token subscriber"); client.publish_by_token("key", client_token, "hello token subscriber");
client.subscribe("key", client_token, [&stop](string_view data) { client.subscribe("key", client_token, [&stop](std::string_view data) {
std::cout << data << "\n"; std::cout << data << "\n";
CHECK_EQ(data, "hello token subscriber"); CHECK_EQ(data, "hello token subscriber");
stop = true; stop = true;
@@ -499,7 +499,7 @@ TEST_CASE("test_server_duplicate_registration_key") {
try { try {
server.register_handler("delay_echo", delay_echo); server.register_handler("delay_echo", delay_echo);
} catch (const std::exception &e) { } catch (const std::exception &e) {
string_view ew{e.what()}; std::string_view ew{e.what()};
std::cerr << ew << '\n'; std::cerr << ew << '\n';
CHECK_EQ(ew, "duplicate registration key !"); CHECK_EQ(ew, "duplicate registration key !");
} }