Skip to content

为啥不提供异步接口 #26

Description

@windflowerly

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


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

LOG_VERB("Resolve host (%s)", request_->host().c_str());

// The protocol depends on the host, both V4 and V6 are supported.
resolver_.async_resolve(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().c_str());
error_.Set(Error::kResolveError, "Host resolve error");
FinishRequest();
return;
}

LOG_VERB("Connect socket");

AsyncWaitDeadlineTimer(connect_timeout_);

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

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

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

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions