Clio develop
The XRP Ledger API server.
Loading...
Searching...
No Matches
Connection.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/Taggable.hpp"
23#include "web/ng/Error.hpp"
24#include "web/ng/Request.hpp"
25#include "web/ng/Response.hpp"
26
27#include <boost/asio/spawn.hpp>
28#include <boost/beast/core/flat_buffer.hpp>
29
30#include <chrono>
31#include <concepts>
32#include <cstddef>
33#include <expected>
34#include <memory>
35#include <optional>
36#include <string>
37#include <utility>
38
39namespace web::ng {
40
45protected:
46 std::string ip_; // client ip
47 std::optional<bool> isAdmin_;
48
49public:
56 ConnectionMetadata(std::string ip, util::TagDecoratorFactory const& tagDecoratorFactory);
57
63 virtual bool
64 wasUpgraded() const = 0;
65
71 std::string const&
72 ip() const;
73
79 void
80 setIp(std::string newIp)
81 {
82 ip_ = std::move(newIp);
83 }
84
90 bool
91 isAdmin() const;
92
100 template <std::invocable T>
101 void
102 setIsAdmin(T&& setter)
103 {
104 if (not isAdmin_.has_value())
105 isAdmin_ = setter();
106 }
107};
108
113protected:
114 boost::beast::flat_buffer buffer_;
115
116public:
121 static constexpr std::chrono::steady_clock::duration kDEFAULT_TIMEOUT = std::chrono::seconds{11};
122
130 Connection(std::string ip, boost::beast::flat_buffer buffer, util::TagDecoratorFactory const& tagDecoratorFactory);
131
138 virtual void
139 setTimeout(std::chrono::steady_clock::duration newTimeout) = 0;
140
148 virtual std::optional<Error>
149 send(Response response, boost::asio::yield_context yield) = 0;
150
157 virtual std::expected<Request, Error>
158 receive(boost::asio::yield_context yield) = 0;
159
165 virtual void
166 close(boost::asio::yield_context yield) = 0;
167};
168
172using ConnectionPtr = std::unique_ptr<Connection>;
173
174} // namespace web::ng
A factory for TagDecorator instantiation.
Definition Taggable.hpp:182
A base class that allows attaching a tag decorator to a subclass.
Definition Taggable.hpp:253
An interface for a connection metadata class.
Definition Connection.hpp:44
void setIp(std::string newIp)
Set the ip of the client.
Definition Connection.hpp:80
std::string const & ip() const
Get the ip of the client.
Definition Connection.cpp:37
void setIsAdmin(T &&setter)
Set the isAdmin field.
Definition Connection.hpp:102
ConnectionMetadata(std::string ip, util::TagDecoratorFactory const &tagDecoratorFactory)
Construct a new ConnectionMetadata object.
Definition Connection.cpp:31
virtual bool wasUpgraded() const =0
Whether the connection was upgraded. Upgraded connections are websocket connections.
bool isAdmin() const
Get whether the client is an admin.
Definition Connection.cpp:43
A class representing a connection to a client.
Definition Connection.hpp:112
virtual std::expected< Request, Error > receive(boost::asio::yield_context yield)=0
Receive a request from the client.
Connection(std::string ip, boost::beast::flat_buffer buffer, util::TagDecoratorFactory const &tagDecoratorFactory)
Construct a new Connection object.
Definition Connection.cpp:48
virtual void close(boost::asio::yield_context yield)=0
Gracefully close the connection.
virtual void setTimeout(std::chrono::steady_clock::duration newTimeout)=0
Get the timeout for send, receive, and close operations. For WebSocket connections,...
virtual std::optional< Error > send(Response response, boost::asio::yield_context yield)=0
Send a response to the client.
static constexpr std::chrono::steady_clock::duration kDEFAULT_TIMEOUT
The default timeout for send, receive, and close operations.
Definition Connection.hpp:121
Represents an HTTP or Websocket response.
Definition Response.hpp:40