forked from boostorg/beast
Fixes to support Asio changes (API Change):
This adjusts Beast's interfaces and implementation to match the changes in Boost.Asio.
This commit is contained in:
@@ -151,10 +151,7 @@ class worker : public std::enable_shared_from_this<worker>
|
||||
|
||||
crawl_report& report_;
|
||||
tcp::resolver resolver_;
|
||||
tcp::socket socket_;
|
||||
net::steady_timer timer_;
|
||||
net::strand<
|
||||
net::io_context::executor_type> strand_;
|
||||
beast::tcp_stream stream_;
|
||||
beast::flat_buffer buffer_; // (Must persist between reads)
|
||||
http::request<http::empty_body> req_;
|
||||
http::response<http::string_body> res_;
|
||||
@@ -167,11 +164,8 @@ public:
|
||||
crawl_report& report,
|
||||
net::io_context& ioc)
|
||||
: report_(report)
|
||||
, resolver_(ioc)
|
||||
, socket_(ioc)
|
||||
, timer_(ioc,
|
||||
(chrono::steady_clock::time_point::max)())
|
||||
, strand_(ioc.get_executor())
|
||||
, resolver_(beast::make_strand(ioc))
|
||||
, stream_(beast::make_strand(ioc))
|
||||
{
|
||||
// Set up the common fields of the request
|
||||
req_.version(11);
|
||||
@@ -184,44 +178,9 @@ public:
|
||||
void
|
||||
run()
|
||||
{
|
||||
// Run the timer. The timer is operated
|
||||
// continuously, this simplifies the code.
|
||||
on_timer({});
|
||||
|
||||
do_get_host();
|
||||
}
|
||||
|
||||
void
|
||||
on_timer(beast::error_code ec)
|
||||
{
|
||||
if(ec && ec != net::error::operation_aborted)
|
||||
{
|
||||
// Should never happen
|
||||
report_.aggregate(
|
||||
[](crawl_report& rep)
|
||||
{
|
||||
++rep.timer_failures;
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
// Verify that the timer really expired since the deadline may have moved.
|
||||
if(timer_.expiry() <= chrono::steady_clock::now())
|
||||
{
|
||||
socket_.shutdown(tcp::socket::shutdown_both, ec);
|
||||
socket_.close(ec);
|
||||
return;
|
||||
}
|
||||
|
||||
// Wait on the timer
|
||||
timer_.async_wait(
|
||||
net::bind_executor(
|
||||
strand_,
|
||||
beast::bind_front_handler(
|
||||
&worker::on_timer,
|
||||
shared_from_this())));
|
||||
}
|
||||
|
||||
void
|
||||
do_get_host()
|
||||
{
|
||||
@@ -230,27 +189,19 @@ public:
|
||||
|
||||
// nullptr means no more work
|
||||
if(! host)
|
||||
{
|
||||
timer_.cancel_one();
|
||||
return;
|
||||
}
|
||||
|
||||
// The Host HTTP field is required
|
||||
req_.set(http::field::host, host);
|
||||
|
||||
// Set the timer
|
||||
timer_.expires_after(chrono::seconds(timeout));
|
||||
|
||||
// Set up an HTTP GET request message
|
||||
// Look up the domain name
|
||||
resolver_.async_resolve(
|
||||
host,
|
||||
"http",
|
||||
net::bind_executor(
|
||||
strand_,
|
||||
beast::bind_front_handler(
|
||||
&worker::on_resolve,
|
||||
shared_from_this())));
|
||||
beast::bind_front_handler(
|
||||
&worker::on_resolve,
|
||||
shared_from_this()));
|
||||
}
|
||||
|
||||
void
|
||||
@@ -268,18 +219,16 @@ public:
|
||||
return do_get_host();
|
||||
}
|
||||
|
||||
// Set the timer
|
||||
timer_.expires_after(chrono::seconds(timeout));
|
||||
// Set a timeout on the operation
|
||||
stream_.expires_after(std::chrono::seconds(10));
|
||||
|
||||
// Make the connection on the IP address we get from a lookup
|
||||
net::async_connect(
|
||||
socket_,
|
||||
beast::async_connect(
|
||||
stream_,
|
||||
results,
|
||||
net::bind_executor(
|
||||
strand_,
|
||||
beast::bind_front_handler(
|
||||
&worker::on_connect,
|
||||
shared_from_this())));
|
||||
beast::bind_front_handler(
|
||||
&worker::on_connect,
|
||||
shared_from_this()));
|
||||
}
|
||||
|
||||
void
|
||||
@@ -295,18 +244,16 @@ public:
|
||||
return do_get_host();
|
||||
}
|
||||
|
||||
// Set the timer
|
||||
timer_.expires_after(chrono::seconds(timeout));
|
||||
// Set a timeout on the operation
|
||||
stream_.expires_after(std::chrono::seconds(10));
|
||||
|
||||
// Send the HTTP request to the remote host
|
||||
http::async_write(
|
||||
socket_,
|
||||
stream_,
|
||||
req_,
|
||||
net::bind_executor(
|
||||
strand_,
|
||||
beast::bind_front_handler(
|
||||
&worker::on_write,
|
||||
shared_from_this())));
|
||||
beast::bind_front_handler(
|
||||
&worker::on_write,
|
||||
shared_from_this()));
|
||||
}
|
||||
|
||||
void
|
||||
@@ -325,21 +272,16 @@ public:
|
||||
});
|
||||
return do_get_host();
|
||||
}
|
||||
|
||||
// Set the timer
|
||||
timer_.expires_after(chrono::seconds(timeout));
|
||||
|
||||
// Receive the HTTP response
|
||||
res_ = {};
|
||||
http::async_read(
|
||||
socket_,
|
||||
stream_,
|
||||
buffer_,
|
||||
res_,
|
||||
net::bind_executor(
|
||||
strand_,
|
||||
beast::bind_front_handler(
|
||||
&worker::on_read,
|
||||
shared_from_this())));
|
||||
beast::bind_front_handler(
|
||||
&worker::on_read,
|
||||
shared_from_this()));
|
||||
}
|
||||
|
||||
void
|
||||
@@ -368,8 +310,8 @@ public:
|
||||
});
|
||||
|
||||
// Gracefully close the socket
|
||||
socket_.shutdown(tcp::socket::shutdown_both, ec);
|
||||
socket_.close(ec);
|
||||
stream_.socket().shutdown(tcp::socket::shutdown_both, ec);
|
||||
stream_.close();
|
||||
|
||||
// If we get here then the connection is closed gracefully
|
||||
|
||||
@@ -412,7 +354,7 @@ int main(int argc, char* argv[])
|
||||
auto const threads = std::max<int>(1, std::atoi(argv[1]));
|
||||
|
||||
// The io_context is required for all I/O
|
||||
net::io_context ioc{1};
|
||||
net::io_context ioc;
|
||||
|
||||
// The work keeps io_context::run from returning
|
||||
auto work = net::make_work_guard(ioc);
|
||||
|
||||
Reference in New Issue
Block a user