本文整理汇总了C++中AsyncOp::request方法的典型用法代码示例。如果您正苦于以下问题:C++ AsyncOp::request方法的具体用法?C++ AsyncOp::request怎么用?C++ AsyncOp::request使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类AsyncOp
的用法示例。
在下文中一共展示了AsyncOp::request方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: startCommand
void NetworkInterfaceASIO::startCommand(const TaskExecutor::CallbackHandle& cbHandle,
const RemoteCommandRequest& request,
const RemoteCommandCompletionFn& onFinish) {
invariant(onFinish);
{
stdx::lock_guard<stdx::mutex> lk(_inProgressMutex);
const auto insertResult = _inGetConnection.emplace(cbHandle);
// We should never see the same CallbackHandle added twice
invariant(insertResult.second);
}
LOG(2) << "startCommand: " << request.toString();
auto startTime = now();
auto nextStep = [this, startTime, cbHandle, request, onFinish](
StatusWith<ConnectionPool::ConnectionHandle> swConn) {
if (!swConn.isOK()) {
LOG(2) << "Failed to get connection from pool: " << swConn.getStatus();
bool wasPreviouslyCanceled = false;
{
stdx::lock_guard<stdx::mutex> lk(_inProgressMutex);
wasPreviouslyCanceled = _inGetConnection.erase(cbHandle) == 0;
}
onFinish(wasPreviouslyCanceled
? Status(ErrorCodes::CallbackCanceled, "Callback canceled")
: swConn.getStatus());
signalWorkAvailable();
return;
}
auto conn = static_cast<connection_pool_asio::ASIOConnection*>(swConn.getValue().get());
AsyncOp* op = nullptr;
stdx::unique_lock<stdx::mutex> lk(_inProgressMutex);
const auto eraseCount = _inGetConnection.erase(cbHandle);
// If we didn't find the request, we've been canceled
if (eraseCount == 0) {
lk.unlock();
onFinish({ErrorCodes::CallbackCanceled, "Callback canceled"});
// Though we were canceled, we know that the stream is fine, so indicate success.
conn->indicateSuccess();
signalWorkAvailable();
return;
}
// We can't release the AsyncOp until we know we were not canceled.
auto ownedOp = conn->releaseAsyncOp();
op = ownedOp.get();
// Sanity check that we are getting a clean AsyncOp.
invariant(!op->canceled());
invariant(!op->timedOut());
// Now that we're inProgress, an external cancel can touch our op, but
// not until we release the inProgressMutex.
_inProgress.emplace(op, std::move(ownedOp));
op->_cbHandle = std::move(cbHandle);
op->_request = std::move(request);
op->_onFinish = std::move(onFinish);
op->_connectionPoolHandle = std::move(swConn.getValue());
op->_start = startTime;
// This ditches the lock and gets us onto the strand (so we're
// threadsafe)
op->_strand.post([this, op] {
// Set timeout now that we have the correct request object
if (op->_request.timeout != RemoteCommandRequest::kNoTimeout) {
op->_timeoutAlarm =
op->_owner->_timerFactory->make(&op->_strand, op->_request.timeout);
std::shared_ptr<AsyncOp::AccessControl> access;
std::size_t generation;
{
stdx::lock_guard<stdx::mutex> lk(op->_access->mutex);
access = op->_access;
generation = access->id;
}
op->_timeoutAlarm->asyncWait([this, op, access, generation](std::error_code ec) {
if (!ec) {
// We must pass a check for safe access before using op inside the
// callback or we may attempt access on an invalid pointer.
stdx::lock_guard<stdx::mutex> lk(access->mutex);
if (generation != access->id) {
// The operation has been cleaned up, do not access.
return;
}
//.........这里部分代码省略.........