Skip to content

Commit 004fdd9

Browse files
committed
Simplify signal_set API with result-based error handling
- Replace throwing/error_code overload pairs with system::result<void> - Replace multiple signal constructors with variadic template - Add error checking for signal() calls when restoring SIG_DFL - Fix signal validation to allow signal 0
1 parent 6a39d2d commit 004fdd9

7 files changed

Lines changed: 118 additions & 313 deletions

File tree

example/https-client/https_client.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,14 @@ run_client(
6363
// Connect to the server (throws on error)
6464
(co_await s.connect(corosio::endpoint(addr, port))).value();
6565

66+
// Configure TLS context
67+
corosio::tls::context ctx;
68+
ctx.set_hostname(hostname);
69+
ctx.set_default_verify_paths().value();
70+
ctx.set_verify_mode(corosio::tls::verify_mode::peer).value();
71+
6672
// Wrap socket in TLS stream
67-
corosio::wolfssl_stream secure(s);
73+
corosio::wolfssl_stream secure(s, ctx);
6874

6975
// Perform TLS handshake
7076
(co_await secure.handshake(corosio::wolfssl_stream::client)).value();

include/boost/corosio/signal_set.hpp

Lines changed: 23 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@
2020
#include <boost/capy/concept/io_awaitable.hpp>
2121
#include <boost/capy/concept/executor.hpp>
2222
#include <boost/system/error_code.hpp>
23+
#include <boost/system/result.hpp>
2324

25+
#include <concepts>
2426
#include <coroutine>
2527
#include <stop_token>
2628

@@ -73,15 +75,6 @@ class BOOST_COROSIO_DECL signal_set : public io_object
7375
return {ec_, signal_number_};
7476
}
7577

76-
template<typename Ex>
77-
auto await_suspend(
78-
std::coroutine_handle<> h,
79-
Ex const& ex) -> std::coroutine_handle<>
80-
{
81-
s_.get().wait(h, ex, token_, &ec_, &signal_number_);
82-
return std::noop_coroutine();
83-
}
84-
8578
template<typename Ex>
8679
auto await_suspend(
8780
std::coroutine_handle<> h,
@@ -104,61 +97,42 @@ class BOOST_COROSIO_DECL signal_set : public io_object
10497
system::error_code*,
10598
int*) = 0;
10699

107-
virtual system::error_code add(int signal_number) = 0;
108-
virtual system::error_code remove(int signal_number) = 0;
109-
virtual system::error_code clear() = 0;
100+
virtual system::result<void> add(int signal_number) = 0;
101+
virtual system::result<void> remove(int signal_number) = 0;
102+
virtual system::result<void> clear() = 0;
110103
virtual void cancel() = 0;
111104
};
112105

113-
public:
114106
/** Destructor.
115107
116108
Cancels any pending operations and releases signal resources.
117109
*/
118110
~signal_set();
119111

120-
/** Construct a signal set without adding any signals.
112+
/** Construct an empty signal set.
121113
122114
@param ctx The execution context that will own this signal set.
123115
*/
124116
explicit signal_set(capy::execution_context& ctx);
125117

126-
/** Construct a signal set and add one signal.
127-
128-
@param ctx The execution context that will own this signal set.
129-
@param signal_number_1 The signal number to be added.
130-
131-
@throws boost::system::system_error Thrown on failure.
132-
*/
133-
signal_set(capy::execution_context& ctx, int signal_number_1);
134-
135-
/** Construct a signal set and add two signals.
118+
/** Construct a signal set with initial signals.
136119
137120
@param ctx The execution context that will own this signal set.
138-
@param signal_number_1 The first signal number to be added.
139-
@param signal_number_2 The second signal number to be added.
121+
@param signal First signal number to add.
122+
@param signals Additional signal numbers to add.
140123
141124
@throws boost::system::system_error Thrown on failure.
142125
*/
126+
template<std::convertible_to<int>... Signals>
143127
signal_set(
144128
capy::execution_context& ctx,
145-
int signal_number_1,
146-
int signal_number_2);
147-
148-
/** Construct a signal set and add three signals.
149-
150-
@param ctx The execution context that will own this signal set.
151-
@param signal_number_1 The first signal number to be added.
152-
@param signal_number_2 The second signal number to be added.
153-
@param signal_number_3 The third signal number to be added.
154-
155-
@throws boost::system::system_error Thrown on failure.
156-
*/
157-
signal_set(
158-
capy::execution_context& ctx,
159-
int signal_number_1,
160-
int signal_number_2,
161-
int signal_number_3);
129+
int signal,
130+
Signals... signals)
131+
: signal_set(ctx)
132+
{
133+
add(signal).value();
134+
(add(signals).value(), ...);
135+
}
162136

163137
/** Move constructor.
164138
@@ -192,19 +166,9 @@ class BOOST_COROSIO_DECL signal_set : public io_object
192166
193167
@param signal_number The signal to be added to the set.
194168
195-
@throws boost::system::system_error Thrown on failure.
169+
@return Success, or an error if the signal could not be added.
196170
*/
197-
void add(int signal_number);
198-
199-
/** Add a signal to the signal set.
200-
201-
This function adds the specified signal to the set. It has no
202-
effect if the signal is already in the set.
203-
204-
@param signal_number The signal to be added to the set.
205-
@param ec Set to indicate what error occurred, if any.
206-
*/
207-
void add(int signal_number, system::error_code& ec);
171+
system::result<void> add(int signal_number);
208172

209173
/** Remove a signal from the signal set.
210174
@@ -213,37 +177,18 @@ class BOOST_COROSIO_DECL signal_set : public io_object
213177
214178
@param signal_number The signal to be removed from the set.
215179
216-
@throws boost::system::system_error Thrown on failure.
217-
*/
218-
void remove(int signal_number);
219-
220-
/** Remove a signal from the signal set.
221-
222-
This function removes the specified signal from the set. It has
223-
no effect if the signal is not in the set.
224-
225-
@param signal_number The signal to be removed from the set.
226-
@param ec Set to indicate what error occurred, if any.
227-
*/
228-
void remove(int signal_number, system::error_code& ec);
229-
230-
/** Remove all signals from the signal set.
231-
232-
This function removes all signals from the set. It has no effect
233-
if the set is already empty.
234-
235-
@throws boost::system::system_error Thrown on failure.
180+
@return Success, or an error if the signal could not be removed.
236181
*/
237-
void clear();
182+
system::result<void> remove(int signal_number);
238183

239184
/** Remove all signals from the signal set.
240185
241186
This function removes all signals from the set. It has no effect
242187
if the set is already empty.
243188
244-
@param ec Set to indicate what error occurred, if any.
189+
@return Success, or an error if resetting any signal handler fails.
245190
*/
246-
void clear(system::error_code& ec);
191+
system::result<void> clear();
247192

248193
/** Cancel all operations associated with the signal set.
249194

0 commit comments

Comments
 (0)