Clio  develop
The XRP Ledger API server.
Loading...
Searching...
No Matches
Connection.hpp
1#pragma once
2
3#include "util/Taggable.hpp"
4#include "web/ng/Error.hpp"
5#include "web/ng/Request.hpp"
6#include "web/ng/Response.hpp"
7
8#include <boost/asio/spawn.hpp>
9#include <boost/beast/core/flat_buffer.hpp>
10
11#include <chrono>
12#include <concepts>
13#include <cstddef>
14#include <expected>
15#include <memory>
16#include <optional>
17#include <string>
18#include <utility>
19
20namespace web::ng {
21
26protected:
27 std::string ip_; // client ip
28 std::optional<bool> isAdmin_;
29 bool isProxyConnection_ = false;
30
31public:
38 ConnectionMetadata(std::string ip, util::TagDecoratorFactory const& tagDecoratorFactory);
39
45 [[nodiscard]] virtual bool
46 wasUpgraded() const = 0;
47
53 [[nodiscard]] std::string const&
54 ip() const;
55
61 void
62 setIp(std::string newIp)
63 {
64 ip_ = std::move(newIp);
65 }
66
70 void
72 {
73 isProxyConnection_ = true;
74 }
75
81 [[nodiscard]] bool
83 {
84 return isProxyConnection_;
85 }
86
92 [[nodiscard]] bool
93 isAdmin() const;
94
102 template <std::invocable T>
103 void
104 setIsAdmin(T&& setter)
105 {
106 if (not isAdmin_.has_value())
107 isAdmin_ = setter();
108 }
109};
110
115protected:
116 boost::beast::flat_buffer buffer_;
117
118public:
124 static constexpr std::chrono::steady_clock::duration kDefaultTimeout = std::chrono::seconds{11};
125
134 std::string ip,
135 boost::beast::flat_buffer buffer,
136 util::TagDecoratorFactory const& tagDecoratorFactory
137 );
138
145 virtual void
146 setTimeout(std::chrono::steady_clock::duration newTimeout) = 0;
147
155 virtual std::expected<void, Error>
156 send(Response response, boost::asio::yield_context yield) = 0;
157
164 virtual std::expected<Request, Error>
165 receive(boost::asio::yield_context yield) = 0;
166
172 virtual void
173 close(boost::asio::yield_context yield) = 0;
174};
175
179using ConnectionPtr = std::unique_ptr<Connection>;
180
181} // namespace web::ng
A factory for TagDecorator instantiation.
Definition Taggable.hpp:165
A base class that allows attaching a tag decorator to a subclass.
Definition Taggable.hpp:236
void markAsProxyConnection()
Mark this connection as coming through a trusted proxy.
Definition Connection.hpp:71
void setIp(std::string newIp)
Set the ip of the client.
Definition Connection.hpp:62
std::string const & ip() const
Get the ip of the client.
Definition Connection.cpp:21
void setIsAdmin(T &&setter)
Set the isAdmin field.
Definition Connection.hpp:104
ConnectionMetadata(std::string ip, util::TagDecoratorFactory const &tagDecoratorFactory)
Construct a new ConnectionMetadata object.
Definition Connection.cpp:12
bool isProxyConnection() const
Whether this connection was identified as coming through a trusted proxy.
Definition Connection.hpp:82
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:27
virtual std::expected< Request, Error > receive(boost::asio::yield_context yield)=0
Receive a request from the client.
virtual std::expected< void, Error > send(Response response, boost::asio::yield_context yield)=0
Send a response to the client.
static constexpr std::chrono::steady_clock::duration kDefaultTimeout
The default timeout for send, receive, and close operations.
Definition Connection.hpp:124
Connection(std::string ip, boost::beast::flat_buffer buffer, util::TagDecoratorFactory const &tagDecoratorFactory)
Construct a new Connection object.
Definition Connection.cpp:32
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,...
Represents an HTTP or Websocket response.
Definition Response.hpp:21