]> git.proxmox.com Git - ceph.git/blobdiff - ceph/src/boost/libs/beast/example/websocket/client/async/websocket_client_async.cpp
import new upstream nautilus stable release 14.2.8
[ceph.git] / ceph / src / boost / libs / beast / example / websocket / client / async / websocket_client_async.cpp
index 98505dd11d275116b1940ad79b8842520af703bb..15b127d333ee7c2f23f4455247bdff3ab8093846 100644 (file)
@@ -1,5 +1,5 @@
 //
-// Copyright (c) 2016-2017 Vinnie Falco (vinnie dot falco at gmail dot com)
+// Copyright (c) 2016-2019 Vinnie Falco (vinnie dot falco at gmail dot com)
 //
 // Distributed under the Boost Software License, Version 1.0. (See accompanying
 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
 
 #include <boost/beast/core.hpp>
 #include <boost/beast/websocket.hpp>
-#include <boost/asio/connect.hpp>
-#include <boost/asio/ip/tcp.hpp>
+#include <boost/asio/strand.hpp>
 #include <cstdlib>
 #include <functional>
 #include <iostream>
 #include <memory>
 #include <string>
 
-using tcp = boost::asio::ip::tcp;               // from <boost/asio/ip/tcp.hpp>
-namespace websocket = boost::beast::websocket;  // from <boost/beast/websocket.hpp>
+namespace beast = boost::beast;         // from <boost/beast.hpp>
+namespace http = beast::http;           // from <boost/beast/http.hpp>
+namespace websocket = beast::websocket; // from <boost/beast/websocket.hpp>
+namespace net = boost::asio;            // from <boost/asio.hpp>
+using tcp = boost::asio::ip::tcp;       // from <boost/asio/ip/tcp.hpp>
 
 //------------------------------------------------------------------------------
 
 // Report a failure
 void
-fail(boost::system::error_code ec, char const* what)
+fail(beast::error_code ec, char const* what)
 {
     std::cerr << what << ": " << ec.message() << "\n";
 }
@@ -39,17 +41,17 @@ fail(boost::system::error_code ec, char const* what)
 class session : public std::enable_shared_from_this<session>
 {
     tcp::resolver resolver_;
-    websocket::stream<tcp::socket> ws_;
-    boost::beast::multi_buffer buffer_;
+    websocket::stream<beast::tcp_stream> ws_;
+    beast::flat_buffer buffer_;
     std::string host_;
     std::string text_;
 
 public:
     // Resolver and socket require an io_context
     explicit
-    session(boost::asio::io_context& ioc)
-        : resolver_(ioc)
-        , ws_(ioc)
+    session(net::io_context& ioc)
+        : resolver_(net::make_strand(ioc))
+        , ws_(net::make_strand(ioc))
     {
     }
 
@@ -68,65 +70,78 @@ 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
     on_resolve(
-        boost::system::error_code ec,
+        beast::error_code ec,
         tcp::resolver::results_type results)
     {
         if(ec)
             return fail(ec, "resolve");
 
+        // Set the timeout for the operation
+        beast::get_lowest_layer(ws_).expires_after(std::chrono::seconds(30));
+
         // Make the connection on the IP address we get from a lookup
-        boost::asio::async_connect(
-            ws_.next_layer(),
-            results.begin(),
-            results.end(),
-            std::bind(
+        beast::get_lowest_layer(ws_).async_connect(
+            results,
+            beast::bind_front_handler(
                 &session::on_connect,
-                shared_from_this(),
-                std::placeholders::_1));
+                shared_from_this()));
     }
 
     void
-    on_connect(boost::system::error_code ec)
+    on_connect(beast::error_code ec, tcp::resolver::results_type::endpoint_type)
     {
         if(ec)
             return fail(ec, "connect");
 
+        // Turn off the timeout on the tcp_stream, because
+        // the websocket stream has its own timeout system.
+        beast::get_lowest_layer(ws_).expires_never();
+
+        // Set suggested timeout settings for the websocket
+        ws_.set_option(
+            websocket::stream_base::timeout::suggested(
+                beast::role_type::client));
+
+        // Set a decorator to change the User-Agent of the handshake
+        ws_.set_option(websocket::stream_base::decorator(
+            [](websocket::request_type& req)
+            {
+                req.set(http::field::user_agent,
+                    std::string(BOOST_BEAST_VERSION_STRING) +
+                        " websocket-client-async");
+            }));
+
         // 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
-    on_handshake(boost::system::error_code ec)
+    on_handshake(beast::error_code ec)
     {
         if(ec)
             return fail(ec, "handshake");
         
         // Send the message
         ws_.async_write(
-            boost::asio::buffer(text_),
-            std::bind(
+            net::buffer(text_),
+            beast::bind_front_handler(
                 &session::on_write,
-                shared_from_this(),
-                std::placeholders::_1,
-                std::placeholders::_2));
+                shared_from_this()));
     }
 
     void
     on_write(
-        boost::system::error_code ec,
+        beast::error_code ec,
         std::size_t bytes_transferred)
     {
         boost::ignore_unused(bytes_transferred);
@@ -137,16 +152,14 @@ 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
     on_read(
-        boost::system::error_code ec,
+        beast::error_code ec,
         std::size_t bytes_transferred)
     {
         boost::ignore_unused(bytes_transferred);
@@ -156,22 +169,21 @@ 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
-    on_close(boost::system::error_code ec)
+    on_close(beast::error_code ec)
     {
         if(ec)
             return fail(ec, "close");
 
         // If we get here then the connection is closed gracefully
 
-        // The buffers() function helps print a ConstBufferSequence
-        std::cout << boost::beast::buffers(buffer_.data()) << std::endl;
+        // The make_printable() function helps print a ConstBufferSequence
+        std::cout << beast::make_printable(buffer_.data()) << std::endl;
     }
 };
 
@@ -193,7 +205,7 @@ int main(int argc, char** argv)
     auto const text = argv[3];
 
     // The io_context is required for all I/O
-    boost::asio::io_context ioc;
+    net::io_context ioc;
 
     // Launch the asynchronous operation
     std::make_shared<session>(ioc)->run(host, port, text);