Examples use bind_front_handler

This commit is contained in:
Vinnie Falco
2019-02-14 16:20:27 -08:00
parent 92add2afa6
commit 18447553ca
26 changed files with 248 additions and 397 deletions

View File

@ -3,6 +3,7 @@ Version 215:
* basic_stream uses boost::shared_ptr * basic_stream uses boost::shared_ptr
* Remove bind_back_handler * Remove bind_back_handler
* bind_front_handler works with member functions * bind_front_handler works with member functions
* Examples use bind_front_handler
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------

View File

@ -261,21 +261,18 @@ public:
// Set the control callback. This will be called // Set the control callback. This will be called
// on every incoming ping, pong, and close frame. // on every incoming ping, pong, and close frame.
derived().ws().control_callback( derived().ws().control_callback(
std::bind( beast::bind_front_handler(
&websocket_session::on_control_callback, &websocket_session::on_control_callback,
this, this));
std::placeholders::_1,
std::placeholders::_2));
// VFALCO What about the timer? // VFALCO What about the timer?
// Accept the websocket handshake // Accept the websocket handshake
derived().ws().async_accept( derived().ws().async_accept(
req, req,
std::bind( beast::bind_front_handler(
&websocket_session::on_accept, &websocket_session::on_accept,
derived().shared_from_this(), derived().shared_from_this()));
std::placeholders::_1));
} }
void void
@ -314,10 +311,9 @@ public:
// Now send the ping // Now send the ping
derived().ws().async_ping({}, derived().ws().async_ping({},
std::bind( beast::bind_front_handler(
&websocket_session::on_ping, &websocket_session::on_ping,
derived().shared_from_this(), derived().shared_from_this()));
std::placeholders::_1));
} }
else else
{ {
@ -334,10 +330,9 @@ public:
timer_.async_wait( timer_.async_wait(
net::bind_executor( net::bind_executor(
derived().ws().get_executor(), // use the strand derived().ws().get_executor(), // use the strand
std::bind( beast::bind_front_handler(
&websocket_session::on_timer, &websocket_session::on_timer,
derived().shared_from_this(), derived().shared_from_this())));
std::placeholders::_1)));
} }
// Called to indicate activity from the remote peer // Called to indicate activity from the remote peer
@ -393,11 +388,9 @@ public:
// Read a message into our buffer // Read a message into our buffer
derived().ws().async_read( derived().ws().async_read(
buffer_, buffer_,
std::bind( beast::bind_front_handler(
&websocket_session::on_read, &websocket_session::on_read,
derived().shared_from_this(), derived().shared_from_this()));
std::placeholders::_1,
std::placeholders::_2));
} }
void void
@ -425,11 +418,9 @@ public:
derived().ws().text(derived().ws().got_text()); derived().ws().text(derived().ws().got_text());
derived().ws().async_write( derived().ws().async_write(
buffer_.data(), buffer_.data(),
std::bind( beast::bind_front_handler(
&websocket_session::on_write, &websocket_session::on_write,
derived().shared_from_this(), derived().shared_from_this()));
std::placeholders::_1,
std::placeholders::_2));
} }
void void
@ -510,10 +501,9 @@ public:
// Close the WebSocket Connection // Close the WebSocket Connection
ws_.async_close( ws_.async_close(
websocket::close_code::normal, websocket::close_code::normal,
std::bind( beast::bind_front_handler(
&plain_websocket_session::on_close, &plain_websocket_session::on_close,
shared_from_this(), shared_from_this()));
std::placeholders::_1));
} }
void void
@ -581,10 +571,9 @@ public:
// Perform the SSL shutdown // Perform the SSL shutdown
ws_.next_layer().async_shutdown( ws_.next_layer().async_shutdown(
std::bind( beast::bind_front_handler(
&ssl_websocket_session::on_shutdown, &ssl_websocket_session::on_shutdown,
shared_from_this(), shared_from_this()));
std::placeholders::_1));
} }
void void
@ -724,10 +713,9 @@ class http_session
http::async_write( http::async_write(
self_.derived().stream(), self_.derived().stream(),
msg_, msg_,
std::bind( beast::bind_front_handler(
&http_session::on_write, &http_session::on_write,
self_.derived().shared_from_this(), self_.derived().shared_from_this(),
std::placeholders::_1,
msg_.need_eof())); msg_.need_eof()));
} }
}; };
@ -780,15 +768,16 @@ public:
derived().stream(), derived().stream(),
buffer_, buffer_,
req_, req_,
std::bind( beast::bind_front_handler(
&http_session::on_read, &http_session::on_read,
derived().shared_from_this(), derived().shared_from_this()));
std::placeholders::_1));
} }
void void
on_read(beast::error_code ec) on_read(beast::error_code ec, std::size_t bytes_transferred)
{ {
boost::ignore_unused(bytes_transferred);
// This means they closed the connection // This means they closed the connection
if(ec == http::error::end_of_stream) if(ec == http::error::end_of_stream)
return derived().do_eof(); return derived().do_eof();
@ -814,8 +803,10 @@ public:
} }
void void
on_write(beast::error_code ec, bool close) on_write(bool close, beast::error_code ec, std::size_t bytes_transferred)
{ {
boost::ignore_unused(bytes_transferred);
// Happens when the timer closes the socket // Happens when the timer closes the socket
if(ec == net::error::operation_aborted) if(ec == net::error::operation_aborted)
return; return;
@ -882,7 +873,7 @@ public:
if(! stream_.get_executor().running_in_this_thread()) if(! stream_.get_executor().running_in_this_thread())
return net::post( return net::post(
stream_.get_executor(), stream_.get_executor(),
std::bind( beast::bind_front_handler(
&plain_http_session::run, &plain_http_session::run,
shared_from_this())); shared_from_this()));
@ -958,7 +949,7 @@ public:
if(! stream_.get_executor().running_in_this_thread()) if(! stream_.get_executor().running_in_this_thread())
return net::post( return net::post(
stream_.get_executor(), stream_.get_executor(),
std::bind( beast::bind_front_handler(
&ssl_http_session::run, &ssl_http_session::run,
shared_from_this())); shared_from_this()));
@ -970,11 +961,9 @@ public:
stream_.async_handshake( stream_.async_handshake(
ssl::stream_base::server, ssl::stream_base::server,
buffer_.data(), buffer_.data(),
std::bind( beast::bind_front_handler(
&ssl_http_session::on_handshake, &ssl_http_session::on_handshake,
shared_from_this(), shared_from_this()));
std::placeholders::_1,
std::placeholders::_2));
} }
void void
on_handshake( on_handshake(
@ -1000,10 +989,9 @@ public:
// Perform the SSL shutdown // Perform the SSL shutdown
stream_.async_shutdown( stream_.async_shutdown(
std::bind( beast::bind_front_handler(
&ssl_http_session::on_shutdown, &ssl_http_session::on_shutdown,
shared_from_this(), shared_from_this()));
std::placeholders::_1));
} }
void void
@ -1052,11 +1040,9 @@ public:
async_detect_ssl( async_detect_ssl(
stream_, stream_,
buffer_, buffer_,
std::bind( beast::bind_front_handler(
&detect_session::on_detect, &detect_session::on_detect,
shared_from_this(), this->shared_from_this()));
std::placeholders::_1,
std::placeholders::_2));
} }
void void
@ -1150,11 +1136,9 @@ public:
do_accept() do_accept()
{ {
acceptor_.async_accept( acceptor_.async_accept(
std::bind( beast::bind_front_handler(
&listener::on_accept, &listener::on_accept,
shared_from_this(), shared_from_this()));
std::placeholders::_1,
std::placeholders::_2));
} }
void void

