Clio develop
The XRP Ledger API server.
Loading...
Searching...
No Matches
WsConnection.hpp
1//------------------------------------------------------------------------------
2/*
3 This file is part of clio: https://github.com/XRPLF/clio
4 Copyright (c) 2024, the clio developers.
5
6 Permission to use, copy, modify, and distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19
20#pragma once
21
22#include "util/log/Logger.hpp"
23#include "util/requests/Types.hpp"
24
25#include <boost/asio/bind_cancellation_slot.hpp>
26#include <boost/asio/cancellation_signal.hpp>
27#include <boost/asio/cancellation_type.hpp>
28#include <boost/asio/io_context.hpp>
29#include <boost/asio/spawn.hpp>
30#include <boost/beast/core/tcp_stream.hpp>
31#include <boost/beast/websocket/stream.hpp>
32
33#include <chrono>
34#include <expected>
35#include <memory>
36#include <optional>
37#include <string>
38#include <utility>
39#include <vector>
40
41namespace util::requests {
42
49public:
50 virtual ~WsConnection() = default;
51
59 virtual std::expected<std::string, RequestError>
61 boost::asio::yield_context yield,
62 std::optional<std::chrono::steady_clock::duration> timeout = std::nullopt
63 ) = 0;
64
73 virtual std::optional<RequestError>
75 std::string const& message,
76 boost::asio::yield_context yield,
77 std::optional<std::chrono::steady_clock::duration> timeout = std::nullopt
78 ) = 0;
79
87 virtual std::optional<RequestError>
88 close(boost::asio::yield_context yield, std::chrono::steady_clock::duration timeout = kDEFAULT_TIMEOUT) = 0;
89
90 static constexpr std::chrono::seconds kDEFAULT_TIMEOUT{5};
91};
92using WsConnectionPtr = std::unique_ptr<WsConnection>;
93
98 util::Logger log_{"WsConnectionBuilder"};
99 std::string host_;
100 std::string port_;
101 std::vector<HttpHeader> headers_;
102 std::chrono::steady_clock::duration connectionTimeout_{kDEFAULT_TIMEOUT};
103 std::chrono::steady_clock::duration wsHandshakeTimeout_{kDEFAULT_TIMEOUT};
104 std::string target_{"/"};
105
106public:
113 WsConnectionBuilder(std::string host, std::string port);
114
122 addHeader(HttpHeader header);
123
131 addHeaders(std::vector<HttpHeader> headers);
132
140 setTarget(std::string target);
141
149 setConnectionTimeout(std::chrono::steady_clock::duration timeout);
150
158 setWsHandshakeTimeout(std::chrono::steady_clock::duration timeout);
159
166 std::expected<WsConnectionPtr, RequestError>
167 sslConnect(boost::asio::yield_context yield) const;
168
175 std::expected<WsConnectionPtr, RequestError>
176 plainConnect(boost::asio::yield_context yield) const;
177
184 std::expected<WsConnectionPtr, RequestError>
185 connect(boost::asio::yield_context yield) const;
186
187 static constexpr std::chrono::seconds kDEFAULT_TIMEOUT{5};
189private:
190 template <typename StreamDataType>
191 std::expected<WsConnectionPtr, RequestError>
192 connectImpl(StreamDataType&& streamData, boost::asio::yield_context yield) const;
193};
194
195} // namespace util::requests
A simple thread-safe logger for the channel specified in the constructor.
Definition Logger.hpp:110
Builder for WebSocket connections.
Definition WsConnection.hpp:97
std::expected< WsConnectionPtr, RequestError > connect(boost::asio::yield_context yield) const
Connect to the host trying SSL first then plain if SSL fails.
Definition WsConnection.cpp:120
std::expected< WsConnectionPtr, RequestError > plainConnect(boost::asio::yield_context yield) const
Connect to the host without SSL asynchronously.
Definition WsConnection.cpp:114
WsConnectionBuilder(std::string host, std::string port)
Create a new connection builder.
Definition WsConnection.cpp:55
static constexpr std::chrono::seconds kDEFAULT_TIMEOUT
Definition WsConnection.hpp:187
WsConnectionBuilder & setConnectionTimeout(std::chrono::steady_clock::duration timeout)
Set the timeout for connection establishing operations. Default is 5 seconds.
Definition WsConnection.cpp:82
WsConnectionBuilder & setTarget(std::string target)
Set the target of the request.
Definition WsConnection.cpp:75
WsConnectionBuilder & addHeader(HttpHeader header)
Add a header to the request.
Definition WsConnection.cpp:61
std::expected< WsConnectionPtr, RequestError > sslConnect(boost::asio::yield_context yield) const
Connect to the host using SSL asynchronously.
Definition WsConnection.cpp:96
WsConnectionBuilder & setWsHandshakeTimeout(std::chrono::steady_clock::duration timeout)
Set the timeout for WebSocket handshake. Default is 5 seconds.
Definition WsConnection.cpp:89
WsConnectionBuilder & addHeaders(std::vector< HttpHeader > headers)
Add multiple headers to the request.
Definition WsConnection.cpp:68
Interface for WebSocket connections. It is used to hide SSL and plain connections behind the same int...
Definition WsConnection.hpp:48
static constexpr std::chrono::seconds kDEFAULT_TIMEOUT
Definition WsConnection.hpp:90
virtual std::optional< RequestError > close(boost::asio::yield_context yield, std::chrono::steady_clock::duration timeout=kDEFAULT_TIMEOUT)=0
Close the WebSocket.
virtual std::optional< RequestError > write(std::string const &message, boost::asio::yield_context yield, std::optional< std::chrono::steady_clock::duration > timeout=std::nullopt)=0
Write a message to the WebSocket.
virtual std::expected< std::string, RequestError > read(boost::asio::yield_context yield, std::optional< std::chrono::steady_clock::duration > timeout=std::nullopt)=0
Read a message from the WebSocket.
HTTP header.
Definition Types.hpp:70