@@ -48,7 +48,8 @@ Socket/connection classes all are not copyable but moveable and templated to dis
48
48
### span<typename TYPE>
49
49
>#include "socketwrapper/span.hpp" (also included by all socket headers)
50
50
51
- Non owning abstraction of a view to memory used to generalize the interface to the reading and sending methods of the socket classes. Can be created from various container/array types.
51
+ Non owning abstraction of a view to memory used to generalize the interface to the reading and sending methods of the socket classes.
52
+ Can be created from all container types that can be represented by a pointer and a length.
52
53
The interface of the span class is the same as for most std container classes (providing begin(), end(), front(), back(), empty(), size(), get(), data()).
53
54
Methods:
54
55
- Constructor:
@@ -170,15 +171,12 @@ Methods:
170
171
// Default constructor of a not connected tcp connection
171
172
tcp_connection();
172
173
173
- // Construct a tcp connection that immediately connects to the remote in the constructor defined by the parameters.
174
- tcp_connection(const std::string_view remote_address, const uint16_t remote_port);
175
-
176
174
// Construct a tcp connection from a net::endpoint<IP_VER>
177
175
tcp_connection(const endpoint<IP_VER>& endpoint);
178
176
```
179
177
- Config:
180
178
```cpp
181
- // Connect a not connected socket to a given endpoint
179
+ // Connect a default constructed socket to a given endpoint
182
180
void connect(const endpoint<IP_VER>& endpoint);
183
181
```
184
182
- Reading:
@@ -192,6 +190,10 @@ Methods:
192
190
// Immediately return and call the callback function after there is data available.
193
191
void async_read(net::span<T> buffer, CALLBACK_TYPE&& callback) const;
194
192
193
+ // Immediately returns an awaitable that can be co_awaited in a C++20 coroutine
194
+ // Only available when compiling with C++20 or higher
195
+ net::op_awaitable<size_t, net::tcp_connection::stream_read_operation<T>> async_read(net::span<T> buffer) const;
196
+
195
197
// Immediately return and get a future to get the number of elements received at a later timepoint
196
198
std::future<size_t> promised_read(net::span<T> buffer) const;
197
199
```
@@ -203,6 +205,10 @@ Methods:
203
205
// Immediately returns and invokes the callback after all in the given buffer is send. Caller is responsible to keep the data the span shows alive.
204
206
void async_send(net::span<T> buffer, CALLBACK_TYPE&& callback) const;
205
207
208
+ // Immediately returns an awaitable that can be co_awaited in a C++20 coroutine
209
+ // Only available when compiling with C++20 or higher
210
+ net::op_awaitable<size_t, net::tcp_connection::stream_write_operation<T>> async_send(net::span<T> buffer) const;
211
+
206
212
// Immediately return and get a future to get the number of elements written at a later point in time
207
213
std::future<size_t> promised_send(net::span<T> buffer) const;
208
214
```
@@ -223,7 +229,7 @@ Methods:
223
229
tcp_acceptor();
224
230
225
231
// Immediately creates a socket that listens on the given address and port with a connection backlog of `backlog`
226
- tcp_acceptor(const std::string_view bind_addr, const uint16_t port , const size_t backlog = 5);
232
+ tcp_acceptor(const endpoint<IP_VER>& endpoint , const size_t backlog = 5);
227
233
```
228
234
- Config:
229
235
```cpp
@@ -241,6 +247,10 @@ Methods:
241
247
// Immediately returns and invokes the callback when a new connection is established
242
248
void async_accept(CALLBACK_TYPE&& callback) const;
243
249
250
+ // Immediately returns an awaitable that can be co_awaited in a C++20 coroutine
251
+ // Only available when compiling with C++20 or higher
252
+ net::op_awaitable<net::tcp_connection<IP_VER>, net::tcp_acceptor::stream_accept_operation> async_accept() const;
253
+
244
254
// Immediately return and get a future to access the accepted socket at a later point in time
245
255
std::future<net::tcp_connection<IP_VER>> promised_accept() const;
246
256
```
@@ -260,9 +270,6 @@ Methods:
260
270
// Construct a non connected tls connection
261
271
tls_connection(std::string_view cert_path, std::string_view key_path);
262
272
263
- // Construct a tls connection that immediately connects to the remote in the constructor defined by the parameters.
264
- tls_connection(std::string_view cert_path, std::string_view key_path, std::string_view conn_addr, uint16_t port);
265
-
266
273
// Construct a tls connection from an endpoint and immediately connect it
267
274
tls_connection(std::string_view cert_path, std::string_view key_path, const endpoint<IP_VER>& endpoint);
268
275
```
@@ -285,9 +292,6 @@ Methods:
285
292
// Construct a non-bound tls_acceptor
286
293
tls_acceptor(std::string_view cert_path, std::string_view key_path);
287
294
288
- // Construct a tls acceptor from address string and port and set it into listening state
289
- tls_acceptor(std::string_view cert_path, std::string_view key_path, std::string_view bind_addr, uint16_t port, size_t backlog = 5);
290
-
291
295
// Construct a tls acceptor from an endpoint and set it into listening state
292
296
tls_acceptor(std::string_view cert_path, std::string_view key_path, const endpoint<IP_VER>& endpoint);
293
297
```
@@ -309,9 +313,6 @@ Methods:
309
313
// Creates a non-bound UDP socket that is ready to send data but can not receive data.
310
314
udp_socket();
311
315
312
- // Creates a UDP socket that is bound to a given address and port so it can send and receive data after construction.
313
- udp_socket(const std::string_view bind_addr, const uint16_t port);
314
-
315
316
// Creates a UDP socket that is bound to a given endpoint so it can send and receive data directly after construction
316
317
udp_socket(const endpoint<IP_VER>& endpoint);
317
318
```
@@ -331,21 +332,26 @@ Methods:
331
332
// Immediately return and invoke the callback when data is read into the buffer. Caller is responsible to keep the underlying buffer alive.
332
333
void async_read(span<T> buffer, CALLBACK_TYPE&& callback) const;
333
334
335
+ // Immediately returns an awaitable that can be co_awaited in a C++20 coroutine
336
+ // Only available when compiling with C++20 or higher
337
+ net::op_awaitable<std::pair<size_t, std::optional<endpoint<IP_VER>>>, net::udp_socket::dgram_read_operation<T>> async_read(span<T> buffer) const;
338
+
334
339
// Immediately return and get a future to get the number of elements read and the connection info of the sender at a later point in time
335
340
std::future<std::pair<size_t, endpoint<IP_VER>>> promised_read(span<T> buffer) const;
336
341
```
337
342
- Writing:
338
343
```cpp
339
344
// Send all data in the given buffer to a remote endpoint.
340
- size_t send(const std::string_view addr, const uint16_t port, span<T>&& buffer) const;
341
345
size_t send(const endpoint<IP_VER>& endpoint_to, span<T> buffer) const;
342
346
343
347
// Immediately return and invoke the callback after the data is sent to a remote represented by the given address and port parameter.
344
- void async_send(const std::string_view addr, const uint16_t port, span<T>&& buffer, CALLBACK_TYPE&& callback) const;
345
348
void async_send(const endpoint<IP_VER>& endpoint_to, span<T> buffer, CALLBACK_TYPE&& callback) const;
346
349
350
+ // Immediately returns an awaitable that can be co_awaited in a C++20 coroutine
351
+ // Only available when compiling with C++20 or higher
352
+ net::op_awaitable<size_t, net::udp_socket::dgram_write_operation<T>> async_write(const endpoint<IP_VER>& endpoint_to, span<T> buffer) const;
353
+
347
354
// Immediately return and get a future to get the number of elements written at a later point in time
348
- std::future<size_t> promised_send(const std::string_view addr, const uint16_t port, span<T>&& buffer) const;
349
355
std::future<size_t> promised_send(const endpoint<IP_VER>& endpoint_to, span<T> buffer) const;
350
356
```
351
357
- Shorthand identifier:
@@ -354,6 +360,29 @@ Methods:
354
360
using udp_socket_v6 = udp_socket<net::ip_version::v6>;
355
361
```
356
362
363
+ ### task<return_type>
364
+ >#include "socketwrapper/task.hpp"
365
+
366
+ Representation of a lazily evaluated coroutine without any special functionality that holds a `std::coroutine_handle` of the parent coroutine frame.
367
+ It defines a `promise_type` and implements the `awaitable` which allows awaiting this type.
368
+ To start execution, this type needs to be awaited by a parent coroutine.
369
+ User can use `net::block_on()` or `net::to_future()` to transform a coroutine that returns a `net::task` into a synchronous function.
370
+ This is a helper class to give a user a coroutine class to utilize the networking functions that return an `net::op_awaitable`.
371
+ This class is only available when compiling with C++20 or higher
372
+
373
+ Example of a coroutine that returns `net::task`:
374
+ ```cpp
375
+ net::task<size_t> example(size_t input)
376
+ {
377
+ co_return input * 2;
378
+ }
379
+
380
+ net::task<void> example_two()
381
+ {
382
+ auto number = co_await example(5);
383
+ }
384
+ ```
385
+
357
386
## Utility Functions:
358
387
>#include "socketwrapper/utility.hpp"
359
388
@@ -378,12 +407,25 @@ All of the following functions live in the namespace `net`
378
407
inline constexpr T network_to_host(T in);
379
408
```
380
409
381
- ## Async helper functions:
382
- This functions are implicitly included with every socket class .
410
+ ## Runtime helper functions:
411
+ This functions are implicitly included with every socket header file .
383
412
384
413
- Run the asynchronous context until all callbacks are handled:
385
414
```cpp
386
- // Blocks until the asynchronous context runs out of registered callbacks .
415
+ // Blocks until all registered async operations are handled and all completion handlers finished execution .
387
416
void async_run();
388
417
```
389
-
418
+ - Block the current thread until the coroutine represented by the `net::task` parameter is completely evaluated.
419
+ Only available when compiling with C++20 or higher
420
+ ```cpp
421
+ template <typename return_type>
422
+ return_type block_on(net::task<return_type> awaitable_task);
423
+ ```
424
+ - Convert a lazily evaluated coroutine that is represented by `net::task` into an eagerly evaluated future.
425
+ By performing this conversion the task starts execution right away until it reaches its first suspension point while
426
+ the task itself would normally be suspended right away and only starts execution if it is awaited.
427
+ Only available when compiling with C++20 or higher
428
+ ```cpp
429
+ template <typename return_type>
430
+ std::future<return_type> spawn(net::task<return_type> awaitable_task);
431
+ ```
0 commit comments