View File

@ -243,11 +243,9 @@ public:
// Set the control callback. This will be called // Set the control callback. This will be called
// on every incoming ping, pong, and close frame. // on every incoming ping, pong, and close frame.
ws_.control_callback( ws_.control_callback(
std::bind( beast::bind_front_handler(
&websocket_session::on_control_callback, &websocket_session::on_control_callback,
this, this));
std::placeholders::_1,
std::placeholders::_2));
// Run the timer. The timer is operated // Run the timer. The timer is operated
// continuously, this simplifies the code. // continuously, this simplifies the code.
@ -259,10 +257,9 @@ public:
// Accept the websocket handshake // Accept the websocket handshake
ws_.async_accept( ws_.async_accept(
req, req,
std::bind( beast::bind_front_handler(
&websocket_session::on_accept, &websocket_session::on_accept,
shared_from_this(), shared_from_this()));
std::placeholders::_1));
} }
void void
@ -301,10 +298,9 @@ public:
// Now send the ping // Now send the ping
ws_.async_ping({}, ws_.async_ping({},
std::bind( beast::bind_front_handler(
&websocket_session::on_ping, &websocket_session::on_ping,
shared_from_this(), shared_from_this()));
std::placeholders::_1));
} }
else else
{ {
@ -324,10 +320,9 @@ public:
timer_.async_wait( timer_.async_wait(
net::bind_executor( net::bind_executor(
ws_.get_executor(), // use the strand ws_.get_executor(), // use the strand
std::bind( beast::bind_front_handler(
&websocket_session::on_timer, &websocket_session::on_timer,
shared_from_this(), shared_from_this())));
std::placeholders::_1)));
} }
// Called to indicate activity from the remote peer // Called to indicate activity from the remote peer
@ -383,11 +378,9 @@ public:
// Read a message into our buffer // Read a message into our buffer
ws_.async_read( ws_.async_read(
buffer_, buffer_,
std::bind( beast::bind_front_handler(
&websocket_session::on_read, &websocket_session::on_read,
shared_from_this(), shared_from_this()));
std::placeholders::_1,
std::placeholders::_2));
} }
void void
@ -415,11 +408,9 @@ public:
ws_.text(ws_.got_text()); ws_.text(ws_.got_text());
ws_.async_write( ws_.async_write(
buffer_.data(), buffer_.data(),
std::bind( beast::bind_front_handler(
&websocket_session::on_write, &websocket_session::on_write,
shared_from_this(), shared_from_this()));
std::placeholders::_1,
std::placeholders::_2));
} }
void void
@ -520,10 +511,9 @@ class http_session : public std::enable_shared_from_this<http_session>
http::async_write( http::async_write(
self_.stream_, self_.stream_,
msg_, msg_,
std::bind( beast::bind_front_handler(
&http_session::on_write, &http_session::on_write,
self_.shared_from_this(), self_.shared_from_this(),
std::placeholders::_1,
msg_.need_eof())); msg_.need_eof()));
} }
}; };
@ -584,15 +574,16 @@ public:
// Read a request // Read a request
http::async_read(stream_, buffer_, req_, http::async_read(stream_, buffer_, req_,
std::bind( beast::bind_front_handler(
&http_session::on_read, &http_session::on_read,
shared_from_this(), shared_from_this()));
std::placeholders::_1));
} }
void void
on_read(beast::error_code ec) on_read(beast::error_code ec, std::size_t bytes_transferred)
{ {
boost::ignore_unused(bytes_transferred);
// This means they closed the connection // This means they closed the connection
if(ec == http::error::end_of_stream) if(ec == http::error::end_of_stream)
return do_close(); return do_close();
@ -618,8 +609,10 @@ public:
} }
void void
on_write(beast::error_code ec, bool close) on_write(bool close, beast::error_code ec, std::size_t bytes_transferred)
{ {
boost::ignore_unused(bytes_transferred);
// Happens when the timer closes the socket // Happens when the timer closes the socket
if(ec == net::error::operation_aborted) if(ec == net::error::operation_aborted)
return; return;
@ -720,11 +713,9 @@ public:
do_accept() do_accept()
{ {
acceptor_.async_accept( acceptor_.async_accept(
std::bind( beast::bind_front_handler(
&listener::on_accept, &listener::on_accept,
shared_from_this(), shared_from_this()));
std::placeholders::_1,
std::placeholders::_2));
} }
void void

View File

@ -287,8 +287,8 @@ async_detect_ssl(
AsyncReadStream, AsyncReadStream,
DynamicBuffer, DynamicBuffer,
BOOST_ASIO_HANDLER_TYPE( BOOST_ASIO_HANDLER_TYPE(
CompletionToken, void(beast::error_code, boost::tribool))>{ CompletionToken, void(beast::error_code, boost::tribool))>(
stream, buffer, init.completion_handler}(beast::error_code{}, 0); stream, buffer, std::move(init.completion_handler))(beast::error_code{}, 0);
// This hook lets the caller see a return value when appropriate. // This hook lets the caller see a return value when appropriate.
// For example this might return std::future<error_code, boost::tribool> if // For example this might return std::future<error_code, boost::tribool> if

View File

@ -389,7 +389,7 @@ public:
template<class... Args> template<class... Args>
void void
operator()(Args&&... args) const operator()(Args&&... args)
{ {
h_(std::forward<Args>(args)...); h_(std::forward<Args>(args)...);
} }

View File

@ -89,11 +89,9 @@ public:
resolver_.async_resolve( resolver_.async_resolve(
host, host,
port, port,
std::bind( beast::bind_front_handler(
&session::on_resolve, &session::on_resolve,
shared_from_this(), shared_from_this()));
std::placeholders::_1,
std::placeholders::_2));
} }
void void
@ -111,14 +109,13 @@ public:
beast::async_connect( beast::async_connect(
beast::get_lowest_layer(stream_), beast::get_lowest_layer(stream_),
results, results,
std::bind( beast::bind_front_handler(
&session::on_connect, &session::on_connect,
shared_from_this(), shared_from_this()));
std::placeholders::_1));
} }
void void
on_connect(beast::error_code ec) on_connect(beast::error_code ec, tcp::resolver::results_type::endpoint_type)
{ {
if(ec) if(ec)
return fail(ec, "connect"); return fail(ec, "connect");
@ -126,10 +123,9 @@ public:
// Perform the SSL handshake // Perform the SSL handshake
stream_.async_handshake( stream_.async_handshake(
ssl::stream_base::client, ssl::stream_base::client,
std::bind( beast::bind_front_handler(
&session::on_handshake, &session::on_handshake,
shared_from_this(), shared_from_this()));
std::placeholders::_1));
} }
void void
@ -143,11 +139,9 @@ public:
// Send the HTTP request to the remote host // Send the HTTP request to the remote host
http::async_write(stream_, req_, http::async_write(stream_, req_,
std::bind( beast::bind_front_handler(
&session::on_write, &session::on_write,
shared_from_this(), shared_from_this()));
std::placeholders::_1,
std::placeholders::_2));
} }
void void
@ -162,11 +156,9 @@ public:
// Receive the HTTP response // Receive the HTTP response
http::async_read(stream_, buffer_, res_, http::async_read(stream_, buffer_, res_,
std::bind( beast::bind_front_handler(
&session::on_read, &session::on_read,
shared_from_this(), shared_from_this()));
std::placeholders::_1,
std::placeholders::_2));
} }
void void
@ -187,10 +179,9 @@ public:
// Gracefully close the stream // Gracefully close the stream
stream_.async_shutdown( stream_.async_shutdown(
std::bind( beast::bind_front_handler(
&session::on_shutdown, &session::on_shutdown,
shared_from_this(), shared_from_this()));
std::placeholders::_1));
} }
void void

