[英]boost::asio::deadline_timer cancel() method is not calling timer handler
提升1.69
你好 我正在嘗試創建一個類來執行https的請求,但也存在超時以進行錯誤檢查。
我正在為https使用boost beast函數(效果很好),我正在使用deadline_timer類來設置計時器
我將https請求和計時器設置為在相同的IO_context上運行。 這個想法是,如果發生超時,我想拋出一個錯誤並停止執行IO上下文,或者如果https工作正常,我想停止計時器。
我正在嘗試使用cancel()
方法停止計時器並根據文檔說明調用超時句柄
此函數強制對計時器執行所有未完成的異步等待操作。 每個取消操作的處理程序將使用boost :: asio :: error :: operation_aborted錯誤代碼調用
但是,永遠不會調用計時器處理程序。 取消計時器時如何調用計時器處理程序?
這是一些代碼:
/* Set up timeout timer and start it */
boost::asio::deadline_timer timer{query->ioc,boost::posix_time::seconds(TIMEOUT_HTTPS)};
timer.async_wait(std::bind(&HttpsCom::on_timeout, this, std::placeholders::_1, query));
// Look up the domain name
std::cout << "Making the request..." << std::endl;
tcp::resolver resolver{query->ioc};
resolver.async_resolve(host, port, std::bind(&HttpsCom::on_resolve,this,std::placeholders::_1,std::placeholders::_2, query));
query->ioc.run();
if (latestError == HTTPS_SUCCESS){
std::size_t test = timer.cancel();
std::cout << test << std::endl; // Timer gets cancel as test = 1
}
這是我的計時器處理程序
void HttpsCom::on_timeout(const boost::system::error_code& ec, std::shared_ptr<HTTPSQueryStruct_t> query){
std::cout << "Timeout" << std::endl;
}
這是我的關閉處理程序(HTTPS成功)
void HttpsCom::on_shutdown(boost::system::error_code ec, std::shared_ptr<HTTPSQueryStruct_t> query){
std::cout << "on_shutdown()" << std::endl;
// boost::ignore_unused(ec);
std::cerr << "on_shutdown" << ": " << ec.message() << std::endl;
/* If we get here the connection was closed gracefully */
setLatestErrorCode(HTTPS_SUCCESS);
query->ioc.stop();
}
我解決了這個問題。 似乎停止ioc只是刪除或以某種方式忘記了排隊的處理程序。 因此,在停止ioc后調用timer.cancel()函數時,您將無處可去,因為它已經忘記了處理程序。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.