Clio  develop
The XRP Ledger API server.
Loading...
Searching...
No Matches
StreamData.hpp
1#pragma once
2
3#include "util/requests/Types.hpp"
4#include "util/requests/impl/SslContext.hpp"
5
6#include <boost/asio/associated_executor.hpp>
7#include <boost/asio/spawn.hpp>
8#include <boost/asio/ssl/context.hpp>
9#include <boost/asio/ssl/stream.hpp>
10#include <boost/beast/core/error.hpp>
11#include <boost/beast/core/tcp_stream.hpp>
12#include <boost/beast/ssl/ssl_stream.hpp>
13#include <boost/beast/websocket.hpp>
14#include <boost/beast/websocket/stream.hpp>
15
16#include <expected>
17#include <utility>
18
19namespace util::requests::impl {
20
21template <typename StreamType>
22struct PlainStreamData {
23 static constexpr bool kSSL_ENABLED = false;
24
25 explicit PlainStreamData(boost::asio::yield_context yield)
26 : stream(boost::asio::get_associated_executor(yield))
27 {
28 }
29
30 StreamType stream;
31};
32
35
36template <typename StreamType>
37class SslStreamData {
38 boost::asio::ssl::context sslContext_;
39
40public:
41 static constexpr bool kSSL_ENABLED = true;
42
43 static std::expected<SslStreamData, RequestError>
44 create(boost::asio::yield_context yield)
45 {
46 auto sslContext = makeClientSslContext();
47 if (not sslContext.has_value()) {
48 return std::unexpected{std::move(sslContext.error())};
49 }
50 return SslStreamData{std::move(sslContext).value(), yield};
51 }
52
53 StreamType stream;
54
55private:
56 SslStreamData(boost::asio::ssl::context sslContext, boost::asio::yield_context yield)
57 : sslContext_(std::move(sslContext))
58 , stream(boost::asio::get_associated_executor(yield), sslContext_)
59
60 {
61 }
62};
63
65using SslWsStreamData = SslStreamData<
66 boost::beast::websocket::stream<boost::asio::ssl::stream<boost::beast::tcp_stream>>>;
67
68} // namespace util::requests::impl
Definition StreamData.hpp:37
Definition StreamData.hpp:22