View File

@ -76,11 +76,9 @@ public:
resolver_.async_resolve( resolver_.async_resolve(
host, host,
port, port,
std::bind( beast::bind_front_handler(
&session::on_resolve, &session::on_resolve,
shared_from_this(), shared_from_this()));
std::placeholders::_1,
std::placeholders::_2));
} }
void void
@ -98,14 +96,13 @@ public:
beast::async_connect( beast::async_connect(
stream_, stream_,
results, results,
std::bind( beast::bind_front_handler(
&session::on_connect, &session::on_connect,
shared_from_this(), shared_from_this()));
std::placeholders::_1));
} }
void void
on_connect(beast::error_code ec) on_connect(beast::error_code ec, tcp::resolver::results_type::endpoint_type)
{ {
if(ec) if(ec)
return fail(ec, "connect"); return fail(ec, "connect");
@ -115,11 +112,9 @@ public:
// Send the HTTP request to the remote host // Send the HTTP request to the remote host
http::async_write(stream_, req_, http::async_write(stream_, req_,
std::bind( beast::bind_front_handler(
&session::on_write, &session::on_write,
shared_from_this(), shared_from_this()));
std::placeholders::_1,
std::placeholders::_2));
} }
void void
@ -134,11 +129,9 @@ public:
// Receive the HTTP response // Receive the HTTP response
http::async_read(stream_, buffer_, res_, http::async_read(stream_, buffer_, res_,
std::bind( beast::bind_front_handler(
&session::on_read, &session::on_read,
shared_from_this(), shared_from_this()));
std::placeholders::_1,
std::placeholders::_2));
} }
void void

View File

@ -217,10 +217,9 @@ public:
timer_.async_wait( timer_.async_wait(
net::bind_executor( net::bind_executor(
strand_, strand_,
std::bind( beast::bind_front_handler(
&worker::on_timer, &worker::on_timer,
shared_from_this(), shared_from_this())));
std::placeholders::_1)));
} }
void void
@ -249,11 +248,9 @@ public:
"http", "http",
net::bind_executor( net::bind_executor(
strand_, strand_,
std::bind( beast::bind_front_handler(
&worker::on_resolve, &worker::on_resolve,
shared_from_this(), shared_from_this())));
std::placeholders::_1,
std::placeholders::_2)));
} }
void void
@ -277,18 +274,16 @@ public:
// Make the connection on the IP address we get from a lookup // Make the connection on the IP address we get from a lookup
net::async_connect( net::async_connect(
socket_, socket_,
results.begin(), results,
results.end(),
net::bind_executor( net::bind_executor(
strand_, strand_,
std::bind( beast::bind_front_handler(
&worker::on_connect, &worker::on_connect,
shared_from_this(), shared_from_this())));
std::placeholders::_1)));
} }
void void
on_connect(beast::error_code ec) on_connect(beast::error_code ec, tcp::resolver::results_type::endpoint_type)
{ {
if(ec) if(ec)
{ {
@ -309,11 +304,9 @@ public:
req_, req_,
net::bind_executor( net::bind_executor(
strand_, strand_,
std::bind( beast::bind_front_handler(
&worker::on_write, &worker::on_write,
shared_from_this(), shared_from_this())));
std::placeholders::_1,
std::placeholders::_2)));
} }
void void
@ -344,11 +337,9 @@ public:
res_, res_,
net::bind_executor( net::bind_executor(
strand_, strand_,
std::bind( beast::bind_front_handler(
&worker::on_read, &worker::on_read,
shared_from_this(), shared_from_this())));
std::placeholders::_1,
std::placeholders::_2)));
} }
void void

