|
| 1 | +// |
| 2 | +// Copyright (c) 2025 Vinnie Falco ([email protected]) |
| 3 | +// |
| 4 | +// Distributed under the Boost Software License, Version 1.0. (See accompanying |
| 5 | +// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
| 6 | +// |
| 7 | +// Official repository: https://github.com/cppalliance/ws_io |
| 8 | +// |
| 9 | + |
| 10 | +#ifndef BOOST_WS_IO_IMPL_CLIENT_HPP |
| 11 | +#define BOOST_WS_IO_IMPL_CLIENT_HPP |
| 12 | + |
| 13 | +#include <boost/asio/async_result.hpp> |
| 14 | +#include <boost/http_proto/response_view.hpp> |
| 15 | +#include <boost/asio/coroutine.hpp> |
| 16 | + |
| 17 | +namespace boost { |
| 18 | +namespace ws_io { |
| 19 | + |
| 20 | +//------------------------------------------------ |
| 21 | + |
| 22 | +template<class AsyncStream> |
| 23 | +template<class Handler> |
| 24 | +class client<AsyncStream>:: |
| 25 | + handshake_op |
| 26 | + : public asio::coroutine |
| 27 | +{ |
| 28 | + client<AsyncStream>& cs_; |
| 29 | + Handler h_; |
| 30 | + |
| 31 | +public: |
| 32 | + template<class Handler_> |
| 33 | + handshake_op( |
| 34 | + client<AsyncStream>& cs, |
| 35 | + Handler_&& h, |
| 36 | + core::string_view host, |
| 37 | + core::string_view target) |
| 38 | + : cs_(cs) |
| 39 | + , h_(std::forward<Handler_>(h)) |
| 40 | + { |
| 41 | + } |
| 42 | + |
| 43 | +}; |
| 44 | + |
| 45 | +//------------------------------------------------ |
| 46 | + |
| 47 | +template<class AsyncStream> |
| 48 | +struct client<AsyncStream>:: |
| 49 | + run_handshake_op |
| 50 | +{ |
| 51 | + client<AsyncStream>& self; |
| 52 | + |
| 53 | + using executor_type = typename |
| 54 | + client<AsyncStream>::executor_type; |
| 55 | + |
| 56 | + executor_type |
| 57 | + get_executor() const noexcept |
| 58 | + { |
| 59 | + return self.next_layer().get_executor(); |
| 60 | + } |
| 61 | + |
| 62 | + template<class HandshakeHandler> |
| 63 | + void operator()( |
| 64 | + HandshakeHandler&& h, |
| 65 | + core::string_view host, |
| 66 | + core::string_view target |
| 67 | + /*,request_type&& req |
| 68 | + ,detail::sec_ws_key_type key |
| 69 | + ,response_type* res_p*/ |
| 70 | + ) |
| 71 | + { |
| 72 | + handshake_op<typename std::decay< |
| 73 | + HandshakeHandler>::type>( |
| 74 | + self, |
| 75 | + std::forward<HandshakeHandler>(h), |
| 76 | + host, |
| 77 | + target); |
| 78 | + } |
| 79 | +}; |
| 80 | + |
| 81 | +//------------------------------------------------ |
| 82 | + |
| 83 | +template<class AsyncStream> |
| 84 | +client<AsyncStream>:: |
| 85 | +client( |
| 86 | + AsyncStream& stream, |
| 87 | + http_proto::context& ctx) |
| 88 | + : stream_(stream) |
| 89 | + , ctx_(ctx) |
| 90 | +{ |
| 91 | +} |
| 92 | + |
| 93 | +template<class AsyncStream> |
| 94 | +template< |
| 95 | + BOOST_ASIO_COMPLETION_TOKEN_FOR(void( |
| 96 | + ::boost::system::error_code, |
| 97 | + ::boost::http_proto::response_view)) HandshakeHandler, |
| 98 | + class Decorator> |
| 99 | +BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(HandshakeHandler, void( |
| 100 | + ::boost::system::error_code, |
| 101 | + ::boost::http_proto::response_view)) |
| 102 | +client<AsyncStream>:: |
| 103 | +async_handshake( |
| 104 | + core::string_view host, |
| 105 | + core::string_view target, |
| 106 | + Decorator decorator, |
| 107 | + HandshakeHandler&& handler) |
| 108 | +{ |
| 109 | + return asio::async_initiate< |
| 110 | + HandshakeHandler, |
| 111 | + void(system::error_code, http_proto::response_view)>( |
| 112 | + run_handshake_op{*this}, |
| 113 | + handler, |
| 114 | + host, |
| 115 | + target); |
| 116 | +} |
| 117 | + |
| 118 | +} // ws_io |
| 119 | +} // boost |
| 120 | + |
| 121 | +#endif |
0 commit comments