forked from boostorg/beast
Update for Net-TS Asio (API Change):
fix #769 The following classes are removed: * handler_type * async_result * async_completion * is_dynamic_buffer * is_const_buffer_sequence * is_mutable_buffer_sequence * handler_alloc Actions Required: * Use BOOST_ASIO_HANDLER_TYPE instead of handler_type * Use BOOST_ASIO_INITFN_RESULT_TYPE instead of async_result * Use boost::asio::async_completion * Use boost::asio::is_dynamic_buffer * Use boost::asio::is_const_buffer_sequence * Use boost::asio::is_mutable_buffer_sequence * boost::asio::associated_allocator_t replaces handler_alloc
This commit is contained in:
@@ -50,11 +50,11 @@ class session : public std::enable_shared_from_this<session>
|
||||
http::response<http::string_body> res_;
|
||||
|
||||
public:
|
||||
// Resolver and stream require an io_service
|
||||
// Resolver and stream require an io_context
|
||||
explicit
|
||||
session(boost::asio::io_service& ios, ssl::context& ctx)
|
||||
: resolver_(ios)
|
||||
, stream_(ios, ctx)
|
||||
session(boost::asio::io_context& ioc, ssl::context& ctx)
|
||||
: resolver_(ioc)
|
||||
, stream_(ioc, ctx)
|
||||
{
|
||||
}
|
||||
|
||||
@@ -73,7 +73,9 @@ public:
|
||||
req_.set(http::field::user_agent, BOOST_BEAST_VERSION_STRING);
|
||||
|
||||
// Look up the domain name
|
||||
resolver_.async_resolve({host, port},
|
||||
resolver_.async_resolve(
|
||||
host,
|
||||
port,
|
||||
std::bind(
|
||||
&session::on_resolve,
|
||||
shared_from_this(),
|
||||
@@ -84,7 +86,7 @@ public:
|
||||
void
|
||||
on_resolve(
|
||||
boost::system::error_code ec,
|
||||
tcp::resolver::iterator result)
|
||||
tcp::resolver::results_type results)
|
||||
{
|
||||
if(ec)
|
||||
return fail(ec, "resolve");
|
||||
@@ -92,7 +94,8 @@ public:
|
||||
// Make the connection on the IP address we get from a lookup
|
||||
boost::asio::async_connect(
|
||||
stream_.next_layer(),
|
||||
result,
|
||||
results.begin(),
|
||||
results.end(),
|
||||
std::bind(
|
||||
&session::on_connect,
|
||||
shared_from_this(),
|
||||
@@ -202,8 +205,8 @@ int main(int argc, char** argv)
|
||||
auto const port = argv[2];
|
||||
auto const target = argv[3];
|
||||
|
||||
// The io_service is required for all I/O
|
||||
boost::asio::io_service ios;
|
||||
// The io_context is required for all I/O
|
||||
boost::asio::io_context ioc;
|
||||
|
||||
// The SSL context is required, and holds certificates
|
||||
ssl::context ctx{ssl::context::sslv23_client};
|
||||
@@ -212,11 +215,11 @@ int main(int argc, char** argv)
|
||||
load_root_certificates(ctx);
|
||||
|
||||
// Launch the asynchronous operation
|
||||
std::make_shared<session>(ios, ctx)->run(host, port, target);
|
||||
std::make_shared<session>(ioc, ctx)->run(host, port, target);
|
||||
|
||||
// Run the I/O service. The call will return when
|
||||
// the get operation is complete.
|
||||
ios.run();
|
||||
ioc.run();
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
@@ -46,11 +46,11 @@ class session : public std::enable_shared_from_this<session>
|
||||
http::response<http::string_body> res_;
|
||||
|
||||
public:
|
||||
// Resolver and socket require an io_service
|
||||
// Resolver and socket require an io_context
|
||||
explicit
|
||||
session(boost::asio::io_service& ios)
|
||||
: resolver_(ios)
|
||||
, socket_(ios)
|
||||
session(boost::asio::io_context& ioc)
|
||||
: resolver_(ioc)
|
||||
, socket_(ioc)
|
||||
{
|
||||
}
|
||||
|
||||
@@ -69,7 +69,9 @@ public:
|
||||
req_.set(http::field::user_agent, BOOST_BEAST_VERSION_STRING);
|
||||
|
||||
// Look up the domain name
|
||||
resolver_.async_resolve({host, port},
|
||||
resolver_.async_resolve(
|
||||
host,
|
||||
port,
|
||||
std::bind(
|
||||
&session::on_resolve,
|
||||
shared_from_this(),
|
||||
@@ -80,13 +82,16 @@ public:
|
||||
void
|
||||
on_resolve(
|
||||
boost::system::error_code ec,
|
||||
tcp::resolver::iterator result)
|
||||
tcp::resolver::results_type results)
|
||||
{
|
||||
if(ec)
|
||||
return fail(ec, "resolve");
|
||||
|
||||
// Make the connection on the IP address we get from a lookup
|
||||
boost::asio::async_connect(socket_, result,
|
||||
boost::asio::async_connect(
|
||||
socket_,
|
||||
results.begin(),
|
||||
results.end(),
|
||||
std::bind(
|
||||
&session::on_connect,
|
||||
shared_from_this(),
|
||||
@@ -168,15 +173,15 @@ int main(int argc, char** argv)
|
||||
auto const port = argv[2];
|
||||
auto const target = argv[3];
|
||||
|
||||
// The io_service is required for all I/O
|
||||
boost::asio::io_service ios;
|
||||
// The io_context is required for all I/O
|
||||
boost::asio::io_context ioc;
|
||||
|
||||
// Launch the asynchronous operation
|
||||
std::make_shared<session>(ios)->run(host, port, target);
|
||||
std::make_shared<session>(ioc)->run(host, port, target);
|
||||
|
||||
// Run the I/O service. The call will return when
|
||||
// the get operation is complete.
|
||||
ios.run();
|
||||
ioc.run();
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
@@ -46,23 +46,23 @@ do_session(
|
||||
std::string const& host,
|
||||
std::string const& port,
|
||||
std::string const& target,
|
||||
boost::asio::io_service& ios,
|
||||
boost::asio::io_context& ioc,
|
||||
ssl::context& ctx,
|
||||
boost::asio::yield_context yield)
|
||||
{
|
||||
boost::system::error_code ec;
|
||||
|
||||
// These objects perform our I/O
|
||||
tcp::resolver resolver{ios};
|
||||
ssl::stream<tcp::socket> stream{ios, ctx};
|
||||
tcp::resolver resolver{ioc};
|
||||
ssl::stream<tcp::socket> stream{ioc, ctx};
|
||||
|
||||
// Look up the domain name
|
||||
auto const lookup = resolver.async_resolve({host, port}, yield[ec]);
|
||||
auto const results = resolver.async_resolve(host, port, yield[ec]);
|
||||
if(ec)
|
||||
return fail(ec, "resolve");
|
||||
|
||||
// Make the connection on the IP address we get from a lookup
|
||||
boost::asio::async_connect(stream.next_layer(), lookup, yield[ec]);
|
||||
boost::asio::async_connect(stream.next_layer(), results.begin(), results.end(), yield[ec]);
|
||||
if(ec)
|
||||
return fail(ec, "connect");
|
||||
|
||||
@@ -126,8 +126,8 @@ int main(int argc, char** argv)
|
||||
auto const port = argv[2];
|
||||
auto const target = argv[3];
|
||||
|
||||
// The io_service is required for all I/O
|
||||
boost::asio::io_service ios;
|
||||
// The io_context is required for all I/O
|
||||
boost::asio::io_context ioc;
|
||||
|
||||
// The SSL context is required, and holds certificates
|
||||
ssl::context ctx{ssl::context::sslv23_client};
|
||||
@@ -136,18 +136,18 @@ int main(int argc, char** argv)
|
||||
load_root_certificates(ctx);
|
||||
|
||||
// Launch the asynchronous operation
|
||||
boost::asio::spawn(ios, std::bind(
|
||||
boost::asio::spawn(ioc, std::bind(
|
||||
&do_session,
|
||||
std::string(host),
|
||||
std::string(port),
|
||||
std::string(target),
|
||||
std::ref(ios),
|
||||
std::ref(ioc),
|
||||
std::ref(ctx),
|
||||
std::placeholders::_1));
|
||||
|
||||
// Run the I/O service. The call will return when
|
||||
// the get operation is complete.
|
||||
ios.run();
|
||||
ioc.run();
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
@@ -42,22 +42,22 @@ do_session(
|
||||
std::string const& host,
|
||||
std::string const& port,
|
||||
std::string const& target,
|
||||
boost::asio::io_service& ios,
|
||||
boost::asio::io_context& ioc,
|
||||
boost::asio::yield_context yield)
|
||||
{
|
||||
boost::system::error_code ec;
|
||||
|
||||
// These objects perform our I/O
|
||||
tcp::resolver resolver{ios};
|
||||
tcp::socket socket{ios};
|
||||
tcp::resolver resolver{ioc};
|
||||
tcp::socket socket{ioc};
|
||||
|
||||
// Look up the domain name
|
||||
auto const lookup = resolver.async_resolve({host, port}, yield[ec]);
|
||||
auto const results = resolver.async_resolve(host, port, yield[ec]);
|
||||
if(ec)
|
||||
return fail(ec, "resolve");
|
||||
|
||||
// Make the connection on the IP address we get from a lookup
|
||||
boost::asio::async_connect(socket, lookup, yield[ec]);
|
||||
boost::asio::async_connect(socket, results.begin(), results.end(), yield[ec]);
|
||||
if(ec)
|
||||
return fail(ec, "connect");
|
||||
|
||||
@@ -114,21 +114,21 @@ int main(int argc, char** argv)
|
||||
auto const port = argv[2];
|
||||
auto const target = argv[3];
|
||||
|
||||
// The io_service is required for all I/O
|
||||
boost::asio::io_service ios;
|
||||
// The io_context is required for all I/O
|
||||
boost::asio::io_context ioc;
|
||||
|
||||
// Launch the asynchronous operation
|
||||
boost::asio::spawn(ios, std::bind(
|
||||
boost::asio::spawn(ioc, std::bind(
|
||||
&do_session,
|
||||
std::string(host),
|
||||
std::string(port),
|
||||
std::string(target),
|
||||
std::ref(ios),
|
||||
std::ref(ioc),
|
||||
std::placeholders::_1));
|
||||
|
||||
// Run the I/O service. The call will return when
|
||||
// the get operation is complete.
|
||||
ios.run();
|
||||
ioc.run();
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
@@ -18,10 +18,11 @@
|
||||
#include <boost/beast/core.hpp>
|
||||
#include <boost/beast/http.hpp>
|
||||
#include <boost/beast/version.hpp>
|
||||
#include <boost/asio/bind_executor.hpp>
|
||||
#include <boost/asio/connect.hpp>
|
||||
#include <boost/asio/ip/tcp.hpp>
|
||||
#include <boost/asio/post.hpp>
|
||||
#include <boost/asio/strand.hpp>
|
||||
#include <boost/optional.hpp>
|
||||
#include <atomic>
|
||||
#include <chrono>
|
||||
#include <cstdlib>
|
||||
@@ -43,16 +44,17 @@ namespace chrono = std::chrono; // from <chrono>
|
||||
// This structure aggregates statistics on all the sites
|
||||
class crawl_report
|
||||
{
|
||||
boost::asio::io_service& ios_;
|
||||
boost::asio::io_service::strand strand_;
|
||||
boost::asio::io_context& ioc_;
|
||||
boost::asio::strand<
|
||||
boost::asio::io_context::executor_type> strand_;
|
||||
std::atomic<std::size_t> index_;
|
||||
std::vector<char const*> const& hosts_;
|
||||
std::size_t count_ = 0;
|
||||
|
||||
public:
|
||||
crawl_report(boost::asio::io_service& ios)
|
||||
: ios_(ios)
|
||||
, strand_(ios_)
|
||||
crawl_report(boost::asio::io_context& ioc)
|
||||
: ioc_(ioc)
|
||||
, strand_(ioc_.get_executor())
|
||||
, index_(0)
|
||||
, hosts_(urls_large_data())
|
||||
{
|
||||
@@ -64,18 +66,19 @@ public:
|
||||
void
|
||||
aggregate(F const& f)
|
||||
{
|
||||
ios_.post(strand_.wrap(
|
||||
[&, f]
|
||||
{
|
||||
f(*this);
|
||||
if(count_ % 100 == 0)
|
||||
boost::asio::post(
|
||||
strand_,
|
||||
[&, f]
|
||||
{
|
||||
std::cerr <<
|
||||
"Progress: " << count_ << " of " << hosts_.size() << "\n";
|
||||
//std::cerr << *this;
|
||||
}
|
||||
++count_;
|
||||
}));
|
||||
f(*this);
|
||||
if(count_ % 100 == 0)
|
||||
{
|
||||
std::cerr <<
|
||||
"Progress: " << count_ << " of " << hosts_.size() << "\n";
|
||||
//std::cerr << *this;
|
||||
}
|
||||
++count_;
|
||||
});
|
||||
}
|
||||
|
||||
// Returns the next host to check
|
||||
@@ -148,7 +151,8 @@ class worker : public std::enable_shared_from_this<worker>
|
||||
tcp::resolver resolver_;
|
||||
tcp::socket socket_;
|
||||
boost::asio::steady_timer timer_;
|
||||
boost::asio::io_service::strand strand_;
|
||||
boost::asio::strand<
|
||||
boost::asio::io_context::executor_type> strand_;
|
||||
boost::beast::flat_buffer buffer_; // (Must persist between reads)
|
||||
http::request<http::empty_body> req_;
|
||||
http::response<http::string_body> res_;
|
||||
@@ -156,16 +160,16 @@ class worker : public std::enable_shared_from_this<worker>
|
||||
public:
|
||||
worker(worker&&) = default;
|
||||
|
||||
// Resolver and socket require an io_service
|
||||
// Resolver and socket require an io_context
|
||||
worker(
|
||||
crawl_report& report,
|
||||
boost::asio::io_service& ios)
|
||||
boost::asio::io_context& ioc)
|
||||
: report_(report)
|
||||
, resolver_(ios)
|
||||
, socket_(ios)
|
||||
, timer_(ios,
|
||||
, resolver_(ioc)
|
||||
, socket_(ioc)
|
||||
, timer_(ioc,
|
||||
(chrono::steady_clock::time_point::max)())
|
||||
, strand_(ios)
|
||||
, strand_(ioc.get_executor())
|
||||
{
|
||||
// Set up the common fields of the request
|
||||
req_.version(11);
|
||||
@@ -200,7 +204,7 @@ public:
|
||||
}
|
||||
|
||||
// Verify that the timer really expired since the deadline may have moved.
|
||||
if(timer_.expires_at() <= chrono::steady_clock::now())
|
||||
if(timer_.expiry() <= chrono::steady_clock::now())
|
||||
{
|
||||
socket_.shutdown(tcp::socket::shutdown_both, ec);
|
||||
socket_.close(ec);
|
||||
@@ -209,10 +213,12 @@ public:
|
||||
|
||||
// Wait on the timer
|
||||
timer_.async_wait(
|
||||
strand_.wrap(std::bind(
|
||||
&worker::on_timer,
|
||||
shared_from_this(),
|
||||
std::placeholders::_1)));
|
||||
boost::asio::bind_executor(
|
||||
strand_,
|
||||
std::bind(
|
||||
&worker::on_timer,
|
||||
shared_from_this(),
|
||||
std::placeholders::_1)));
|
||||
}
|
||||
|
||||
void
|
||||
@@ -224,8 +230,7 @@ public:
|
||||
// nullptr means no more work
|
||||
if(! host)
|
||||
{
|
||||
boost::system::error_code ec;
|
||||
timer_.cancel(ec);
|
||||
timer_.cancel_one();
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -233,23 +238,26 @@ public:
|
||||
req_.set(http::field::host, host);
|
||||
|
||||
// Set the timer
|
||||
timer_.expires_from_now(chrono::seconds(timeout));
|
||||
timer_.expires_after(chrono::seconds(timeout));
|
||||
|
||||
// Set up an HTTP GET request message
|
||||
// Look up the domain name
|
||||
resolver_.async_resolve(
|
||||
tcp::resolver::query{host, "http"},
|
||||
strand_.wrap(std::bind(
|
||||
&worker::on_resolve,
|
||||
shared_from_this(),
|
||||
std::placeholders::_1,
|
||||
std::placeholders::_2)));
|
||||
host,
|
||||
"http",
|
||||
boost::asio::bind_executor(
|
||||
strand_,
|
||||
std::bind(
|
||||
&worker::on_resolve,
|
||||
shared_from_this(),
|
||||
std::placeholders::_1,
|
||||
std::placeholders::_2)));
|
||||
}
|
||||
|
||||
void
|
||||
on_resolve(
|
||||
boost::system::error_code ec,
|
||||
tcp::resolver::iterator result)
|
||||
tcp::resolver::results_type results)
|
||||
{
|
||||
if(ec)
|
||||
{
|
||||
@@ -262,16 +270,19 @@ public:
|
||||
}
|
||||
|
||||
// Set the timer
|
||||
timer_.expires_from_now(chrono::seconds(timeout));
|
||||
timer_.expires_after(chrono::seconds(timeout));
|
||||
|
||||
// Make the connection on the IP address we get from a lookup
|
||||
boost::asio::async_connect(
|
||||
socket_,
|
||||
result,
|
||||
strand_.wrap(std::bind(
|
||||
&worker::on_connect,
|
||||
shared_from_this(),
|
||||
std::placeholders::_1)));
|
||||
results.begin(),
|
||||
results.end(),
|
||||
boost::asio::bind_executor(
|
||||
strand_,
|
||||
std::bind(
|
||||
&worker::on_connect,
|
||||
shared_from_this(),
|
||||
std::placeholders::_1)));
|
||||
}
|
||||
|
||||
void
|
||||
@@ -288,17 +299,19 @@ public:
|
||||
}
|
||||
|
||||
// Set the timer
|
||||
timer_.expires_from_now(chrono::seconds(timeout));
|
||||
timer_.expires_after(chrono::seconds(timeout));
|
||||
|
||||
// Send the HTTP request to the remote host
|
||||
http::async_write(
|
||||
socket_,
|
||||
req_,
|
||||
strand_.wrap(std::bind(
|
||||
&worker::on_write,
|
||||
shared_from_this(),
|
||||
std::placeholders::_1,
|
||||
std::placeholders::_2)));
|
||||
boost::asio::bind_executor(
|
||||
strand_,
|
||||
std::bind(
|
||||
&worker::on_write,
|
||||
shared_from_this(),
|
||||
std::placeholders::_1,
|
||||
std::placeholders::_2)));
|
||||
}
|
||||
|
||||
void
|
||||
@@ -319,18 +332,20 @@ public:
|
||||
}
|
||||
|
||||
// Set the timer
|
||||
timer_.expires_from_now(chrono::seconds(timeout));
|
||||
timer_.expires_after(chrono::seconds(timeout));
|
||||
|
||||
// Receive the HTTP response
|
||||
http::async_read(
|
||||
socket_,
|
||||
buffer_,
|
||||
res_,
|
||||
strand_.wrap(std::bind(
|
||||
&worker::on_read,
|
||||
shared_from_this(),
|
||||
std::placeholders::_1,
|
||||
std::placeholders::_2)));
|
||||
boost::asio::bind_executor(
|
||||
strand_,
|
||||
std::bind(
|
||||
&worker::on_read,
|
||||
shared_from_this(),
|
||||
std::placeholders::_1,
|
||||
std::placeholders::_2)));
|
||||
}
|
||||
|
||||
void
|
||||
@@ -400,41 +415,41 @@ int main(int argc, char* argv[])
|
||||
" http-crawl 100 1\n";
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
auto const threads = std::max<std::size_t>(1, std::atoi(argv[1]));
|
||||
auto const threads = std::max<int>(1, std::atoi(argv[1]));
|
||||
|
||||
// The io_service is required for all I/O
|
||||
boost::asio::io_service ios{1};
|
||||
// The io_context is required for all I/O
|
||||
boost::asio::io_context ioc{1};
|
||||
|
||||
// The work keeps io_service::run from returning
|
||||
boost::optional<boost::asio::io_service::work> work{ios};
|
||||
// The work keeps io_context::run from returning
|
||||
auto work = boost::asio::make_work_guard(ioc);
|
||||
|
||||
// The report holds the aggregated statistics
|
||||
crawl_report report{ios};
|
||||
crawl_report report{ioc};
|
||||
|
||||
timer t;
|
||||
|
||||
// Create and launch the worker threads.
|
||||
std::vector<std::thread> workers;
|
||||
workers.reserve(threads + 1);
|
||||
for(std::size_t i = 0; i < threads; ++i)
|
||||
for(int i = 0; i < threads; ++i)
|
||||
workers.emplace_back(
|
||||
[&report]
|
||||
{
|
||||
// We use a separate io_service for each worker because
|
||||
// We use a separate io_context for each worker because
|
||||
// the asio resolver simulates asynchronous operation using
|
||||
// a dedicated worker thread per io_service, and we want to
|
||||
// a dedicated worker thread per io_context, and we want to
|
||||
// do a lot of name resolutions in parallel.
|
||||
boost::asio::io_service ios{1};
|
||||
std::make_shared<worker>(report, ios)->run();
|
||||
ios.run();
|
||||
boost::asio::io_context ioc{1};
|
||||
std::make_shared<worker>(report, ioc)->run();
|
||||
ioc.run();
|
||||
});
|
||||
|
||||
// Add another thread to run the main io_service which
|
||||
// Add another thread to run the main io_context which
|
||||
// is used to aggregate the statistics
|
||||
workers.emplace_back(
|
||||
[&ios]
|
||||
[&ioc]
|
||||
{
|
||||
ios.run();
|
||||
ioc.run();
|
||||
});
|
||||
|
||||
// Now block until all threads exit
|
||||
@@ -442,11 +457,10 @@ int main(int argc, char* argv[])
|
||||
{
|
||||
auto& thread = workers[i];
|
||||
|
||||
// If this is the last thread, destroy the
|
||||
// If this is the last thread, reset the
|
||||
// work object so that it can return from run.
|
||||
//if(&thread == &workers.back())
|
||||
if(i == workers.size() - 1)
|
||||
work = boost::none;
|
||||
work.reset();
|
||||
|
||||
// Wait for the thread to exit
|
||||
thread.join();
|
||||
|
||||
@@ -47,8 +47,8 @@ int main(int argc, char** argv)
|
||||
auto const port = argv[2];
|
||||
auto const target = argv[3];
|
||||
|
||||
// The io_service is required for all I/O
|
||||
boost::asio::io_service ios;
|
||||
// The io_context is required for all I/O
|
||||
boost::asio::io_context ioc;
|
||||
|
||||
// The SSL context is required, and holds certificates
|
||||
ssl::context ctx{ssl::context::sslv23_client};
|
||||
@@ -57,14 +57,14 @@ int main(int argc, char** argv)
|
||||
load_root_certificates(ctx);
|
||||
|
||||
// These objects perform our I/O
|
||||
tcp::resolver resolver{ios};
|
||||
ssl::stream<tcp::socket> stream{ios, ctx};
|
||||
tcp::resolver resolver{ioc};
|
||||
ssl::stream<tcp::socket> stream{ioc, ctx};
|
||||
|
||||
// Look up the domain name
|
||||
auto const lookup = resolver.resolve({host, port});
|
||||
auto const results = resolver.resolve(host, port);
|
||||
|
||||
// Make the connection on the IP address we get from a lookup
|
||||
boost::asio::connect(stream.next_layer(), lookup);
|
||||
boost::asio::connect(stream.next_layer(), results.begin(), results.end());
|
||||
|
||||
// Perform the SSL handshake
|
||||
stream.handshake(ssl::stream_base::client);
|
||||
|
||||
@@ -45,18 +45,18 @@ int main(int argc, char** argv)
|
||||
auto const port = argv[2];
|
||||
auto const target = argv[3];
|
||||
|
||||
// The io_service is required for all I/O
|
||||
boost::asio::io_service ios;
|
||||
// The io_context is required for all I/O
|
||||
boost::asio::io_context ioc;
|
||||
|
||||
// These objects perform our I/O
|
||||
tcp::resolver resolver{ios};
|
||||
tcp::socket socket{ios};
|
||||
tcp::resolver resolver{ioc};
|
||||
tcp::socket socket{ioc};
|
||||
|
||||
// Look up the domain name
|
||||
auto const lookup = resolver.resolve({host, port});
|
||||
auto const results = resolver.resolve(host, port);
|
||||
|
||||
// Make the connection on the IP address we get from a lookup
|
||||
boost::asio::connect(socket, lookup);
|
||||
boost::asio::connect(socket, results.begin(), results.end());
|
||||
|
||||
// Set up an HTTP GET request message
|
||||
http::request<http::string_body> req{http::verb::get, target, 11};
|
||||
|
||||
@@ -18,6 +18,7 @@
|
||||
#include <boost/beast/core.hpp>
|
||||
#include <boost/beast/http.hpp>
|
||||
#include <boost/beast/version.hpp>
|
||||
#include <boost/asio/bind_executor.hpp>
|
||||
#include <boost/asio/ip/tcp.hpp>
|
||||
#include <boost/asio/ssl/stream.hpp>
|
||||
#include <boost/asio/strand.hpp>
|
||||
@@ -244,18 +245,21 @@ class session : public std::enable_shared_from_this<session>
|
||||
http::async_write(
|
||||
self_.stream_,
|
||||
*sp,
|
||||
self_.strand_.wrap(std::bind(
|
||||
&session::on_write,
|
||||
self_.shared_from_this(),
|
||||
std::placeholders::_1,
|
||||
std::placeholders::_2,
|
||||
! sp->keep_alive())));
|
||||
boost::asio::bind_executor(
|
||||
self_.strand_,
|
||||
std::bind(
|
||||
&session::on_write,
|
||||
self_.shared_from_this(),
|
||||
std::placeholders::_1,
|
||||
std::placeholders::_2,
|
||||
! sp->keep_alive())));
|
||||
}
|
||||
};
|
||||
|
||||
tcp::socket socket_;
|
||||
ssl::stream<tcp::socket&> stream_;
|
||||
boost::asio::io_service::strand strand_;
|
||||
boost::asio::strand<
|
||||
boost::asio::io_context::executor_type> strand_;
|
||||
boost::beast::flat_buffer buffer_;
|
||||
std::string const& doc_root_;
|
||||
http::request<http::string_body> req_;
|
||||
@@ -271,7 +275,7 @@ public:
|
||||
std::string const& doc_root)
|
||||
: socket_(std::move(socket))
|
||||
, stream_(socket_, ctx)
|
||||
, strand_(socket_.get_io_service())
|
||||
, strand_(socket_.get_executor())
|
||||
, doc_root_(doc_root)
|
||||
, lambda_(*this)
|
||||
{
|
||||
@@ -284,10 +288,12 @@ public:
|
||||
// Perform the SSL handshake
|
||||
stream_.async_handshake(
|
||||
ssl::stream_base::server,
|
||||
strand_.wrap(std::bind(
|
||||
&session::on_handshake,
|
||||
shared_from_this(),
|
||||
std::placeholders::_1)));
|
||||
boost::asio::bind_executor(
|
||||
strand_,
|
||||
std::bind(
|
||||
&session::on_handshake,
|
||||
shared_from_this(),
|
||||
std::placeholders::_1)));
|
||||
}
|
||||
|
||||
void
|
||||
@@ -304,11 +310,13 @@ public:
|
||||
{
|
||||
// Read a request
|
||||
http::async_read(stream_, buffer_, req_,
|
||||
strand_.wrap(std::bind(
|
||||
&session::on_read,
|
||||
shared_from_this(),
|
||||
std::placeholders::_1,
|
||||
std::placeholders::_2)));
|
||||
boost::asio::bind_executor(
|
||||
strand_,
|
||||
std::bind(
|
||||
&session::on_read,
|
||||
shared_from_this(),
|
||||
std::placeholders::_1,
|
||||
std::placeholders::_2)));
|
||||
}
|
||||
|
||||
void
|
||||
@@ -359,10 +367,12 @@ public:
|
||||
{
|
||||
// Perform the SSL shutdown
|
||||
stream_.async_shutdown(
|
||||
strand_.wrap(std::bind(
|
||||
&session::on_shutdown,
|
||||
shared_from_this(),
|
||||
std::placeholders::_1)));
|
||||
boost::asio::bind_executor(
|
||||
strand_,
|
||||
std::bind(
|
||||
&session::on_shutdown,
|
||||
shared_from_this(),
|
||||
std::placeholders::_1)));
|
||||
}
|
||||
|
||||
void
|
||||
@@ -387,13 +397,13 @@ class listener : public std::enable_shared_from_this<listener>
|
||||
|
||||
public:
|
||||
listener(
|
||||
boost::asio::io_service& ios,
|
||||
boost::asio::io_context& ioc,
|
||||
ssl::context& ctx,
|
||||
tcp::endpoint endpoint,
|
||||
std::string const& doc_root)
|
||||
: ctx_(ctx)
|
||||
, acceptor_(ios)
|
||||
, socket_(ios)
|
||||
, acceptor_(ioc)
|
||||
, socket_(ioc)
|
||||
, doc_root_(doc_root)
|
||||
{
|
||||
boost::system::error_code ec;
|
||||
@@ -416,7 +426,7 @@ public:
|
||||
|
||||
// Start listening for connections
|
||||
acceptor_.listen(
|
||||
boost::asio::socket_base::max_connections, ec);
|
||||
boost::asio::socket_base::max_listen_connections, ec);
|
||||
if(ec)
|
||||
{
|
||||
fail(ec, "listen");
|
||||
@@ -478,13 +488,13 @@ int main(int argc, char* argv[])
|
||||
" http-server-async-ssl 0.0.0.0 8080 . 1\n";
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
auto const address = boost::asio::ip::address::from_string(argv[1]);
|
||||
auto const address = boost::asio::ip::make_address(argv[1]);
|
||||
auto const port = static_cast<unsigned short>(std::atoi(argv[2]));
|
||||
std::string const doc_root = argv[3];
|
||||
auto const threads = std::max<std::size_t>(1, std::atoi(argv[4]));
|
||||
auto const threads = std::max<int>(1, std::atoi(argv[4]));
|
||||
|
||||
// The io_service is required for all I/O
|
||||
boost::asio::io_service ios{threads};
|
||||
// The io_context is required for all I/O
|
||||
boost::asio::io_context ioc{threads};
|
||||
|
||||
// The SSL context is required, and holds certificates
|
||||
ssl::context ctx{ssl::context::sslv23};
|
||||
@@ -494,7 +504,7 @@ int main(int argc, char* argv[])
|
||||
|
||||
// Create and launch a listening port
|
||||
std::make_shared<listener>(
|
||||
ios,
|
||||
ioc,
|
||||
ctx,
|
||||
tcp::endpoint{address, port},
|
||||
doc_root)->run();
|
||||
@@ -504,11 +514,11 @@ int main(int argc, char* argv[])
|
||||
v.reserve(threads - 1);
|
||||
for(auto i = threads - 1; i > 0; --i)
|
||||
v.emplace_back(
|
||||
[&ios]
|
||||
[&ioc]
|
||||
{
|
||||
ios.run();
|
||||
ioc.run();
|
||||
});
|
||||
ios.run();
|
||||
ioc.run();
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
#include <boost/beast/core.hpp>
|
||||
#include <boost/beast/http.hpp>
|
||||
#include <boost/beast/version.hpp>
|
||||
#include <boost/asio/bind_executor.hpp>
|
||||
#include <boost/asio/ip/tcp.hpp>
|
||||
#include <boost/asio/strand.hpp>
|
||||
#include <boost/config.hpp>
|
||||
@@ -240,17 +241,20 @@ class session : public std::enable_shared_from_this<session>
|
||||
http::async_write(
|
||||
self_.socket_,
|
||||
*sp,
|
||||
self_.strand_.wrap(std::bind(
|
||||
&session::on_write,
|
||||
self_.shared_from_this(),
|
||||
std::placeholders::_1,
|
||||
std::placeholders::_2,
|
||||
! sp->keep_alive())));
|
||||
boost::asio::bind_executor(
|
||||
self_.strand_,
|
||||
std::bind(
|
||||
&session::on_write,
|
||||
self_.shared_from_this(),
|
||||
std::placeholders::_1,
|
||||
std::placeholders::_2,
|
||||
! sp->keep_alive())));
|
||||
}
|
||||
};
|
||||
|
||||
tcp::socket socket_;
|
||||
boost::asio::io_service::strand strand_;
|
||||
boost::asio::strand<
|
||||
boost::asio::io_context::executor_type> strand_;
|
||||
boost::beast::flat_buffer buffer_;
|
||||
std::string const& doc_root_;
|
||||
http::request<http::string_body> req_;
|
||||
@@ -264,7 +268,7 @@ public:
|
||||
tcp::socket socket,
|
||||
std::string const& doc_root)
|
||||
: socket_(std::move(socket))
|
||||
, strand_(socket_.get_io_service())
|
||||
, strand_(socket_.get_executor())
|
||||
, doc_root_(doc_root)
|
||||
, lambda_(*this)
|
||||
{
|
||||
@@ -282,11 +286,13 @@ public:
|
||||
{
|
||||
// Read a request
|
||||
http::async_read(socket_, buffer_, req_,
|
||||
strand_.wrap(std::bind(
|
||||
&session::on_read,
|
||||
shared_from_this(),
|
||||
std::placeholders::_1,
|
||||
std::placeholders::_2)));
|
||||
boost::asio::bind_executor(
|
||||
strand_,
|
||||
std::bind(
|
||||
&session::on_read,
|
||||
shared_from_this(),
|
||||
std::placeholders::_1,
|
||||
std::placeholders::_2)));
|
||||
}
|
||||
|
||||
void
|
||||
@@ -354,11 +360,11 @@ class listener : public std::enable_shared_from_this<listener>
|
||||
|
||||
public:
|
||||
listener(
|
||||
boost::asio::io_service& ios,
|
||||
boost::asio::io_context& ioc,
|
||||
tcp::endpoint endpoint,
|
||||
std::string const& doc_root)
|
||||
: acceptor_(ios)
|
||||
, socket_(ios)
|
||||
: acceptor_(ioc)
|
||||
, socket_(ioc)
|
||||
, doc_root_(doc_root)
|
||||
{
|
||||
boost::system::error_code ec;
|
||||
@@ -381,7 +387,7 @@ public:
|
||||
|
||||
// Start listening for connections
|
||||
acceptor_.listen(
|
||||
boost::asio::socket_base::max_connections, ec);
|
||||
boost::asio::socket_base::max_listen_connections, ec);
|
||||
if(ec)
|
||||
{
|
||||
fail(ec, "listen");
|
||||
@@ -442,17 +448,17 @@ int main(int argc, char* argv[])
|
||||
" http-server-async 0.0.0.0 8080 . 1\n";
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
auto const address = boost::asio::ip::address::from_string(argv[1]);
|
||||
auto const address = boost::asio::ip::make_address(argv[1]);
|
||||
auto const port = static_cast<unsigned short>(std::atoi(argv[2]));
|
||||
std::string const doc_root = argv[3];
|
||||
auto const threads = std::max<std::size_t>(1, std::atoi(argv[4]));
|
||||
auto const threads = std::max<int>(1, std::atoi(argv[4]));
|
||||
|
||||
// The io_service is required for all I/O
|
||||
boost::asio::io_service ios{threads};
|
||||
// The io_context is required for all I/O
|
||||
boost::asio::io_context ioc{threads};
|
||||
|
||||
// Create and launch a listening port
|
||||
std::make_shared<listener>(
|
||||
ios,
|
||||
ioc,
|
||||
tcp::endpoint{address, port},
|
||||
doc_root)->run();
|
||||
|
||||
@@ -461,11 +467,11 @@ int main(int argc, char* argv[])
|
||||
v.reserve(threads - 1);
|
||||
for(auto i = threads - 1; i > 0; --i)
|
||||
v.emplace_back(
|
||||
[&ios]
|
||||
[&ioc]
|
||||
{
|
||||
ios.run();
|
||||
ioc.run();
|
||||
});
|
||||
ios.run();
|
||||
ioc.run();
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
@@ -308,7 +308,7 @@ do_session(
|
||||
// Accepts incoming connections and launches the sessions
|
||||
void
|
||||
do_listen(
|
||||
boost::asio::io_service& ios,
|
||||
boost::asio::io_context& ioc,
|
||||
ssl::context& ctx,
|
||||
tcp::endpoint endpoint,
|
||||
std::string const& doc_root,
|
||||
@@ -317,7 +317,7 @@ do_listen(
|
||||
boost::system::error_code ec;
|
||||
|
||||
// Open the acceptor
|
||||
tcp::acceptor acceptor(ios);
|
||||
tcp::acceptor acceptor(ioc);
|
||||
acceptor.open(endpoint.protocol(), ec);
|
||||
if(ec)
|
||||
return fail(ec, "open");
|
||||
@@ -328,19 +328,19 @@ do_listen(
|
||||
return fail(ec, "bind");
|
||||
|
||||
// Start listening for connections
|
||||
acceptor.listen(boost::asio::socket_base::max_connections, ec);
|
||||
acceptor.listen(boost::asio::socket_base::max_listen_connections, ec);
|
||||
if(ec)
|
||||
return fail(ec, "listen");
|
||||
|
||||
for(;;)
|
||||
{
|
||||
tcp::socket socket(ios);
|
||||
tcp::socket socket(ioc);
|
||||
acceptor.async_accept(socket, yield[ec]);
|
||||
if(ec)
|
||||
fail(ec, "accept");
|
||||
else
|
||||
boost::asio::spawn(
|
||||
acceptor.get_io_service(),
|
||||
acceptor.get_executor().context(),
|
||||
std::bind(
|
||||
&do_session,
|
||||
std::move(socket),
|
||||
@@ -361,13 +361,13 @@ int main(int argc, char* argv[])
|
||||
" http-server-coro-ssl 0.0.0.0 8080 . 1\n";
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
auto const address = boost::asio::ip::address::from_string(argv[1]);
|
||||
auto const address = boost::asio::ip::make_address(argv[1]);
|
||||
auto const port = static_cast<unsigned short>(std::atoi(argv[2]));
|
||||
std::string const doc_root = argv[3];
|
||||
auto const threads = std::max<std::size_t>(1, std::atoi(argv[4]));
|
||||
auto const threads = std::max<int>(1, std::atoi(argv[4]));
|
||||
|
||||
// The io_service is required for all I/O
|
||||
boost::asio::io_service ios{threads};
|
||||
// The io_context is required for all I/O
|
||||
boost::asio::io_context ioc{threads};
|
||||
|
||||
// The SSL context is required, and holds certificates
|
||||
ssl::context ctx{ssl::context::sslv23};
|
||||
@@ -376,10 +376,10 @@ int main(int argc, char* argv[])
|
||||
load_server_certificate(ctx);
|
||||
|
||||
// Spawn a listening port
|
||||
boost::asio::spawn(ios,
|
||||
boost::asio::spawn(ioc,
|
||||
std::bind(
|
||||
&do_listen,
|
||||
std::ref(ios),
|
||||
std::ref(ioc),
|
||||
std::ref(ctx),
|
||||
tcp::endpoint{address, port},
|
||||
doc_root,
|
||||
@@ -390,11 +390,11 @@ int main(int argc, char* argv[])
|
||||
v.reserve(threads - 1);
|
||||
for(auto i = threads - 1; i > 0; --i)
|
||||
v.emplace_back(
|
||||
[&ios]
|
||||
[&ioc]
|
||||
{
|
||||
ios.run();
|
||||
ioc.run();
|
||||
});
|
||||
ios.run();
|
||||
ioc.run();
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
@@ -293,7 +293,7 @@ do_session(
|
||||
// Accepts incoming connections and launches the sessions
|
||||
void
|
||||
do_listen(
|
||||
boost::asio::io_service& ios,
|
||||
boost::asio::io_context& ioc,
|
||||
tcp::endpoint endpoint,
|
||||
std::string const& doc_root,
|
||||
boost::asio::yield_context yield)
|
||||
@@ -301,7 +301,7 @@ do_listen(
|
||||
boost::system::error_code ec;
|
||||
|
||||
// Open the acceptor
|
||||
tcp::acceptor acceptor(ios);
|
||||
tcp::acceptor acceptor(ioc);
|
||||
acceptor.open(endpoint.protocol(), ec);
|
||||
if(ec)
|
||||
return fail(ec, "open");
|
||||
@@ -312,19 +312,19 @@ do_listen(
|
||||
return fail(ec, "bind");
|
||||
|
||||
// Start listening for connections
|
||||
acceptor.listen(boost::asio::socket_base::max_connections, ec);
|
||||
acceptor.listen(boost::asio::socket_base::max_listen_connections, ec);
|
||||
if(ec)
|
||||
return fail(ec, "listen");
|
||||
|
||||
for(;;)
|
||||
{
|
||||
tcp::socket socket(ios);
|
||||
tcp::socket socket(ioc);
|
||||
acceptor.async_accept(socket, yield[ec]);
|
||||
if(ec)
|
||||
fail(ec, "accept");
|
||||
else
|
||||
boost::asio::spawn(
|
||||
acceptor.get_io_service(),
|
||||
acceptor.get_executor().context(),
|
||||
std::bind(
|
||||
&do_session,
|
||||
std::move(socket),
|
||||
@@ -344,19 +344,19 @@ int main(int argc, char* argv[])
|
||||
" http-server-coro 0.0.0.0 8080 . 1\n";
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
auto const address = boost::asio::ip::address::from_string(argv[1]);
|
||||
auto const address = boost::asio::ip::make_address(argv[1]);
|
||||
auto const port = static_cast<unsigned short>(std::atoi(argv[2]));
|
||||
std::string const doc_root = argv[3];
|
||||
auto const threads = std::max<std::size_t>(1, std::atoi(argv[4]));
|
||||
auto const threads = std::max<int>(1, std::atoi(argv[4]));
|
||||
|
||||
// The io_service is required for all I/O
|
||||
boost::asio::io_service ios{threads};
|
||||
// The io_context is required for all I/O
|
||||
boost::asio::io_context ioc{threads};
|
||||
|
||||
// Spawn a listening port
|
||||
boost::asio::spawn(ios,
|
||||
boost::asio::spawn(ioc,
|
||||
std::bind(
|
||||
&do_listen,
|
||||
std::ref(ios),
|
||||
std::ref(ioc),
|
||||
tcp::endpoint{address, port},
|
||||
doc_root,
|
||||
std::placeholders::_1));
|
||||
@@ -366,11 +366,11 @@ int main(int argc, char* argv[])
|
||||
v.reserve(threads - 1);
|
||||
for(auto i = threads - 1; i > 0; --i)
|
||||
v.emplace_back(
|
||||
[&ios]
|
||||
[&ioc]
|
||||
{
|
||||
ios.run();
|
||||
ioc.run();
|
||||
});
|
||||
ios.run();
|
||||
ioc.run();
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
@@ -97,7 +97,7 @@ private:
|
||||
std::string doc_root_;
|
||||
|
||||
// The socket for the currently connected client.
|
||||
tcp::socket socket_{acceptor_.get_io_service()};
|
||||
tcp::socket socket_{acceptor_.get_executor().context()};
|
||||
|
||||
// The buffer for performing reads
|
||||
boost::beast::flat_static_buffer<8192> buffer_;
|
||||
@@ -110,7 +110,7 @@ private:
|
||||
|
||||
// The timer putting a time limit on requests.
|
||||
boost::asio::basic_waitable_timer<std::chrono::steady_clock> request_deadline_{
|
||||
acceptor_.get_io_service(), (std::chrono::steady_clock::time_point::max)()};
|
||||
acceptor_.get_executor().context(), (std::chrono::steady_clock::time_point::max)()};
|
||||
|
||||
// The string-based response message.
|
||||
boost::optional<http::response<http::string_body, http::basic_fields<alloc_t>>> string_response_;
|
||||
@@ -142,7 +142,7 @@ private:
|
||||
else
|
||||
{
|
||||
// Request must be fully processed within 60 seconds.
|
||||
request_deadline_.expires_from_now(
|
||||
request_deadline_.expires_after(
|
||||
std::chrono::seconds(60));
|
||||
|
||||
read_request();
|
||||
@@ -288,7 +288,7 @@ private:
|
||||
void check_deadline()
|
||||
{
|
||||
// The deadline may have moved, so check it has really passed.
|
||||
if (request_deadline_.expires_at() <= std::chrono::steady_clock::now())
|
||||
if (request_deadline_.expiry() <= std::chrono::steady_clock::now())
|
||||
{
|
||||
// Close socket to cancel any outstanding operation.
|
||||
boost::beast::error_code ec;
|
||||
@@ -322,14 +322,14 @@ int main(int argc, char* argv[])
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
auto address = ip::address::from_string(argv[1]);
|
||||
auto const address = boost::asio::ip::make_address(argv[1]);
|
||||
unsigned short port = static_cast<unsigned short>(std::atoi(argv[2]));
|
||||
std::string doc_root = argv[3];
|
||||
int num_workers = std::atoi(argv[4]);
|
||||
bool spin = (std::strcmp(argv[5], "spin") == 0);
|
||||
|
||||
boost::asio::io_service ios{1};
|
||||
tcp::acceptor acceptor{ios, {address, port}};
|
||||
boost::asio::io_context ioc{1};
|
||||
tcp::acceptor acceptor{ioc, {address, port}};
|
||||
|
||||
std::list<http_worker> workers;
|
||||
for (int i = 0; i < num_workers; ++i)
|
||||
@@ -339,9 +339,9 @@ int main(int argc, char* argv[])
|
||||
}
|
||||
|
||||
if (spin)
|
||||
for (;;) ios.poll();
|
||||
for (;;) ioc.poll();
|
||||
else
|
||||
ios.run();
|
||||
ioc.run();
|
||||
}
|
||||
catch (const std::exception& e)
|
||||
{
|
||||
|
||||
@@ -19,6 +19,7 @@
|
||||
#include <boost/beast/core.hpp>
|
||||
#include <boost/beast/http.hpp>
|
||||
#include <boost/beast/version.hpp>
|
||||
#include <boost/asio/bind_executor.hpp>
|
||||
#include <boost/asio/ip/tcp.hpp>
|
||||
#include <boost/asio/ssl/stream.hpp>
|
||||
#include <boost/asio/strand.hpp>
|
||||
@@ -255,12 +256,14 @@ class session
|
||||
http::async_write(
|
||||
self_.derived().stream(),
|
||||
*sp,
|
||||
self_.strand_.wrap(std::bind(
|
||||
&session::on_write,
|
||||
self_.derived().shared_from_this(),
|
||||
std::placeholders::_1,
|
||||
std::placeholders::_2,
|
||||
! sp->keep_alive())));
|
||||
boost::asio::bind_executor(
|
||||
self_.strand_,
|
||||
std::bind(
|
||||
&session::on_write,
|
||||
self_.derived().shared_from_this(),
|
||||
std::placeholders::_1,
|
||||
std::placeholders::_2,
|
||||
! sp->keep_alive())));
|
||||
}
|
||||
};
|
||||
|
||||
@@ -270,19 +273,20 @@ class session
|
||||
send_lambda lambda_;
|
||||
|
||||
protected:
|
||||
boost::asio::io_service::strand strand_;
|
||||
boost::asio::strand<
|
||||
boost::asio::io_context::executor_type> strand_;
|
||||
boost::beast::flat_buffer buffer_;
|
||||
|
||||
public:
|
||||
// Take ownership of the buffer
|
||||
explicit
|
||||
session(
|
||||
boost::asio::io_service& ios,
|
||||
boost::asio::io_context& ioc,
|
||||
boost::beast::flat_buffer buffer,
|
||||
std::string const& doc_root)
|
||||
: doc_root_(doc_root)
|
||||
, lambda_(*this)
|
||||
, strand_(ios)
|
||||
, strand_(ioc.get_executor())
|
||||
, buffer_(std::move(buffer))
|
||||
{
|
||||
}
|
||||
@@ -295,11 +299,13 @@ public:
|
||||
derived().stream(),
|
||||
buffer_,
|
||||
req_,
|
||||
strand_.wrap(std::bind(
|
||||
&session::on_read,
|
||||
derived().shared_from_this(),
|
||||
std::placeholders::_1,
|
||||
std::placeholders::_2)));
|
||||
boost::asio::bind_executor(
|
||||
strand_,
|
||||
std::bind(
|
||||
&session::on_read,
|
||||
derived().shared_from_this(),
|
||||
std::placeholders::_1,
|
||||
std::placeholders::_2)));
|
||||
}
|
||||
|
||||
void
|
||||
@@ -352,7 +358,8 @@ class plain_session
|
||||
, public std::enable_shared_from_this<plain_session>
|
||||
{
|
||||
tcp::socket socket_;
|
||||
boost::asio::io_service::strand strand_;
|
||||
boost::asio::strand<
|
||||
boost::asio::io_context::executor_type> strand_;
|
||||
|
||||
public:
|
||||
// Create the session
|
||||
@@ -361,11 +368,11 @@ public:
|
||||
boost::beast::flat_buffer buffer,
|
||||
std::string const& doc_root)
|
||||
: session<plain_session>(
|
||||
socket.get_io_service(),
|
||||
socket.get_executor().context(),
|
||||
std::move(buffer),
|
||||
doc_root)
|
||||
, socket_(std::move(socket))
|
||||
, strand_(socket_.get_io_service())
|
||||
, strand_(socket_.get_executor())
|
||||
{
|
||||
}
|
||||
|
||||
@@ -401,7 +408,8 @@ class ssl_session
|
||||
{
|
||||
tcp::socket socket_;
|
||||
ssl::stream<tcp::socket&> stream_;
|
||||
boost::asio::io_service::strand strand_;
|
||||
boost::asio::strand<
|
||||
boost::asio::io_context::executor_type> strand_;
|
||||
|
||||
public:
|
||||
// Create the session
|
||||
@@ -411,12 +419,12 @@ public:
|
||||
boost::beast::flat_buffer buffer,
|
||||
std::string const& doc_root)
|
||||
: session<ssl_session>(
|
||||
socket.get_io_service(),
|
||||
socket.get_executor().context(),
|
||||
std::move(buffer),
|
||||
doc_root)
|
||||
, socket_(std::move(socket))
|
||||
, stream_(socket_, ctx)
|
||||
, strand_(stream_.get_io_service())
|
||||
, strand_(stream_.get_executor())
|
||||
{
|
||||
}
|
||||
|
||||
@@ -436,11 +444,13 @@ public:
|
||||
stream_.async_handshake(
|
||||
ssl::stream_base::server,
|
||||
buffer_.data(),
|
||||
strand_.wrap(std::bind(
|
||||
&ssl_session::on_handshake,
|
||||
shared_from_this(),
|
||||
std::placeholders::_1,
|
||||
std::placeholders::_2)));
|
||||
boost::asio::bind_executor(
|
||||
strand_,
|
||||
std::bind(
|
||||
&ssl_session::on_handshake,
|
||||
shared_from_this(),
|
||||
std::placeholders::_1,
|
||||
std::placeholders::_2)));
|
||||
}
|
||||
void
|
||||
on_handshake(
|
||||
@@ -461,10 +471,12 @@ public:
|
||||
{
|
||||
// Perform the SSL shutdown
|
||||
stream_.async_shutdown(
|
||||
strand_.wrap(std::bind(
|
||||
&ssl_session::on_shutdown,
|
||||
shared_from_this(),
|
||||
std::placeholders::_1)));
|
||||
boost::asio::bind_executor(
|
||||
strand_,
|
||||
std::bind(
|
||||
&ssl_session::on_shutdown,
|
||||
shared_from_this(),
|
||||
std::placeholders::_1)));
|
||||
}
|
||||
|
||||
void
|
||||
@@ -484,7 +496,8 @@ class detect_session : public std::enable_shared_from_this<detect_session>
|
||||
{
|
||||
tcp::socket socket_;
|
||||
ssl::context& ctx_;
|
||||
boost::asio::io_service::strand strand_;
|
||||
boost::asio::strand<
|
||||
boost::asio::io_context::executor_type> strand_;
|
||||
std::string const& doc_root_;
|
||||
boost::beast::flat_buffer buffer_;
|
||||
|
||||
@@ -496,7 +509,7 @@ public:
|
||||
std::string const& doc_root)
|
||||
: socket_(std::move(socket))
|
||||
, ctx_(ctx)
|
||||
, strand_(socket_.get_io_service())
|
||||
, strand_(socket_.get_executor())
|
||||
, doc_root_(doc_root)
|
||||
{
|
||||
}
|
||||
@@ -508,11 +521,13 @@ public:
|
||||
async_detect_ssl(
|
||||
socket_,
|
||||
buffer_,
|
||||
strand_.wrap(std::bind(
|
||||
&detect_session::on_detect,
|
||||
shared_from_this(),
|
||||
std::placeholders::_1,
|
||||
std::placeholders::_2)));
|
||||
boost::asio::bind_executor(
|
||||
strand_,
|
||||
std::bind(
|
||||
&detect_session::on_detect,
|
||||
shared_from_this(),
|
||||
std::placeholders::_1,
|
||||
std::placeholders::_2)));
|
||||
|
||||
}
|
||||
|
||||
@@ -545,21 +560,22 @@ public:
|
||||
class listener : public std::enable_shared_from_this<listener>
|
||||
{
|
||||
ssl::context& ctx_;
|
||||
boost::asio::io_service::strand strand_;
|
||||
boost::asio::strand<
|
||||
boost::asio::io_context::executor_type> strand_;
|
||||
tcp::acceptor acceptor_;
|
||||
tcp::socket socket_;
|
||||
std::string const& doc_root_;
|
||||
|
||||
public:
|
||||
listener(
|
||||
boost::asio::io_service& ios,
|
||||
boost::asio::io_context& ioc,
|
||||
ssl::context& ctx,
|
||||
tcp::endpoint endpoint,
|
||||
std::string const& doc_root)
|
||||
: ctx_(ctx)
|
||||
, strand_(ios)
|
||||
, acceptor_(ios)
|
||||
, socket_(ios)
|
||||
, strand_(ioc.get_executor())
|
||||
, acceptor_(ioc)
|
||||
, socket_(ioc)
|
||||
, doc_root_(doc_root)
|
||||
{
|
||||
boost::system::error_code ec;
|
||||
@@ -582,7 +598,7 @@ public:
|
||||
|
||||
// Start listening for connections
|
||||
acceptor_.listen(
|
||||
boost::asio::socket_base::max_connections, ec);
|
||||
boost::asio::socket_base::max_listen_connections, ec);
|
||||
if(ec)
|
||||
{
|
||||
fail(ec, "listen");
|
||||
@@ -644,13 +660,13 @@ int main(int argc, char* argv[])
|
||||
" http-server-sync 0.0.0.0 8080 .\n";
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
auto const address = boost::asio::ip::address::from_string(argv[1]);
|
||||
auto const address = boost::asio::ip::make_address(argv[1]);
|
||||
auto const port = static_cast<unsigned short>(std::atoi(argv[2]));
|
||||
std::string const doc_root = argv[3];
|
||||
auto const threads = std::max<std::size_t>(1, std::atoi(argv[4]));
|
||||
auto const threads = std::max<int>(1, std::atoi(argv[4]));
|
||||
|
||||
// The io_service is required for all I/O
|
||||
boost::asio::io_service ios{threads};
|
||||
// The io_context is required for all I/O
|
||||
boost::asio::io_context ioc{threads};
|
||||
|
||||
// The SSL context is required, and holds certificates
|
||||
ssl::context ctx{ssl::context::sslv23};
|
||||
@@ -660,7 +676,7 @@ int main(int argc, char* argv[])
|
||||
|
||||
// Create and launch a listening port
|
||||
std::make_shared<listener>(
|
||||
ios,
|
||||
ioc,
|
||||
ctx,
|
||||
tcp::endpoint{address, port},
|
||||
doc_root)->run();
|
||||
@@ -670,11 +686,11 @@ int main(int argc, char* argv[])
|
||||
v.reserve(threads - 1);
|
||||
for(auto i = threads - 1; i > 0; --i)
|
||||
v.emplace_back(
|
||||
[&ios]
|
||||
[&ioc]
|
||||
{
|
||||
ios.run();
|
||||
ioc.run();
|
||||
});
|
||||
ios.run();
|
||||
ioc.run();
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
@@ -75,7 +75,7 @@ private:
|
||||
|
||||
// The timer for putting a deadline on connection processing.
|
||||
boost::asio::basic_waitable_timer<std::chrono::steady_clock> deadline_{
|
||||
socket_.get_io_service(), std::chrono::seconds(60)};
|
||||
socket_.get_executor().context(), std::chrono::seconds(60)};
|
||||
|
||||
// Asynchronously receive a complete request message.
|
||||
void
|
||||
@@ -231,16 +231,16 @@ main(int argc, char* argv[])
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
auto address = ip::address::from_string(argv[1]);
|
||||
auto const address = boost::asio::ip::make_address(argv[1]);
|
||||
unsigned short port = static_cast<unsigned short>(std::atoi(argv[2]));
|
||||
|
||||
boost::asio::io_service ios{1};
|
||||
boost::asio::io_context ioc{1};
|
||||
|
||||
tcp::acceptor acceptor{ios, {address, port}};
|
||||
tcp::socket socket{ios};
|
||||
tcp::acceptor acceptor{ioc, {address, port}};
|
||||
tcp::socket socket{ioc};
|
||||
http_server(acceptor, socket);
|
||||
|
||||
ios.run();
|
||||
ioc.run();
|
||||
}
|
||||
catch(std::exception const& e)
|
||||
{
|
||||
|
||||
@@ -18,6 +18,7 @@
|
||||
#include <boost/beast/core.hpp>
|
||||
#include <boost/beast/http.hpp>
|
||||
#include <boost/beast/version.hpp>
|
||||
#include <boost/asio/bind_executor.hpp>
|
||||
#include <boost/asio/coroutine.hpp>
|
||||
#include <boost/asio/ip/tcp.hpp>
|
||||
#include <boost/asio/ssl/stream.hpp>
|
||||
@@ -247,18 +248,21 @@ class session
|
||||
http::async_write(
|
||||
self_.socket_,
|
||||
*sp,
|
||||
self_.strand_.wrap(std::bind(
|
||||
&session::loop,
|
||||
self_.shared_from_this(),
|
||||
std::placeholders::_1,
|
||||
std::placeholders::_2,
|
||||
! sp->keep_alive())));
|
||||
boost::asio::bind_executor(
|
||||
self_.strand_,
|
||||
std::bind(
|
||||
&session::loop,
|
||||
self_.shared_from_this(),
|
||||
std::placeholders::_1,
|
||||
std::placeholders::_2,
|
||||
! sp->keep_alive())));
|
||||
}
|
||||
};
|
||||
|
||||
tcp::socket socket_;
|
||||
ssl::stream<tcp::socket&> stream_;
|
||||
boost::asio::io_service::strand strand_;
|
||||
boost::asio::strand<
|
||||
boost::asio::io_context::executor_type> strand_;
|
||||
boost::beast::flat_buffer buffer_;
|
||||
std::string const& doc_root_;
|
||||
http::request<http::string_body> req_;
|
||||
@@ -274,7 +278,7 @@ public:
|
||||
std::string const& doc_root)
|
||||
: socket_(std::move(socket))
|
||||
, stream_(socket_, ctx)
|
||||
, strand_(socket_.get_io_service())
|
||||
, strand_(socket_.get_executor())
|
||||
, doc_root_(doc_root)
|
||||
, lambda_(*this)
|
||||
{
|
||||
@@ -300,12 +304,14 @@ public:
|
||||
// Perform the SSL handshake
|
||||
yield stream_.async_handshake(
|
||||
ssl::stream_base::server,
|
||||
strand_.wrap(std::bind(
|
||||
&session::loop,
|
||||
shared_from_this(),
|
||||
std::placeholders::_1,
|
||||
0,
|
||||
false)));
|
||||
boost::asio::bind_executor(
|
||||
strand_,
|
||||
std::bind(
|
||||
&session::loop,
|
||||
shared_from_this(),
|
||||
std::placeholders::_1,
|
||||
0,
|
||||
false)));
|
||||
if(ec)
|
||||
return fail(ec, "handshake");
|
||||
|
||||
@@ -313,12 +319,14 @@ public:
|
||||
{
|
||||
// Read a request
|
||||
yield http::async_read(stream_, buffer_, req_,
|
||||
strand_.wrap(std::bind(
|
||||
&session::loop,
|
||||
shared_from_this(),
|
||||
std::placeholders::_1,
|
||||
std::placeholders::_2,
|
||||
false)));
|
||||
boost::asio::bind_executor(
|
||||
strand_,
|
||||
std::bind(
|
||||
&session::loop,
|
||||
shared_from_this(),
|
||||
std::placeholders::_1,
|
||||
std::placeholders::_2,
|
||||
false)));
|
||||
if(ec == http::error::end_of_stream)
|
||||
{
|
||||
// The remote host closed the connection
|
||||
@@ -344,12 +352,14 @@ public:
|
||||
|
||||
// Perform the SSL shutdown
|
||||
yield stream_.async_shutdown(
|
||||
strand_.wrap(std::bind(
|
||||
&session::loop,
|
||||
shared_from_this(),
|
||||
std::placeholders::_1,
|
||||
0,
|
||||
false)));
|
||||
boost::asio::bind_executor(
|
||||
strand_,
|
||||
std::bind(
|
||||
&session::loop,
|
||||
shared_from_this(),
|
||||
std::placeholders::_1,
|
||||
0,
|
||||
false)));
|
||||
if(ec)
|
||||
return fail(ec, "shutdown");
|
||||
|
||||
@@ -373,13 +383,13 @@ class listener
|
||||
|
||||
public:
|
||||
listener(
|
||||
boost::asio::io_service& ios,
|
||||
boost::asio::io_context& ioc,
|
||||
ssl::context& ctx,
|
||||
tcp::endpoint endpoint,
|
||||
std::string const& doc_root)
|
||||
: ctx_(ctx)
|
||||
, acceptor_(ios)
|
||||
, socket_(ios)
|
||||
, acceptor_(ioc)
|
||||
, socket_(ioc)
|
||||
, doc_root_(doc_root)
|
||||
{
|
||||
boost::system::error_code ec;
|
||||
@@ -402,7 +412,7 @@ public:
|
||||
|
||||
// Start listening for connections
|
||||
acceptor_.listen(
|
||||
boost::asio::socket_base::max_connections, ec);
|
||||
boost::asio::socket_base::max_listen_connections, ec);
|
||||
if(ec)
|
||||
{
|
||||
fail(ec, "listen");
|
||||
@@ -464,13 +474,13 @@ int main(int argc, char* argv[])
|
||||
" http-server-stackless-ssl 0.0.0.0 8080 . 1\n";
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
auto const address = boost::asio::ip::address::from_string(argv[1]);
|
||||
auto const address = boost::asio::ip::make_address(argv[1]);
|
||||
auto const port = static_cast<unsigned short>(std::atoi(argv[2]));
|
||||
std::string const doc_root = argv[3];
|
||||
auto const threads = std::max<std::size_t>(1, std::atoi(argv[4]));
|
||||
auto const threads = std::max<int>(1, std::atoi(argv[4]));
|
||||
|
||||
// The io_service is required for all I/O
|
||||
boost::asio::io_service ios{threads};
|
||||
// The io_context is required for all I/O
|
||||
boost::asio::io_context ioc{threads};
|
||||
|
||||
// The SSL context is required, and holds certificates
|
||||
ssl::context ctx{ssl::context::sslv23};
|
||||
@@ -480,7 +490,7 @@ int main(int argc, char* argv[])
|
||||
|
||||
// Create and launch a listening port
|
||||
std::make_shared<listener>(
|
||||
ios,
|
||||
ioc,
|
||||
ctx,
|
||||
tcp::endpoint{address, port},
|
||||
doc_root)->run();
|
||||
@@ -490,11 +500,11 @@ int main(int argc, char* argv[])
|
||||
v.reserve(threads - 1);
|
||||
for(auto i = threads - 1; i > 0; --i)
|
||||
v.emplace_back(
|
||||
[&ios]
|
||||
[&ioc]
|
||||
{
|
||||
ios.run();
|
||||
ioc.run();
|
||||
});
|
||||
ios.run();
|
||||
ioc.run();
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
#include <boost/beast/core.hpp>
|
||||
#include <boost/beast/http.hpp>
|
||||
#include <boost/beast/version.hpp>
|
||||
#include <boost/asio/bind_executor.hpp>
|
||||
#include <boost/asio/coroutine.hpp>
|
||||
#include <boost/asio/ip/tcp.hpp>
|
||||
#include <boost/asio/strand.hpp>
|
||||
@@ -244,17 +245,20 @@ class session
|
||||
http::async_write(
|
||||
self_.socket_,
|
||||
*sp,
|
||||
self_.strand_.wrap(std::bind(
|
||||
&session::loop,
|
||||
self_.shared_from_this(),
|
||||
std::placeholders::_1,
|
||||
std::placeholders::_2,
|
||||
! sp->keep_alive())));
|
||||
boost::asio::bind_executor(
|
||||
self_.strand_,
|
||||
std::bind(
|
||||
&session::loop,
|
||||
self_.shared_from_this(),
|
||||
std::placeholders::_1,
|
||||
std::placeholders::_2,
|
||||
! sp->keep_alive())));
|
||||
}
|
||||
};
|
||||
|
||||
tcp::socket socket_;
|
||||
boost::asio::io_service::strand strand_;
|
||||
boost::asio::strand<
|
||||
boost::asio::io_context::executor_type> strand_;
|
||||
boost::beast::flat_buffer buffer_;
|
||||
std::string const& doc_root_;
|
||||
http::request<http::string_body> req_;
|
||||
@@ -268,7 +272,7 @@ public:
|
||||
tcp::socket socket,
|
||||
std::string const& doc_root)
|
||||
: socket_(std::move(socket))
|
||||
, strand_(socket_.get_io_service())
|
||||
, strand_(socket_.get_executor())
|
||||
, doc_root_(doc_root)
|
||||
, lambda_(*this)
|
||||
{
|
||||
@@ -295,12 +299,14 @@ public:
|
||||
{
|
||||
// Read a request
|
||||
yield http::async_read(socket_, buffer_, req_,
|
||||
strand_.wrap(std::bind(
|
||||
&session::loop,
|
||||
shared_from_this(),
|
||||
std::placeholders::_1,
|
||||
std::placeholders::_2,
|
||||
false)));
|
||||
boost::asio::bind_executor(
|
||||
strand_,
|
||||
std::bind(
|
||||
&session::loop,
|
||||
shared_from_this(),
|
||||
std::placeholders::_1,
|
||||
std::placeholders::_2,
|
||||
false)));
|
||||
if(ec == http::error::end_of_stream)
|
||||
{
|
||||
// The remote host closed the connection
|
||||
@@ -346,11 +352,11 @@ class listener
|
||||
|
||||
public:
|
||||
listener(
|
||||
boost::asio::io_service& ios,
|
||||
boost::asio::io_context& ioc,
|
||||
tcp::endpoint endpoint,
|
||||
std::string const& doc_root)
|
||||
: acceptor_(ios)
|
||||
, socket_(ios)
|
||||
: acceptor_(ioc)
|
||||
, socket_(ioc)
|
||||
, doc_root_(doc_root)
|
||||
{
|
||||
boost::system::error_code ec;
|
||||
@@ -372,7 +378,7 @@ public:
|
||||
}
|
||||
|
||||
// Start listening for connections
|
||||
acceptor_.listen(boost::asio::socket_base::max_connections, ec);
|
||||
acceptor_.listen(boost::asio::socket_base::max_listen_connections, ec);
|
||||
if(ec)
|
||||
{
|
||||
fail(ec, "listen");
|
||||
@@ -433,17 +439,17 @@ int main(int argc, char* argv[])
|
||||
" http-server-stackless 0.0.0.0 8080 . 1\n";
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
auto const address = boost::asio::ip::address::from_string(argv[1]);
|
||||
auto const address = boost::asio::ip::make_address(argv[1]);
|
||||
auto const port = static_cast<unsigned short>(std::atoi(argv[2]));
|
||||
std::string const doc_root = argv[3];
|
||||
auto const threads = std::max<std::size_t>(1, std::atoi(argv[4]));
|
||||
auto const threads = std::max<int>(1, std::atoi(argv[4]));
|
||||
|
||||
// The io_service is required for all I/O
|
||||
boost::asio::io_service ios{threads};
|
||||
// The io_context is required for all I/O
|
||||
boost::asio::io_context ioc{threads};
|
||||
|
||||
// Create and launch a listening port
|
||||
std::make_shared<listener>(
|
||||
ios,
|
||||
ioc,
|
||||
tcp::endpoint{address, port},
|
||||
doc_root)->run();
|
||||
|
||||
@@ -452,11 +458,11 @@ int main(int argc, char* argv[])
|
||||
v.reserve(threads - 1);
|
||||
for(auto i = threads - 1; i > 0; --i)
|
||||
v.emplace_back(
|
||||
[&ios]
|
||||
[&ioc]
|
||||
{
|
||||
ios.run();
|
||||
ioc.run();
|
||||
});
|
||||
ios.run();
|
||||
ioc.run();
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
@@ -311,12 +311,12 @@ int main(int argc, char* argv[])
|
||||
" http-server-sync-ssl 0.0.0.0 8080 .\n";
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
auto const address = boost::asio::ip::address::from_string(argv[1]);
|
||||
auto const address = boost::asio::ip::make_address(argv[1]);
|
||||
auto const port = static_cast<unsigned short>(std::atoi(argv[2]));
|
||||
std::string const doc_root = argv[3];
|
||||
|
||||
// The io_service is required for all I/O
|
||||
boost::asio::io_service ios{1};
|
||||
// The io_context is required for all I/O
|
||||
boost::asio::io_context ioc{1};
|
||||
|
||||
// The SSL context is required, and holds certificates
|
||||
ssl::context ctx{ssl::context::sslv23};
|
||||
@@ -325,11 +325,11 @@ int main(int argc, char* argv[])
|
||||
load_server_certificate(ctx);
|
||||
|
||||
// The acceptor receives incoming connections
|
||||
tcp::acceptor acceptor{ios, {address, port}};
|
||||
tcp::acceptor acceptor{ioc, {address, port}};
|
||||
for(;;)
|
||||
{
|
||||
// This will receive the new connection
|
||||
tcp::socket socket{ios};
|
||||
tcp::socket socket{ioc};
|
||||
|
||||
// Block until we get a connection
|
||||
acceptor.accept(socket);
|
||||
|
||||
@@ -298,19 +298,19 @@ int main(int argc, char* argv[])
|
||||
" http-server-sync 0.0.0.0 8080 .\n";
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
auto const address = boost::asio::ip::address::from_string(argv[1]);
|
||||
auto const address = boost::asio::ip::make_address(argv[1]);
|
||||
auto const port = static_cast<unsigned short>(std::atoi(argv[2]));
|
||||
std::string const doc_root = argv[3];
|
||||
|
||||
// The io_service is required for all I/O
|
||||
boost::asio::io_service ios{1};
|
||||
// The io_context is required for all I/O
|
||||
boost::asio::io_context ioc{1};
|
||||
|
||||
// The acceptor receives incoming connections
|
||||
tcp::acceptor acceptor{ios, {address, port}};
|
||||
tcp::acceptor acceptor{ioc, {address, port}};
|
||||
for(;;)
|
||||
{
|
||||
// This will receive the new connection
|
||||
tcp::socket socket{ios};
|
||||
tcp::socket socket{ioc};
|
||||
|
||||
// Block until we get a connection
|
||||
acceptor.accept(socket);
|
||||
|
||||
Reference in New Issue
Block a user