Skip to content

Commit fbc68b6

Browse files
committed
fpagliughi#97 Allow acceptors to choose the reuse option in the constructor
1 parent a2d786f commit fbc68b6

File tree

4 files changed

+57
-10
lines changed

4 files changed

+57
-10
lines changed

include/sockpp/acceptor.h

Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
// --------------------------------------------------------------------------
1212
// This file is part of the "sockpp" C++ socket library.
1313
//
14-
// Copyright (c) 2014-2023 Frank Pagliughi
14+
// Copyright (c) 2014-2024 Frank Pagliughi
1515
// All rights reserved.
1616
//
1717
// Redistribution and use in source and binary forms, with or without
@@ -108,7 +108,7 @@ class acceptor : public socket
108108
* address.
109109
* @param addr The address to which this server should be bound.
110110
* @param queSize The listener queue size.
111-
* @param reuse A reuse option for the socket. This can be SO_REUSE_ADDR
111+
* @param reuse A reuse option for the socket. This can be SO_REUSEADDR
112112
* or SO_REUSEPORT, and is set before it tries to bind. A
113113
* value of zero doesn;t set an option.
114114
* @throws std::system_error
@@ -127,6 +127,19 @@ class acceptor : public socket
127127
acceptor(const sock_address& addr, int queSize, error_code& ec) noexcept {
128128
ec = open(addr, queSize).error();
129129
}
130+
/**
131+
* Creates an acceptor socket and starts it listening to the specified
132+
* address.
133+
* @param addr The address to which this server should be bound.
134+
* @param queSize The listener queue size.
135+
* @param reuse A reuse option for the socket. This can be SO_REUSEADDR
136+
* or SO_REUSEPORT, and is set before it tries to bind. A
137+
* value of zero doesn;t set an option.
138+
* @param ec The error code, on failure
139+
*/
140+
acceptor(const sock_address& addr, int queSize, int reuse, error_code& ec) noexcept {
141+
ec = open(addr, queSize, reuse).error();
142+
}
130143
/**
131144
* Move constructor.
132145
* Creates an acceptor by moving the other acceptor to this one.
@@ -163,7 +176,7 @@ class acceptor : public socket
163176
* listening.
164177
* @param addr The address to which this server should be bound.
165178
* @param queSize The listener queue size.
166-
* @param reuse A reuse option for the socket. This can be SO_REUSE_ADDR
179+
* @param reuse A reuse option for the socket. This can be SO_REUSEADDR
167180
* or SO_REUSEPORT, and is set before it tries to bind. A
168181
* value of zero doesn;t set an option.
169182
* @return The error code on failure.
@@ -221,14 +234,26 @@ class acceptor_tmpl : public acceptor
221234
throw std::system_error{res.error()};
222235
}
223236
/**
224-
* Creates a acceptor and starts it listening on the specified address.
237+
* Creates an acceptor and starts it listening on the specified address.
225238
* @param addr The TCP address on which to listen.
226239
* @param queSize The listener queue size.
227240
* @param ec The error code, on failure
228241
*/
229242
acceptor_tmpl(const addr_t& addr, int queSize, error_code& ec) noexcept {
230243
ec = open(addr, queSize).error();
231244
}
245+
/**
246+
* Creates a acceptor and starts it listening on the specified address.
247+
* @param addr The TCP address on which to listen.
248+
* @param queSize The listener queue size.
249+
* @param reuse A reuse option for the socket. This can be SO_REUSEADDR
250+
* or SO_REUSEPORT, and is set before it tries to bind. A
251+
* value of zero doesn't set an option.
252+
* @param ec The error code, on failure
253+
*/
254+
acceptor_tmpl(const addr_t& addr, int queSize, int reuse, error_code& ec) noexcept {
255+
ec = open(addr, queSize, reuse).error();
256+
}
232257
/**
233258
* Creates a acceptor and starts it listening on the specified port.
234259
* The acceptor binds to the specified port for any address on the local
@@ -252,6 +277,20 @@ class acceptor_tmpl : public acceptor
252277
acceptor_tmpl(in_port_t port, int queSize, error_code& ec) noexcept {
253278
ec = open(port, queSize).error();
254279
}
280+
/**
281+
* Creates a acceptor and starts it listening on the specified port.
282+
* The acceptor binds to the specified port for any address on the local
283+
* host.
284+
* @param port The TCP port on which to listen.
285+
* @param queSize The listener queue size.
286+
* @param reuse A reuse option for the socket. This can be SO_REUSEADDR
287+
* or SO_REUSEPORT, and is set before it tries to bind. A
288+
* value of zero doesn't set an option.
289+
* @param ec The error code, on failure
290+
*/
291+
acceptor_tmpl(in_port_t port, int queSize, int reuse, error_code& ec) noexcept {
292+
ec = open(port, queSize, reuse).error();
293+
}
255294
/**
256295
* Move constructor.
257296
* Creates an acceptor by moving the other acceptor to this one.
@@ -290,7 +329,7 @@ class acceptor_tmpl : public acceptor
290329
* listening.
291330
* @param addr The address to which this server should be bound.
292331
* @param queSize The listener queue size.
293-
* @param reuse A reuse option for the socket. This can be SO_REUSE_ADDR
332+
* @param reuse A reuse option for the socket. This can be SO_REUSEADDR
294333
* or SO_REUSEPORT, and is set before it tries to bind. A
295334
* value of zero doesn;t set an option.
296335
* @return @em true on success, @em false on error
@@ -303,7 +342,7 @@ class acceptor_tmpl : public acceptor
303342
* listening.
304343
* @param port The TCP port on which to listen.
305344
* @param queSize The listener queue size.
306-
* @param reuse A reuse option for the socket. This can be SO_REUSE_ADDR
345+
* @param reuse A reuse option for the socket. This can be SO_REUSEADDR
307346
* or SO_REUSEPORT, and is set before it tries to bind. A
308347
* value of zero doesn;t set an option.
309348
* @return @em true on success, @em false on error

include/sockpp/socket.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,7 @@ class socket
384384
/**
385385
* Binds the socket to the specified address.
386386
* @param addr The address to which we get bound.
387-
* @param reuse A reuse option for the socket. This can be SO_REUSE_ADDR
387+
* @param reuse A reuse option for the socket. This can be SO_REUSEADDR
388388
* or SO_REUSEPORT if supported on the platform, and is set
389389
* before it tries to bind. A value of zero doesn't set any
390390
* option on the socket.

include/sockpp/tls/context.h

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,15 +167,23 @@ class tls_context_builder
167167
ctx_.set_verify(tls_context::verify_t::PEER);
168168
return *this;
169169
}
170-
170+
/**
171+
* Load the certificate chain from a file.
172+
* @param certFile The certificate chain file.
173+
* @return Error code on failure.
174+
*/
171175
auto cert_file(const string& certFile) -> self& {
172176
if (!ec_) {
173177
if (auto res = ctx_.set_cert_file(certFile); !res)
174178
ec_ = res.error();
175179
}
176180
return *this;
177181
}
178-
182+
/**
183+
* Set the private key from a file.
184+
* @param keyFile The private key file.
185+
* @return Error code on faliure.
186+
*/
179187
auto key_file(const string& keyFile) -> self& {
180188
if (!ec_) {
181189
if (auto res = ctx_.set_key_file(keyFile); !res)

include/sockpp/tls/openssl_context.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ class tls_context
174174
*/
175175
result<> set_cert_file(const string& certFile);
176176
/**
177-
* Set the private key file.
177+
* Set the private key from a file.
178178
* @param keyFile The private key file.
179179
* @return Error code on faliure.
180180
*/

0 commit comments

Comments
 (0)