-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathHasSocketOperationsAsync.hpp
66 lines (58 loc) · 2.31 KB
/
HasSocketOperationsAsync.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#pragma once
#include <expected>
#include <concepts>
#include <span>
#include <cstddef>
#include <chrono>
#include <optional>
#include "ws_client/errors.hpp"
#include "ws_client/Buffer.hpp"
#include "ws_client/utils/Timeout.hpp"
namespace ws_client
{
using std::span;
using std::byte;
/**
* Concept for asynchronous socket template type parameters based on coroutines.
* Requires the socket to support reading and writing bytes to the underlying socket, and closing the socket.
* The functions MUST NOT throw exceptions, and instead return WSError object.
*/
template <typename T, template <typename...> typename TTask>
concept HasSocketOperationsAsync = requires(T t, span<byte> buffer, Timeout<>& timeout, bool fail_connection) {
/**
* Reads data from socket into `buffer`.
* Does not guarantee to fill buffer completely, partial reads are possible.
*
* @return The number of bytes read, or an error.
*/
{ t.read_some(buffer, timeout) } -> std::same_as<TTask<expected<size_t, WSError>>>;
/**
* Writes `buffer` to underlying socket.
* Does not guarantee to write complete `buffer` to socket, partial writes are possible.
*
* @return The number of bytes written, or an error.
*/
{ t.write_some(buffer, timeout) } -> std::same_as<TTask<expected<size_t, WSError>>>;
/**
* Shuts down socket communication.
* This function should be called before closing the socket
* for a clean shutdown.
* The return value in case of error may be ignored by the caller.
* Safe to call multiple times.
*
* @param fail_connection If `true`, the connection is failed immediately,
* e.g. in case of an error. If `false`, the connection
* is gracefully closed.
*/
{ t.shutdown(fail_connection, timeout) } -> std::same_as<TTask<expected<void, WSError>>>;
/**
* Close the socket connection and all associated resources.
* Safe to call multiple times.
*
* @param fail_connection If `true`, the connection is failed immediately,
* e.g. in case of an error. If `false`, the connection
* is gracefully closed.
*/
{ t.close(fail_connection) } -> std::same_as<TTask<expected<void, WSError>>>;
};
} // namespace ws_client