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
@@ -43,15 +43,17 @@
#include <thread>
#include <vector>
using tcp = boost::asio::ip::tcp; // from <boost/asio/ip/tcp.hpp>
namespace http = boost::beast::http; // from <boost/beast/http.hpp>
namespace websocket = boost::beast::websocket; // from <boost/beast/websocket.hpp>
namespace beast = boost::beast; // from <boost/beast.hpp>
namespace http = beast::http; // from <boost/beast/http.hpp>
namespace websocket = beast::websocket; // from <boost/beast/websocket.hpp>
namespace net = boost::asio; // from <boost/asio.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 << (std::string(what) + ": " + ec.message() + "\n");
}
@@ -81,7 +83,7 @@ setup_stream(websocket::stream<NextLayer>& ws)
void
do_sync_session(tcp::socket& socket)
{
boost::system::error_code ec;
beast::error_code ec;
websocket::stream<tcp::socket> ws{std::move(socket)};
setup_stream(ws);
@@ -98,7 +100,7 @@ do_sync_session(tcp::socket& socket)
for(;;)
{
boost::beast::multi_buffer buffer;
beast::multi_buffer buffer;
ws.read(buffer, ec);
if(ec == websocket::error::closed)
@@ -114,10 +116,10 @@ do_sync_session(tcp::socket& socket)
void
do_sync_listen(
boost::asio::io_context& ioc,
net::io_context& ioc,
tcp::endpoint endpoint)
{
boost::system::error_code ec;
beast::error_code ec;
tcp::acceptor acceptor{ioc, endpoint};
for(;;)
{
@@ -139,9 +141,9 @@ do_sync_listen(
class async_session : public std::enable_shared_from_this<async_session>
{
websocket::stream<tcp::socket> ws_;
boost::asio::strand<
boost::asio::io_context::executor_type> strand_;
boost::beast::multi_buffer buffer_;
net::strand<
net::io_context::executor_type> strand_;
beast::multi_buffer buffer_;
public:
// Take ownership of the socket
@@ -164,7 +166,7 @@ public:
res.set(http::field::server,
"Boost.Beast/" + std::to_string(BOOST_BEAST_VERSION) + "-Async");
},
boost::asio::bind_executor(
net::bind_executor(
strand_,
std::bind(
&async_session::on_accept,
@@ -173,7 +175,7 @@ public:
}
void
on_accept(boost::system::error_code ec)
on_accept(beast::error_code ec)
{
if(ec)
return fail(ec, "accept");
@@ -188,7 +190,7 @@ public:
// Read a message into our buffer
ws_.async_read(
buffer_,
boost::asio::bind_executor(
net::bind_executor(
strand_,
std::bind(
&async_session::on_read,
@@ -199,7 +201,7 @@ public:
void
on_read(
boost::system::error_code ec,
beast::error_code ec,
std::size_t bytes_transferred)
{
boost::ignore_unused(bytes_transferred);
@@ -215,7 +217,7 @@ public:
ws_.text(ws_.got_text());
ws_.async_write(
buffer_.data(),
boost::asio::bind_executor(
net::bind_executor(
strand_,
std::bind(
&async_session::on_write,
@@ -226,7 +228,7 @@ public:
void
on_write(
boost::system::error_code ec,
beast::error_code ec,
std::size_t bytes_transferred)
{
boost::ignore_unused(bytes_transferred);
@@ -245,20 +247,20 @@ public:
// Accepts incoming connections and launches the sessions
class async_listener : public std::enable_shared_from_this<async_listener>
{
boost::asio::strand<
boost::asio::io_context::executor_type> strand_;
net::strand<
net::io_context::executor_type> strand_;
tcp::acceptor acceptor_;
tcp::socket socket_;
public:
async_listener(
boost::asio::io_context& ioc,
net::io_context& ioc,
tcp::endpoint endpoint)
: strand_(ioc.get_executor())
, acceptor_(ioc)
, socket_(ioc)
{
boost::system::error_code ec;
beast::error_code ec;
// Open the acceptor
acceptor_.open(endpoint.protocol(), ec);
@@ -269,7 +271,7 @@ public:
}
// Allow address reuse
acceptor_.set_option(boost::asio::socket_base::reuse_address(true), ec);
acceptor_.set_option(net::socket_base::reuse_address(true), ec);
if(ec)
{
fail(ec, "set_option");
@@ -286,7 +288,7 @@ public:
// Start listening for connections
acceptor_.listen(
boost::asio::socket_base::max_listen_connections, ec);
net::socket_base::max_listen_connections, ec);
if(ec)
{
fail(ec, "listen");
@@ -308,7 +310,7 @@ public:
{
acceptor_.async_accept(
socket_,
boost::asio::bind_executor(
net::bind_executor(
strand_,
std::bind(
&async_listener::on_accept,
@@ -317,7 +319,7 @@ public:
}
void
on_accept(boost::system::error_code ec)
on_accept(beast::error_code ec)
{
if(ec)
{
@@ -337,9 +339,9 @@ public:
//------------------------------------------------------------------------------
void
do_coro_session(tcp::socket& socket, boost::asio::yield_context yield)
do_coro_session(tcp::socket& socket, net::yield_context yield)
{
boost::system::error_code ec;
beast::error_code ec;
websocket::stream<tcp::socket> ws{std::move(socket)};
setup_stream(ws);
@@ -356,7 +358,7 @@ do_coro_session(tcp::socket& socket, boost::asio::yield_context yield)
for(;;)
{
boost::beast::multi_buffer buffer;
beast::multi_buffer buffer;
ws.async_read(buffer, yield[ec]);
if(ec == websocket::error::closed)
@@ -373,18 +375,18 @@ do_coro_session(tcp::socket& socket, boost::asio::yield_context yield)
void
do_coro_listen(
boost::asio::io_context& ioc,
net::io_context& ioc,
tcp::endpoint endpoint,
boost::asio::yield_context yield)
net::yield_context yield)
{
boost::system::error_code ec;
beast::error_code ec;
tcp::acceptor acceptor(ioc);
acceptor.open(endpoint.protocol(), ec);
if(ec)
return fail(ec, "open");
acceptor.set_option(boost::asio::socket_base::reuse_address(true), ec);
acceptor.set_option(net::socket_base::reuse_address(true), ec);
if(ec)
return fail(ec, "set_option");
@@ -392,7 +394,7 @@ do_coro_listen(
if(ec)
return fail(ec, "bind");
acceptor.listen(boost::asio::socket_base::max_listen_connections, ec);
acceptor.listen(net::socket_base::max_listen_connections, ec);
if(ec)
return fail(ec, "listen");
@@ -407,7 +409,7 @@ do_coro_listen(
continue;
}
boost::asio::spawn(
net::spawn(
acceptor.get_executor().context(),
std::bind(
&do_coro_session,
@@ -433,12 +435,12 @@ int main(int argc, char* argv[])
" starting-port+2 for coroutine.\n";
return EXIT_FAILURE;
}
auto const address = boost::asio::ip::make_address(argv[1]);
auto const address = net::ip::make_address(argv[1]);
auto const port = static_cast<unsigned short>(std::atoi(argv[2]));
auto const threads = std::max<int>(1, std::atoi(argv[3]));
// The io_context is required for all I/O
boost::asio::io_context ioc{threads};
net::io_context ioc{threads};
// Create sync port
std::thread(std::bind(
@@ -457,7 +459,7 @@ int main(int argc, char* argv[])
static_cast<unsigned short>(port + 1u)})->run();
// Create coro port
boost::asio::spawn(ioc,
net::spawn(ioc,
std::bind(
&do_coro_listen,
std::ref(ioc),