Skip to content

Commit

Permalink
Update doc and coro io (#558)
Browse files Browse the repository at this point in the history
  • Loading branch information
qicosmos authored Apr 18, 2024
1 parent d1798b0 commit 8afd772
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 38 deletions.
15 changes: 2 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -456,26 +456,15 @@ async_simple::coro::Lazy<void> test_download() {
```c++
async_simple::coro::Lazy<void> 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");
Expand Down
3 changes: 1 addition & 2 deletions example/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -200,8 +200,7 @@ async_simple::coro::Lazy<void> 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");
Expand Down
10 changes: 5 additions & 5 deletions include/cinatra/ylt/coro_io/coro_io.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class callback_awaitor_base {
template <typename Op>
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 {
Expand All @@ -59,7 +59,7 @@ class callback_awaitor_base {

private:
Derived &awaitor;
const Op &op;
Op &op;
};

public:
Expand Down Expand Up @@ -87,7 +87,7 @@ class callback_awaitor_base {
Derived *obj;
};
template <typename Op>
callback_awaitor_impl<Op> await_resume(const Op &op) noexcept {
callback_awaitor_impl<Op> await_resume(Op &&op) noexcept {
return callback_awaitor_impl<Op>{static_cast<Derived &>(*this), op};
}

Expand Down Expand Up @@ -289,7 +289,7 @@ inline async_simple::coro::Lazy<void> sleep_for(const Duration &d,
co_await timer.async_await();
}
template <typename Duration>
inline async_simple::coro::Lazy<void> sleep_for(const Duration &d) {
inline async_simple::coro::Lazy<void> sleep_for(Duration d) {
if (auto executor = co_await async_simple::CurrentExecutor();
executor != nullptr) {
co_await async_simple::coro::sleep(d);
Expand All @@ -302,7 +302,7 @@ inline async_simple::coro::Lazy<void> sleep_for(const Duration &d) {

template <typename R, typename Func, typename Executor>
struct post_helper {
void operator()(auto handler) const {
void operator()(auto handler) {
asio::dispatch(e, [this, handler]() {
try {
if constexpr (std::is_same_v<R, async_simple::Try<void>>) {
Expand Down
8 changes: 3 additions & 5 deletions lang/coro_http_client_introduction.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<void(resp_data)> on_ws_msg);
async_simple::coro::Lazy<resp_data> read_websocket();
```
websocket 连接服务器接口:
```c++
Expand Down Expand Up @@ -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<resp_data> write_websocket(std::string msg,
bool need_mask = true,
opcode op = opcode::text);
```

Expand Down
15 changes: 2 additions & 13 deletions lang/english/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -414,26 +414,15 @@ async_simple::coro::Lazy<void> test_download() {
```c++
async_simple::coro::Lazy<void> 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");
Expand Down

0 comments on commit 8afd772

Please sign in to comment.