avhttp icon indicating copy to clipboard operation
avhttp copied to clipboard

请问如何正确设置超时?或者说正确关闭http_stream?http_stream的async_open似乎也是同步的。

Open Eggache666 opened this issue 5 years ago • 0 comments

我在之前一个issue中提出了超时问题,回答的是multi_download接口中设置超时,我想知道http_stream如何正确设置。

           // 以下面一个链接完全打开较慢为例,或者有的链接等待很长时间,而http_stream无法设置超时断开,我用异步定时器设置超时断开,但是却一直在下载中。这个async_open似乎在同步执行中,请教如何正确关闭这种完全打开很慢或者说卡死了的链接?

	std::string url = "https://dl.bintray.com/boostorg/release/1.70.0/source/boost_1_70_0.zip";

	boost::asio::io_service ios;

	boost::asio::deadline_timer timer(ios);
	avhttp::http_stream h(ios);

	timer.expires_from_now(boost::posix_time::seconds(5));
	timer.async_wait([&](const boost::system::error_code& ec) {
		std::cout << "cancel\n";
		h.close();
		});

	h.check_certificate(false);
	h.async_open(url, [&](const boost::system::error_code& ec) {
		if (!ec)
		{
			std::cout << "openning...\n";
			std::ofstream("boost.zip", std::ios::binary) << &h;
			h.close();
			timer.cancel();
		}
		});

	ios.run();

Eggache666 avatar Jan 09 '20 03:01 Eggache666