net is a namespace alias for boost::asio:

The namespace alias beast::net replaces boost::asio in all code
and documentation.
This commit is contained in:
Vinnie Falco
2018-12-02 17:51:43 -08:00
parent a5739c3ea0
commit f995fd41a3
185 changed files with 2486 additions and 2392 deletions
@@ -28,15 +28,17 @@
#include <memory>
#include <string>
using tcp = boost::asio::ip::tcp; // from <boost/asio/ip/tcp.hpp>
namespace beast = boost::beast; // from <boost/beast.hpp>
namespace http = beast::http; // from <boost/beast/http.hpp>
namespace net = boost::asio; // from <boost/asio.hpp>
namespace ssl = boost::asio::ssl; // from <boost/asio/ssl.hpp>
namespace http = boost::beast::http; // from <boost/beast/http.hpp>
using tcp = boost::asio::ip::tcp; // from <boost/asio/ip/tcp.hpp>
//------------------------------------------------------------------------------
// Report a failure
void
fail(boost::system::error_code ec, char const* what)
fail(beast::error_code ec, char const* what)
{
std::cerr << what << ": " << ec.message() << "\n";
}
@@ -46,14 +48,14 @@ class session : public std::enable_shared_from_this<session>
{
tcp::resolver resolver_;
ssl::stream<tcp::socket> stream_;
boost::beast::flat_buffer buffer_; // (Must persist between reads)
beast::flat_buffer buffer_; // (Must persist between reads)
http::request<http::empty_body> req_;
http::response<http::string_body> res_;
public:
// Resolver and stream require an io_context
explicit
session(boost::asio::io_context& ioc, ssl::context& ctx)
session(net::io_context& ioc, ssl::context& ctx)
: resolver_(ioc)
, stream_(ioc, ctx)
{
@@ -70,7 +72,7 @@ public:
// Set SNI Hostname (many hosts need this to handshake successfully)
if(! SSL_set_tlsext_host_name(stream_.native_handle(), host))
{
boost::system::error_code ec{static_cast<int>(::ERR_get_error()), boost::asio::error::get_ssl_category()};
beast::error_code ec{static_cast<int>(::ERR_get_error()), net::error::get_ssl_category()};
std::cerr << ec.message() << "\n";
return;
}
@@ -95,14 +97,14 @@ public:
void
on_resolve(
boost::system::error_code ec,
beast::error_code ec,
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(
net::async_connect(
stream_.next_layer(),
results.begin(),
results.end(),
@@ -113,7 +115,7 @@ public:
}
void
on_connect(boost::system::error_code ec)
on_connect(beast::error_code ec)
{
if(ec)
return fail(ec, "connect");
@@ -128,7 +130,7 @@ public:
}
void
on_handshake(boost::system::error_code ec)
on_handshake(beast::error_code ec)
{
if(ec)
return fail(ec, "handshake");
@@ -144,7 +146,7 @@ public:
void
on_write(
boost::system::error_code ec,
beast::error_code ec,
std::size_t bytes_transferred)
{
boost::ignore_unused(bytes_transferred);
@@ -163,7 +165,7 @@ public:
void
on_read(
boost::system::error_code ec,
beast::error_code ec,
std::size_t bytes_transferred)
{
boost::ignore_unused(bytes_transferred);
@@ -183,9 +185,9 @@ public:
}
void
on_shutdown(boost::system::error_code ec)
on_shutdown(beast::error_code ec)
{
if(ec == boost::asio::error::eof)
if(ec == net::error::eof)
{
// Rationale:
// http://stackoverflow.com/questions/25587403/boost-asio-ssl-async-shutdown-always-finishes-with-an-error
@@ -218,7 +220,7 @@ int main(int argc, char** argv)
int version = argc == 5 && !std::strcmp("1.0", argv[4]) ? 10 : 11;
// The io_context is required for all I/O
boost::asio::io_context ioc;
net::io_context ioc;
// The SSL context is required, and holds certificates
ssl::context ctx{ssl::context::sslv23_client};
+13 -11
View File
@@ -24,14 +24,16 @@
#include <memory>
#include <string>
namespace beast = boost::beast; // from <boost/beast.hpp>
namespace http = beast::http; // from <boost/beast/http.hpp>
namespace net = boost::asio; // from <boost/asio.hpp>
using tcp = boost::asio::ip::tcp; // from <boost/asio/ip/tcp.hpp>
namespace http = boost::beast::http; // from <boost/beast/http.hpp>
//------------------------------------------------------------------------------
// Report a failure
void
fail(boost::system::error_code ec, char const* what)
fail(beast::error_code ec, char const* what)
{
std::cerr << what << ": " << ec.message() << "\n";
}
@@ -41,14 +43,14 @@ class session : public std::enable_shared_from_this<session>
{
tcp::resolver resolver_;
tcp::socket socket_;
boost::beast::flat_buffer buffer_; // (Must persist between reads)
beast::flat_buffer buffer_; // (Must persist between reads)
http::request<http::empty_body> req_;
http::response<http::string_body> res_;
public:
// Resolver and socket require an io_context
explicit
session(boost::asio::io_context& ioc)
session(net::io_context& ioc)
: resolver_(ioc)
, socket_(ioc)
{
@@ -82,14 +84,14 @@ public:
void
on_resolve(
boost::system::error_code ec,
beast::error_code ec,
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(
net::async_connect(
socket_,
results.begin(),
results.end(),
@@ -100,7 +102,7 @@ public:
}
void
on_connect(boost::system::error_code ec)
on_connect(beast::error_code ec)
{
if(ec)
return fail(ec, "connect");
@@ -116,7 +118,7 @@ public:
void
on_write(
boost::system::error_code ec,
beast::error_code ec,
std::size_t bytes_transferred)
{
boost::ignore_unused(bytes_transferred);
@@ -135,7 +137,7 @@ public:
void
on_read(
boost::system::error_code ec,
beast::error_code ec,
std::size_t bytes_transferred)
{
boost::ignore_unused(bytes_transferred);
@@ -150,7 +152,7 @@ public:
socket_.shutdown(tcp::socket::shutdown_both, ec);
// not_connected happens sometimes so don't bother reporting it.
if(ec && ec != boost::system::errc::not_connected)
if(ec && ec != beast::errc::not_connected)
return fail(ec, "shutdown");
// If we get here then the connection is closed gracefully
@@ -177,7 +179,7 @@ int main(int argc, char** argv)
int version = argc == 5 && !std::strcmp("1.0", argv[4]) ? 10 : 11;
// The io_context is required for all I/O
boost::asio::io_context ioc;
net::io_context ioc;
// Launch the asynchronous operation
std::make_shared<session>(ioc)->run(host, port, target, version);
@@ -28,15 +28,17 @@
#include <iostream>
#include <string>
using tcp = boost::asio::ip::tcp; // from <boost/asio/ip/tcp.hpp>
namespace beast = boost::beast; // from <boost/beast.hpp>
namespace http = beast::http; // from <boost/beast/http.hpp>
namespace net = boost::asio; // from <boost/asio.hpp>
namespace ssl = boost::asio::ssl; // from <boost/asio/ssl.hpp>
namespace http = boost::beast::http; // from <boost/beast/http.hpp>
using tcp = boost::asio::ip::tcp; // from <boost/asio/ip/tcp.hpp>
//------------------------------------------------------------------------------
// Report a failure
void
fail(boost::system::error_code ec, char const* what)
fail(beast::error_code ec, char const* what)
{
std::cerr << what << ": " << ec.message() << "\n";
}
@@ -48,11 +50,11 @@ do_session(
std::string const& port,
std::string const& target,
int version,
boost::asio::io_context& ioc,
net::io_context& ioc,
ssl::context& ctx,
boost::asio::yield_context yield)
net::yield_context yield)
{
boost::system::error_code ec;
beast::error_code ec;
// These objects perform our I/O
tcp::resolver resolver{ioc};
@@ -61,7 +63,7 @@ do_session(
// Set SNI Hostname (many hosts need this to handshake successfully)
if(! SSL_set_tlsext_host_name(stream.native_handle(), host.c_str()))
{
ec.assign(static_cast<int>(::ERR_get_error()), boost::asio::error::get_ssl_category());
ec.assign(static_cast<int>(::ERR_get_error()), net::error::get_ssl_category());
std::cerr << ec.message() << "\n";
return;
}
@@ -72,7 +74,7 @@ do_session(
return fail(ec, "resolve");
// Make the connection on the IP address we get from a lookup
boost::asio::async_connect(stream.next_layer(), results.begin(), results.end(), yield[ec]);
net::async_connect(stream.next_layer(), results.begin(), results.end(), yield[ec]);
if(ec)
return fail(ec, "connect");
@@ -92,7 +94,7 @@ do_session(
return fail(ec, "write");
// This buffer is used for reading and must be persisted
boost::beast::flat_buffer b;
beast::flat_buffer b;
// Declare a container to hold the response
http::response<http::dynamic_body> res;
@@ -107,7 +109,7 @@ do_session(
// Gracefully close the stream
stream.async_shutdown(yield[ec]);
if(ec == boost::asio::error::eof)
if(ec == net::error::eof)
{
// Rationale:
// http://stackoverflow.com/questions/25587403/boost-asio-ssl-async-shutdown-always-finishes-with-an-error
@@ -139,7 +141,7 @@ int main(int argc, char** argv)
int version = argc == 5 && !std::strcmp("1.0", argv[4]) ? 10 : 11;
// The io_context is required for all I/O
boost::asio::io_context ioc;
net::io_context ioc;
// The SSL context is required, and holds certificates
ssl::context ctx{ssl::context::sslv23_client};
@@ -151,7 +153,7 @@ int main(int argc, char** argv)
ctx.set_verify_mode(ssl::verify_peer);
// Launch the asynchronous operation
boost::asio::spawn(ioc, std::bind(
net::spawn(ioc, std::bind(
&do_session,
std::string(host),
std::string(port),
+12 -10
View File
@@ -24,14 +24,16 @@
#include <iostream>
#include <string>
namespace beast = boost::beast; // from <boost/beast.hpp>
namespace http = beast::http; // from <boost/beast/http.hpp>
namespace net = boost::asio; // from <boost/asio.hpp>
using tcp = boost::asio::ip::tcp; // from <boost/asio/ip/tcp.hpp>
namespace http = boost::beast::http; // from <boost/beast/http.hpp>
//------------------------------------------------------------------------------
// Report a failure
void
fail(boost::system::error_code ec, char const* what)
fail(beast::error_code ec, char const* what)
{
std::cerr << what << ": " << ec.message() << "\n";
}
@@ -43,10 +45,10 @@ do_session(
std::string const& port,
std::string const& target,
int version,
boost::asio::io_context& ioc,
boost::asio::yield_context yield)
net::io_context& ioc,
net::yield_context yield)
{
boost::system::error_code ec;
beast::error_code ec;
// These objects perform our I/O
tcp::resolver resolver{ioc};
@@ -58,7 +60,7 @@ do_session(
return fail(ec, "resolve");
// Make the connection on the IP address we get from a lookup
boost::asio::async_connect(socket, results.begin(), results.end(), yield[ec]);
net::async_connect(socket, results.begin(), results.end(), yield[ec]);
if(ec)
return fail(ec, "connect");
@@ -73,7 +75,7 @@ do_session(
return fail(ec, "write");
// This buffer is used for reading and must be persisted
boost::beast::flat_buffer b;
beast::flat_buffer b;
// Declare a container to hold the response
http::response<http::dynamic_body> res;
@@ -92,7 +94,7 @@ do_session(
// not_connected happens sometimes
// so don't bother reporting it.
//
if(ec && ec != boost::system::errc::not_connected)
if(ec && ec != beast::errc::not_connected)
return fail(ec, "shutdown");
// If we get here then the connection is closed gracefully
@@ -118,10 +120,10 @@ int main(int argc, char** argv)
int version = argc == 5 && !std::strcmp("1.0", argv[4]) ? 10 : 11;
// The io_context is required for all I/O
boost::asio::io_context ioc;
net::io_context ioc;
// Launch the asynchronous operation
boost::asio::spawn(ioc, std::bind(
net::spawn(ioc, std::bind(
&do_session,
std::string(host),
std::string(port),
+29 -27
View File
@@ -35,24 +35,26 @@
#include <vector>
#include <map>
using tcp = boost::asio::ip::tcp; // from <boost/asio/ip/tcp.hpp>
namespace http = boost::beast::http; // from <boost/beast/http.hpp>
namespace chrono = std::chrono; // from <chrono>
namespace beast = boost::beast; // from <boost/beast.hpp>
namespace http = beast::http; // from <boost/beast/http.hpp>
namespace net = boost::asio; // from <boost/asio.hpp>
using tcp = net::ip::tcp; // from <boost/asio/ip/tcp.hpp>
//------------------------------------------------------------------------------
// This structure aggregates statistics on all the sites
class crawl_report
{
boost::asio::io_context& ioc_;
boost::asio::strand<
boost::asio::io_context::executor_type> strand_;
net::io_context& ioc_;
net::strand<
net::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_context& ioc)
crawl_report(net::io_context& ioc)
: ioc_(ioc)
, strand_(ioc_.get_executor())
, index_(0)
@@ -66,7 +68,7 @@ public:
void
aggregate(F const& f)
{
boost::asio::post(
net::post(
strand_,
[&, f]
{
@@ -150,10 +152,10 @@ class worker : public std::enable_shared_from_this<worker>
crawl_report& report_;
tcp::resolver resolver_;
tcp::socket socket_;
boost::asio::steady_timer timer_;
boost::asio::strand<
boost::asio::io_context::executor_type> strand_;
boost::beast::flat_buffer buffer_; // (Must persist between reads)
net::steady_timer timer_;
net::strand<
net::io_context::executor_type> strand_;
beast::flat_buffer buffer_; // (Must persist between reads)
http::request<http::empty_body> req_;
http::response<http::string_body> res_;
@@ -163,7 +165,7 @@ public:
// Resolver and socket require an io_context
worker(
crawl_report& report,
boost::asio::io_context& ioc)
net::io_context& ioc)
: report_(report)
, resolver_(ioc)
, socket_(ioc)
@@ -190,9 +192,9 @@ public:
}
void
on_timer(boost::system::error_code ec)
on_timer(beast::error_code ec)
{
if(ec && ec != boost::asio::error::operation_aborted)
if(ec && ec != net::error::operation_aborted)
{
// Should never happen
report_.aggregate(
@@ -213,7 +215,7 @@ public:
// Wait on the timer
timer_.async_wait(
boost::asio::bind_executor(
net::bind_executor(
strand_,
std::bind(
&worker::on_timer,
@@ -245,7 +247,7 @@ public:
resolver_.async_resolve(
host,
"http",
boost::asio::bind_executor(
net::bind_executor(
strand_,
std::bind(
&worker::on_resolve,
@@ -256,7 +258,7 @@ public:
void
on_resolve(
boost::system::error_code ec,
beast::error_code ec,
tcp::resolver::results_type results)
{
if(ec)
@@ -273,11 +275,11 @@ public:
timer_.expires_after(chrono::seconds(timeout));
// Make the connection on the IP address we get from a lookup
boost::asio::async_connect(
net::async_connect(
socket_,
results.begin(),
results.end(),
boost::asio::bind_executor(
net::bind_executor(
strand_,
std::bind(
&worker::on_connect,
@@ -286,7 +288,7 @@ public:
}
void
on_connect(boost::system::error_code ec)
on_connect(beast::error_code ec)
{
if(ec)
{
@@ -305,7 +307,7 @@ public:
http::async_write(
socket_,
req_,
boost::asio::bind_executor(
net::bind_executor(
strand_,
std::bind(
&worker::on_write,
@@ -316,7 +318,7 @@ public:
void
on_write(
boost::system::error_code ec,
beast::error_code ec,
std::size_t bytes_transferred)
{
boost::ignore_unused(bytes_transferred);
@@ -339,7 +341,7 @@ public:
socket_,
buffer_,
res_,
boost::asio::bind_executor(
net::bind_executor(
strand_,
std::bind(
&worker::on_read,
@@ -350,7 +352,7 @@ public:
void
on_read(
boost::system::error_code ec,
beast::error_code ec,
std::size_t bytes_transferred)
{
boost::ignore_unused(bytes_transferred);
@@ -418,10 +420,10 @@ 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
boost::asio::io_context ioc{1};
net::io_context ioc{1};
// The work keeps io_context::run from returning
auto work = boost::asio::make_work_guard(ioc);
auto work = net::make_work_guard(ioc);
// The report holds the aggregated statistics
crawl_report report{ioc};
@@ -439,7 +441,7 @@ int main(int argc, char* argv[])
// the asio resolver simulates asynchronous operation using
// a dedicated worker thread per io_context, and we want to
// do a lot of name resolutions in parallel.
boost::asio::io_context ioc{1};
net::io_context ioc{1};
std::make_shared<worker>(report, ioc)->run();
ioc.run();
});
@@ -26,9 +26,11 @@
#include <iostream>
#include <string>
using tcp = boost::asio::ip::tcp; // from <boost/asio/ip/tcp.hpp>
namespace ssl = boost::asio::ssl; // from <boost/asio/ssl.hpp>
namespace http = boost::beast::http; // from <boost/beast/http.hpp>
namespace beast = boost::beast; // from <boost/beast.hpp>
namespace http = beast::http; // from <boost/beast/http.hpp>
namespace net = boost::asio; // from <boost/asio.hpp>
namespace ssl = net::ssl; // from <boost/asio/ssl.hpp>
using tcp = net::ip::tcp; // from <boost/asio/ip/tcp.hpp>
// Performs an HTTP GET and prints the response
int main(int argc, char** argv)
@@ -51,7 +53,7 @@ int main(int argc, char** argv)
int version = argc == 5 && !std::strcmp("1.0", argv[4]) ? 10 : 11;
// The io_context is required for all I/O
boost::asio::io_context ioc;
net::io_context ioc;
// The SSL context is required, and holds certificates
ssl::context ctx{ssl::context::sslv23_client};
@@ -69,15 +71,15 @@ int main(int argc, char** argv)
// Set SNI Hostname (many hosts need this to handshake successfully)
if(! SSL_set_tlsext_host_name(stream.native_handle(), host))
{
boost::system::error_code ec{static_cast<int>(::ERR_get_error()), boost::asio::error::get_ssl_category()};
throw boost::system::system_error{ec};
beast::error_code ec{static_cast<int>(::ERR_get_error()), net::error::get_ssl_category()};
throw beast::system_error{ec};
}
// Look up the domain name
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(), results.begin(), results.end());
net::connect(stream.next_layer(), results.begin(), results.end());
// Perform the SSL handshake
stream.handshake(ssl::stream_base::client);
@@ -91,7 +93,7 @@ int main(int argc, char** argv)
http::write(stream, req);
// This buffer is used for reading and must be persisted
boost::beast::flat_buffer buffer;
beast::flat_buffer buffer;
// Declare a container to hold the response
http::response<http::dynamic_body> res;
@@ -103,16 +105,16 @@ int main(int argc, char** argv)
std::cout << res << std::endl;
// Gracefully close the stream
boost::system::error_code ec;
beast::error_code ec;
stream.shutdown(ec);
if(ec == boost::asio::error::eof)
if(ec == net::error::eof)
{
// Rationale:
// http://stackoverflow.com/questions/25587403/boost-asio-ssl-async-shutdown-always-finishes-with-an-error
ec.assign(0, ec.category());
}
if(ec)
throw boost::system::system_error{ec};
throw beast::system_error{ec};
// If we get here then the connection is closed gracefully
}
+10 -8
View File
@@ -24,8 +24,10 @@
#include <iostream>
#include <string>
using tcp = boost::asio::ip::tcp; // from <boost/asio/ip/tcp.hpp>
namespace http = boost::beast::http; // from <boost/beast/http.hpp>
namespace beast = boost::beast; // from <boost/beast.hpp>
namespace http = beast::http; // from <boost/beast/http.hpp>
namespace net = boost::asio; // from <boost/asio.hpp>
using tcp = net::ip::tcp; // from <boost/asio/ip/tcp.hpp>
// Performs an HTTP GET and prints the response
int main(int argc, char** argv)
@@ -48,7 +50,7 @@ int main(int argc, char** argv)
int version = argc == 5 && !std::strcmp("1.0", argv[4]) ? 10 : 11;
// The io_context is required for all I/O
boost::asio::io_context ioc;
net::io_context ioc;
// These objects perform our I/O
tcp::resolver resolver{ioc};
@@ -58,7 +60,7 @@ int main(int argc, char** argv)
auto const results = resolver.resolve(host, port);
// Make the connection on the IP address we get from a lookup
boost::asio::connect(socket, results.begin(), results.end());
net::connect(socket, results.begin(), results.end());
// Set up an HTTP GET request message
http::request<http::string_body> req{http::verb::get, target, version};
@@ -69,7 +71,7 @@ int main(int argc, char** argv)
http::write(socket, req);
// This buffer is used for reading and must be persisted
boost::beast::flat_buffer buffer;
beast::flat_buffer buffer;
// Declare a container to hold the response
http::response<http::dynamic_body> res;
@@ -81,14 +83,14 @@ int main(int argc, char** argv)
std::cout << res << std::endl;
// Gracefully close the socket
boost::system::error_code ec;
beast::error_code ec;
socket.shutdown(tcp::socket::shutdown_both, ec);
// not_connected happens sometimes
// so don't bother reporting it.
//
if(ec && ec != boost::system::errc::not_connected)
throw boost::system::system_error{ec};
if(ec && ec != beast::errc::not_connected)
throw beast::system_error{ec};
// If we get here then the connection is closed gracefully
}