From 8afd772746ed5e2b41a8fa58adb8218be2f56883 Mon Sep 17 00:00:00 2001 From: qicosmos Date: Thu, 18 Apr 2024 15:58:42 +0800 Subject: [PATCH] Update doc and coro io (#558) --- README.md | 15 ++------------- example/main.cpp | 3 +-- include/cinatra/ylt/coro_io/coro_io.hpp | 10 +++++----- lang/coro_http_client_introduction.md | 8 +++----- lang/english/README.md | 15 ++------------- 5 files changed, 13 insertions(+), 38 deletions(-) diff --git a/README.md b/README.md index 73b7ccb8..0acf467a 100644 --- a/README.md +++ b/README.md @@ -456,26 +456,15 @@ async_simple::coro::Lazy test_download() { ```c++ async_simple::coro::Lazy test_websocket() { coro_http_client client{}; - client.on_ws_close([](std::string_view reason) { - std::cout << "web socket close " << reason << std::endl; - }); - client.on_ws_msg([](resp_data data) { - if (data.net_err) { - std::cout << data.net_err.message() << "\n"; - return; - } - std::cout << data.resp_body << std::endl; - }); - auto r = co_await client.connect("ws://localhost:8090/ws"); if (r.net_err) { co_return; } - co_await client.write_websocket("hello websocket"); // mask as default. + co_await client.write_websocket("hello websocket"); auto data = co_await client.read_websocket(); CHECK(data.resp_body == "hello websocket"); - co_await client.write_websocket("test again", /*need_mask = */ false); + co_await client.write_websocket("test again"); data = co_await client.read_websocket(); CHECK(data.resp_body == "test again"); co_await client.write_websocket("ws close"); diff --git a/example/main.cpp b/example/main.cpp index ca13fc09..4727aadb 100644 --- a/example/main.cpp +++ b/example/main.cpp @@ -200,8 +200,7 @@ async_simple::coro::Lazy use_websocket() { co_return; } - auto result = - co_await client.write_websocket("hello websocket"); // mask as default. + auto result = co_await client.write_websocket("hello websocket"); assert(!result.net_err); auto data = co_await client.read_websocket(); assert(data.resp_body == "hello websocket"); diff --git a/include/cinatra/ylt/coro_io/coro_io.hpp b/include/cinatra/ylt/coro_io/coro_io.hpp index ce6a1d0c..dbc22f04 100644 --- a/include/cinatra/ylt/coro_io/coro_io.hpp +++ b/include/cinatra/ylt/coro_io/coro_io.hpp @@ -38,7 +38,7 @@ class callback_awaitor_base { template class callback_awaitor_impl { public: - callback_awaitor_impl(Derived &awaitor, const Op &op) noexcept + callback_awaitor_impl(Derived &awaitor, Op &op) noexcept : awaitor(awaitor), op(op) {} constexpr bool await_ready() const noexcept { return false; } void await_suspend(std::coroutine_handle<> handle) noexcept { @@ -59,7 +59,7 @@ class callback_awaitor_base { private: Derived &awaitor; - const Op &op; + Op &op; }; public: @@ -87,7 +87,7 @@ class callback_awaitor_base { Derived *obj; }; template - callback_awaitor_impl await_resume(const Op &op) noexcept { + callback_awaitor_impl await_resume(Op &&op) noexcept { return callback_awaitor_impl{static_cast(*this), op}; } @@ -289,7 +289,7 @@ inline async_simple::coro::Lazy sleep_for(const Duration &d, co_await timer.async_await(); } template -inline async_simple::coro::Lazy sleep_for(const Duration &d) { +inline async_simple::coro::Lazy sleep_for(Duration d) { if (auto executor = co_await async_simple::CurrentExecutor(); executor != nullptr) { co_await async_simple::coro::sleep(d); @@ -302,7 +302,7 @@ inline async_simple::coro::Lazy sleep_for(const Duration &d) { template struct post_helper { - void operator()(auto handler) const { + void operator()(auto handler) { asio::dispatch(e, [this, handler]() { try { if constexpr (std::is_same_v>) { diff --git a/lang/coro_http_client_introduction.md b/lang/coro_http_client_introduction.md index 65c7091e..6123b160 100644 --- a/lang/coro_http_client_introduction.md +++ b/lang/coro_http_client_introduction.md @@ -423,13 +423,13 @@ auto r = async_simple::coro::syncAwait( ``` # websocket websocket 的支持需要3步: -- 设置读websocket 数据的回调函数; - 连接服务器; - 发送websocket 数据; +- 读websocket 数据; -设置websocket 读数据接口: +websocket 读数据接口: ```c++ -void on_ws_msg(std::function on_ws_msg); +async_simple::coro::Lazy read_websocket(); ``` websocket 连接服务器接口: ```c++ @@ -458,10 +458,8 @@ enum opcode : std::uint8_t { /// 发送websocket 数据 /// \param msg 要发送的websocket 数据 -/// \param need_mask 是否需要对数据进行mask,默认会mask /// \param op opcode 一般为text、binary或 close 等类型 async_simple::coro::Lazy write_websocket(std::string msg, - bool need_mask = true, opcode op = opcode::text); ``` diff --git a/lang/english/README.md b/lang/english/README.md index f9d1d971..983becb7 100644 --- a/lang/english/README.md +++ b/lang/english/README.md @@ -414,26 +414,15 @@ async_simple::coro::Lazy test_download() { ```c++ async_simple::coro::Lazy test_websocket() { coro_http_client client{}; - client.on_ws_close([](std::string_view reason) { - std::cout << "web socket close " << reason << std::endl; - }); - client.on_ws_msg([](resp_data data) { - if (data.net_err) { - std::cout << data.net_err.message() << "\n"; - return; - } - std::cout << data.resp_body << std::endl; - }); - auto r = co_await client.connect("ws://localhost:8090/ws"); if (r.net_err) { co_return; } - co_await client.write_websocket("hello websocket"); // mask as default. + co_await client.write_websocket("hello websocket"); auto data = co_await client.read_websocket(); CHECK(data.resp_body == "hello websocket"); - co_await client.write_websocket("test again", /*need_mask = */ false); + co_await client.write_websocket("test again"); data = co_await client.read_websocket(); CHECK(data.resp_body == "test again"); co_await client.write_websocket("ws close");