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
@@ -27,15 +27,18 @@
#include <memory>
#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 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";
}
@@ -45,14 +48,14 @@ class session : public std::enable_shared_from_this<session>
{
tcp::resolver resolver_;
websocket::stream<ssl::stream<tcp::socket>> ws_;
boost::beast::multi_buffer buffer_;
beast::multi_buffer buffer_;
std::string host_;
std::string text_;
public:
// Resolver and socket require an io_context
explicit
session(boost::asio::io_context& ioc, ssl::context& ctx)
session(net::io_context& ioc, ssl::context& ctx)
: resolver_(ioc)
, ws_(ioc, ctx)
{
@@ -82,14 +85,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(
ws_.next_layer().next_layer(),
results.begin(),
results.end(),
@@ -100,7 +103,7 @@ public:
}
void
on_connect(boost::system::error_code ec)
on_connect(beast::error_code ec)
{
if(ec)
return fail(ec, "connect");
@@ -115,7 +118,7 @@ public:
}
void
on_ssl_handshake(boost::system::error_code ec)
on_ssl_handshake(beast::error_code ec)
{
if(ec)
return fail(ec, "ssl_handshake");
@@ -129,14 +132,14 @@ public:
}
void
on_handshake(boost::system::error_code ec)
on_handshake(beast::error_code ec)
{
if(ec)
return fail(ec, "handshake");
// Send the message
ws_.async_write(
boost::asio::buffer(text_),
net::buffer(text_),
std::bind(
&session::on_write,
shared_from_this(),
@@ -146,7 +149,7 @@ public:
void
on_write(
boost::system::error_code ec,
beast::error_code ec,
std::size_t bytes_transferred)
{
boost::ignore_unused(bytes_transferred);
@@ -166,7 +169,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,7 +186,7 @@ public:
}
void
on_close(boost::system::error_code ec)
on_close(beast::error_code ec)
{
if(ec)
return fail(ec, "close");
@@ -191,7 +194,7 @@ public:
// If we get here then the connection is closed gracefully
// The buffers() function helps print a ConstBufferSequence
std::cout << boost::beast::buffers(buffer_.data()) << std::endl;
std::cout << beast::buffers(buffer_.data()) << std::endl;
}
};
@@ -213,7 +216,7 @@ int main(int argc, char** argv)
auto const text = argv[3];
// 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};
@@ -23,14 +23,17 @@
#include <memory>
#include <string>
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";
}
@@ -40,14 +43,14 @@ class session : public std::enable_shared_from_this<session>
{
tcp::resolver resolver_;
websocket::stream<tcp::socket> ws_;
boost::beast::multi_buffer buffer_;
beast::multi_buffer buffer_;
std::string host_;
std::string text_;
public:
// Resolver and socket require an io_context
explicit
session(boost::asio::io_context& ioc)
session(net::io_context& ioc)
: resolver_(ioc)
, ws_(ioc)
{
@@ -77,14 +80,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(
ws_.next_layer(),
results.begin(),
results.end(),
@@ -95,7 +98,7 @@ public:
}
void
on_connect(boost::system::error_code ec)
on_connect(beast::error_code ec)
{
if(ec)
return fail(ec, "connect");
@@ -109,14 +112,14 @@ public:
}
void
on_handshake(boost::system::error_code ec)
on_handshake(beast::error_code ec)
{
if(ec)
return fail(ec, "handshake");
// Send the message
ws_.async_write(
boost::asio::buffer(text_),
net::buffer(text_),
std::bind(
&session::on_write,
shared_from_this(),
@@ -126,7 +129,7 @@ public:
void
on_write(
boost::system::error_code ec,
beast::error_code ec,
std::size_t bytes_transferred)
{
boost::ignore_unused(bytes_transferred);
@@ -146,7 +149,7 @@ public:
void
on_read(
boost::system::error_code ec,
beast::error_code ec,
std::size_t bytes_transferred)
{
boost::ignore_unused(bytes_transferred);
@@ -163,7 +166,7 @@ public:
}
void
on_close(boost::system::error_code ec)
on_close(beast::error_code ec)
{
if(ec)
return fail(ec, "close");
@@ -171,7 +174,7 @@ public:
// If we get here then the connection is closed gracefully
// The buffers() function helps print a ConstBufferSequence
std::cout << boost::beast::buffers(buffer_.data()) << std::endl;
std::cout << beast::buffers(buffer_.data()) << std::endl;
}
};
@@ -193,7 +196,7 @@ int main(int argc, char** argv)
auto const text = argv[3];
// 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, text);
@@ -27,15 +27,18 @@
#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 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";
}
@@ -46,11 +49,11 @@ do_session(
std::string const& host,
std::string const& port,
std::string const& text,
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};
@@ -62,7 +65,7 @@ do_session(
return fail(ec, "resolve");
// Make the connection on the IP address we get from a lookup
boost::asio::async_connect(ws.next_layer().next_layer(), results.begin(), results.end(), yield[ec]);
net::async_connect(ws.next_layer().next_layer(), results.begin(), results.end(), yield[ec]);
if(ec)
return fail(ec, "connect");
@@ -77,12 +80,12 @@ do_session(
return fail(ec, "handshake");
// Send the message
ws.async_write(boost::asio::buffer(std::string(text)), yield[ec]);
ws.async_write(net::buffer(std::string(text)), yield[ec]);
if(ec)
return fail(ec, "write");
// This buffer will hold the incoming message
boost::beast::multi_buffer b;
beast::multi_buffer b;
// Read a message into our buffer
ws.async_read(b, yield[ec]);
@@ -97,7 +100,7 @@ do_session(
// If we get here then the connection is closed gracefully
// The buffers() function helps print a ConstBufferSequence
std::cout << boost::beast::buffers(b.data()) << std::endl;
std::cout << beast::buffers(b.data()) << std::endl;
}
//------------------------------------------------------------------------------
@@ -118,7 +121,7 @@ int main(int argc, char** argv)
auto const text = argv[3];
// 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};
@@ -127,7 +130,7 @@ int main(int argc, char** argv)
load_root_certificates(ctx);
// Launch the asynchronous operation
boost::asio::spawn(ioc, std::bind(
net::spawn(ioc, std::bind(
&do_session,
std::string(host),
std::string(port),
@@ -23,14 +23,17 @@
#include <iostream>
#include <string>
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";
}
@@ -41,10 +44,10 @@ do_session(
std::string const& host,
std::string const& port,
std::string const& text,
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};
@@ -56,7 +59,7 @@ do_session(
return fail(ec, "resolve");
// Make the connection on the IP address we get from a lookup
boost::asio::async_connect(ws.next_layer(), results.begin(), results.end(), yield[ec]);
net::async_connect(ws.next_layer(), results.begin(), results.end(), yield[ec]);
if(ec)
return fail(ec, "connect");
@@ -66,12 +69,12 @@ do_session(
return fail(ec, "handshake");
// Send the message
ws.async_write(boost::asio::buffer(std::string(text)), yield[ec]);
ws.async_write(net::buffer(std::string(text)), yield[ec]);
if(ec)
return fail(ec, "write");
// This buffer will hold the incoming message
boost::beast::multi_buffer b;
beast::multi_buffer b;
// Read a message into our buffer
ws.async_read(b, yield[ec]);
@@ -86,7 +89,7 @@ do_session(
// If we get here then the connection is closed gracefully
// The buffers() function helps print a ConstBufferSequence
std::cout << boost::beast::buffers(b.data()) << std::endl;
std::cout << beast::buffers(b.data()) << std::endl;
}
//------------------------------------------------------------------------------
@@ -107,10 +110,10 @@ int main(int argc, char** argv)
auto const text = argv[3];
// 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),
@@ -25,9 +25,12 @@
#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 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>
// Sends a WebSocket message and prints the response
int main(int argc, char** argv)
@@ -48,7 +51,7 @@ int main(int argc, char** argv)
auto const text = argv[3];
// 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};
@@ -64,7 +67,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(ws.next_layer().next_layer(), results.begin(), results.end());
net::connect(ws.next_layer().next_layer(), results.begin(), results.end());
// Perform the SSL handshake
ws.next_layer().handshake(ssl::stream_base::client);
@@ -73,10 +76,10 @@ int main(int argc, char** argv)
ws.handshake(host, "/");
// Send the message
ws.write(boost::asio::buffer(std::string(text)));
ws.write(net::buffer(std::string(text)));
// This buffer will hold the incoming message
boost::beast::multi_buffer b;
beast::multi_buffer b;
// Read a message into our buffer
ws.read(b);
@@ -87,7 +90,7 @@ int main(int argc, char** argv)
// If we get here then the connection is closed gracefully
// The buffers() function helps print a ConstBufferSequence
std::cout << boost::beast::buffers(b.data()) << std::endl;
std::cout << beast::buffers(b.data()) << std::endl;
}
catch(std::exception const& e)
{
@@ -23,8 +23,11 @@
#include <iostream>
#include <string>
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>
// Sends a WebSocket message and prints the response
int main(int argc, char** argv)
@@ -45,7 +48,7 @@ int main(int argc, char** argv)
auto const text = argv[3];
// 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};
@@ -55,16 +58,16 @@ 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(ws.next_layer(), results.begin(), results.end());
net::connect(ws.next_layer(), results.begin(), results.end());
// Perform the websocket handshake
ws.handshake(host, "/");
// Send the message
ws.write(boost::asio::buffer(std::string(text)));
ws.write(net::buffer(std::string(text)));
// This buffer will hold the incoming message
boost::beast::multi_buffer buffer;
beast::multi_buffer buffer;
// Read a message into our buffer
ws.read(buffer);
@@ -75,7 +78,7 @@ int main(int argc, char** argv)
// If we get here then the connection is closed gracefully
// The buffers() function helps print a ConstBufferSequence
std::cout << boost::beast::buffers(buffer.data()) << std::endl;
std::cout << beast::buffers(buffer.data()) << std::endl;
}
catch(std::exception const& e)
{
@@ -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}};