Clio develop
The XRP Ledger API server.
Loading...
Searching...
No Matches
ConnectionBase.hpp
1//------------------------------------------------------------------------------
2/*
3 This file is part of clio: https://github.com/XRPLF/clio
4 Copyright (c) 2023, 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/Taggable.hpp"
23#include "web/SubscriptionContextInterface.hpp"
24
25#include <boost/beast/http.hpp>
26#include <boost/beast/http/status.hpp>
27#include <boost/signals2.hpp>
28#include <boost/signals2/variadic_signal.hpp>
29
30#include <memory>
31#include <stdexcept>
32#include <string>
33#include <utility>
34
35namespace web {
36
37namespace http = boost::beast::http;
38
45protected:
46 boost::system::error_code ec_;
47 bool isAdmin_ = false;
48
49public:
50 std::string const clientIp;
51 bool upgraded = false;
52
59 ConnectionBase(util::TagDecoratorFactory const& tagFactory, std::string ip)
60 : Taggable(tagFactory), clientIp(std::move(ip))
61 {
62 }
63
70 virtual void
71 send(std::string&& msg, http::status status = http::status::ok) = 0;
72
79 virtual void
80 send([[maybe_unused]] std::shared_ptr<std::string> msg)
81 {
82 throw std::logic_error("web server can not send the shared payload");
83 }
84
93
99 bool
101 {
102 return ec_ != boost::system::error_code{};
103 }
104
110 [[nodiscard]] bool
111 isAdmin() const
112 {
113 return isAdmin_;
114 }
115};
116} // namespace web
A factory for TagDecorator instantiation.
Definition Taggable.hpp:169
A base class that allows attaching a tag decorator to a subclass.
Definition Taggable.hpp:240
Taggable(util::TagDecoratorFactory const &tagFactory)
New Taggable from a specified factory.
Definition Taggable.hpp:250
This namespace implements the web server and related components.
Definition Types.hpp:43
std::shared_ptr< SubscriptionContextInterface > SubscriptionContextPtr
An alias for shared pointer to a SubscriptionContextInterface.
Definition SubscriptionContextInterface.hpp:86
Base class for all connections.
Definition ConnectionBase.hpp:44
virtual SubscriptionContextPtr makeSubscriptionContext(util::TagDecoratorFactory const &factory)=0
Get the subscription context for this connection.
virtual void send(std::shared_ptr< std::string > msg)
Send via shared_ptr of string, that enables SubscriptionManager to publish to clients.
Definition ConnectionBase.hpp:80
ConnectionBase(util::TagDecoratorFactory const &tagFactory, std::string ip)
Create a new connection base.
Definition ConnectionBase.hpp:59
bool isAdmin() const
Indicates whether the connection has admin privileges.
Definition ConnectionBase.hpp:111
virtual void send(std::string &&msg, http::status status=http::status::ok)=0
Send the response to the client.
bool dead()
Indicates whether the connection had an error and is considered dead.
Definition ConnectionBase.hpp:100