Clio develop
The XRP Ledger API server.
Loading...
Searching...
No Matches
SubscriptionContext.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/CoroutineGroup.hpp"
23#include "util/Taggable.hpp"
24#include "web/SubscriptionContextInterface.hpp"
25#include "web/ng/Connection.hpp"
26#include "web/ng/Error.hpp"
27#include "web/ng/impl/WsConnection.hpp"
28
29#include <boost/asio/any_io_executor.hpp>
30#include <boost/asio/spawn.hpp>
31#include <boost/signals2/variadic_signal.hpp>
32
33#include <atomic>
34#include <cstddef>
35#include <cstdint>
36#include <functional>
37#include <memory>
38#include <optional>
39#include <string>
40
41namespace web::ng {
42
49public:
53 using ErrorHandler = std::function<bool(Error const&, Connection const&)>;
54
55private:
56 std::reference_wrapper<impl::WsConnectionBase> connection_;
57 std::optional<size_t> maxSendQueueSize_;
58 util::CoroutineGroup tasksGroup_;
59 boost::asio::yield_context yield_;
60 ErrorHandler errorHandler_;
61
62 boost::signals2::signal<void(SubscriptionContextInterface*)> onDisconnect_;
63 std::atomic_bool disconnected_{false};
64
70 std::atomic_uint32_t apiSubversion_ = 0u;
71
72public:
83 util::TagDecoratorFactory const& factory,
84 impl::WsConnectionBase& connection,
85 std::optional<size_t> maxSendQueueSize,
86 boost::asio::yield_context yield,
87 ErrorHandler errorHandler
88 );
89
96 void
97 send(std::shared_ptr<std::string> message) override;
98
104 void
105 onDisconnect(OnDisconnectSlot const& slot) override;
106
111 void
112 setApiSubversion(uint32_t value) override;
113
119 uint32_t
120 apiSubversion() const override;
121
128 void
129 disconnect(boost::asio::yield_context yield);
130};
131
132} // namespace web::ng
CoroutineGroup is a helper class to manage a group of coroutines. It allows to spawn multiple corouti...
Definition CoroutineGroup.hpp:37
A factory for TagDecorator instantiation.
Definition Taggable.hpp:169
An interface to provide connection functionality for subscriptions.
Definition SubscriptionContextInterface.hpp:39
std::function< void(SubscriptionContextInterface *)> OnDisconnectSlot
Alias for on disconnect slot.
Definition SubscriptionContextInterface.hpp:57
A class representing a connection to a client.
Definition Connection.hpp:100
Implementation of SubscriptionContextInterface.
Definition SubscriptionContext.hpp:48
std::function< bool(Error const &, Connection const &)> ErrorHandler
Error handler definition. Error handler returns true if connection should be closed false otherwise.
Definition SubscriptionContext.hpp:53
void onDisconnect(OnDisconnectSlot const &slot) override
Connect a slot to onDisconnect connection signal.
Definition SubscriptionContext.cpp:75
void send(std::shared_ptr< std::string > message) override
Send message to the client.
Definition SubscriptionContext.cpp:54
void setApiSubversion(uint32_t value) override
Set the API subversion.
Definition SubscriptionContext.cpp:81
SubscriptionContext(util::TagDecoratorFactory const &factory, impl::WsConnectionBase &connection, std::optional< size_t > maxSendQueueSize, boost::asio::yield_context yield, ErrorHandler errorHandler)
Construct a new Subscription Context object.
Definition SubscriptionContext.cpp:37
uint32_t apiSubversion() const override
Get the API subversion.
Definition SubscriptionContext.cpp:87
void disconnect(boost::asio::yield_context yield)
Notify the context that related connection is disconnected and wait for all the task to complete.
Definition SubscriptionContext.cpp:93
Definition WsConnection.hpp:55