Update for Net-TS Asio (API Change):

fix #769

The following classes are removed:

* handler_type
* async_result
* async_completion
* is_dynamic_buffer
* is_const_buffer_sequence
* is_mutable_buffer_sequence
* handler_alloc

Actions Required:

* Use BOOST_ASIO_HANDLER_TYPE instead of handler_type
* Use BOOST_ASIO_INITFN_RESULT_TYPE instead of async_result
* Use boost::asio::async_completion
* Use boost::asio::is_dynamic_buffer
* Use boost::asio::is_const_buffer_sequence
* Use boost::asio::is_mutable_buffer_sequence
* boost::asio::associated_allocator_t replaces handler_alloc
This commit is contained in:
Vinnie Falco
2017-10-23 17:11:27 -07:00
parent 9dc9ca13b9
commit 3a28e999af
173 changed files with 3214 additions and 4138 deletions
@@ -18,6 +18,7 @@
#include <boost/beast/core.hpp>
#include <boost/beast/websocket.hpp>
#include <boost/beast/websocket/ssl.hpp>
#include <boost/asio/bind_executor.hpp>
#include <boost/asio/strand.hpp>
#include <boost/asio/ip/tcp.hpp>
#include <boost/asio/ssl/stream.hpp>
@@ -48,7 +49,8 @@ class session : public std::enable_shared_from_this<session>
{
tcp::socket socket_;
websocket::stream<ssl::stream<tcp::socket&>> ws_;
boost::asio::io_service::strand strand_;
boost::asio::strand<
boost::asio::io_context::executor_type> strand_;
boost::beast::multi_buffer buffer_;
public:
@@ -56,7 +58,7 @@ public:
session(tcp::socket socket, ssl::context& ctx)
: socket_(std::move(socket))
, ws_(socket_, ctx)
, strand_(ws_.get_io_service())
, strand_(ws_.get_executor())
{
}
@@ -67,10 +69,12 @@ public:
// Perform the SSL handshake
ws_.next_layer().async_handshake(
ssl::stream_base::server,
strand_.wrap(std::bind(
&session::on_handshake,
shared_from_this(),
std::placeholders::_1)));
boost::asio::bind_executor(
strand_,
std::bind(
&session::on_handshake,
shared_from_this(),
std::placeholders::_1)));
}
void
@@ -81,10 +85,12 @@ public:
// Accept the websocket handshake
ws_.async_accept(
strand_.wrap(std::bind(
&session::on_accept,
shared_from_this(),
std::placeholders::_1)));
boost::asio::bind_executor(
strand_,
std::bind(
&session::on_accept,
shared_from_this(),
std::placeholders::_1)));
}
void
@@ -103,11 +109,13 @@ public:
// Read a message into our buffer
ws_.async_read(
buffer_,
strand_.wrap(std::bind(
&session::on_read,
shared_from_this(),
std::placeholders::_1,
std::placeholders::_2)));
boost::asio::bind_executor(
strand_,
std::bind(
&session::on_read,
shared_from_this(),
std::placeholders::_1,
std::placeholders::_2)));
}
void
@@ -128,11 +136,13 @@ public:
ws_.text(ws_.got_text());
ws_.async_write(
buffer_.data(),
strand_.wrap(std::bind(
&session::on_write,
shared_from_this(),
std::placeholders::_1,
std::placeholders::_2)));
boost::asio::bind_executor(
strand_,
std::bind(
&session::on_write,
shared_from_this(),
std::placeholders::_1,
std::placeholders::_2)));
}
void
@@ -164,12 +174,12 @@ class listener : public std::enable_shared_from_this<listener>
public:
listener(
boost::asio::io_service& ios,
boost::asio::io_context& ioc,
ssl::context& ctx,
tcp::endpoint endpoint)
: ctx_(ctx)
, acceptor_(ios)
, socket_(ios)
, acceptor_(ioc)
, socket_(ioc)
{
boost::system::error_code ec;
@@ -191,7 +201,7 @@ public:
// Start listening for connections
acceptor_.listen(
boost::asio::socket_base::max_connections, ec);
boost::asio::socket_base::max_listen_connections, ec);
if(ec)
{
fail(ec, "listen");
@@ -250,12 +260,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::address::from_string(argv[1]);
auto const address = boost::asio::ip::make_address(argv[1]);
auto const port = static_cast<unsigned short>(std::atoi(argv[2]));
auto const threads = std::max<std::size_t>(1, std::atoi(argv[3]));
auto const threads = std::max<int>(1, std::atoi(argv[3]));
// The io_service is required for all I/O
boost::asio::io_service ios{threads};
// The io_context is required for all I/O
boost::asio::io_context ioc{threads};
// The SSL context is required, and holds certificates
ssl::context ctx{ssl::context::sslv23};
@@ -264,18 +274,18 @@ int main(int argc, char* argv[])
load_server_certificate(ctx);
// Create and launch a listening port
std::make_shared<listener>(ios, ctx, tcp::endpoint{address, port})->run();
std::make_shared<listener>(ioc, ctx, tcp::endpoint{address, port})->run();
// Run the I/O service on the requested number of threads
std::vector<std::thread> v;
v.reserve(threads - 1);
for(auto i = threads - 1; i > 0; --i)
v.emplace_back(
[&ios]
[&ioc]
{
ios.run();
ioc.run();
});
ios.run();
ioc.run();
return EXIT_SUCCESS;
}
@@ -15,6 +15,7 @@
#include <boost/beast/core.hpp>
#include <boost/beast/websocket.hpp>
#include <boost/asio/bind_executor.hpp>
#include <boost/asio/strand.hpp>
#include <boost/asio/ip/tcp.hpp>
#include <algorithm>
@@ -42,7 +43,8 @@ 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::io_service::strand strand_;
boost::asio::strand<
boost::asio::io_context::executor_type> strand_;
boost::beast::multi_buffer buffer_;
public:
@@ -50,7 +52,7 @@ public:
explicit
session(tcp::socket socket)
: ws_(std::move(socket))
, strand_(ws_.get_io_service())
, strand_(ws_.get_executor())
{
}
@@ -60,10 +62,12 @@ public:
{
// Accept the websocket handshake
ws_.async_accept(
strand_.wrap(std::bind(
&session::on_accept,
shared_from_this(),
std::placeholders::_1)));
boost::asio::bind_executor(
strand_,
std::bind(
&session::on_accept,
shared_from_this(),
std::placeholders::_1)));
}
void
@@ -82,11 +86,13 @@ public:
// Read a message into our buffer
ws_.async_read(
buffer_,
strand_.wrap(std::bind(
&session::on_read,
shared_from_this(),
std::placeholders::_1,
std::placeholders::_2)));
boost::asio::bind_executor(
strand_,
std::bind(
&session::on_read,
shared_from_this(),
std::placeholders::_1,
std::placeholders::_2)));
}
void
@@ -107,11 +113,13 @@ public:
ws_.text(ws_.got_text());
ws_.async_write(
buffer_.data(),
strand_.wrap(std::bind(
&session::on_write,
shared_from_this(),
std::placeholders::_1,
std::placeholders::_2)));
boost::asio::bind_executor(
strand_,
std::bind(
&session::on_write,
shared_from_this(),
std::placeholders::_1,
std::placeholders::_2)));
}
void
@@ -142,10 +150,10 @@ class listener : public std::enable_shared_from_this<listener>
public:
listener(
boost::asio::io_service& ios,
boost::asio::io_context& ioc,
tcp::endpoint endpoint)
: acceptor_(ios)
, socket_(ios)
: acceptor_(ioc)
, socket_(ioc)
{
boost::system::error_code ec;
@@ -167,7 +175,7 @@ public:
// Start listening for connections
acceptor_.listen(
boost::asio::socket_base::max_connections, ec);
boost::asio::socket_base::max_listen_connections, ec);
if(ec)
{
fail(ec, "listen");
@@ -226,26 +234,26 @@ 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::address::from_string(argv[1]);
auto const address = boost::asio::ip::make_address(argv[1]);
auto const port = static_cast<unsigned short>(std::atoi(argv[2]));
auto const threads = std::max<std::size_t>(1, std::atoi(argv[3]));
auto const threads = std::max<int>(1, std::atoi(argv[3]));
// The io_service is required for all I/O
boost::asio::io_service ios{threads};
// The io_context is required for all I/O
boost::asio::io_context ioc{threads};
// Create and launch a listening port
std::make_shared<listener>(ios, tcp::endpoint{address, port})->run();
std::make_shared<listener>(ioc, tcp::endpoint{address, port})->run();
// Run the I/O service on the requested number of threads
std::vector<std::thread> v;
v.reserve(threads - 1);
for(auto i = threads - 1; i > 0; --i)
v.emplace_back(
[&ios]
[&ioc]
{
ios.run();
ioc.run();
});
ios.run();
ioc.run();
return EXIT_SUCCESS;
}
@@ -93,7 +93,7 @@ do_session(
// Accepts incoming connections and launches the sessions
void
do_listen(
boost::asio::io_service& ios,
boost::asio::io_context& ioc,
ssl::context& ctx,
tcp::endpoint endpoint,
boost::asio::yield_context yield)
@@ -101,7 +101,7 @@ do_listen(
boost::system::error_code ec;
// Open the acceptor
tcp::acceptor acceptor(ios);
tcp::acceptor acceptor(ioc);
acceptor.open(endpoint.protocol(), ec);
if(ec)
return fail(ec, "open");
@@ -112,19 +112,19 @@ do_listen(
return fail(ec, "bind");
// Start listening for connections
acceptor.listen(boost::asio::socket_base::max_connections, ec);
acceptor.listen(boost::asio::socket_base::max_listen_connections, ec);
if(ec)
return fail(ec, "listen");
for(;;)
{
tcp::socket socket(ios);
tcp::socket socket(ioc);
acceptor.async_accept(socket, yield[ec]);
if(ec)
fail(ec, "accept");
else
boost::asio::spawn(
acceptor.get_io_service(),
acceptor.get_executor().context(),
std::bind(
&do_session,
std::move(socket),
@@ -144,12 +144,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::address::from_string(argv[1]);
auto const address = boost::asio::ip::make_address(argv[1]);
auto const port = static_cast<unsigned short>(std::atoi(argv[2]));
auto const threads = std::max<std::size_t>(1, std::atoi(argv[3]));
auto const threads = std::max<int>(1, std::atoi(argv[3]));
// The io_service is required for all I/O
boost::asio::io_service ios{threads};
// The io_context is required for all I/O
boost::asio::io_context ioc{threads};
// The SSL context is required, and holds certificates
ssl::context ctx{ssl::context::sslv23};
@@ -158,10 +158,10 @@ int main(int argc, char* argv[])
load_server_certificate(ctx);
// Spawn a listening port
boost::asio::spawn(ios,
boost::asio::spawn(ioc,
std::bind(
&do_listen,
std::ref(ios),
std::ref(ioc),
std::ref(ctx),
tcp::endpoint{address, port},
std::placeholders::_1));
@@ -171,11 +171,11 @@ int main(int argc, char* argv[])
v.reserve(threads - 1);
for(auto i = threads - 1; i > 0; --i)
v.emplace_back(
[&ios]
[&ioc]
{
ios.run();
ioc.run();
});
ios.run();
ioc.run();
return EXIT_SUCCESS;
}
@@ -80,14 +80,14 @@ do_session(tcp::socket& socket, boost::asio::yield_context yield)
// Accepts incoming connections and launches the sessions
void
do_listen(
boost::asio::io_service& ios,
boost::asio::io_context& ioc,
tcp::endpoint endpoint,
boost::asio::yield_context yield)
{
boost::system::error_code ec;
// Open the acceptor
tcp::acceptor acceptor(ios);
tcp::acceptor acceptor(ioc);
acceptor.open(endpoint.protocol(), ec);
if(ec)
return fail(ec, "open");
@@ -98,19 +98,19 @@ do_listen(
return fail(ec, "bind");
// Start listening for connections
acceptor.listen(boost::asio::socket_base::max_connections, ec);
acceptor.listen(boost::asio::socket_base::max_listen_connections, ec);
if(ec)
return fail(ec, "listen");
for(;;)
{
tcp::socket socket(ios);
tcp::socket socket(ioc);
acceptor.async_accept(socket, yield[ec]);
if(ec)
fail(ec, "accept");
else
boost::asio::spawn(
acceptor.get_io_service(),
acceptor.get_executor().context(),
std::bind(
&do_session,
std::move(socket),
@@ -129,18 +129,18 @@ 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::address::from_string(argv[1]);
auto const address = boost::asio::ip::make_address(argv[1]);
auto const port = static_cast<unsigned short>(std::atoi(argv[2]));
auto const threads = std::max<std::size_t>(1, std::atoi(argv[3]));
auto const threads = std::max<int>(1, std::atoi(argv[3]));
// The io_service is required for all I/O
boost::asio::io_service ios{threads};
// The io_context is required for all I/O
boost::asio::io_context ioc{threads};
// Spawn a listening port
boost::asio::spawn(ios,
boost::asio::spawn(ioc,
std::bind(
&do_listen,
std::ref(ios),
std::ref(ioc),
tcp::endpoint{address, port},
std::placeholders::_1));
@@ -149,11 +149,11 @@ int main(int argc, char* argv[])
v.reserve(threads - 1);
for(auto i = threads - 1; i > 0; --i)
v.emplace_back(
[&ios]
[&ioc]
{
ios.run();
ioc.run();
});
ios.run();
ioc.run();
return EXIT_SUCCESS;
}
@@ -30,6 +30,7 @@
#include <boost/beast/http.hpp>
#include <boost/beast/version.hpp>
#include <boost/beast/websocket.hpp>
#include <boost/asio/bind_executor.hpp>
#include <boost/asio/spawn.hpp>
#include <boost/asio/strand.hpp>
#include <boost/asio/ip/tcp.hpp>
@@ -113,14 +114,14 @@ do_sync_session(tcp::socket& socket)
void
do_sync_listen(
boost::asio::io_service& ios,
boost::asio::io_context& ioc,
tcp::endpoint endpoint)
{
boost::system::error_code ec;
tcp::acceptor acceptor{ios, endpoint};
tcp::acceptor acceptor{ioc, endpoint};
for(;;)
{
tcp::socket socket{ios};
tcp::socket socket{ioc};
acceptor.accept(socket, ec);
if(ec)
@@ -138,7 +139,8 @@ do_sync_listen(
class async_session : public std::enable_shared_from_this<async_session>
{
websocket::stream<tcp::socket> ws_;
boost::asio::io_service::strand strand_;
boost::asio::strand<
boost::asio::io_context::executor_type> strand_;
boost::beast::multi_buffer buffer_;
public:
@@ -146,7 +148,7 @@ public:
explicit
async_session(tcp::socket socket)
: ws_(std::move(socket))
, strand_(ws_.get_io_service())
, strand_(ws_.get_executor())
{
setup_stream(ws_);
}
@@ -162,10 +164,12 @@ public:
res.set(http::field::server,
"Boost.Beast.async/" + std::to_string(BOOST_BEAST_VERSION));
},
strand_.wrap(std::bind(
&async_session::on_accept,
shared_from_this(),
std::placeholders::_1)));
boost::asio::bind_executor(
strand_,
std::bind(
&async_session::on_accept,
shared_from_this(),
std::placeholders::_1)));
}
void
@@ -184,11 +188,13 @@ public:
// Read a message into our buffer
ws_.async_read(
buffer_,
strand_.wrap(std::bind(
&async_session::on_read,
shared_from_this(),
std::placeholders::_1,
std::placeholders::_2)));
boost::asio::bind_executor(
strand_,
std::bind(
&async_session::on_read,
shared_from_this(),
std::placeholders::_1,
std::placeholders::_2)));
}
void
@@ -209,11 +215,13 @@ public:
ws_.text(ws_.got_text());
ws_.async_write(
buffer_.data(),
strand_.wrap(std::bind(
&async_session::on_write,
shared_from_this(),
std::placeholders::_1,
std::placeholders::_2)));
boost::asio::bind_executor(
strand_,
std::bind(
&async_session::on_write,
shared_from_this(),
std::placeholders::_1,
std::placeholders::_2)));
}
void
@@ -237,17 +245,18 @@ public:
// Accepts incoming connections and launches the sessions
class async_listener : public std::enable_shared_from_this<async_listener>
{
boost::asio::io_service::strand strand_;
boost::asio::strand<
boost::asio::io_context::executor_type> strand_;
tcp::acceptor acceptor_;
tcp::socket socket_;
public:
async_listener(
boost::asio::io_service& ios,
boost::asio::io_context& ioc,
tcp::endpoint endpoint)
: strand_(ios)
, acceptor_(ios)
, socket_(ios)
: strand_(ioc.get_executor())
, acceptor_(ioc)
, socket_(ioc)
{
boost::system::error_code ec;
@@ -269,7 +278,7 @@ public:
// Start listening for connections
acceptor_.listen(
boost::asio::socket_base::max_connections, ec);
boost::asio::socket_base::max_listen_connections, ec);
if(ec)
{
fail(ec, "listen");
@@ -291,10 +300,12 @@ public:
{
acceptor_.async_accept(
socket_,
strand_.wrap(std::bind(
&async_listener::on_accept,
shared_from_this(),
std::placeholders::_1)));
boost::asio::bind_executor(
strand_,
std::bind(
&async_listener::on_accept,
shared_from_this(),
std::placeholders::_1)));
}
void
@@ -354,13 +365,13 @@ do_coro_session(tcp::socket& socket, boost::asio::yield_context yield)
void
do_coro_listen(
boost::asio::io_service& ios,
boost::asio::io_context& ioc,
tcp::endpoint endpoint,
boost::asio::yield_context yield)
{
boost::system::error_code ec;
tcp::acceptor acceptor(ios);
tcp::acceptor acceptor(ioc);
acceptor.open(endpoint.protocol(), ec);
if(ec)
return fail(ec, "open");
@@ -369,13 +380,13 @@ do_coro_listen(
if(ec)
return fail(ec, "bind");
acceptor.listen(boost::asio::socket_base::max_connections, ec);
acceptor.listen(boost::asio::socket_base::max_listen_connections, ec);
if(ec)
return fail(ec, "listen");
for(;;)
{
tcp::socket socket(ios);
tcp::socket socket(ioc);
acceptor.async_accept(socket, yield[ec]);
if(ec)
@@ -385,7 +396,7 @@ do_coro_listen(
}
boost::asio::spawn(
acceptor.get_io_service(),
acceptor.get_executor().context(),
std::bind(
&do_coro_session,
std::move(socket),
@@ -410,17 +421,17 @@ int main(int argc, char* argv[])
" starting-port+2 for coroutine.\n";
return EXIT_FAILURE;
}
auto const address = boost::asio::ip::address::from_string(argv[1]);
auto const address = boost::asio::ip::make_address(argv[1]);
auto const port = static_cast<unsigned short>(std::atoi(argv[2]));
auto const threads = std::max<std::size_t>(1, std::atoi(argv[3]));
auto const threads = std::max<int>(1, std::atoi(argv[3]));
// The io_service is required for all I/O
boost::asio::io_service ios{threads};
// The io_context is required for all I/O
boost::asio::io_context ioc{threads};
// Create sync port
std::thread(std::bind(
&do_sync_listen,
std::ref(ios),
std::ref(ioc),
tcp::endpoint{
address,
static_cast<unsigned short>(port + 0u)}
@@ -428,16 +439,16 @@ int main(int argc, char* argv[])
// Create async port
std::make_shared<async_listener>(
ios,
ioc,
tcp::endpoint{
address,
static_cast<unsigned short>(port + 1u)})->run();
// Create coro port
boost::asio::spawn(ios,
boost::asio::spawn(ioc,
std::bind(
&do_coro_listen,
std::ref(ios),
std::ref(ioc),
tcp::endpoint{
address,
static_cast<unsigned short>(port + 2u)},
@@ -448,11 +459,11 @@ int main(int argc, char* argv[])
v.reserve(threads - 1);
for(auto i = threads - 1; i > 0; --i)
v.emplace_back(
[&ios]
[&ioc]
{
ios.run();
ioc.run();
});
ios.run();
ioc.run();
return EXIT_SUCCESS;
}
@@ -18,6 +18,7 @@
#include <boost/beast/core.hpp>
#include <boost/beast/websocket.hpp>
#include <boost/beast/websocket/ssl.hpp>
#include <boost/asio/bind_executor.hpp>
#include <boost/asio/coroutine.hpp>
#include <boost/asio/ip/tcp.hpp>
#include <boost/asio/ssl/stream.hpp>
@@ -51,7 +52,8 @@ class session
{
tcp::socket socket_;
websocket::stream<ssl::stream<tcp::socket&>> ws_;
boost::asio::io_service::strand strand_;
boost::asio::strand<
boost::asio::io_context::executor_type> strand_;
boost::beast::multi_buffer buffer_;
public:
@@ -59,7 +61,7 @@ public:
session(tcp::socket socket, ssl::context& ctx)
: socket_(std::move(socket))
, ws_(socket_, ctx)
, strand_(ws_.get_io_service())
, strand_(ws_.get_executor())
{
}
@@ -83,21 +85,25 @@ public:
// Perform the SSL handshake
yield ws_.next_layer().async_handshake(
ssl::stream_base::server,
strand_.wrap(std::bind(
&session::loop,
shared_from_this(),
std::placeholders::_1,
0)));
boost::asio::bind_executor(
strand_,
std::bind(
&session::loop,
shared_from_this(),
std::placeholders::_1,
0)));
if(ec)
return fail(ec, "handshake");
// Accept the websocket handshake
yield ws_.async_accept(
strand_.wrap(std::bind(
&session::loop,
shared_from_this(),
std::placeholders::_1,
0)));
boost::asio::bind_executor(
strand_,
std::bind(
&session::loop,
shared_from_this(),
std::placeholders::_1,
0)));
if(ec)
return fail(ec, "accept");
@@ -106,11 +112,13 @@ public:
// Read a message into our buffer
yield ws_.async_read(
buffer_,
strand_.wrap(std::bind(
&session::loop,
shared_from_this(),
std::placeholders::_1,
std::placeholders::_2)));
boost::asio::bind_executor(
strand_,
std::bind(
&session::loop,
shared_from_this(),
std::placeholders::_1,
std::placeholders::_2)));
if(ec == websocket::error::closed)
{
// This indicates that the session was closed
@@ -123,11 +131,13 @@ public:
ws_.text(ws_.got_text());
yield ws_.async_write(
buffer_.data(),
strand_.wrap(std::bind(
&session::loop,
shared_from_this(),
std::placeholders::_1,
std::placeholders::_2)));
boost::asio::bind_executor(
strand_,
std::bind(
&session::loop,
shared_from_this(),
std::placeholders::_1,
std::placeholders::_2)));
if(ec)
return fail(ec, "write");
@@ -152,12 +162,12 @@ class listener
public:
listener(
boost::asio::io_service& ios,
boost::asio::io_context& ioc,
ssl::context& ctx,
tcp::endpoint endpoint)
: ctx_(ctx)
, acceptor_(ios)
, socket_(ios)
, acceptor_(ioc)
, socket_(ioc)
{
boost::system::error_code ec;
@@ -179,7 +189,7 @@ public:
// Start listening for connections
acceptor_.listen(
boost::asio::socket_base::max_connections, ec);
boost::asio::socket_base::max_listen_connections, ec);
if(ec)
{
fail(ec, "listen");
@@ -238,12 +248,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::address::from_string(argv[1]);
auto const address = boost::asio::ip::make_address(argv[1]);
auto const port = static_cast<unsigned short>(std::atoi(argv[2]));
auto const threads = std::max<std::size_t>(1, std::atoi(argv[3]));
auto const threads = std::max<int>(1, std::atoi(argv[3]));
// The io_service is required for all I/O
boost::asio::io_service ios{threads};
// The io_context is required for all I/O
boost::asio::io_context ioc{threads};
// The SSL context is required, and holds certificates
ssl::context ctx{ssl::context::sslv23};
@@ -252,18 +262,18 @@ int main(int argc, char* argv[])
load_server_certificate(ctx);
// Create and launch a listening port
std::make_shared<listener>(ios, ctx, tcp::endpoint{address, port})->run();
std::make_shared<listener>(ioc, ctx, tcp::endpoint{address, port})->run();
// Run the I/O service on the requested number of threads
std::vector<std::thread> v;
v.reserve(threads - 1);
for(auto i = threads - 1; i > 0; --i)
v.emplace_back(
[&ios]
[&ioc]
{
ios.run();
ioc.run();
});
ios.run();
ioc.run();
return EXIT_SUCCESS;
}
@@ -15,6 +15,7 @@
#include <boost/beast/core.hpp>
#include <boost/beast/websocket.hpp>
#include <boost/asio/bind_executor.hpp>
#include <boost/asio/coroutine.hpp>
#include <boost/asio/strand.hpp>
#include <boost/asio/ip/tcp.hpp>
@@ -45,7 +46,8 @@ class session
, public std::enable_shared_from_this<session>
{
websocket::stream<tcp::socket> ws_;
boost::asio::io_service::strand strand_;
boost::asio::strand<
boost::asio::io_context::executor_type> strand_;
boost::beast::multi_buffer buffer_;
public:
@@ -53,7 +55,7 @@ public:
explicit
session(tcp::socket socket)
: ws_(std::move(socket))
, strand_(ws_.get_io_service())
, strand_(ws_.get_executor())
{
}
@@ -75,11 +77,13 @@ public:
{
// Accept the websocket handshake
yield ws_.async_accept(
strand_.wrap(std::bind(
&session::loop,
shared_from_this(),
std::placeholders::_1,
0)));
boost::asio::bind_executor(
strand_,
std::bind(
&session::loop,
shared_from_this(),
std::placeholders::_1,
0)));
if(ec)
return fail(ec, "accept");
@@ -88,11 +92,13 @@ public:
// Read a message into our buffer
yield ws_.async_read(
buffer_,
strand_.wrap(std::bind(
&session::loop,
shared_from_this(),
std::placeholders::_1,
std::placeholders::_2)));
boost::asio::bind_executor(
strand_,
std::bind(
&session::loop,
shared_from_this(),
std::placeholders::_1,
std::placeholders::_2)));
if(ec == websocket::error::closed)
{
// This indicates that the session was closed
@@ -105,11 +111,13 @@ public:
ws_.text(ws_.got_text());
yield ws_.async_write(
buffer_.data(),
strand_.wrap(std::bind(
&session::loop,
shared_from_this(),
std::placeholders::_1,
std::placeholders::_2)));
boost::asio::bind_executor(
strand_,
std::bind(
&session::loop,
shared_from_this(),
std::placeholders::_1,
std::placeholders::_2)));
if(ec)
return fail(ec, "write");
@@ -133,10 +141,10 @@ class listener
public:
listener(
boost::asio::io_service& ios,
boost::asio::io_context& ioc,
tcp::endpoint endpoint)
: acceptor_(ios)
, socket_(ios)
: acceptor_(ioc)
, socket_(ioc)
{
boost::system::error_code ec;
@@ -158,7 +166,7 @@ public:
// Start listening for connections
acceptor_.listen(
boost::asio::socket_base::max_connections, ec);
boost::asio::socket_base::max_listen_connections, ec);
if(ec)
{
fail(ec, "listen");
@@ -217,26 +225,26 @@ 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::address::from_string(argv[1]);
auto const address = boost::asio::ip::make_address(argv[1]);
auto const port = static_cast<unsigned short>(std::atoi(argv[2]));
auto const threads = std::max<std::size_t>(1, std::atoi(argv[3]));
auto const threads = std::max<int>(1, std::atoi(argv[3]));
// The io_service is required for all I/O
boost::asio::io_service ios{threads};
// The io_context is required for all I/O
boost::asio::io_context ioc{threads};
// Create and launch a listening port
std::make_shared<listener>(ios, tcp::endpoint{address, port})->run();
std::make_shared<listener>(ioc, tcp::endpoint{address, port})->run();
// Run the I/O service on the requested number of threads
std::vector<std::thread> v;
v.reserve(threads - 1);
for(auto i = threads - 1; i > 0; --i)
v.emplace_back(
[&ios]
[&ioc]
{
ios.run();
ioc.run();
});
ios.run();
ioc.run();
return EXIT_SUCCESS;
}
@@ -87,11 +87,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::address::from_string(argv[1]);
auto const address = boost::asio::ip::make_address(argv[1]);
auto const port = static_cast<unsigned short>(std::atoi(argv[2]));
// The io_service is required for all I/O
boost::asio::io_service ios{1};
// The io_context is required for all I/O
boost::asio::io_context ioc{1};
// The SSL context is required, and holds certificates
ssl::context ctx{ssl::context::sslv23};
@@ -100,11 +100,11 @@ int main(int argc, char* argv[])
load_server_certificate(ctx);
// The acceptor receives incoming connections
tcp::acceptor acceptor{ios, {address, port}};
tcp::acceptor acceptor{ioc, {address, port}};
for(;;)
{
// This will receive the new connection
tcp::socket socket{ios};
tcp::socket socket{ioc};
// Block until we get a connection
acceptor.accept(socket);
@@ -79,18 +79,18 @@ int main(int argc, char* argv[])
" websocket-server-sync 0.0.0.0 8080\n";
return EXIT_FAILURE;
}
auto const address = boost::asio::ip::address::from_string(argv[1]);
auto const address = boost::asio::ip::make_address(argv[1]);
auto const port = static_cast<unsigned short>(std::atoi(argv[2]));
// The io_service is required for all I/O
boost::asio::io_service ios{1};
// The io_context is required for all I/O
boost::asio::io_context ioc{1};
// The acceptor receives incoming connections
tcp::acceptor acceptor{ios, {address, port}};
tcp::acceptor acceptor{ioc, {address, port}};
for(;;)
{
// This will receive the new connection
tcp::socket socket{ios};
tcp::socket socket{ioc};
// Block until we get a connection
acceptor.accept(socket);