C++ Essence Library 0.1.0
A Utility Library for Modern C++ Programming
Loading...
Searching...
No Matches
sse_connection.hpp
1/*
2 * Copyright (c) 2024 The RefValue Project
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 * THE SOFTWARE.
21 */
22
23#pragma once
24
25#include "../../abi/string.hpp"
26#include "../sse_message.hpp"
27#include "../uri.hpp"
28
29#include <concepts>
30#include <functional>
31#include <memory>
32#include <type_traits>
33#include <utility>
34
35namespace essence::net::abstract {
40 public:
41 sse_connection() = default;
42
43 template <typename T>
44 requires(!std::same_as<std::decay_t<T>, sse_connection>)
45 explicit sse_connection(T&& value) : wrapper_{std::make_shared<wrapper<T>>(std::forward<T>(value))} {}
46
52 [[nodiscard]] bool canceled() const {
53 return wrapper_->canceled();
54 }
55
60 [[nodiscard]] uri request_uri() const {
61 return wrapper_->request_uri();
62 }
63
68 [[nodiscard]] abi::string remote_address() const {
69 return wrapper_->remote_address();
70 }
71
75 void send_message(const sse_message& message) const {
76 wrapper_->send_message(message);
77 }
78
82 void tick() const {
83 wrapper_->tick();
84 }
85
89 void close() const {
90 wrapper_->close();
91 }
92
97 [[nodiscard]] void* underlying_ptr() const {
98 return wrapper_->underlying_ptr();
99 }
100
101 private:
102 struct base {
103 virtual ~base() = default;
104 virtual bool canceled() = 0;
105 virtual uri request_uri() = 0;
106 virtual abi::string remote_address() = 0;
107 virtual void send_message(const sse_message& message) = 0;
108 virtual void tick() = 0;
109 virtual void close() = 0;
110 virtual void* underlying_ptr() = 0;
111 };
112
113 template <typename T>
114 class wrapper final : public base {
115 public:
116 template <std::convertible_to<T> U>
117 explicit wrapper(U&& value) : value_{std::forward<U>(value)} {}
118
119 bool canceled() override {
120 return value_.canceled();
121 }
122
123 uri request_uri() override {
124 return value_.request_uri();
125 }
126
127 abi::string remote_address() override {
128 return value_.remote_address();
129 }
130
131 void send_message(const sse_message& message) override {
132 value_.send_message(message);
133 }
134
135 void tick() override {
136 value_.tick();
137 }
138
139 void close() override {
140 value_.close();
141 }
142
143 void* underlying_ptr() override {
144 return &value_;
145 }
146
147 private:
148 T value_;
149 };
150
151 std::shared_ptr<base> wrapper_;
152 };
153} // namespace essence::net::abstract
154
155namespace essence::net {
156 using sse_connection_handler = std::function<void(abstract::sse_connection connection)>;
157}
An interface: manages an SSE connection and its lifetime.
Definition sse_connection.hpp:39
void send_message(const sse_message &message) const
Sends a message to the client.
Definition sse_connection.hpp:75
bool canceled() const
Checks whether the internal operation has been canceled and if true the user must stop all invocation...
Definition sse_connection.hpp:52
void tick() const
Keep alive and raise an error when the network is disconnected.
Definition sse_connection.hpp:82
uri request_uri() const
Gets the request URI.
Definition sse_connection.hpp:60
abi::string remote_address() const
Gets the remote address.
Definition sse_connection.hpp:68
void close() const
Closes the connection.
Definition sse_connection.hpp:89
void * underlying_ptr() const
Gets the underlying pointer.
Definition sse_connection.hpp:97
Definition uri.hpp:37
A message describing a server-sent event.
Definition sse_message.hpp:34