//------------------------------------------------------------------------------ /* This file is part of clio: https://github.com/XRPLF/clio Copyright (c) 2024, the clio developers. Permission to use, copy, modify, and distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ //============================================================================== #pragma once #include "util/WithTimeout.hpp" #include "util/requests/Types.hpp" #include "util/requests/WsConnection.hpp" #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include namespace util::requests::impl { template class WsConnectionImpl : public WsConnection { StreamType ws_; public: explicit WsConnectionImpl(StreamType ws) : ws_(std::move(ws)) { } std::expected read(boost::asio::yield_context yield, std::optional timeout = std::nullopt) override { boost::beast::error_code errorCode; boost::beast::flat_buffer buffer; auto operation = [&](auto&& token) { ws_.async_read(buffer, token); }; if (timeout) { errorCode = util::withTimeout(operation, yield[errorCode], *timeout); } else { operation(yield[errorCode]); } if (errorCode) return std::unexpected{RequestError{"Read error", errorCode}}; return boost::beast::buffers_to_string(std::move(buffer).data()); } std::optional write( std::string const& message, boost::asio::yield_context yield, std::optional timeout = std::nullopt ) override { boost::beast::error_code errorCode; auto operation = [&](auto&& token) { ws_.async_write(boost::asio::buffer(message), token); }; if (timeout) { errorCode = util::withTimeout(operation, yield, *timeout); } else { operation(yield[errorCode]); } if (errorCode) return RequestError{"Write error", errorCode}; return std::nullopt; } std::optional close(boost::asio::yield_context yield, std::chrono::steady_clock::duration const timeout = DEFAULT_TIMEOUT) override { // Set the timeout for closing the connection boost::beast::websocket::stream_base::timeout wsTimeout{}; ws_.get_option(wsTimeout); wsTimeout.handshake_timeout = timeout; ws_.set_option(wsTimeout); boost::beast::error_code errorCode; ws_.async_close(boost::beast::websocket::close_code::normal, yield[errorCode]); if (errorCode) return RequestError{"Close error", errorCode}; return std::nullopt; } }; using PlainWsConnection = WsConnectionImpl>; using SslWsConnection = WsConnectionImpl>>; } // namespace util::requests::impl