SNode.C
Loading...
Searching...
No Matches
SocketServer.h
Go to the documentation of this file.
1/*
2 * SNode.C - A Slim Toolkit for Network Communication
3 * Copyright (C) Volker Christian <me@vchrist.at>
4 * 2020, 2021, 2022, 2023, 2024, 2025
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as published
8 * by the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20/*
21 * MIT License
22 *
23 * Permission is hereby granted, free of charge, to any person obtaining a copy
24 * of this software and associated documentation files (the "Software"), to deal
25 * in the Software without restriction, including without limitation the rights
26 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
27 * copies of the Software, and to permit persons to whom the Software is
28 * furnished to do so, subject to the following conditions:
29 *
30 * The above copyright notice and this permission notice shall be included in
31 * all copies or substantial portions of the Software.
32 *
33 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
34 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
35 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
36 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
37 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
38 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
39 * THE SOFTWARE.
40 */
41
42#ifndef CORE_SOCKET_STREAM_SOCKETSERVERNEW_H
43#define CORE_SOCKET_STREAM_SOCKETSERVERNEW_H
44
45#include "core/SNodeC.h"
46#include "core/eventreceiver/AcceptEventReceiver.h"
47#include "core/socket/Socket.h" // IWYU pragma: export
48#include "core/socket/State.h" // IWYU pragma: export
49#include "core/socket/stream/SocketContextFactory.h"
50#include "core/timer/Timer.h"
51
52#ifndef DOXYGEN_SHOULD_SKIP_THIS
53
54#include "log/Logger.h"
55#include "utils/Random.h"
56
57#include <algorithm>
58#include <functional> // IWYU pragma: export
59#include <type_traits> // IWYU pragma: export
60
61#endif /* DOXYGEN_SHOULD_SKIP_THIS */
62
63namespace core::socket::stream {
64
65 template <typename SocketAcceptorT, typename SocketContextFactoryT, typename... Args>
66 requires std::is_base_of_v<core::eventreceiver::AcceptEventReceiver, SocketAcceptorT> &&
67 std::is_base_of_v<core::socket::stream::SocketContextFactory, SocketContextFactoryT>
68 class SocketServer : public core::socket::Socket<typename SocketAcceptorT::Config> {
69 private:
70 using SocketAcceptor = SocketAcceptorT;
71 using SocketContextFactory = SocketContextFactoryT;
72
73 using Super = core::socket::Socket<typename SocketAcceptor::Config>;
74
75 public:
76 using SocketConnection = typename SocketAcceptor::SocketConnection;
77 using SocketAddress = typename SocketAcceptor::SocketAddress;
78
79 SocketServer(const std::string& name,
80 const std::function<void(SocketConnection*)>& onConnect,
81 const std::function<void(SocketConnection*)>& onConnected,
82 const std::function<void(SocketConnection*)>& onDisconnect,
83 Args&&... args)
84 : Super(name)
85 , socketContextFactory(std::make_shared<SocketContextFactory>(std::forward<Args>(args)...))
86 , onConnect(onConnect)
87 , onConnected(onConnected)
88 , onDisconnect(onDisconnect) {
89 }
90
91 SocketServer(const std::function<void(SocketConnection*)>& onConnect,
92 const std::function<void(SocketConnection*)>& onConnected,
93 const std::function<void(SocketConnection*)>& onDisconnect,
94 Args&&... args)
95 : SocketServer("", onConnect, onConnected, onDisconnect, std::forward<Args>(args)...) {
96 }
97
98 // VLOG() is used hire as this are log messages for the application
99 SocketServer(const std::string& name, Args&&... args)
100 : SocketServer(
101 name,
102 [](SocketConnection* socketConnection) { // onConnect
103 VLOG(2) << socketConnection->getConnectionName() << ": OnConnect";
104
105 VLOG(2) << " Local: " << socketConnection->getLocalAddress().toString();
106 VLOG(2) << " Peer: " << socketConnection->getRemoteAddress().toString();
107 },
108 [](SocketConnection* socketConnection) { // onConnected
109 VLOG(2) << socketConnection->getConnectionName() << ": OnConnected";
110
111 VLOG(2) << " Local: " << socketConnection->getLocalAddress().toString();
112 VLOG(2) << " Peer: " << socketConnection->getRemoteAddress().toString();
113 },
114 [](SocketConnection* socketConnection) { // onDisconnect
115 VLOG(2) << socketConnection->getConnectionName() << ": OnDisconnect";
116
117 VLOG(2) << " Local: " << socketConnection->getLocalAddress().toString();
118 VLOG(2) << " Peer: " << socketConnection->getRemoteAddress().toString();
119
120 VLOG(2) << " Online Since: " << socketConnection->getOnlineSince();
121 VLOG(2) << " Online Duration: " << socketConnection->getOnlineDuration();
122
123 VLOG(2) << " Total Queued: " << socketConnection->getTotalQueued();
124 VLOG(2) << " Total Sent: " << socketConnection->getTotalSent();
125 VLOG(2) << " Write Delta: " << socketConnection->getTotalQueued() - socketConnection->getTotalSent();
126 VLOG(2) << " Total Read: " << socketConnection->getTotalRead();
127 VLOG(2) << " Total Processed: " << socketConnection->getTotalProcessed();
128 VLOG(2) << " Read Delta: " << socketConnection->getTotalRead() - socketConnection->getTotalProcessed();
129 },
130 std::forward<Args>(args)...) {
131 }
132
133 explicit SocketServer(Args&&... args)
134 : SocketServer("", std::forward<Args>(args)...) {
135 }
136
137 private:
138 void realListen(const std::function<void(const SocketAddress&, core::socket::State)>& onStatus,
139 unsigned int tries,
140 double retryTimeoutScale) const {
142 new SocketAcceptor(
144 onConnect,
147 [server = *this, onStatus, tries, retryTimeoutScale](const SocketAddress& socketAddress,
148 core::socket::State state) mutable {
149 const bool retryFlag = (state & core::socket::State::NO_RETRY) == 0;
150 state &= ~core::socket::State::NO_RETRY;
151 onStatus(socketAddress, state);
152
153 if (retryFlag && server.getConfig().getRetry() // Shall we potentially retry? In case are the ...
154 && (server.getConfig().getRetryTries() == 0 ||
155 tries < server.getConfig().getRetryTries()) // ... limits not reached and has an ...
156 && (state == core::socket::State::ERROR ||
157 (state == core::socket::State::FATAL && server.getConfig().getRetryOnFatal()))) { // error occurred?
158 double relativeRetryTimeout = server.getConfig().getRetryLimit() > 0
159 ? std::min<double>(server.getConfig().getRetryTimeout() * retryTimeoutScale,
160 server.getConfig().getRetryLimit())
161 : server.getConfig().getRetryTimeout() * retryTimeoutScale;
162 relativeRetryTimeout -=
163 utils::Random::getInRange(-server.getConfig().getRetryJitter(), server.getConfig().getRetryJitter()) *
164 relativeRetryTimeout / 100.;
165
166 LOG(INFO) << server.getConfig().getInstanceName() << ": OnStatus";
167 LOG(INFO) << " retrying in " << relativeRetryTimeout << " seconds";
168
170 [server, onStatus, tries, retryTimeoutScale]() mutable {
171 server.getConfig().Local::renew();
172
173 server.realListen(onStatus, tries + 1, retryTimeoutScale * server.getConfig().getRetryBase());
174 },
175 relativeRetryTimeout);
176 }
177 },
178 Super::config);
179 }
180 }
181
182 public:
183 void listen(const std::function<void(const SocketAddress&, core::socket::State)>& onStatus) const {
184 realListen(onStatus, 0, 1);
185 }
186
187 void listen(const SocketAddress& localAddress,
188 const std::function<void(const SocketAddress&, core::socket::State)>& onStatus) const {
189 Super::config->Local::setSocketAddress(localAddress);
190
191 listen(onStatus);
192 }
193
194 void listen(const SocketAddress& localAddress,
195 int backlog,
196 const std::function<void(const SocketAddress&, core::socket::State)>& onStatus) const {
197 Super::config->Local::setBacklog(backlog);
198
199 listen(localAddress, onStatus);
200 }
201
202 std::function<void(SocketConnection*)> setOnConnect(const std::function<void(SocketConnection*)>& onConnect) {
203 std::function<void(SocketConnection*)> oldOnConnect = this->onConnect;
204
205 this->onConnect = onConnect;
206
207 return oldOnConnect;
208 }
209
210 std::function<void(SocketConnection*)> setOnConnected(const std::function<void(SocketConnection*)>& onConnected) {
211 std::function<void(SocketConnection*)> oldOnConnected = this->onConnected;
212
213 this->onConnected = onConnected;
214
215 return oldOnConnected;
216 }
217
218 std::function<void(SocketConnection*)> setOnDisconnect(const std::function<void(SocketConnection*)>& onDisconnect) {
219 std::function<void(SocketConnection*)> oldOnDisconnect = this->onDisconnect;
220
221 this->onDisconnect = onDisconnect;
222
223 return oldOnDisconnect;
224 }
225
226 std::shared_ptr<SocketContextFactory> getSocketContextFactory() {
228 }
229
230 private:
231 std::shared_ptr<SocketContextFactory> socketContextFactory;
232
233 std::function<void(SocketConnection*)> onConnect;
234 std::function<void(SocketConnection*)> onConnected;
235 std::function<void(SocketConnection*)> onDisconnect;
236 };
237
238 template <typename SocketServer, typename... Args>
239 SocketServer Server(const std::string& instanceName,
240 const std::function<void(typename SocketServer::Config&)>& configurator,
241 Args&&... socketContextFactoryArgs) {
242 const SocketServer socketServer(instanceName, std::forward<Args>(socketContextFactoryArgs)...);
243
244 configurator(socketServer.getConfig());
245
246 return socketServer;
247 }
248
249 template <typename SocketServer, typename... Args>
250 SocketServer Server(const std::string& instanceName, Args&&... socketContextFactoryArgs) {
251 return SocketServer(instanceName, std::forward<Args>(socketContextFactoryArgs)...);
252 }
253
254} // namespace core::socket::stream
255
256#endif // CORE_SOCKET_STREAM_SOCKETSERVERNEW_H
#define APPLICATION(req, res)
Definition Router.h:68
static State state()
Definition SNodeC.cpp:76
AcceptEventReceiver(const std::string &name, const utils::Timeval &timeout)
Config & getConfig() const
Definition Socket.hpp:60
State & operator&=(int state)
Definition State.cpp:90
static constexpr int DISABLED
Definition State.h:56
bool operator==(const int &state) const
Definition State.cpp:74
State operator&(int state)
Definition State.cpp:106
static constexpr int ERROR
Definition State.h:57
std::string what() const
Definition State.cpp:114
static constexpr int FATAL
Definition State.h:58
static constexpr int OK
Definition State.h:55
static constexpr int NO_RETRY
Definition State.h:59
SocketAcceptor(const std::shared_ptr< core::socket::stream::SocketContextFactory > &socketContextFactory, const std::function< void(SocketConnection *)> &onConnect, const std::function< void(SocketConnection *)> &onConnected, const std::function< void(SocketConnection *)> &onDisconnect, const std::function< void(const SocketAddress &, core::socket::State)> &onStatus, const std::shared_ptr< Config > &config)
std::function< void(SocketConnection *)> onConnected
SocketAcceptor(const SocketAcceptor &socketAcceptor)
std::function< void(SocketConnection *)> onConnect
std::function< void(SocketConnection *)> onDisconnect
std::shared_ptr< core::socket::stream::SocketContextFactory > socketContextFactory
std::shared_ptr< Config > config
PhysicalServerSocket physicalServerSocket
std::function< void(const SocketAddress &, core::socket::State)> onStatus
std::function< void(SocketConnection *)> setOnConnected(const std::function< void(SocketConnection *)> &onConnected)
void realListen(const std::function< void(const SocketAddress &, core::socket::State)> &onStatus, unsigned int tries, double retryTimeoutScale) const
SocketServer(const std::string &name, const std::function< void(SocketConnection *)> &onConnect, const std::function< void(SocketConnection *)> &onConnected, const std::function< void(SocketConnection *)> &onDisconnect, Args &&... args)
SocketServer(const std::string &name, Args &&... args)
std::function< void(SocketConnection *)> onConnect
SocketServer(const std::function< void(SocketConnection *)> &onConnect, const std::function< void(SocketConnection *)> &onConnected, const std::function< void(SocketConnection *)> &onDisconnect, Args &&... args)
std::function< void(SocketConnection *)> onDisconnect
void listen(const SocketAddress &localAddress, int backlog, const std::function< void(const SocketAddress &, core::socket::State)> &onStatus) const
void listen(const SocketAddress &localAddress, const std::function< void(const SocketAddress &, core::socket::State)> &onStatus) const
std::shared_ptr< SocketContextFactory > socketContextFactory
std::function< void(SocketConnection *)> setOnConnect(const std::function< void(SocketConnection *)> &onConnect)
void listen(const std::function< void(const SocketAddress &, core::socket::State)> &onStatus) const
std::function< void(SocketConnection *)> onConnected
std::shared_ptr< SocketContextFactory > getSocketContextFactory()
std::function< void(SocketConnection *)> setOnDisconnect(const std::function< void(SocketConnection *)> &onDisconnect)
SocketAcceptor(const SocketAcceptor &socketAcceptor)
SocketAcceptor(const std::shared_ptr< core::socket::stream::SocketContextFactory > &socketContextFactory, const std::function< void(SocketConnection *)> &onConnect, const std::function< void(SocketConnection *)> &onConnected, const std::function< void(SocketConnection *)> &onDisconnect, const std::function< void(const SocketAddress &, core::socket::State)> &onStatus, const std::shared_ptr< Config > &config)
static Timer singleshotTimer(const std::function< void()> &dispatcher, const utils::Timeval &timeout)
Definition Timer.cpp:57
Controller(const std::shared_ptr< web::http::server::Request > &request, const std::shared_ptr< web::http::server::Response > &response)
void send(const std::string &chunk)
Definition Response.cpp:165
WebAppT(const std::string &name, const Router &router)
Definition WebAppT.h:80
WebAppT(const std::string &name)
Definition WebAppT.h:76
static void free()
Definition WebApp.cpp:72
static void init(int argc, char *argv[])
Definition WebApp.cpp:56
static void stop()
Definition WebApp.cpp:64
static core::TickStatus tick(const utils::Timeval &timeOut=0)
Definition WebApp.cpp:68
static core::State state()
Definition WebApp.cpp:76
static int start(const utils::Timeval &timeOut={LONG_MAX, 0})
Definition WebApp.cpp:60
WebApp(const Router &router)
Definition WebApp.cpp:52
const std::string & getInstanceName() const
std::string toString(bool expanded=true) const override
void listen(const std::string &ipOrHostname, uint16_t port, const std::function< void(const SocketAddress &, core::socket::State)> &onStatus) const
void listen(uint16_t port, int backlog, const std::function< void(const SocketAddress &, core::socket::State)> &onStatus) const
void listen(const std::string &ipOrHostname, uint16_t port, int backlog, const std::function< void(const SocketAddress &, core::socket::State)> &onStatus) const
void listen(uint16_t port, const std::function< void(const SocketAddress &, core::socket::State)> &onStatus) const
void getAttribute(const std::function< void(Attribute &)> &onFound, const std::function< void(const std::string &)> &onNotFound, const std::string &subKey="") const
static double getInRange(double ll, double ul)
Definition Random.cpp:52
Timeval(const std::initializer_list< time_t > &initList) noexcept
Definition Timeval.cpp:59
int main(int argc, char *argv[])
SocketServer Server(const std::string &instanceName, const std::function< void(typename SocketServer::Config &)> &configurator, Args &&... socketContextFactoryArgs)
SocketServer Server(const std::string &instanceName, Args &&... socketContextFactoryArgs)
Definition Timer.h:59
TickStatus
Definition TickStatus.h:51
State
Definition State.h:51
SocketServer< SocketContextFactory, SocketContextFactoryArgs... > Server(const std::string &instanceName, const std::function< void(typename SocketServer< SocketContextFactory, SocketContextFactoryArgs... >::Config &)> &configurator, SocketContextFactoryArgs &&... socketContextFactoryArgs)
SocketServer< SocketContextFactory, SocketContextFactoryArgs... > Server(const std::string &instanceName, SocketContextFactoryArgs &&... socketContextFactoryArgs)
Definition Client.h:52