View File

@ -248,11 +248,9 @@ class session : public std::enable_shared_from_this<session>
http::async_write( http::async_write(
self_.stream_, self_.stream_,
*sp, *sp,
std::bind( beast::bind_front_handler(
&session::on_write, &session::on_write,
self_.shared_from_this(), self_.shared_from_this(),
std::placeholders::_1,
std::placeholders::_2,
sp->need_eof())); sp->need_eof()));
} }
}; };
@ -287,10 +285,9 @@ public:
// Perform the SSL handshake // Perform the SSL handshake
stream_.async_handshake( stream_.async_handshake(
ssl::stream_base::server, ssl::stream_base::server,
std::bind( beast::bind_front_handler(
&session::on_handshake, &session::on_handshake,
shared_from_this(), shared_from_this()));
std::placeholders::_1));
} }
void void
@ -314,11 +311,9 @@ public:
// Read a request // Read a request
http::async_read(stream_, buffer_, req_, http::async_read(stream_, buffer_, req_,
std::bind( beast::bind_front_handler(
&session::on_read, &session::on_read,
shared_from_this(), shared_from_this()));
std::placeholders::_1,
std::placeholders::_2));
} }
void void
@ -341,9 +336,9 @@ public:
void void
on_write( on_write(
bool close,
beast::error_code ec, beast::error_code ec,
std::size_t bytes_transferred, std::size_t bytes_transferred)
bool close)
{ {
boost::ignore_unused(bytes_transferred); boost::ignore_unused(bytes_transferred);
@ -372,10 +367,9 @@ public:
// Perform the SSL shutdown // Perform the SSL shutdown
stream_.async_shutdown( stream_.async_shutdown(
std::bind( beast::bind_front_handler(
&session::on_shutdown, &session::on_shutdown,
shared_from_this(), shared_from_this()));
std::placeholders::_1));
} }
void void
@ -456,11 +450,9 @@ public:
do_accept() do_accept()
{ {
acceptor_.async_accept( acceptor_.async_accept(
std::bind( beast::bind_front_handler(
&listener::on_accept, &listener::on_accept,
shared_from_this(), shared_from_this()));
std::placeholders::_1,
std::placeholders::_2));
} }
void void

View File

@ -244,11 +244,9 @@ class session : public std::enable_shared_from_this<session>
http::async_write( http::async_write(
self_.stream_, self_.stream_,
*sp, *sp,
std::bind( beast::bind_front_handler(
&session::on_write, &session::on_write,
self_.shared_from_this(), self_.shared_from_this(),
std::placeholders::_1,
std::placeholders::_2,
sp->need_eof())); sp->need_eof()));
} }
}; };
@ -290,11 +288,9 @@ public:
// Read a request // Read a request
http::async_read(stream_, buffer_, req_, http::async_read(stream_, buffer_, req_,
std::bind( beast::bind_front_handler(
&session::on_read, &session::on_read,
shared_from_this(), shared_from_this()));
std::placeholders::_1,
std::placeholders::_2));
} }
void void
@ -317,9 +313,9 @@ public:
void void
on_write( on_write(
bool close,
beast::error_code ec, beast::error_code ec,
std::size_t bytes_transferred, std::size_t bytes_transferred)
bool close)
{ {
boost::ignore_unused(bytes_transferred); boost::ignore_unused(bytes_transferred);
@ -416,11 +412,9 @@ public:
do_accept() do_accept()
{ {
acceptor_.async_accept( acceptor_.async_accept(
std::bind( beast::bind_front_handler(
&listener::on_accept, &listener::on_accept,
shared_from_this(), shared_from_this()));
std::placeholders::_1,
std::placeholders::_2));
} }
void void

View File

@ -266,11 +266,9 @@ class session
http::async_write( http::async_write(
self_.derived().stream(), self_.derived().stream(),
*sp, *sp,
std::bind( beast::bind_front_handler(
&session::on_write, &session::on_write,
self_.derived().shared_from_this(), self_.derived().shared_from_this(),
std::placeholders::_1,
std::placeholders::_2,
sp->need_eof())); sp->need_eof()));
} }
}; };
@ -306,11 +304,9 @@ public:
derived().stream(), derived().stream(),
buffer_, buffer_,
req_, req_,
std::bind( beast::bind_front_handler(
&session::on_read, &session::on_read,
derived().shared_from_this(), derived().shared_from_this()));
std::placeholders::_1,
std::placeholders::_2));
} }
void void
@ -333,9 +329,9 @@ public:
void void
on_write( on_write(
bool close,
beast::error_code ec, beast::error_code ec,
std::size_t bytes_transferred, std::size_t bytes_transferred)
bool close)
{ {
boost::ignore_unused(bytes_transferred); boost::ignore_unused(bytes_transferred);
@ -442,11 +438,9 @@ public:
stream_.async_handshake( stream_.async_handshake(
ssl::stream_base::server, ssl::stream_base::server,
buffer_.data(), buffer_.data(),
std::bind( beast::bind_front_handler(
&ssl_session::on_handshake, &ssl_session::on_handshake,
shared_from_this(), shared_from_this()));
std::placeholders::_1,
std::placeholders::_2));
} }
void void
on_handshake( on_handshake(
@ -470,10 +464,9 @@ public:
// Perform the SSL shutdown // Perform the SSL shutdown
stream_.async_shutdown( stream_.async_shutdown(
std::bind( beast::bind_front_handler(
&ssl_session::on_shutdown, &ssl_session::on_shutdown,
shared_from_this(), shared_from_this()));
std::placeholders::_1));
} }
void void
@ -518,11 +511,9 @@ public:
async_detect_ssl( async_detect_ssl(
stream_, stream_,
buffer_, buffer_,
std::bind( beast::bind_front_handler(
&detect_session::on_detect, &detect_session::on_detect,
shared_from_this(), shared_from_this()));
std::placeholders::_1,
std::placeholders::_2));
} }
void void
@ -616,11 +607,9 @@ public:
do_accept() do_accept()
{ {
acceptor_.async_accept( acceptor_.async_accept(
std::bind( beast::bind_front_handler(
&listener::on_accept, &listener::on_accept,
shared_from_this(), shared_from_this()));
std::placeholders::_1,
std::placeholders::_2));
} }
void void

