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
38namespace web::ng {
39
44protected:
45 std::string ip_; // client ip
46 std::optional<bool> isAdmin_;
47
48public:
55 ConnectionMetadata(std::string ip, util::TagDecoratorFactory const& tagDecoratorFactory);
56
62 virtual bool
63 wasUpgraded() const = 0;
64
70 std::string const&
71 ip() const;
72
78 bool
79 isAdmin() const;
80
88 template <std::invocable T>
89 void
90 setIsAdmin(T&& setter)
91 {
92 if (not isAdmin_.has_value())
93 isAdmin_ = setter();
94 }
95};
96
101protected:
102 boost::beast::flat_buffer buffer_;
103
104public:
109 static constexpr std::chrono::steady_clock::duration kDEFAULT_TIMEOUT = std::chrono::seconds{11};
110
118 Connection(std::string ip, boost::beast::flat_buffer buffer, util::TagDecoratorFactory const& tagDecoratorFactory);
119
126 virtual void
127 setTimeout(std::chrono::steady_clock::duration newTimeout) = 0;
128
136 virtual std::optional<Error>
137 send(Response response, boost::asio::yield_context yield) = 0;
138
145 virtual std::expected<Request, Error>
146 receive(boost::asio::yield_context yield) = 0;
147
153 virtual void
154 close(boost::asio::yield_context yield) = 0;
155};
156
160using ConnectionPtr = std::unique_ptr<Connection>;
161
162} // namespace web::ng
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
An interface for a connection metadata class.
Definition Connection.hpp:43
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:90
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:100
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:109
Represents an HTTP or Websocket response.
Definition Response.hpp:40