From f02df7ffa4ebad636f2802ea890897f30a14824d Mon Sep 17 00:00:00 2001 From: qicosmos Date: Tue, 23 Sep 2025 08:36:11 +0800 Subject: [PATCH] context --- include/rest_rpc/rest_rpc_protocol.hpp | 3 +-- include/rest_rpc/rpc_connection.hpp | 27 +++++++++++++++++++++++++- tests/test_rest_rpc1.cpp | 7 ++++++- 3 files changed, 33 insertions(+), 4 deletions(-) diff --git a/include/rest_rpc/rest_rpc_protocol.hpp b/include/rest_rpc/rest_rpc_protocol.hpp index 367b25f..a7004f0 100644 --- a/include/rest_rpc/rest_rpc_protocol.hpp +++ b/include/rest_rpc/rest_rpc_protocol.hpp @@ -4,8 +4,6 @@ #include #else #include -#endif - inline uint64_t htonll(uint64_t value) { return ((uint64_t)htonl(value & 0xFFFFFFFF) << 32) | htonl(value >> 32); } @@ -13,6 +11,7 @@ inline uint64_t htonll(uint64_t value) { inline uint64_t ntohll(uint64_t value) { return ((uint64_t)ntohl(value & 0xFFFFFFFF) << 32) | ntohl(value >> 32); } +#endif namespace rest_rpc { inline constexpr uint8_t REST_MAGIC_NUM = 39; diff --git a/include/rest_rpc/rpc_connection.hpp b/include/rest_rpc/rpc_connection.hpp index 4b71d60..3efbeaa 100644 --- a/include/rest_rpc/rpc_connection.hpp +++ b/include/rest_rpc/rpc_connection.hpp @@ -6,7 +6,32 @@ #include "use_asio.hpp" namespace rest_rpc { -class rpc_connection { +class rpc_connection; + +class rpc_context { +public: + static auto &context() { + thread_local rpc_context instance; + delay_ = true; + return instance; + } + + void set_connection(std::shared_ptr conn) { weak_ = conn; } + + auto get_connection() { return weak_; } + + static bool delay() { return delay_; } + + static bool set_delay(bool r) { delay_ = r; } + + template void response(T &&t) {} + +private: + std::weak_ptr weak_; + inline static bool delay_ = false; +}; + +class rpc_connection : public std::enable_shared_from_this { public: rpc_connection(tcp_socket socket, uint64_t conn_id, rpc_router &router, bool &cross_ending) diff --git a/tests/test_rest_rpc1.cpp b/tests/test_rest_rpc1.cpp index 8fbe2d4..ffa4f28 100644 --- a/tests/test_rest_rpc1.cpp +++ b/tests/test_rest_rpc1.cpp @@ -75,7 +75,12 @@ TEST_CASE("test router") { CHECK(result1.ec == rpc_errc::ok); } -asio::awaitable void_returning_coroutine() { co_return; } +asio::awaitable void_returning_coroutine() { + auto executor = co_await asio::this_coro::executor; + asio::io_context &ioc = static_cast(executor.context()); + + co_return; +} TEST_CASE("test server start") { rest_rpc_server server("127.0.0.1:9005");