Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-09-17 08:38:23

0001 //
0002 // Copyright (c) 2023-2025 Ivica Siladic, Bruno Iljazovic, Korina Simicevic
0003 //
0004 // Distributed under the Boost Software License, Version 1.0.
0005 // (See accompanying file LICENSE or copy at http://www.boost.org/LICENSE_1_0.txt)
0006 //
0007 
0008 #ifndef BOOST_MQTT5_WEBSOCKET_HPP
0009 #define BOOST_MQTT5_WEBSOCKET_HPP
0010 
0011 #include <boost/mqtt5/detail/async_traits.hpp>
0012 #include <boost/mqtt5/detail/shutdown.hpp>
0013 
0014 #include <boost/mqtt5/types.hpp>
0015 
0016 #include <boost/beast/http/field.hpp>
0017 #include <boost/beast/websocket/rfc6455.hpp>
0018 #include <boost/beast/websocket/stream.hpp>
0019 
0020 namespace boost::mqtt5 {
0021 
0022 // Trait definition for Beast
0023 template <typename Stream>
0024 struct ws_handshake_traits<boost::beast::websocket::stream<Stream>> {
0025 
0026     template <typename CompletionToken>
0027     static decltype(auto) async_handshake(
0028         boost::beast::websocket::stream<Stream>& stream,
0029         authority_path ap, CompletionToken&& token
0030     ) {
0031         using namespace boost::beast;
0032 
0033         // Set suggested timeout settings for the websocket
0034         stream.set_option(
0035             websocket::stream_base::timeout::suggested(role_type::client)
0036         );
0037 
0038         stream.binary(true);
0039 
0040         // Set a decorator to change the User-Agent of the handshake
0041         stream.set_option(websocket::stream_base::decorator(
0042             [](websocket::request_type& req) {
0043                 req.set(http::field::sec_websocket_protocol, "mqtt");
0044                 req.set(http::field::user_agent, "boost.mqtt");
0045             })
0046         );
0047 
0048         stream.async_handshake(
0049             ap.host + ':' + ap.port, ap.path,
0050             std::forward<CompletionToken>(token)
0051         );
0052     }
0053 };
0054 
0055 namespace detail {
0056 
0057 // in namespace boost::mqtt5::detail to enable ADL
0058 template <typename Stream, typename ShutdownHandler>
0059 void async_shutdown(
0060     boost::beast::websocket::stream<Stream>& stream, ShutdownHandler&& handler
0061 ) {
0062     stream.async_close(
0063         beast::websocket::close_code::normal,
0064         std::move(handler)
0065     );
0066 }
0067 
0068 } // end namespace detail
0069 
0070 } // end namespace boost::mqtt5
0071 
0072 #endif // !BOOST_MQTT5_WEBSOCKET_HPP