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
30public:
37 ConnectionMetadata(std::string ip, util::TagDecoratorFactory const& tagDecoratorFactory);
38
44 virtual bool
45 wasUpgraded() const = 0;
46
52 std::string const&
53 ip() const;
54
60 void
61 setIp(std::string newIp)
62 {
63 ip_ = std::move(newIp);
64 }
65
71 bool
72 isAdmin() const;
73
81 template <std::invocable T>
82 void
83 setIsAdmin(T&& setter)
84 {
85 if (not isAdmin_.has_value())
86 isAdmin_ = setter();
87 }
88};
89
94protected:
95 boost::beast::flat_buffer buffer_;
96
97public:
103 static constexpr std::chrono::steady_clock::duration kDEFAULT_TIMEOUT =
104 std::chrono::seconds{11};
105
114 std::string ip,
115 boost::beast::flat_buffer buffer,
116 util::TagDecoratorFactory const& tagDecoratorFactory
117 );
118
125 virtual void
126 setTimeout(std::chrono::steady_clock::duration newTimeout) = 0;
127
135 virtual std::expected<void, Error>
136 send(Response response, boost::asio::yield_context yield) = 0;
137
144 virtual std::expected<Request, Error>
145 receive(boost::asio::yield_context yield) = 0;
146
152 virtual void
153 close(boost::asio::yield_context yield) = 0;
154};
155
159using ConnectionPtr = std::unique_ptr<Connection>;
160
161} // 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 setIp(std::string newIp)
Set the ip of the client.
Definition Connection.hpp:61
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:83
ConnectionMetadata(std::string ip, util::TagDecoratorFactory const &tagDecoratorFactory)
Construct a new ConnectionMetadata object.
Definition Connection.cpp:12
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.
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,...
static constexpr std::chrono::steady_clock::duration kDEFAULT_TIMEOUT
The default timeout for send, receive, and close operations.
Definition Connection.hpp:103
Represents an HTTP or Websocket response.
Definition Response.hpp:21