View File

@ -251,11 +251,9 @@ class session
http::async_write( http::async_write(
self_.stream_, self_.stream_,
*sp, *sp,
std::bind( beast::bind_front_handler(
&session::loop, &session::loop,
self_.shared_from_this(), self_.shared_from_this(),
std::placeholders::_1,
std::placeholders::_2,
sp->need_eof())); sp->need_eof()));
} }
}; };
@ -283,15 +281,15 @@ public:
void void
run() run()
{ {
loop({}, 0, false); loop(false, {}, 0);
} }
#include <boost/asio/yield.hpp> #include <boost/asio/yield.hpp>
void void
loop( loop(
bool close,
beast::error_code ec, beast::error_code ec,
std::size_t bytes_transferred, std::size_t bytes_transferred)
bool close)
{ {
boost::ignore_unused(bytes_transferred); boost::ignore_unused(bytes_transferred);
reenter(*this) reenter(*this)
@ -307,11 +305,9 @@ public:
// Read a request // Read a request
yield http::async_read(stream_, buffer_, req_, yield http::async_read(stream_, buffer_, req_,
std::bind( beast::bind_front_handler(
&session::loop, &session::loop,
shared_from_this(), shared_from_this(),
std::placeholders::_1,
std::placeholders::_2,
false)); false));
if(ec == http::error::end_of_stream) if(ec == http::error::end_of_stream)
{ {
@ -419,10 +415,9 @@ public:
{ {
yield acceptor_.async_accept( yield acceptor_.async_accept(
socket_, socket_,
std::bind( beast::bind_front_handler(
&listener::loop, &listener::loop,
shared_from_this(), shared_from_this()));
std::placeholders::_1));
if(ec) if(ec)
{ {
fail(ec, "accept"); fail(ec, "accept");

View File

@ -75,11 +75,9 @@ public:
resolver_.async_resolve( resolver_.async_resolve(
host, host,
port, port,
std::bind( beast::bind_front_handler(
&session::on_resolve, &session::on_resolve,
shared_from_this(), shared_from_this()));
std::placeholders::_1,
std::placeholders::_2));
} }
void void
@ -97,14 +95,13 @@ public:
beast::async_connect( beast::async_connect(
beast::get_lowest_layer(ws_), beast::get_lowest_layer(ws_),
results, results,
std::bind( beast::bind_front_handler(
&session::on_connect, &session::on_connect,
shared_from_this(), shared_from_this()));
std::placeholders::_1));
} }
void void
on_connect(beast::error_code ec) on_connect(beast::error_code ec, tcp::resolver::results_type::endpoint_type)
{ {
if(ec) if(ec)
return fail(ec, "connect"); return fail(ec, "connect");
@ -115,10 +112,9 @@ public:
// Perform the SSL handshake // Perform the SSL handshake
ws_.next_layer().async_handshake( ws_.next_layer().async_handshake(
ssl::stream_base::client, ssl::stream_base::client,
std::bind( beast::bind_front_handler(
&session::on_ssl_handshake, &session::on_ssl_handshake,
shared_from_this(), shared_from_this()));
std::placeholders::_1));
} }
void void
@ -129,10 +125,9 @@ public:
// Perform the websocket handshake // Perform the websocket handshake
ws_.async_handshake(host_, "/", ws_.async_handshake(host_, "/",
std::bind( beast::bind_front_handler(
&session::on_handshake, &session::on_handshake,
shared_from_this(), shared_from_this()));
std::placeholders::_1));
} }
void void
@ -144,11 +139,9 @@ public:
// Send the message // Send the message
ws_.async_write( ws_.async_write(
net::buffer(text_), net::buffer(text_),
std::bind( beast::bind_front_handler(
&session::on_write, &session::on_write,
shared_from_this(), shared_from_this()));
std::placeholders::_1,
std::placeholders::_2));
} }
void void
@ -164,11 +157,9 @@ public:
// Read a message into our buffer // Read a message into our buffer
ws_.async_read( ws_.async_read(
buffer_, buffer_,
std::bind( beast::bind_front_handler(
&session::on_read, &session::on_read,
shared_from_this(), shared_from_this()));
std::placeholders::_1,
std::placeholders::_2));
} }
void void
@ -183,10 +174,9 @@ public:
// Close the WebSocket connection // Close the WebSocket connection
ws_.async_close(websocket::close_code::normal, ws_.async_close(websocket::close_code::normal,
std::bind( beast::bind_front_handler(
&session::on_close, &session::on_close,
shared_from_this(), shared_from_this()));
std::placeholders::_1));
} }
void void

View File

