sprinfall / webcc

Lightweight C++ HTTP client and server library based on Asio for embedding purpose.
GNU Lesser General Public License v3.0
270 stars 61 forks source link

为啥不提供异步接口 #26

Open windflowerly opened 2 years ago

windflowerly commented 2 years ago

我看了您的部分代码(主要是关于http client部分的),发现其本身就是支持异步调用的。


void ClientBase::AsyncResolve(string_view defaultport) { std::string port = request->port(); if (port.empty()) { port = ToString(default_port); }

LOGVERB("Resolve host (%s)", request->host().c_str());

// The protocol depends on the host, both V4 and V6 are supported. resolver_.asyncresolve(request->host(), port, std::bind(&ClientBase::OnResolve, this, _1, _2)); }

void ClientBase::OnResolve(boost::system::error_code ec, tcp::resolver::results_type endpoints) { if (ec) { LOG_ERRO("Host resolve error (%s)", ec.message().cstr()); error.Set(Error::kResolveError, "Host resolve error"); FinishRequest(); return; }

LOG_VERB("Connect socket");

AsyncWaitDeadlineTimer(connecttimeout);

socket->AsyncConnect(request->host(), endpoints, std::bind(&ClientBase::OnConnect, this, _1, _2)); }

以上代码就是异步调用的。只是在ClientBase::FinishRequest函数中强行做了一个同步。

我比较好奇的是,为啥不开放异步调用接口。对整个框架其实也没有额外的负担。

sprinfall commented 2 years ago

暂时还没想好接口应该怎么设计(回调?future?), Keep-Alive(持久连接)和对象生命周期都不好管理, 异步 client 接口用起来也不方便。 最初是参考 Python requests 程序库来做的,requests (v2) 也没有提供异步的客户端接口。

sprinfall commented 9 months ago

之前通过条件变量强行做的同步被去掉了,但是一个 request 调用下来仍然是”同步“的。