From 18447553caca6b9054e77ed0719d85f2bb24be00 Mon Sep 17 00:00:00 2001 From: Vinnie Falco Date: Thu, 14 Feb 2019 16:20:27 -0800 Subject: [PATCH] Examples use bind_front_handler --- CHANGELOG.md | 1 + .../server-flex/advanced_server_flex.cpp | 86 ++++++++----------- example/advanced/server/advanced_server.cpp | 55 +++++------- example/common/detect_ssl.hpp | 4 +- example/common/session_alloc.hpp | 2 +- .../async-ssl/http_client_async_ssl.cpp | 35 +++----- .../http/client/async/http_client_async.cpp | 25 ++---- example/http/client/crawl/http_crawl.cpp | 33 +++---- .../async-ssl/http_server_async_ssl.cpp | 30 +++---- .../http/server/async/http_server_async.cpp | 20 ++--- example/http/server/flex/http_server_flex.cpp | 37 +++----- .../stackless/http_server_stackless.cpp | 19 ++-- .../async-ssl/websocket_client_async_ssl.cpp | 40 ++++----- .../client/async/websocket_client_async.cpp | 35 +++----- .../async-ssl/websocket_server_async_ssl.cpp | 28 +++--- .../server/async/websocket_server_async.cpp | 23 ++--- .../server/chat-multi/http_session.cpp | 12 +-- .../websocket/server/chat-multi/listener.cpp | 12 +-- .../server/chat-multi/websocket_session.cpp | 26 ++---- .../server/chat-multi/websocket_session.hpp | 5 +- .../server/fast/websocket_server_fast.cpp | 29 +++---- test/beast/core/basic_stream.cpp | 11 +-- test/beast/core/buffered_read_stream.cpp | 9 +- test/beast/websocket/test.hpp | 38 ++++---- test/bench/wsload/wsload.cpp | 29 +++---- .../include/boost/beast/test/websocket.hpp | 1 + 26 files changed, 248 insertions(+), 397 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 02c55cd2..153118f2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ Version 215: * basic_stream uses boost::shared_ptr * Remove bind_back_handler * bind_front_handler works with member functions +* Examples use bind_front_handler -------------------------------------------------------------------------------- diff --git a/example/advanced/server-flex/advanced_server_flex.cpp b/example/advanced/server-flex/advanced_server_flex.cpp index 7c9f74b7..a6036e0a 100644 --- a/example/advanced/server-flex/advanced_server_flex.cpp +++ b/example/advanced/server-flex/advanced_server_flex.cpp @@ -261,21 +261,18 @@ public: // Set the control callback. This will be called // on every incoming ping, pong, and close frame. derived().ws().control_callback( - std::bind( + beast::bind_front_handler( &websocket_session::on_control_callback, - this, - std::placeholders::_1, - std::placeholders::_2)); + this)); // VFALCO What about the timer? // Accept the websocket handshake derived().ws().async_accept( req, - std::bind( + beast::bind_front_handler( &websocket_session::on_accept, - derived().shared_from_this(), - std::placeholders::_1)); + derived().shared_from_this())); } void @@ -314,10 +311,9 @@ public: // Now send the ping derived().ws().async_ping({}, - std::bind( + beast::bind_front_handler( &websocket_session::on_ping, - derived().shared_from_this(), - std::placeholders::_1)); + derived().shared_from_this())); } else { @@ -334,10 +330,9 @@ public: timer_.async_wait( net::bind_executor( derived().ws().get_executor(), // use the strand - std::bind( + beast::bind_front_handler( &websocket_session::on_timer, - derived().shared_from_this(), - std::placeholders::_1))); + derived().shared_from_this()))); } // Called to indicate activity from the remote peer @@ -393,11 +388,9 @@ public: // Read a message into our buffer derived().ws().async_read( buffer_, - std::bind( + beast::bind_front_handler( &websocket_session::on_read, - derived().shared_from_this(), - std::placeholders::_1, - std::placeholders::_2)); + derived().shared_from_this())); } void @@ -425,11 +418,9 @@ public: derived().ws().text(derived().ws().got_text()); derived().ws().async_write( buffer_.data(), - std::bind( + beast::bind_front_handler( &websocket_session::on_write, - derived().shared_from_this(), - std::placeholders::_1, - std::placeholders::_2)); + derived().shared_from_this())); } void @@ -510,10 +501,9 @@ public: // Close the WebSocket Connection ws_.async_close( websocket::close_code::normal, - std::bind( + beast::bind_front_handler( &plain_websocket_session::on_close, - shared_from_this(), - std::placeholders::_1)); + shared_from_this())); } void @@ -581,10 +571,9 @@ public: // Perform the SSL shutdown ws_.next_layer().async_shutdown( - std::bind( + beast::bind_front_handler( &ssl_websocket_session::on_shutdown, - shared_from_this(), - std::placeholders::_1)); + shared_from_this())); } void @@ -724,10 +713,9 @@ class http_session http::async_write( self_.derived().stream(), msg_, - std::bind( + beast::bind_front_handler( &http_session::on_write, self_.derived().shared_from_this(), - std::placeholders::_1, msg_.need_eof())); } }; @@ -780,15 +768,16 @@ public: derived().stream(), buffer_, req_, - std::bind( + beast::bind_front_handler( &http_session::on_read, - derived().shared_from_this(), - std::placeholders::_1)); + derived().shared_from_this())); } 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 if(ec == http::error::end_of_stream) return derived().do_eof(); @@ -814,8 +803,10 @@ public: } 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 if(ec == net::error::operation_aborted) return; @@ -882,7 +873,7 @@ public: if(! stream_.get_executor().running_in_this_thread()) return net::post( stream_.get_executor(), - std::bind( + beast::bind_front_handler( &plain_http_session::run, shared_from_this())); @@ -958,7 +949,7 @@ public: if(! stream_.get_executor().running_in_this_thread()) return net::post( stream_.get_executor(), - std::bind( + beast::bind_front_handler( &ssl_http_session::run, shared_from_this())); @@ -970,11 +961,9 @@ public: stream_.async_handshake( ssl::stream_base::server, buffer_.data(), - std::bind( + beast::bind_front_handler( &ssl_http_session::on_handshake, - shared_from_this(), - std::placeholders::_1, - std::placeholders::_2)); + shared_from_this())); } void on_handshake( @@ -1000,10 +989,9 @@ public: // Perform the SSL shutdown stream_.async_shutdown( - std::bind( + beast::bind_front_handler( &ssl_http_session::on_shutdown, - shared_from_this(), - std::placeholders::_1)); + shared_from_this())); } void @@ -1052,11 +1040,9 @@ public: async_detect_ssl( stream_, buffer_, - std::bind( + beast::bind_front_handler( &detect_session::on_detect, - shared_from_this(), - std::placeholders::_1, - std::placeholders::_2)); + this->shared_from_this())); } void @@ -1150,11 +1136,9 @@ public: do_accept() { acceptor_.async_accept( - std::bind( + beast::bind_front_handler( &listener::on_accept, - shared_from_this(), - std::placeholders::_1, - std::placeholders::_2)); + shared_from_this())); } void diff --git a/example/advanced/server/advanced_server.cpp b/example/advanced/server/advanced_server.cpp index a417095e..39ac3733 100644 --- a/example/advanced/server/advanced_server.cpp +++ b/example/advanced/server/advanced_server.cpp @@ -243,11 +243,9 @@ public: // Set the control callback. This will be called // on every incoming ping, pong, and close frame. ws_.control_callback( - std::bind( + beast::bind_front_handler( &websocket_session::on_control_callback, - this, - std::placeholders::_1, - std::placeholders::_2)); + this)); // Run the timer. The timer is operated // continuously, this simplifies the code. @@ -259,10 +257,9 @@ public: // Accept the websocket handshake ws_.async_accept( req, - std::bind( + beast::bind_front_handler( &websocket_session::on_accept, - shared_from_this(), - std::placeholders::_1)); + shared_from_this())); } void @@ -301,10 +298,9 @@ public: // Now send the ping ws_.async_ping({}, - std::bind( + beast::bind_front_handler( &websocket_session::on_ping, - shared_from_this(), - std::placeholders::_1)); + shared_from_this())); } else { @@ -324,10 +320,9 @@ public: timer_.async_wait( net::bind_executor( ws_.get_executor(), // use the strand - std::bind( + beast::bind_front_handler( &websocket_session::on_timer, - shared_from_this(), - std::placeholders::_1))); + shared_from_this()))); } // Called to indicate activity from the remote peer @@ -383,11 +378,9 @@ public: // Read a message into our buffer ws_.async_read( buffer_, - std::bind( + beast::bind_front_handler( &websocket_session::on_read, - shared_from_this(), - std::placeholders::_1, - std::placeholders::_2)); + shared_from_this())); } void @@ -415,11 +408,9 @@ public: ws_.text(ws_.got_text()); ws_.async_write( buffer_.data(), - std::bind( + beast::bind_front_handler( &websocket_session::on_write, - shared_from_this(), - std::placeholders::_1, - std::placeholders::_2)); + shared_from_this())); } void @@ -520,10 +511,9 @@ class http_session : public std::enable_shared_from_this http::async_write( self_.stream_, msg_, - std::bind( + beast::bind_front_handler( &http_session::on_write, self_.shared_from_this(), - std::placeholders::_1, msg_.need_eof())); } }; @@ -584,15 +574,16 @@ public: // Read a request http::async_read(stream_, buffer_, req_, - std::bind( + beast::bind_front_handler( &http_session::on_read, - shared_from_this(), - std::placeholders::_1)); + shared_from_this())); } 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 if(ec == http::error::end_of_stream) return do_close(); @@ -618,8 +609,10 @@ public: } 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 if(ec == net::error::operation_aborted) return; @@ -720,11 +713,9 @@ public: do_accept() { acceptor_.async_accept( - std::bind( + beast::bind_front_handler( &listener::on_accept, - shared_from_this(), - std::placeholders::_1, - std::placeholders::_2)); + shared_from_this())); } void diff --git a/example/common/detect_ssl.hpp b/example/common/detect_ssl.hpp index 9e6c394c..46da82f8 100644 --- a/example/common/detect_ssl.hpp +++ b/example/common/detect_ssl.hpp @@ -287,8 +287,8 @@ async_detect_ssl( AsyncReadStream, DynamicBuffer, BOOST_ASIO_HANDLER_TYPE( - CompletionToken, void(beast::error_code, boost::tribool))>{ - stream, buffer, init.completion_handler}(beast::error_code{}, 0); + CompletionToken, void(beast::error_code, boost::tribool))>( + stream, buffer, std::move(init.completion_handler))(beast::error_code{}, 0); // This hook lets the caller see a return value when appropriate. // For example this might return std::future if diff --git a/example/common/session_alloc.hpp b/example/common/session_alloc.hpp index f6da45bc..4490c7cb 100644 --- a/example/common/session_alloc.hpp +++ b/example/common/session_alloc.hpp @@ -389,7 +389,7 @@ public: template void - operator()(Args&&... args) const + operator()(Args&&... args) { h_(std::forward(args)...); } diff --git a/example/http/client/async-ssl/http_client_async_ssl.cpp b/example/http/client/async-ssl/http_client_async_ssl.cpp index 27d66f35..a7fa08c3 100644 --- a/example/http/client/async-ssl/http_client_async_ssl.cpp +++ b/example/http/client/async-ssl/http_client_async_ssl.cpp @@ -89,11 +89,9 @@ public: resolver_.async_resolve( host, port, - std::bind( + beast::bind_front_handler( &session::on_resolve, - shared_from_this(), - std::placeholders::_1, - std::placeholders::_2)); + shared_from_this())); } void @@ -111,14 +109,13 @@ public: beast::async_connect( beast::get_lowest_layer(stream_), results, - std::bind( + beast::bind_front_handler( &session::on_connect, - shared_from_this(), - std::placeholders::_1)); + shared_from_this())); } void - on_connect(beast::error_code ec) + on_connect(beast::error_code ec, tcp::resolver::results_type::endpoint_type) { if(ec) return fail(ec, "connect"); @@ -126,10 +123,9 @@ public: // Perform the SSL handshake stream_.async_handshake( ssl::stream_base::client, - std::bind( + beast::bind_front_handler( &session::on_handshake, - shared_from_this(), - std::placeholders::_1)); + shared_from_this())); } void @@ -143,11 +139,9 @@ public: // Send the HTTP request to the remote host http::async_write(stream_, req_, - std::bind( + beast::bind_front_handler( &session::on_write, - shared_from_this(), - std::placeholders::_1, - std::placeholders::_2)); + shared_from_this())); } void @@ -162,11 +156,9 @@ public: // Receive the HTTP response http::async_read(stream_, buffer_, res_, - std::bind( + beast::bind_front_handler( &session::on_read, - shared_from_this(), - std::placeholders::_1, - std::placeholders::_2)); + shared_from_this())); } void @@ -187,10 +179,9 @@ public: // Gracefully close the stream stream_.async_shutdown( - std::bind( + beast::bind_front_handler( &session::on_shutdown, - shared_from_this(), - std::placeholders::_1)); + shared_from_this())); } void diff --git a/example/http/client/async/http_client_async.cpp b/example/http/client/async/http_client_async.cpp index 3bb5121b..cc1de0b1 100644 --- a/example/http/client/async/http_client_async.cpp +++ b/example/http/client/async/http_client_async.cpp @@ -76,11 +76,9 @@ public: resolver_.async_resolve( host, port, - std::bind( + beast::bind_front_handler( &session::on_resolve, - shared_from_this(), - std::placeholders::_1, - std::placeholders::_2)); + shared_from_this())); } void @@ -98,14 +96,13 @@ public: beast::async_connect( stream_, results, - std::bind( + beast::bind_front_handler( &session::on_connect, - shared_from_this(), - std::placeholders::_1)); + shared_from_this())); } void - on_connect(beast::error_code ec) + on_connect(beast::error_code ec, tcp::resolver::results_type::endpoint_type) { if(ec) return fail(ec, "connect"); @@ -115,11 +112,9 @@ public: // Send the HTTP request to the remote host http::async_write(stream_, req_, - std::bind( + beast::bind_front_handler( &session::on_write, - shared_from_this(), - std::placeholders::_1, - std::placeholders::_2)); + shared_from_this())); } void @@ -134,11 +129,9 @@ public: // Receive the HTTP response http::async_read(stream_, buffer_, res_, - std::bind( + beast::bind_front_handler( &session::on_read, - shared_from_this(), - std::placeholders::_1, - std::placeholders::_2)); + shared_from_this())); } void diff --git a/example/http/client/crawl/http_crawl.cpp b/example/http/client/crawl/http_crawl.cpp index e67d915e..d964a3be 100644 --- a/example/http/client/crawl/http_crawl.cpp +++ b/example/http/client/crawl/http_crawl.cpp @@ -217,10 +217,9 @@ public: timer_.async_wait( net::bind_executor( strand_, - std::bind( + beast::bind_front_handler( &worker::on_timer, - shared_from_this(), - std::placeholders::_1))); + shared_from_this()))); } void @@ -249,11 +248,9 @@ public: "http", net::bind_executor( strand_, - std::bind( + beast::bind_front_handler( &worker::on_resolve, - shared_from_this(), - std::placeholders::_1, - std::placeholders::_2))); + shared_from_this()))); } void @@ -277,18 +274,16 @@ public: // Make the connection on the IP address we get from a lookup net::async_connect( socket_, - results.begin(), - results.end(), + results, net::bind_executor( strand_, - std::bind( + beast::bind_front_handler( &worker::on_connect, - shared_from_this(), - std::placeholders::_1))); + shared_from_this()))); } void - on_connect(beast::error_code ec) + on_connect(beast::error_code ec, tcp::resolver::results_type::endpoint_type) { if(ec) { @@ -309,11 +304,9 @@ public: req_, net::bind_executor( strand_, - std::bind( + beast::bind_front_handler( &worker::on_write, - shared_from_this(), - std::placeholders::_1, - std::placeholders::_2))); + shared_from_this()))); } void @@ -344,11 +337,9 @@ public: res_, net::bind_executor( strand_, - std::bind( + beast::bind_front_handler( &worker::on_read, - shared_from_this(), - std::placeholders::_1, - std::placeholders::_2))); + shared_from_this()))); } void diff --git a/example/http/server/async-ssl/http_server_async_ssl.cpp b/example/http/server/async-ssl/http_server_async_ssl.cpp index 678a0486..0115f0d5 100644 --- a/example/http/server/async-ssl/http_server_async_ssl.cpp +++ b/example/http/server/async-ssl/http_server_async_ssl.cpp @@ -248,11 +248,9 @@ class session : public std::enable_shared_from_this http::async_write( self_.stream_, *sp, - std::bind( + beast::bind_front_handler( &session::on_write, self_.shared_from_this(), - std::placeholders::_1, - std::placeholders::_2, sp->need_eof())); } }; @@ -287,10 +285,9 @@ public: // Perform the SSL handshake stream_.async_handshake( ssl::stream_base::server, - std::bind( + beast::bind_front_handler( &session::on_handshake, - shared_from_this(), - std::placeholders::_1)); + shared_from_this())); } void @@ -314,11 +311,9 @@ public: // Read a request http::async_read(stream_, buffer_, req_, - std::bind( + beast::bind_front_handler( &session::on_read, - shared_from_this(), - std::placeholders::_1, - std::placeholders::_2)); + shared_from_this())); } void @@ -341,9 +336,9 @@ public: void on_write( + bool close, beast::error_code ec, - std::size_t bytes_transferred, - bool close) + std::size_t bytes_transferred) { boost::ignore_unused(bytes_transferred); @@ -372,10 +367,9 @@ public: // Perform the SSL shutdown stream_.async_shutdown( - std::bind( + beast::bind_front_handler( &session::on_shutdown, - shared_from_this(), - std::placeholders::_1)); + shared_from_this())); } void @@ -456,11 +450,9 @@ public: do_accept() { acceptor_.async_accept( - std::bind( + beast::bind_front_handler( &listener::on_accept, - shared_from_this(), - std::placeholders::_1, - std::placeholders::_2)); + shared_from_this())); } void diff --git a/example/http/server/async/http_server_async.cpp b/example/http/server/async/http_server_async.cpp index db061b2a..5839dd7c 100644 --- a/example/http/server/async/http_server_async.cpp +++ b/example/http/server/async/http_server_async.cpp @@ -244,11 +244,9 @@ class session : public std::enable_shared_from_this http::async_write( self_.stream_, *sp, - std::bind( + beast::bind_front_handler( &session::on_write, self_.shared_from_this(), - std::placeholders::_1, - std::placeholders::_2, sp->need_eof())); } }; @@ -290,11 +288,9 @@ public: // Read a request http::async_read(stream_, buffer_, req_, - std::bind( + beast::bind_front_handler( &session::on_read, - shared_from_this(), - std::placeholders::_1, - std::placeholders::_2)); + shared_from_this())); } void @@ -317,9 +313,9 @@ public: void on_write( + bool close, beast::error_code ec, - std::size_t bytes_transferred, - bool close) + std::size_t bytes_transferred) { boost::ignore_unused(bytes_transferred); @@ -416,11 +412,9 @@ public: do_accept() { acceptor_.async_accept( - std::bind( + beast::bind_front_handler( &listener::on_accept, - shared_from_this(), - std::placeholders::_1, - std::placeholders::_2)); + shared_from_this())); } void diff --git a/example/http/server/flex/http_server_flex.cpp b/example/http/server/flex/http_server_flex.cpp index 4e87fdcb..01d29503 100644 --- a/example/http/server/flex/http_server_flex.cpp +++ b/example/http/server/flex/http_server_flex.cpp @@ -266,11 +266,9 @@ class session http::async_write( self_.derived().stream(), *sp, - std::bind( + beast::bind_front_handler( &session::on_write, self_.derived().shared_from_this(), - std::placeholders::_1, - std::placeholders::_2, sp->need_eof())); } }; @@ -306,11 +304,9 @@ public: derived().stream(), buffer_, req_, - std::bind( + beast::bind_front_handler( &session::on_read, - derived().shared_from_this(), - std::placeholders::_1, - std::placeholders::_2)); + derived().shared_from_this())); } void @@ -333,9 +329,9 @@ public: void on_write( + bool close, beast::error_code ec, - std::size_t bytes_transferred, - bool close) + std::size_t bytes_transferred) { boost::ignore_unused(bytes_transferred); @@ -442,11 +438,9 @@ public: stream_.async_handshake( ssl::stream_base::server, buffer_.data(), - std::bind( + beast::bind_front_handler( &ssl_session::on_handshake, - shared_from_this(), - std::placeholders::_1, - std::placeholders::_2)); + shared_from_this())); } void on_handshake( @@ -470,10 +464,9 @@ public: // Perform the SSL shutdown stream_.async_shutdown( - std::bind( + beast::bind_front_handler( &ssl_session::on_shutdown, - shared_from_this(), - std::placeholders::_1)); + shared_from_this())); } void @@ -518,11 +511,9 @@ public: async_detect_ssl( stream_, buffer_, - std::bind( + beast::bind_front_handler( &detect_session::on_detect, - shared_from_this(), - std::placeholders::_1, - std::placeholders::_2)); + shared_from_this())); } void @@ -616,11 +607,9 @@ public: do_accept() { acceptor_.async_accept( - std::bind( + beast::bind_front_handler( &listener::on_accept, - shared_from_this(), - std::placeholders::_1, - std::placeholders::_2)); + shared_from_this())); } void diff --git a/example/http/server/stackless/http_server_stackless.cpp b/example/http/server/stackless/http_server_stackless.cpp index 6678803c..1161933c 100644 --- a/example/http/server/stackless/http_server_stackless.cpp +++ b/example/http/server/stackless/http_server_stackless.cpp @@ -251,11 +251,9 @@ class session http::async_write( self_.stream_, *sp, - std::bind( + beast::bind_front_handler( &session::loop, self_.shared_from_this(), - std::placeholders::_1, - std::placeholders::_2, sp->need_eof())); } }; @@ -283,15 +281,15 @@ public: void run() { - loop({}, 0, false); + loop(false, {}, 0); } #include void loop( + bool close, beast::error_code ec, - std::size_t bytes_transferred, - bool close) + std::size_t bytes_transferred) { boost::ignore_unused(bytes_transferred); reenter(*this) @@ -307,11 +305,9 @@ public: // Read a request yield http::async_read(stream_, buffer_, req_, - std::bind( + beast::bind_front_handler( &session::loop, shared_from_this(), - std::placeholders::_1, - std::placeholders::_2, false)); if(ec == http::error::end_of_stream) { @@ -419,10 +415,9 @@ public: { yield acceptor_.async_accept( socket_, - std::bind( + beast::bind_front_handler( &listener::loop, - shared_from_this(), - std::placeholders::_1)); + shared_from_this())); if(ec) { fail(ec, "accept"); diff --git a/example/websocket/client/async-ssl/websocket_client_async_ssl.cpp b/example/websocket/client/async-ssl/websocket_client_async_ssl.cpp index dc387f82..ab812d68 100644 --- a/example/websocket/client/async-ssl/websocket_client_async_ssl.cpp +++ b/example/websocket/client/async-ssl/websocket_client_async_ssl.cpp @@ -75,11 +75,9 @@ public: resolver_.async_resolve( host, port, - std::bind( + beast::bind_front_handler( &session::on_resolve, - shared_from_this(), - std::placeholders::_1, - std::placeholders::_2)); + shared_from_this())); } void @@ -97,14 +95,13 @@ public: beast::async_connect( beast::get_lowest_layer(ws_), results, - std::bind( + beast::bind_front_handler( &session::on_connect, - shared_from_this(), - std::placeholders::_1)); + shared_from_this())); } void - on_connect(beast::error_code ec) + on_connect(beast::error_code ec, tcp::resolver::results_type::endpoint_type) { if(ec) return fail(ec, "connect"); @@ -115,10 +112,9 @@ public: // Perform the SSL handshake ws_.next_layer().async_handshake( ssl::stream_base::client, - std::bind( + beast::bind_front_handler( &session::on_ssl_handshake, - shared_from_this(), - std::placeholders::_1)); + shared_from_this())); } void @@ -129,10 +125,9 @@ public: // Perform the websocket handshake ws_.async_handshake(host_, "/", - std::bind( + beast::bind_front_handler( &session::on_handshake, - shared_from_this(), - std::placeholders::_1)); + shared_from_this())); } void @@ -144,11 +139,9 @@ public: // Send the message ws_.async_write( net::buffer(text_), - std::bind( + beast::bind_front_handler( &session::on_write, - shared_from_this(), - std::placeholders::_1, - std::placeholders::_2)); + shared_from_this())); } void @@ -164,11 +157,9 @@ public: // Read a message into our buffer ws_.async_read( buffer_, - std::bind( + beast::bind_front_handler( &session::on_read, - shared_from_this(), - std::placeholders::_1, - std::placeholders::_2)); + shared_from_this())); } void @@ -183,10 +174,9 @@ public: // Close the WebSocket connection ws_.async_close(websocket::close_code::normal, - std::bind( + beast::bind_front_handler( &session::on_close, - shared_from_this(), - std::placeholders::_1)); + shared_from_this())); } void diff --git a/example/websocket/client/async/websocket_client_async.cpp b/example/websocket/client/async/websocket_client_async.cpp index f0ef2018..05040014 100644 --- a/example/websocket/client/async/websocket_client_async.cpp +++ b/example/websocket/client/async/websocket_client_async.cpp @@ -70,11 +70,9 @@ public: resolver_.async_resolve( host, port, - std::bind( + beast::bind_front_handler( &session::on_resolve, - shared_from_this(), - std::placeholders::_1, - std::placeholders::_2)); + shared_from_this())); } void @@ -92,24 +90,22 @@ public: beast::async_connect( beast::get_lowest_layer(ws_), results, - std::bind( + beast::bind_front_handler( &session::on_connect, - shared_from_this(), - std::placeholders::_1)); + shared_from_this())); } void - on_connect(beast::error_code ec) + on_connect(beast::error_code ec, tcp::resolver::results_type::endpoint_type) { if(ec) return fail(ec, "connect"); // Perform the websocket handshake ws_.async_handshake(host_, "/", - std::bind( + beast::bind_front_handler( &session::on_handshake, - shared_from_this(), - std::placeholders::_1)); + shared_from_this())); } void @@ -121,11 +117,9 @@ public: // Send the message ws_.async_write( net::buffer(text_), - std::bind( + beast::bind_front_handler( &session::on_write, - shared_from_this(), - std::placeholders::_1, - std::placeholders::_2)); + shared_from_this())); } void @@ -141,11 +135,9 @@ public: // Read a message into our buffer ws_.async_read( buffer_, - std::bind( + beast::bind_front_handler( &session::on_read, - shared_from_this(), - std::placeholders::_1, - std::placeholders::_2)); + shared_from_this())); } void @@ -160,10 +152,9 @@ public: // Close the WebSocket connection ws_.async_close(websocket::close_code::normal, - std::bind( + beast::bind_front_handler( &session::on_close, - shared_from_this(), - std::placeholders::_1)); + shared_from_this())); } void diff --git a/example/websocket/server/async-ssl/websocket_server_async_ssl.cpp b/example/websocket/server/async-ssl/websocket_server_async_ssl.cpp index a26eb023..f3b6004c 100644 --- a/example/websocket/server/async-ssl/websocket_server_async_ssl.cpp +++ b/example/websocket/server/async-ssl/websocket_server_async_ssl.cpp @@ -69,10 +69,9 @@ public: // Perform the SSL handshake ws_.next_layer().async_handshake( ssl::stream_base::server, - std::bind( + beast::bind_front_handler( &session::on_handshake, - shared_from_this(), - std::placeholders::_1)); + shared_from_this())); } void @@ -83,10 +82,9 @@ public: // Accept the websocket handshake ws_.async_accept( - std::bind( + beast::bind_front_handler( &session::on_accept, - shared_from_this(), - std::placeholders::_1)); + shared_from_this())); } void @@ -105,11 +103,9 @@ public: // Read a message into our buffer ws_.async_read( buffer_, - std::bind( + beast::bind_front_handler( &session::on_read, - shared_from_this(), - std::placeholders::_1, - std::placeholders::_2)); + shared_from_this())); } void @@ -130,11 +126,9 @@ public: ws_.text(ws_.got_text()); ws_.async_write( buffer_.data(), - std::bind( + beast::bind_front_handler( &session::on_write, - shared_from_this(), - std::placeholders::_1, - std::placeholders::_2)); + shared_from_this())); } void @@ -220,11 +214,9 @@ public: do_accept() { acceptor_.async_accept( - std::bind( + beast::bind_front_handler( &listener::on_accept, - shared_from_this(), - std::placeholders::_1, - std::placeholders::_2)); + shared_from_this())); } void diff --git a/example/websocket/server/async/websocket_server_async.cpp b/example/websocket/server/async/websocket_server_async.cpp index d2d1426e..7e4c0281 100644 --- a/example/websocket/server/async/websocket_server_async.cpp +++ b/example/websocket/server/async/websocket_server_async.cpp @@ -60,10 +60,9 @@ public: { // Accept the websocket handshake ws_.async_accept( - std::bind( + beast::bind_front_handler( &session::on_accept, - shared_from_this(), - std::placeholders::_1)); + shared_from_this())); } void @@ -82,11 +81,9 @@ public: // Read a message into our buffer ws_.async_read( buffer_, - std::bind( + beast::bind_front_handler( &session::on_read, - shared_from_this(), - std::placeholders::_1, - std::placeholders::_2)); + shared_from_this())); } void @@ -107,11 +104,9 @@ public: ws_.text(ws_.got_text()); ws_.async_write( buffer_.data(), - std::bind( + beast::bind_front_handler( &session::on_write, - shared_from_this(), - std::placeholders::_1, - std::placeholders::_2)); + shared_from_this())); } void @@ -194,11 +189,9 @@ public: do_accept() { acceptor_.async_accept( - std::bind( + beast::bind_front_handler( &listener::on_accept, - shared_from_this(), - std::placeholders::_1, - std::placeholders::_2)); + shared_from_this())); } void diff --git a/example/websocket/server/chat-multi/http_session.cpp b/example/websocket/server/chat-multi/http_session.cpp index 7e4cc814..5264cc06 100644 --- a/example/websocket/server/chat-multi/http_session.cpp +++ b/example/websocket/server/chat-multi/http_session.cpp @@ -206,11 +206,9 @@ run() // Read a request http::async_read(stream_, buffer_, req_, - std::bind( + beast::bind_front_handler( &http_session::on_read, - shared_from_this(), - std::placeholders::_1, - std::placeholders::_2)); + shared_from_this())); } // Report a failure @@ -348,9 +346,7 @@ on_write(beast::error_code ec, std::size_t, bool close) // Read another request http::async_read(stream_, buffer_, req_, - std::bind( + beast::bind_front_handler( &http_session::on_read, - shared_from_this(), - std::placeholders::_1, - std::placeholders::_2)); + shared_from_this())); } diff --git a/example/websocket/server/chat-multi/listener.cpp b/example/websocket/server/chat-multi/listener.cpp index 7ba8087b..9c618714 100644 --- a/example/websocket/server/chat-multi/listener.cpp +++ b/example/websocket/server/chat-multi/listener.cpp @@ -61,11 +61,9 @@ run() { // Start accepting a connection acceptor_.async_accept( - std::bind( + beast::bind_front_handler( &listener::on_accept, - shared_from_this(), - std::placeholders::_1, - std::placeholders::_2)); + shared_from_this())); } // Report a failure @@ -94,9 +92,7 @@ on_accept(beast::error_code ec, tcp::socket socket) // Accept another connection acceptor_.async_accept( - std::bind( + beast::bind_front_handler( &listener::on_accept, - shared_from_this(), - std::placeholders::_1, - std::placeholders::_2)); + shared_from_this())); } diff --git a/example/websocket/server/chat-multi/websocket_session.cpp b/example/websocket/server/chat-multi/websocket_session.cpp index dbb4a121..70af3eba 100644 --- a/example/websocket/server/chat-multi/websocket_session.cpp +++ b/example/websocket/server/chat-multi/websocket_session.cpp @@ -52,11 +52,9 @@ on_accept(beast::error_code ec) // Read a message ws_.async_read( buffer_, - std::bind( + beast::bind_front_handler( &websocket_session::on_read, - shared_from_this(), - std::placeholders::_1, - std::placeholders::_2)); + shared_from_this())); } void @@ -76,11 +74,9 @@ on_read(beast::error_code ec, std::size_t) // Read another message ws_.async_read( buffer_, - std::bind( + beast::bind_front_handler( &websocket_session::on_read, - shared_from_this(), - std::placeholders::_1, - std::placeholders::_2)); + shared_from_this())); } void @@ -93,7 +89,7 @@ send(boost::shared_ptr const& ss) if(! ws_.get_executor().running_in_this_thread()) return net::post( ws_.get_executor(), - std::bind( + beast::bind_front_handler( &websocket_session::send, shared_from_this(), ss)); @@ -108,11 +104,9 @@ send(boost::shared_ptr const& ss) // We are not currently writing, so send this immediately ws_.async_write( net::buffer(*queue_.front()), - std::bind( + beast::bind_front_handler( &websocket_session::on_write, - shared_from_this(), - std::placeholders::_1, - std::placeholders::_2)); + shared_from_this())); } void @@ -130,9 +124,7 @@ on_write(beast::error_code ec, std::size_t) if(! queue_.empty()) ws_.async_write( net::buffer(*queue_.front()), - std::bind( + beast::bind_front_handler( &websocket_session::on_write, - shared_from_this(), - std::placeholders::_1, - std::placeholders::_2)); + shared_from_this())); } diff --git a/example/websocket/server/chat-multi/websocket_session.hpp b/example/websocket/server/chat-multi/websocket_session.hpp index aa54a3c0..f10e35c0 100644 --- a/example/websocket/server/chat-multi/websocket_session.hpp +++ b/example/websocket/server/chat-multi/websocket_session.hpp @@ -60,10 +60,9 @@ run(http::request> req) // Accept the websocket handshake ws_.async_accept( req, - std::bind( + beast::bind_front_handler( &websocket_session::on_accept, - shared_from_this(), - std::placeholders::_1)); + shared_from_this())); } #endif diff --git a/example/websocket/server/fast/websocket_server_fast.cpp b/example/websocket/server/fast/websocket_server_fast.cpp index 02ee2d1c..1414d969 100644 --- a/example/websocket/server/fast/websocket_server_fast.cpp +++ b/example/websocket/server/fast/websocket_server_fast.cpp @@ -131,9 +131,9 @@ do_sync_listen( if(ec) return fail(ec, "accept"); - std::thread{std::bind( + std::thread(std::bind( &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, "Boost.Beast/" + std::to_string(BOOST_BEAST_VERSION) + "-Async"); }, - std::bind( + beast::bind_front_handler( &async_session::on_accept, - shared_from_this(), - std::placeholders::_1)); + shared_from_this())); } void @@ -187,11 +186,9 @@ public: // Read a message into our buffer ws_.async_read( buffer_, - std::bind( + beast::bind_front_handler( &async_session::on_read, - shared_from_this(), - std::placeholders::_1, - std::placeholders::_2)); + shared_from_this())); } void @@ -212,11 +209,9 @@ public: ws_.text(ws_.got_text()); ws_.async_write( buffer_.data(), - std::bind( + beast::bind_front_handler( &async_session::on_write, - shared_from_this(), - std::placeholders::_1, - std::placeholders::_2)); + shared_from_this())); } void @@ -297,11 +292,9 @@ public: do_accept() { acceptor_.async_accept( - std::bind( + beast::bind_front_handler( &async_listener::on_accept, - shared_from_this(), - std::placeholders::_1, - std::placeholders::_2)); + shared_from_this())); } void @@ -428,7 +421,7 @@ int main(int argc, char* argv[]) net::io_context ioc{threads}; // Create sync port - std::thread(std::bind( + std::thread(beast::bind_front_handler( &do_sync_listen, std::ref(ioc), tcp::endpoint{ diff --git a/test/beast/core/basic_stream.cpp b/test/beast/core/basic_stream.cpp index 6189a71d..317210ca 100644 --- a/test/beast/core/basic_stream.cpp +++ b/test/beast/core/basic_stream.cpp @@ -261,19 +261,16 @@ private: if(s_.empty()) socket_.async_wait( net::socket_base::wait_read, - std::bind( + bind_front_handler( &session::on_read, - shared_from_this(), - std::placeholders::_1)); + shared_from_this())); else net::async_write( socket_, net::const_buffer(s_.data(), s_.size()), - std::bind( + bind_front_handler( &session::on_write, - shared_from_this(), - std::placeholders::_1, - std::placeholders::_2)); + shared_from_this())); } protected: diff --git a/test/beast/core/buffered_read_stream.cpp b/test/beast/core/buffered_read_stream.cpp index d08413ae..13d447a4 100644 --- a/test/beast/core/buffered_read_stream.cpp +++ b/test/beast/core/buffered_read_stream.cpp @@ -12,8 +12,9 @@ #include #include -#include #include +#include +#include #include #include #include @@ -104,11 +105,9 @@ public: brs_->buffer().prepare(5), net::buffer("Hello", 5))); net::async_read(*brs_, net::buffer(&s_[0], s_.size()), - std::bind( + bind_front_handler( &loop::on_read, - shared_from_this(), - std::placeholders::_1, - std::placeholders::_2)); + shared_from_this())); } }; diff --git a/test/beast/websocket/test.hpp b/test/beast/websocket/test.hpp index 33e474a8..185990e1 100644 --- a/test/beast/websocket/test.hpp +++ b/test/beast/websocket/test.hpp @@ -10,6 +10,7 @@ #ifndef BEAST_TEST_WEBSOCKET_TEST_HPP #define BEAST_TEST_WEBSOCKET_TEST_HPP +#include #include #include #include @@ -131,10 +132,9 @@ public: async_handshake() { ws_.async_handshake("localhost", "/", - std::bind( + bind_front_handler( &echo_server::on_handshake, - this, - std::placeholders::_1)); + this)); } void @@ -146,10 +146,9 @@ public: if(ws_.is_open()) { ws_.async_close({}, - std::bind( + bind_front_handler( &echo_server::on_close, - this, - std::placeholders::_1)); + this)); } else { @@ -192,10 +191,10 @@ public: void do_accept() { - ws_.async_accept(std::bind( - &echo_server::on_accept, - this, - std::placeholders::_1)); + ws_.async_accept( + bind_front_handler( + &echo_server::on_accept, + this)); } void @@ -216,10 +215,9 @@ public: if(close_) { return ws_.async_close({}, - std::bind( + bind_front_handler( &echo_server::on_close, - this, - std::placeholders::_1)); + this)); } do_read(); @@ -229,27 +227,25 @@ public: do_read() { ws_.async_read(buffer_, - std::bind( + beast::bind_front_handler( &echo_server::on_read, - this, - std::placeholders::_1)); + this)); } void - on_read(error_code ec) + on_read(error_code ec, std::size_t) { if(ec) return fail(ec); ws_.text(ws_.got_text()); ws_.async_write(buffer_.data(), - std::bind( + beast::bind_front_handler( &echo_server::on_write, - this, - std::placeholders::_1)); + this)); } void - on_write(error_code ec) + on_write(error_code ec, std::size_t) { if(ec) return fail(ec); diff --git a/test/bench/wsload/wsload.cpp b/test/bench/wsload/wsload.cpp index e4ef8236..e15c1520 100644 --- a/test/bench/wsload/wsload.cpp +++ b/test/bench/wsload/wsload.cpp @@ -153,10 +153,9 @@ public: run() { ws_.next_layer().async_connect(ep_, - alloc_.wrap(std::bind( + alloc_.wrap(beast::bind_front_handler( &connection::on_connect, - shared_from_this(), - std::placeholders::_1))); + shared_from_this()))); } private: @@ -169,10 +168,9 @@ private: ws_.async_handshake( ep_.address().to_string() + ":" + std::to_string(ep_.port()), "/", - alloc_.wrap(std::bind( + alloc_.wrap(beast::bind_front_handler( &connection::on_handshake, - shared_from_this(), - std::placeholders::_1))); + shared_from_this()))); } void @@ -191,14 +189,13 @@ private: double(4) / beast::buffer_size(tb_)}; ws_.async_write_some(true, beast::buffers_prefix(dist(rng_), tb_), - alloc_.wrap(std::bind( + alloc_.wrap(beast::bind_front_handler( &connection::on_write, - shared_from_this(), - std::placeholders::_1))); + shared_from_this()))); } void - on_write(beast::error_code ec) + on_write(beast::error_code ec, std::size_t) { if(ec) return fail(ec, "write"); @@ -207,24 +204,22 @@ private: return do_read(); ws_.async_close({}, - alloc_.wrap(std::bind( + alloc_.wrap(beast::bind_front_handler( &connection::on_close, - shared_from_this(), - std::placeholders::_1))); + shared_from_this()))); } void do_read() { ws_.async_read(buffer_, - alloc_.wrap(std::bind( + alloc_.wrap(beast::bind_front_handler( &connection::on_read, - shared_from_this(), - std::placeholders::_1))); + shared_from_this()))); } void - on_read(beast::error_code ec) + on_read(beast::error_code ec, std::size_t) { if(ec) return fail(ec, "read"); diff --git a/test/extras/include/boost/beast/test/websocket.hpp b/test/extras/include/boost/beast/test/websocket.hpp index 07d64cfc..38cca050 100644 --- a/test/extras/include/boost/beast/test/websocket.hpp +++ b/test/extras/include/boost/beast/test/websocket.hpp @@ -25,6 +25,7 @@ namespace boost { namespace beast { namespace test { +// DEPRECATED class ws_echo_server { std::ostream& log_;