@ -70,11 +70,9 @@ public:
resolver_.async_resolve( resolver_.async_resolve(
host, host,
port, port,
std::bind( beast::bind_front_handler(
&session::on_resolve, &session::on_resolve,
shared_from_this(), shared_from_this()));
std::placeholders::_1,
std::placeholders::_2));
} }
void void
@ -92,24 +90,22 @@ public:
beast::async_connect( beast::async_connect(
beast::get_lowest_layer(ws_), beast::get_lowest_layer(ws_),
results, results,
std::bind( beast::bind_front_handler(
&session::on_connect, &session::on_connect,
shared_from_this(), shared_from_this()));
std::placeholders::_1));
} }
void void
on_connect(beast::error_code ec) on_connect(beast::error_code ec, tcp::resolver::results_type::endpoint_type)
{ {
if(ec) if(ec)
return fail(ec, "connect"); return fail(ec, "connect");
// Perform the websocket handshake // Perform the websocket handshake
ws_.async_handshake(host_, "/", ws_.async_handshake(host_, "/",
std::bind( beast::bind_front_handler(
&session::on_handshake, &session::on_handshake,
shared_from_this(), shared_from_this()));
std::placeholders::_1));
} }
void void
@ -121,11 +117,9 @@ public:
// Send the message // Send the message
ws_.async_write( ws_.async_write(
net::buffer(text_), net::buffer(text_),
std::bind( beast::bind_front_handler(
&session::on_write, &session::on_write,
shared_from_this(), shared_from_this()));
std::placeholders::_1,
std::placeholders::_2));
} }
void void
@ -141,11 +135,9 @@ public:
// Read a message into our buffer // Read a message into our buffer
ws_.async_read( ws_.async_read(
buffer_, buffer_,
std::bind( beast::bind_front_handler(
&session::on_read, &session::on_read,
shared_from_this(), shared_from_this()));
std::placeholders::_1,
std::placeholders::_2));
} }
void void
@ -160,10 +152,9 @@ public:
// Close the WebSocket connection // Close the WebSocket connection
ws_.async_close(websocket::close_code::normal, ws_.async_close(websocket::close_code::normal,
std::bind( beast::bind_front_handler(
&session::on_close, &session::on_close,
shared_from_this(), shared_from_this()));
std::placeholders::_1));
} }
void void

View File

@ -69,10 +69,9 @@ public:
// Perform the SSL handshake // Perform the SSL handshake
ws_.next_layer().async_handshake( ws_.next_layer().async_handshake(
ssl::stream_base::server, ssl::stream_base::server,
std::bind( beast::bind_front_handler(
&session::on_handshake, &session::on_handshake,
shared_from_this(), shared_from_this()));
std::placeholders::_1));
} }
void void
@ -83,10 +82,9 @@ public:
// Accept the websocket handshake // Accept the websocket handshake
ws_.async_accept( ws_.async_accept(
std::bind( beast::bind_front_handler(
&session::on_accept, &session::on_accept,
shared_from_this(), shared_from_this()));
std::placeholders::_1));
} }
void void
@ -105,11 +103,9 @@ public:
// Read a message into our buffer // Read a message into our buffer
ws_.async_read( ws_.async_read(
buffer_, buffer_,
std::bind( beast::bind_front_handler(
&session::on_read, &session::on_read,
shared_from_this(), shared_from_this()));
std::placeholders::_1,
std::placeholders::_2));
} }
void void
@ -130,11 +126,9 @@ public:
ws_.text(ws_.got_text()); ws_.text(ws_.got_text());
ws_.async_write( ws_.async_write(
buffer_.data(), buffer_.data(),
std::bind( beast::bind_front_handler(
&session::on_write, &session::on_write,
shared_from_this(), shared_from_this()));
std::placeholders::_1,
std::placeholders::_2));
} }
void void
@ -220,11 +214,9 @@ public:
do_accept() do_accept()
{ {
acceptor_.async_accept( acceptor_.async_accept(
std::bind( beast::bind_front_handler(
&listener::on_accept, &listener::on_accept,
shared_from_this(), shared_from_this()));
std::placeholders::_1,
std::placeholders::_2));
} }
void void

View File

@ -60,10 +60,9 @@ public:
{ {
// Accept the websocket handshake // Accept the websocket handshake
ws_.async_accept( ws_.async_accept(
std::bind( beast::bind_front_handler(
&session::on_accept, &session::on_accept,
shared_from_this(), shared_from_this()));
std::placeholders::_1));
} }
void void
@ -82,11 +81,9 @@ public:
// Read a message into our buffer // Read a message into our buffer
ws_.async_read( ws_.async_read(
buffer_, buffer_,
std::bind( beast::bind_front_handler(
&session::on_read, &session::on_read,
shared_from_this(), shared_from_this()));
std::placeholders::_1,
std::placeholders::_2));
} }
void void
@ -107,11 +104,9 @@ public:
ws_.text(ws_.got_text()); ws_.text(ws_.got_text());
ws_.async_write( ws_.async_write(
buffer_.data(), buffer_.data(),
std::bind( beast::bind_front_handler(
&session::on_write, &session::on_write,
shared_from_this(), shared_from_this()));
std::placeholders::_1,
std::placeholders::_2));
} }
void void
@ -194,11 +189,9 @@ public:
do_accept() do_accept()
{ {
acceptor_.async_accept( acceptor_.async_accept(
std::bind( beast::bind_front_handler(
&listener::on_accept, &listener::on_accept,
shared_from_this(), shared_from_this()));
std::placeholders::_1,
std::placeholders::_2));
} }
void void

View File

@ -206,11 +206,9 @@ run()
// Read a request // Read a request
http::async_read(stream_, buffer_, req_, http::async_read(stream_, buffer_, req_,
std::bind( beast::bind_front_handler(
&http_session::on_read, &http_session::on_read,
shared_from_this(), shared_from_this()));
std::placeholders::_1,
std::placeholders::_2));
} }
// Report a failure // Report a failure
@ -348,9 +346,7 @@ on_write(beast::error_code ec, std::size_t, bool close)
// Read another request // Read another request
http::async_read(stream_, buffer_, req_, http::async_read(stream_, buffer_, req_,
std::bind( beast::bind_front_handler(
&http_session::on_read, &http_session::on_read,
shared_from_this(), shared_from_this()));
std::placeholders::_1,
std::placeholders::_2));
} }

View File

@ -61,11 +61,9 @@ run()
{ {
// Start accepting a connection // Start accepting a connection
acceptor_.async_accept( acceptor_.async_accept(
std::bind( beast::bind_front_handler(
&listener::on_accept, &listener::on_accept,
shared_from_this(), shared_from_this()));
std::placeholders::_1,
std::placeholders::_2));
} }
// Report a failure // Report a failure
@ -94,9 +92,7 @@ on_accept(beast::error_code ec, tcp::socket socket)
// Accept another connection // Accept another connection
acceptor_.async_accept( acceptor_.async_accept(
std::bind( beast::bind_front_handler(
&listener::on_accept, &listener::on_accept,
shared_from_this(), shared_from_this()));
std::placeholders::_1,
std::placeholders::_2));
} }

View File

