SNode.C
Loading...
Searching...
No Matches
ConfigPhysicalSocket.cpp
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
21
22#include "net/config/ConfigSection.hpp"
23
24#ifndef DOXYGEN_SHOULD_SKIP_THIS
25
26#include "log/Logger.h"
27
28#include <functional>
29#include <sys/socket.h>
30
31#endif // DOXYGEN_SHOULD_SKIP_THIS
32
33#define XSTR(s) STR(s)
34#define STR(s) #s
35
36namespace net::config {
37
39 : ConfigSection(instance, "socket", "Configuration of socket behavior") {
41 "--reuse-address{true}",
42 SOL_SOCKET,
43 SO_REUSEADDR,
44 "Reuse socket address",
45 "bool",
47 CLI::IsMember({"true", "false"}));
48
49 retryOpt = addFlag( //
50 "--retry{true}",
51 "Automatically retry listen|connect",
52 "bool",
53 XSTR(RETRY),
54 CLI::IsMember({"true", "false"}));
55
57 "--retry-on-fatal{true}",
58 [this]() {
59 if (retryOnFatalOpt->as<bool>() && !retryOpt->as<bool>()) {
61 }
62 },
63 "Retry also on fatal errors",
64 "bool",
66 CLI::IsMember({"true", "false"}));
68
70 "--retry-timeout",
71 "Timeout of the retry timer",
72 "sec",
76
78 "--retry-tries",
79 "Number of retry attempts before giving up (0 = unlimited)",
80 "tries",
82 CLI::TypeValidator<unsigned int>());
84
86 "--retry-base",
87 "Base of exponential backoff",
88 "base",
92
94 "--retry-jitter",
95 "Maximum jitter in percent to apply randomly to calculated retry timeout (0 to disable)",
96 "jitter",
98 CLI::Range(0., 100.));
100
102 "--retry-limit",
103 "Upper limit in seconds of retry timeout (0 for infinite)",
104 "sec",
108 }
109
113
115 int optLevel,
116 int optName,
117 const std::string& description,
118 const std::string& typeName,
119 const std::string& defaultValue,
120 const CLI::Validator& validator) {
121 return addFlagFunction(
122 name,
123 [this, strippedName = name.substr(0, name.find('{')), optLevel, optName]() {
124 try {
125 if (section->get_option(strippedName)->as<bool>()) {
127 } else {
129 }
130 } catch (CLI::OptionNotFound& err) {
131 LOG(ERROR) << err.what();
132 }
133 },
135 typeName,
137 validator);
138 }
139
145
151
157
163
165 if (reuseAddress) {
166 addSocketOption(SOL_SOCKET, SO_REUSEADDR, 1);
167 } else {
168 removeSocketOption(SO_REUSEADDR);
169 }
170
172 ->default_val(reuseAddress ? "true" : "false")
173 ->clear();
174
175 return *this;
176 }
177
179 return reuseAddressOpt->as<bool>();
180 }
181
205
207 return retryOpt->as<bool>();
208 }
209
212 ->default_val(retry ? "true" : "false")
213 ->clear();
214
215 return *this;
216 }
217
219 return retryOnFatalOpt->as<bool>();
220 }
221
225 ->clear();
226
227 return *this;
228 }
229
231 return retryTimeoutOpt->as<double>();
232 }
233
237 ->clear();
238
239 return *this;
240 }
241
242 unsigned int ConfigPhysicalSocket::getRetryTries() const {
243 return retryTriesOpt->as<unsigned int>();
244 }
245
247 retryBaseOpt //
249 ->clear();
250
251 return *this;
252 }
253
255 return retryBaseOpt->as<double>();
256 }
257
261 ->clear();
262
263 return *this;
264 }
265
266 unsigned int ConfigPhysicalSocket::getRetryLimit() const {
267 return retryLimitOpt->as<unsigned int>();
268 }
269
273 ->clear();
274
275 return *this;
276 }
277
279 return retryJitterOpt->as<double>();
280 }
281
282} // namespace net::config
#define XSTR(s)
#define STR(a)
Definition clients.h:26