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
@@ -31,15 +31,18 @@
#include <thread>
#include <vector>
using tcp = boost::asio::ip::tcp; // from <boost/asio/ip/tcp.hpp>
namespace ssl = boost::asio::ssl; // from <boost/asio/ssl.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>
namespace ssl = boost::asio::ssl; // from <boost/asio/ssl.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";
}
@@ -49,9 +52,9 @@ class session : public std::enable_shared_from_this<session>
{
tcp::socket socket_;
websocket::stream<ssl::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
@@ -69,7 +72,7 @@ public:
// Perform the SSL handshake
ws_.next_layer().async_handshake(
ssl::stream_base::server,
boost::asio::bind_executor(
net::bind_executor(
strand_,
std::bind(
&session::on_handshake,
@@ -78,14 +81,14 @@ public:
}
void
on_handshake(boost::system::error_code ec)
on_handshake(beast::error_code ec)
{
if(ec)
return fail(ec, "handshake");
// Accept the websocket handshake
ws_.async_accept(
boost::asio::bind_executor(
net::bind_executor(
strand_,
std::bind(
&session::on_accept,
@@ -94,7 +97,7 @@ public:
}
void
on_accept(boost::system::error_code ec)
on_accept(beast::error_code ec)
{
if(ec)
return fail(ec, "accept");
@@ -109,7 +112,7 @@ public:
// Read a message into our buffer
ws_.async_read(
buffer_,
boost::asio::bind_executor(
net::bind_executor(
strand_,
std::bind(
&session::on_read,
@@ -120,7 +123,7 @@ public:
void
on_read(
boost::system::error_code ec,
beast::error_code ec,
std::size_t bytes_transferred)
{
boost::ignore_unused(bytes_transferred);
@@ -136,7 +139,7 @@ public:
ws_.text(ws_.got_text());
ws_.async_write(
buffer_.data(),
boost::asio::bind_executor(
net::bind_executor(
strand_,
std::bind(
&session::on_write,
@@ -147,7 +150,7 @@ public:
void
on_write(
boost::system::error_code ec,
beast::error_code ec,
std::size_t bytes_transferred)
{
boost::ignore_unused(bytes_transferred);
@@ -174,14 +177,14 @@ class listener : public std::enable_shared_from_this<listener>
public:
listener(
boost::asio::io_context& ioc,
net::io_context& ioc,
ssl::context& ctx,
tcp::endpoint endpoint)
: ctx_(ctx)
, acceptor_(ioc)
, socket_(ioc)
{
boost::system::error_code ec;
beast::error_code ec;
// Open the acceptor
acceptor_.open(endpoint.protocol(), ec);
@@ -192,7 +195,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");
@@ -209,7 +212,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");
@@ -238,7 +241,7 @@ public:
}
void
on_accept(boost::system::error_code ec)
on_accept(beast::error_code ec)
{
if(ec)
{
@@ -268,12 +271,12 @@ int main(int argc, char* argv[])
" websocket-server-async-ssl 0.0.0.0 8080 1\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};
// The SSL context is required, and holds certificates
ssl::context ctx{ssl::context::sslv23};
@@ -27,14 +27,17 @@
#include <thread>
#include <vector>
using tcp = boost::asio::ip::tcp; // from <boost/asio/ip/tcp.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 << what << ": " << ec.message() << "\n";
}
@@ -43,9 +46,9 @@ fail(boost::system::error_code ec, char const* what)
class session : public std::enable_shared_from_this<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
@@ -62,7 +65,7 @@ public:
{
// Accept the websocket handshake
ws_.async_accept(
boost::asio::bind_executor(
net::bind_executor(
strand_,
std::bind(
&session::on_accept,
@@ -71,7 +74,7 @@ public:
}
void
on_accept(boost::system::error_code ec)
on_accept(beast::error_code ec)
{
if(ec)
return fail(ec, "accept");
@@ -86,7 +89,7 @@ public:
// Read a message into our buffer
ws_.async_read(
buffer_,
boost::asio::bind_executor(
net::bind_executor(
strand_,
std::bind(
&session::on_read,
@@ -97,7 +100,7 @@ public:
void
on_read(
boost::system::error_code ec,
beast::error_code ec,
std::size_t bytes_transferred)
{
boost::ignore_unused(bytes_transferred);
@@ -113,7 +116,7 @@ public:
ws_.text(ws_.got_text());
ws_.async_write(
buffer_.data(),
boost::asio::bind_executor(
net::bind_executor(
strand_,
std::bind(
&session::on_write,
@@ -124,7 +127,7 @@ public:
void
on_write(
boost::system::error_code ec,
beast::error_code ec,
std::size_t bytes_transferred)
{
boost::ignore_unused(bytes_transferred);
@@ -150,12 +153,12 @@ class listener : public std::enable_shared_from_this<listener>
public:
listener(
boost::asio::io_context& ioc,
net::io_context& ioc,
tcp::endpoint endpoint)
: acceptor_(ioc)
, socket_(ioc)
{
boost::system::error_code ec;
beast::error_code ec;
// Open the acceptor
acceptor_.open(endpoint.protocol(), ec);
@@ -166,7 +169,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");
@@ -183,7 +186,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");
@@ -212,7 +215,7 @@ public:
}
void
on_accept(boost::system::error_code ec)
on_accept(beast::error_code ec)
{
if(ec)
{
@@ -242,12 +245,12 @@ int main(int argc, char* argv[])
" websocket-server-async 0.0.0.0 8080 1\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 and launch a listening port
std::make_shared<listener>(ioc, tcp::endpoint{address, port})->run();
@@ -30,15 +30,18 @@
#include <thread>
#include <vector>
using tcp = boost::asio::ip::tcp; // from <boost/asio/ip/tcp.hpp>
namespace ssl = boost::asio::ssl; // from <boost/asio/ssl.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>
namespace ssl = boost::asio::ssl; // from <boost/asio/ssl.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,9 +51,9 @@ void
do_session(
tcp::socket& socket,
ssl::context& ctx,
boost::asio::yield_context yield)
net::yield_context yield)
{
boost::system::error_code ec;
beast::error_code ec;
// Construct the stream by moving in the socket
websocket::stream<ssl::stream<tcp::socket&>> ws{socket, ctx};
@@ -68,7 +71,7 @@ do_session(
for(;;)
{
// This buffer will hold the incoming message
boost::beast::multi_buffer buffer;
beast::multi_buffer buffer;
// Read a message
ws.async_read(buffer, yield[ec]);
@@ -93,12 +96,12 @@ do_session(
// Accepts incoming connections and launches the sessions
void
do_listen(
boost::asio::io_context& ioc,
net::io_context& ioc,
ssl::context& ctx,
tcp::endpoint endpoint,
boost::asio::yield_context yield)
net::yield_context yield)
{
boost::system::error_code ec;
beast::error_code ec;
// Open the acceptor
tcp::acceptor acceptor(ioc);
@@ -107,7 +110,7 @@ do_listen(
return fail(ec, "open");
// 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)
return fail(ec, "set_option");
@@ -117,7 +120,7 @@ do_listen(
return fail(ec, "bind");
// Start listening for connections
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");
@@ -128,7 +131,7 @@ do_listen(
if(ec)
fail(ec, "accept");
else
boost::asio::spawn(
net::spawn(
acceptor.get_executor().context(),
std::bind(
&do_session,
@@ -149,12 +152,12 @@ int main(int argc, char* argv[])
" websocket-server-coro-ssl 0.0.0.0 8080 1\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};
// The SSL context is required, and holds certificates
ssl::context ctx{ssl::context::sslv23};
@@ -163,7 +166,7 @@ int main(int argc, char* argv[])
load_server_certificate(ctx);
// Spawn a listening port
boost::asio::spawn(ioc,
net::spawn(ioc,
std::bind(
&do_listen,
std::ref(ioc),
@@ -26,23 +26,26 @@
#include <thread>
#include <vector>
using tcp = boost::asio::ip::tcp; // from <boost/asio/ip/tcp.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 << what << ": " << ec.message() << "\n";
}
// Echoes back all received WebSocket messages
void
do_session(tcp::socket& socket, boost::asio::yield_context yield)
do_session(tcp::socket& socket, net::yield_context yield)
{
boost::system::error_code ec;
beast::error_code ec;
// Construct the stream by moving in the socket
websocket::stream<tcp::socket> ws{std::move(socket)};
@@ -55,7 +58,7 @@ do_session(tcp::socket& socket, boost::asio::yield_context yield)
for(;;)
{
// This buffer will hold the incoming message
boost::beast::multi_buffer buffer;
beast::multi_buffer buffer;
// Read a message
ws.async_read(buffer, yield[ec]);
@@ -80,11 +83,11 @@ do_session(tcp::socket& socket, boost::asio::yield_context yield)
// Accepts incoming connections and launches the sessions
void
do_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;
// Open the acceptor
tcp::acceptor acceptor(ioc);
@@ -93,7 +96,7 @@ do_listen(
return fail(ec, "open");
// 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)
return fail(ec, "set_option");
@@ -103,7 +106,7 @@ do_listen(
return fail(ec, "bind");
// Start listening for connections
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");
@@ -114,7 +117,7 @@ do_listen(
if(ec)
fail(ec, "accept");
else
boost::asio::spawn(
net::spawn(
acceptor.get_executor().context(),
std::bind(
&do_session,
@@ -134,15 +137,15 @@ int main(int argc, char* argv[])
" websocket-server-coro 0.0.0.0 8080 1\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};
// Spawn a listening port
boost::asio::spawn(ioc,
net::spawn(ioc,
std::bind(
&do_listen,
std::ref(ioc),
@@ -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),
@@ -32,29 +32,32 @@
#include <thread>
#include <vector>
using tcp = boost::asio::ip::tcp; // from <boost/asio/ip/tcp.hpp>
namespace ssl = boost::asio::ssl; // from <boost/asio/ssl.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>
namespace ssl = boost::asio::ssl; // from <boost/asio/ssl.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";
}
// Echoes back all received WebSocket messages
class session
: public boost::asio::coroutine
: public net::coroutine
, public std::enable_shared_from_this<session>
{
tcp::socket socket_;
websocket::stream<ssl::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
@@ -75,7 +78,7 @@ public:
#include <boost/asio/yield.hpp>
void
loop(
boost::system::error_code ec,
beast::error_code ec,
std::size_t bytes_transferred)
{
boost::ignore_unused(bytes_transferred);
@@ -85,7 +88,7 @@ public:
// Perform the SSL handshake
yield ws_.next_layer().async_handshake(
ssl::stream_base::server,
boost::asio::bind_executor(
net::bind_executor(
strand_,
std::bind(
&session::loop,
@@ -97,7 +100,7 @@ public:
// Accept the websocket handshake
yield ws_.async_accept(
boost::asio::bind_executor(
net::bind_executor(
strand_,
std::bind(
&session::loop,
@@ -112,7 +115,7 @@ public:
// Read a message into our buffer
yield ws_.async_read(
buffer_,
boost::asio::bind_executor(
net::bind_executor(
strand_,
std::bind(
&session::loop,
@@ -131,7 +134,7 @@ public:
ws_.text(ws_.got_text());
yield ws_.async_write(
buffer_.data(),
boost::asio::bind_executor(
net::bind_executor(
strand_,
std::bind(
&session::loop,
@@ -153,7 +156,7 @@ public:
// Accepts incoming connections and launches the sessions
class listener
: public boost::asio::coroutine
: public net::coroutine
, public std::enable_shared_from_this<listener>
{
ssl::context& ctx_;
@@ -162,14 +165,14 @@ class listener
public:
listener(
boost::asio::io_context& ioc,
net::io_context& ioc,
ssl::context& ctx,
tcp::endpoint endpoint)
: ctx_(ctx)
, acceptor_(ioc)
, socket_(ioc)
{
boost::system::error_code ec;
beast::error_code ec;
// Open the acceptor
acceptor_.open(endpoint.protocol(), ec);
@@ -180,7 +183,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");
@@ -197,7 +200,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");
@@ -216,7 +219,7 @@ public:
#include <boost/asio/yield.hpp>
void
loop(boost::system::error_code ec = {})
loop(beast::error_code ec = {})
{
reenter(*this)
{
@@ -256,12 +259,12 @@ int main(int argc, char* argv[])
" websocket-server-async-ssl 0.0.0.0 8080 1\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};
// The SSL context is required, and holds certificates
ssl::context ctx{ssl::context::sslv23};
@@ -28,27 +28,30 @@
#include <thread>
#include <vector>
using tcp = boost::asio::ip::tcp; // from <boost/asio/ip/tcp.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 << what << ": " << ec.message() << "\n";
}
// Echoes back all received WebSocket messages
class session
: public boost::asio::coroutine
: public net::coroutine
, public std::enable_shared_from_this<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
@@ -69,7 +72,7 @@ public:
#include <boost/asio/yield.hpp>
void
loop(
boost::system::error_code ec,
beast::error_code ec,
std::size_t bytes_transferred)
{
boost::ignore_unused(bytes_transferred);
@@ -77,7 +80,7 @@ public:
{
// Accept the websocket handshake
yield ws_.async_accept(
boost::asio::bind_executor(
net::bind_executor(
strand_,
std::bind(
&session::loop,
@@ -92,7 +95,7 @@ public:
// Read a message into our buffer
yield ws_.async_read(
buffer_,
boost::asio::bind_executor(
net::bind_executor(
strand_,
std::bind(
&session::loop,
@@ -111,7 +114,7 @@ public:
ws_.text(ws_.got_text());
yield ws_.async_write(
buffer_.data(),
boost::asio::bind_executor(
net::bind_executor(
strand_,
std::bind(
&session::loop,
@@ -133,7 +136,7 @@ public:
// Accepts incoming connections and launches the sessions
class listener
: public boost::asio::coroutine
: public net::coroutine
, public std::enable_shared_from_this<listener>
{
tcp::acceptor acceptor_;
@@ -141,12 +144,12 @@ class listener
public:
listener(
boost::asio::io_context& ioc,
net::io_context& ioc,
tcp::endpoint endpoint)
: acceptor_(ioc)
, socket_(ioc)
{
boost::system::error_code ec;
beast::error_code ec;
// Open the acceptor
acceptor_.open(endpoint.protocol(), ec);
@@ -157,7 +160,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");
@@ -174,7 +177,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");
@@ -193,7 +196,7 @@ public:
#include <boost/asio/yield.hpp>
void
loop(boost::system::error_code ec = {})
loop(beast::error_code ec = {})
{
reenter(*this)
{
@@ -233,12 +236,12 @@ int main(int argc, char* argv[])
" websocket-server-stackless 0.0.0.0 8080 1\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 and launch a listening port
std::make_shared<listener>(ioc, tcp::endpoint{address, port})->run();
@@ -26,9 +26,12 @@
#include <string>
#include <thread>
using tcp = boost::asio::ip::tcp; // from <boost/asio/ip/tcp.hpp>
namespace ssl = boost::asio::ssl; // from <boost/asio/ssl.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>
namespace ssl = boost::asio::ssl; // from <boost/asio/ssl.hpp>
using tcp = boost::asio::ip::tcp; // from <boost/asio/ip/tcp.hpp>
//------------------------------------------------------------------------------
@@ -50,7 +53,7 @@ do_session(tcp::socket& socket, ssl::context& ctx)
for(;;)
{
// This buffer will hold the incoming message
boost::beast::multi_buffer buffer;
beast::multi_buffer buffer;
// Read a message
ws.read(buffer);
@@ -60,7 +63,7 @@ do_session(tcp::socket& socket, ssl::context& ctx)
ws.write(buffer.data());
}
}
catch(boost::system::system_error const& se)
catch(beast::system_error const& se)
{
// This indicates that the session was closed
if(se.code() != websocket::error::closed)
@@ -87,11 +90,11 @@ int main(int argc, char* argv[])
" websocket-server-sync-ssl 0.0.0.0 8080\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]));
// The io_context is required for all I/O
boost::asio::io_context ioc{1};
net::io_context ioc{1};
// The SSL context is required, and holds certificates
ssl::context ctx{ssl::context::sslv23};
@@ -22,8 +22,11 @@
#include <string>
#include <thread>
using tcp = boost::asio::ip::tcp; // from <boost/asio/ip/tcp.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>
//------------------------------------------------------------------------------
@@ -42,7 +45,7 @@ do_session(tcp::socket& socket)
for(;;)
{
// This buffer will hold the incoming message
boost::beast::multi_buffer buffer;
beast::multi_buffer buffer;
// Read a message
ws.read(buffer);
@@ -52,7 +55,7 @@ do_session(tcp::socket& socket)
ws.write(buffer.data());
}
}
catch(boost::system::system_error const& se)
catch(beast::system_error const& se)
{
// This indicates that the session was closed
if(se.code() != websocket::error::closed)
@@ -79,11 +82,11 @@ int main(int argc, char* argv[])
" websocket-server-sync 0.0.0.0 8080\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]));
// The io_context is required for all I/O
boost::asio::io_context ioc{1};
net::io_context ioc{1};
// The acceptor receives incoming connections
tcp::acceptor acceptor{ioc, {address, port}};