@ -52,11 +52,9 @@ on_accept(beast::error_code ec)
// Read a message // Read a message
ws_.async_read( ws_.async_read(
buffer_, buffer_,
std::bind( beast::bind_front_handler(
&websocket_session::on_read, &websocket_session::on_read,
shared_from_this(), shared_from_this()));
std::placeholders::_1,
std::placeholders::_2));
} }
void void
@ -76,11 +74,9 @@ on_read(beast::error_code ec, std::size_t)
// Read another message // Read another message
ws_.async_read( ws_.async_read(
buffer_, buffer_,
std::bind( beast::bind_front_handler(
&websocket_session::on_read, &websocket_session::on_read,
shared_from_this(), shared_from_this()));
std::placeholders::_1,
std::placeholders::_2));
} }
void void
@ -93,7 +89,7 @@ send(boost::shared_ptr<std::string const> const& ss)
if(! ws_.get_executor().running_in_this_thread()) if(! ws_.get_executor().running_in_this_thread())
return net::post( return net::post(
ws_.get_executor(), ws_.get_executor(),
std::bind( beast::bind_front_handler(
&websocket_session::send, &websocket_session::send,
shared_from_this(), shared_from_this(),
ss)); ss));
@ -108,11 +104,9 @@ send(boost::shared_ptr<std::string const> const& ss)
// We are not currently writing, so send this immediately // We are not currently writing, so send this immediately
ws_.async_write( ws_.async_write(
net::buffer(*queue_.front()), net::buffer(*queue_.front()),
std::bind( beast::bind_front_handler(
&websocket_session::on_write, &websocket_session::on_write,
shared_from_this(), shared_from_this()));
std::placeholders::_1,
std::placeholders::_2));
} }
void void
@ -130,9 +124,7 @@ on_write(beast::error_code ec, std::size_t)
if(! queue_.empty()) if(! queue_.empty())
ws_.async_write( ws_.async_write(
net::buffer(*queue_.front()), net::buffer(*queue_.front()),
std::bind( beast::bind_front_handler(
&websocket_session::on_write, &websocket_session::on_write,
shared_from_this(), shared_from_this()));
std::placeholders::_1,
std::placeholders::_2));
} }

View File

@ -60,10 +60,9 @@ run(http::request<Body, http::basic_fields<Allocator>> req)
// Accept the websocket handshake // Accept the websocket handshake
ws_.async_accept( ws_.async_accept(
req, req,
std::bind( beast::bind_front_handler(
&websocket_session::on_accept, &websocket_session::on_accept,
shared_from_this(), shared_from_this()));
std::placeholders::_1));
} }
#endif #endif

View File

@ -131,9 +131,9 @@ do_sync_listen(
if(ec) if(ec)
return fail(ec, "accept"); return fail(ec, "accept");
std::thread{std::bind( std::thread(std::bind(
&do_sync_session, &do_sync_session,
ws_type(std::move(socket)))}.detach(); ws_type(std::move(socket)))).detach();
} }
} }
@ -165,10 +165,9 @@ public:
res.set(http::field::server, res.set(http::field::server,
"Boost.Beast/" + std::to_string(BOOST_BEAST_VERSION) + "-Async"); "Boost.Beast/" + std::to_string(BOOST_BEAST_VERSION) + "-Async");
}, },
std::bind( beast::bind_front_handler(
&async_session::on_accept, &async_session::on_accept,
shared_from_this(), shared_from_this()));
std::placeholders::_1));
} }
void void
@ -187,11 +186,9 @@ public:
// Read a message into our buffer // Read a message into our buffer
ws_.async_read( ws_.async_read(
buffer_, buffer_,
std::bind( beast::bind_front_handler(
&async_session::on_read, &async_session::on_read,
shared_from_this(), shared_from_this()));
std::placeholders::_1,
std::placeholders::_2));
} }
void void
@ -212,11 +209,9 @@ public:
ws_.text(ws_.got_text()); ws_.text(ws_.got_text());
ws_.async_write( ws_.async_write(
buffer_.data(), buffer_.data(),
std::bind( beast::bind_front_handler(
&async_session::on_write, &async_session::on_write,
shared_from_this(), shared_from_this()));
std::placeholders::_1,
std::placeholders::_2));
} }
void void
@ -297,11 +292,9 @@ public:
do_accept() do_accept()
{ {
acceptor_.async_accept( acceptor_.async_accept(
std::bind( beast::bind_front_handler(
&async_listener::on_accept, &async_listener::on_accept,
shared_from_this(), shared_from_this()));
std::placeholders::_1,
std::placeholders::_2));
} }
void void
@ -428,7 +421,7 @@ int main(int argc, char* argv[])
net::io_context ioc{threads}; net::io_context ioc{threads};
// Create sync port // Create sync port
std::thread(std::bind( std::thread(beast::bind_front_handler(
&do_sync_listen, &do_sync_listen,
std::ref(ioc), std::ref(ioc),
tcp::endpoint{ tcp::endpoint{

View File

@ -261,19 +261,16 @@ private:
if(s_.empty()) if(s_.empty())
socket_.async_wait( socket_.async_wait(
net::socket_base::wait_read, net::socket_base::wait_read,
std::bind( bind_front_handler(
&session::on_read, &session::on_read,
shared_from_this(), shared_from_this()));
std::placeholders::_1));
else else
net::async_write( net::async_write(
socket_, socket_,
net::const_buffer(s_.data(), s_.size()), net::const_buffer(s_.data(), s_.size()),
std::bind( bind_front_handler(
&session::on_write, &session::on_write,
shared_from_this(), shared_from_this()));
std::placeholders::_1,
std::placeholders::_2));
} }
protected: protected:

View File

@ -12,8 +12,9 @@
#include <boost/beast/core/multi_buffer.hpp> #include <boost/beast/core/multi_buffer.hpp>
#include <boost/beast/_experimental/test/stream.hpp> #include <boost/beast/_experimental/test/stream.hpp>
#include <boost/beast/test/yield_to.hpp>
#include <boost/beast/_experimental/unit_test/suite.hpp> #include <boost/beast/_experimental/unit_test/suite.hpp>
#include <boost/beast/core/bind_handler.hpp>
#include <boost/beast/test/yield_to.hpp>
#include <boost/asio/buffer.hpp> #include <boost/asio/buffer.hpp>
#include <boost/asio/io_context.hpp> #include <boost/asio/io_context.hpp>
#include <boost/asio/read.hpp> #include <boost/asio/read.hpp>
@ -104,11 +105,9 @@ public:
brs_->buffer().prepare(5), net::buffer("Hello", 5))); brs_->buffer().prepare(5), net::buffer("Hello", 5)));
net::async_read(*brs_, net::async_read(*brs_,
net::buffer(&s_[0], s_.size()), net::buffer(&s_[0], s_.size()),
std::bind( bind_front_handler(
&loop::on_read, &loop::on_read,
shared_from_this(), shared_from_this()));
std::placeholders::_1,
std::placeholders::_2));
} }
}; };

