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
20
#
include
"ConfigPhysicalSocket.h"
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
36
namespace
net::
config
{
37
38
ConfigPhysicalSocket
::
ConfigPhysicalSocket
(
ConfigInstance
*
instance
)
39
:
ConfigSection
(
instance
,
"socket"
,
"Configuration of socket behavior"
) {
40
reuseAddressOpt
=
addSocketOption
(
//
41
"--reuse-address{true}"
,
42
SOL_SOCKET,
43
SO_REUSEADDR,
44
"Reuse socket address"
,
45
"bool"
,
46
XSTR
(
REUSE_ADDRESS
),
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
56
retryOnFatalOpt
=
addFlagFunction
(
//
57
"--retry-on-fatal{true}"
,
58
[
this
]() {
59
if
(
retryOnFatalOpt
->
as
<
bool
>() && !
retryOpt
->
as
<
bool
>()) {
60
throw
CLI
::
RequiresError
(
retryOnFatalOpt
->
get_name
(),
retryOpt
->
get_name
().
append
(
"=true"
));
61
}
62
},
63
"Retry also on fatal errors"
,
64
"bool"
,
65
XSTR
(
RETRY_ON_FATAL
),
66
CLI
::
IsMember
({
"true"
,
"false"
}));
67
retryOnFatalOpt
->
needs
(
retryOpt
);
68
69
retryTimeoutOpt
=
addOption
(
//
70
"--retry-timeout"
,
71
"Timeout of the retry timer"
,
72
"sec"
,
73
RETRY_TIMEOUT
,
74
CLI
::
NonNegativeNumber
);
75
retryTimeoutOpt
->
needs
(
retryOpt
);
76
77
retryTriesOpt
=
addOption
(
//
78
"--retry-tries"
,
79
"Number of retry attempts before giving up (0 = unlimited)"
,
80
"tries"
,
81
RETRY_TRIES
,
82
CLI
::
TypeValidator
<
unsigned
int
>());
83
retryTriesOpt
->
needs
(
retryOpt
);
84
85
retryBaseOpt
=
addOption
(
//
86
"--retry-base"
,
87
"Base of exponential backoff"
,
88
"base"
,
89
RETRY_BASE
,
90
CLI
::
PositiveNumber
);
91
retryBaseOpt
->
needs
(
retryOpt
);
92
93
retryJitterOpt
=
addOption
(
//
94
"--retry-jitter"
,
95
"Maximum jitter in percent to apply randomly to calculated retry timeout (0 to disable)"
,
96
"jitter"
,
97
RETRY_JITTER
,
98
CLI
::
Range
(0., 100.));
99
retryJitterOpt
->
needs
(
retryOpt
);
100
101
retryLimitOpt
=
addOption
(
//
102
"--retry-limit"
,
103
"Upper limit in seconds of retry timeout (0 for infinite)"
,
104
"sec"
,
105
RETRY_LIMIT
,
106
CLI
::
NonNegativeNumber
);
107
retryLimitOpt
->
needs
(
retryOpt
);
108
}
109
110
const
std
::
map
<
int
,
const
net
::
phy
::
PhysicalSocketOption
>&
ConfigPhysicalSocket
::
getSocketOptions
() {
111
return
socketOptionsMap
;
112
}
113
114
CLI
::
Option
*
ConfigPhysicalSocket
::
addSocketOption
(
const
std
::
string
&
name
,
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
>()) {
126
socketOptionsMap
.
emplace
(
optName
,
net
::
phy
::
PhysicalSocketOption
(
optLevel
,
optName
, 1));
127
}
else
{
128
socketOptionsMap
.
erase
(
optName
);
129
}
130
}
catch
(
CLI
::
OptionNotFound
&
err
) {
131
LOG(
ERROR
) <<
err
.
what
();
132
}
133
},
134
description
,
135
typeName
,
136
defaultValue
,
137
validator
);
138
}
139
140
ConfigPhysicalSocket
&
ConfigPhysicalSocket
::
addSocketOption
(
int
optLevel
,
int
optName
,
int
optValue
) {
141
socketOptionsMap
.
emplace
(
optName
,
net
::
phy
::
PhysicalSocketOption
(
optLevel
,
optName
,
optValue
));
142
143
return
*
this
;
144
}
145
146
ConfigPhysicalSocket
&
ConfigPhysicalSocket
::
addSocketOption
(
int
optLevel
,
int
optName
,
const
std
::
string
&
optValue
) {
147
socketOptionsMap
.
emplace
(
optName
,
net
::
phy
::
PhysicalSocketOption
(
optLevel
,
optName
,
optValue
));
148
149
return
*
this
;
150
}
151
152
ConfigPhysicalSocket
&
ConfigPhysicalSocket
::
addSocketOption
(
int
optLevel
,
int
optName
,
const
std
::
vector
<
char
>&
optValue
) {
153
socketOptionsMap
.
emplace
(
optName
,
net
::
phy
::
PhysicalSocketOption
(
optLevel
,
optName
,
optValue
));
154
155
return
*
this
;
156
}
157
158
ConfigPhysicalSocket
&
ConfigPhysicalSocket
::
removeSocketOption
(
int
optName
) {
159
socketOptionsMap
.
erase
(
optName
);
160
161
return
*
this
;
162
}
163
164
ConfigPhysicalSocket
&
ConfigPhysicalSocket
::
setReuseAddress
(
bool
reuseAddress
) {
165
if
(
reuseAddress
) {
166
addSocketOption
(SOL_SOCKET, SO_REUSEADDR, 1);
167
}
else
{
168
removeSocketOption
(SO_REUSEADDR);
169
}
170
171
reuseAddressOpt
//
172
->
default_val
(
reuseAddress
?
"true"
:
"false"
)
173
->
clear
();
174
175
return
*
this
;
176
}
177
178
bool
ConfigPhysicalSocket
::
getReuseAddress
()
const
{
179
return
reuseAddressOpt
->
as
<
bool
>();
180
}
181
182
ConfigPhysicalSocket
&
ConfigPhysicalSocket
::
setRetry
(
bool
retry
) {
183
retryOpt
//
184
->
default_val
(
retry
?
"true"
:
"false"
)
185
->
clear
();
186
187
if
(
retry
) {
188
retryLimitOpt
->
remove_needs
(
retryOpt
);
189
retryJitterOpt
->
remove_needs
(
retryOpt
);
190
retryBaseOpt
->
remove_needs
(
retryOpt
);
191
retryTriesOpt
->
remove_needs
(
retryOpt
);
192
retryTimeoutOpt
->
remove_needs
(
retryOpt
);
193
retryOnFatalOpt
->
remove_needs
(
retryOpt
);
194
}
else
{
195
retryLimitOpt
->
needs
(
retryOpt
);
196
retryJitterOpt
->
needs
(
retryOpt
);
197
retryBaseOpt
->
needs
(
retryOpt
);
198
retryTriesOpt
->
needs
(
retryOpt
);
199
retryTimeoutOpt
->
needs
(
retryOpt
);
200
retryOnFatalOpt
->
needs
(
retryOpt
);
201
}
202
203
return
*
this
;
204
}
205
206
bool
ConfigPhysicalSocket
::
getRetry
()
const
{
207
return
retryOpt
->
as
<
bool
>();
208
}
209
210
ConfigPhysicalSocket
&
ConfigPhysicalSocket
::
setRetryOnFatal
(
bool
retry
) {
211
retryOnFatalOpt
//
212
->
default_val
(
retry
?
"true"
:
"false"
)
213
->
clear
();
214
215
return
*
this
;
216
}
217
218
bool
ConfigPhysicalSocket
::
getRetryOnFatal
()
const
{
219
return
retryOnFatalOpt
->
as
<
bool
>();
220
}
221
222
ConfigPhysicalSocket
&
ConfigPhysicalSocket
::
setRetryTimeout
(
double
sec
) {
223
retryTimeoutOpt
//
224
->
default_val
(
sec
)
225
->
clear
();
226
227
return
*
this
;
228
}
229
230
double
ConfigPhysicalSocket
::
getRetryTimeout
()
const
{
231
return
retryTimeoutOpt
->
as
<
double
>();
232
}
233
234
ConfigPhysicalSocket
&
ConfigPhysicalSocket
::
setRetryTries
(
unsigned
int
tries
) {
235
retryTriesOpt
//
236
->
default_val
(
tries
)
237
->
clear
();
238
239
return
*
this
;
240
}
241
242
unsigned
int
ConfigPhysicalSocket
::
getRetryTries
()
const
{
243
return
retryTriesOpt
->
as
<
unsigned
int
>();
244
}
245
246
ConfigPhysicalSocket
&
ConfigPhysicalSocket
::
setRetryBase
(
double
base
) {
247
retryBaseOpt
//
248
->
default_val
(
base
)
249
->
clear
();
250
251
return
*
this
;
252
}
253
254
double
ConfigPhysicalSocket
::
getRetryBase
()
const
{
255
return
retryBaseOpt
->
as
<
double
>();
256
}
257
258
ConfigPhysicalSocket
&
ConfigPhysicalSocket
::
setRetryLimit
(
unsigned
int
limit
) {
259
retryLimitOpt
//
260
->
default_val
(
limit
)
261
->
clear
();
262
263
return
*
this
;
264
}
265
266
unsigned
int
ConfigPhysicalSocket
::
getRetryLimit
()
const
{
267
return
retryLimitOpt
->
as
<
unsigned
int
>();
268
}
269
270
ConfigPhysicalSocket
&
ConfigPhysicalSocket
::
setRetryJitter
(
double
percent
) {
271
retryJitterOpt
//
272
->
default_val
(
percent
)
273
->
clear
();
274
275
return
*
this
;
276
}
277
278
double
ConfigPhysicalSocket
::
getRetryJitter
()
const
{
279
return
retryJitterOpt
->
as
<
double
>();
280
}
281
282
}
// namespace net::config
XSTR
#define XSTR(s)
Definition
ConfigPhysicalSocket.cpp:33
STR
#define STR(a)
Definition
clients.h:26
net::config
Definition
ConfigSection.hpp:43
net
config
ConfigPhysicalSocket.cpp
Generated on Mon Feb 10 2025 20:21:23 for SNode.C by
1.11.0