View File

@ -10,6 +10,7 @@
#ifndef BEAST_TEST_WEBSOCKET_TEST_HPP #ifndef BEAST_TEST_WEBSOCKET_TEST_HPP
#define BEAST_TEST_WEBSOCKET_TEST_HPP #define BEAST_TEST_WEBSOCKET_TEST_HPP
#include <boost/beast/core/bind_handler.hpp>
#include <boost/beast/core/buffer_size.hpp> #include <boost/beast/core/buffer_size.hpp>
#include <boost/beast/core/buffers_prefix.hpp> #include <boost/beast/core/buffers_prefix.hpp>
#include <boost/beast/core/buffers_to_string.hpp> #include <boost/beast/core/buffers_to_string.hpp>
@ -131,10 +132,9 @@ public:
async_handshake() async_handshake()
{ {
ws_.async_handshake("localhost", "/", ws_.async_handshake("localhost", "/",
std::bind( bind_front_handler(
&echo_server::on_handshake, &echo_server::on_handshake,
this, this));
std::placeholders::_1));
} }
void void
@ -146,10 +146,9 @@ public:
if(ws_.is_open()) if(ws_.is_open())
{ {
ws_.async_close({}, ws_.async_close({},
std::bind( bind_front_handler(
&echo_server::on_close, &echo_server::on_close,
this, this));
std::placeholders::_1));
} }
else else
{ {
@ -192,10 +191,10 @@ public:
void void
do_accept() do_accept()
{ {
ws_.async_accept(std::bind( ws_.async_accept(
&echo_server::on_accept, bind_front_handler(
this, &echo_server::on_accept,
std::placeholders::_1)); this));
} }
void void
@ -216,10 +215,9 @@ public:
if(close_) if(close_)
{ {
return ws_.async_close({}, return ws_.async_close({},
std::bind( bind_front_handler(
&echo_server::on_close, &echo_server::on_close,
this, this));
std::placeholders::_1));
} }
do_read(); do_read();
@ -229,27 +227,25 @@ public:
do_read() do_read()
{ {
ws_.async_read(buffer_, ws_.async_read(buffer_,
std::bind( beast::bind_front_handler(
&echo_server::on_read, &echo_server::on_read,
this, this));
std::placeholders::_1));
} }
void void
on_read(error_code ec) on_read(error_code ec, std::size_t)
{ {
if(ec) if(ec)
return fail(ec); return fail(ec);
ws_.text(ws_.got_text()); ws_.text(ws_.got_text());
ws_.async_write(buffer_.data(), ws_.async_write(buffer_.data(),
std::bind( beast::bind_front_handler(
&echo_server::on_write, &echo_server::on_write,
this, this));
std::placeholders::_1));
} }
void void
on_write(error_code ec) on_write(error_code ec, std::size_t)
{ {
if(ec) if(ec)
return fail(ec); return fail(ec);

View File

@ -153,10 +153,9 @@ public:
run() run()
{ {
ws_.next_layer().async_connect(ep_, ws_.next_layer().async_connect(ep_,
alloc_.wrap(std::bind( alloc_.wrap(beast::bind_front_handler(
&connection::on_connect, &connection::on_connect,
shared_from_this(), shared_from_this())));
std::placeholders::_1)));
} }
private: private:
@ -169,10 +168,9 @@ private:
ws_.async_handshake( ws_.async_handshake(
ep_.address().to_string() + ":" + std::to_string(ep_.port()), ep_.address().to_string() + ":" + std::to_string(ep_.port()),
"/", "/",
alloc_.wrap(std::bind( alloc_.wrap(beast::bind_front_handler(
&connection::on_handshake, &connection::on_handshake,
shared_from_this(), shared_from_this())));
std::placeholders::_1)));
} }
void void
@ -191,14 +189,13 @@ private:
double(4) / beast::buffer_size(tb_)}; double(4) / beast::buffer_size(tb_)};
ws_.async_write_some(true, ws_.async_write_some(true,
beast::buffers_prefix(dist(rng_), tb_), beast::buffers_prefix(dist(rng_), tb_),
alloc_.wrap(std::bind( alloc_.wrap(beast::bind_front_handler(
&connection::on_write, &connection::on_write,
shared_from_this(), shared_from_this())));
std::placeholders::_1)));
} }
void void
on_write(beast::error_code ec) on_write(beast::error_code ec, std::size_t)
{ {
if(ec) if(ec)
return fail(ec, "write"); return fail(ec, "write");
@ -207,24 +204,22 @@ private:
return do_read(); return do_read();
ws_.async_close({}, ws_.async_close({},
alloc_.wrap(std::bind( alloc_.wrap(beast::bind_front_handler(
&connection::on_close, &connection::on_close,
shared_from_this(), shared_from_this())));
std::placeholders::_1)));
} }
void void
do_read() do_read()
{ {
ws_.async_read(buffer_, ws_.async_read(buffer_,
alloc_.wrap(std::bind( alloc_.wrap(beast::bind_front_handler(
&connection::on_read, &connection::on_read,
shared_from_this(), shared_from_this())));
std::placeholders::_1)));
} }
void void
on_read(beast::error_code ec) on_read(beast::error_code ec, std::size_t)
{ {
if(ec) if(ec)
return fail(ec, "read"); return fail(ec, "read");

View File

@ -25,6 +25,7 @@ namespace boost {
namespace beast { namespace beast {
namespace test { namespace test {
// DEPRECATED
class ws_echo_server class ws_echo_server
{ {
std::ostream& log_; std::ostream& log_;