userver
C++ Async Framework
Loading...
Searching...
No Matches
common.hpp
Go to the documentation of this file.
1
#
pragma
once
2
3
/// @file userver/engine/io/common.hpp
4
/// @brief Common definitions and base classes for stream like objects
5
6
#
include
<
cstddef
>
7
#
include
<
memory
>
8
9
#
include
<
userver
/
engine
/
deadline
.
hpp
>
10
11
USERVER_NAMESPACE_BEGIN
12
13
namespace
engine::
io
{
14
15
/// File descriptor of an invalid pipe end.
16
inline
constexpr
int
kInvalidFd
= -1;
17
18
/// @ingroup userver_base_classes
19
///
20
/// Interface for readable streams
21
class
ReadableBase
{
22
public
:
23
virtual
~ReadableBase();
24
25
/// Whether the stream is valid.
26
virtual
bool
IsValid
()
const
= 0;
27
28
/// Suspends current task until the stream has data available.
29
[[
nodiscard
]]
virtual
bool
WaitReadable
(Deadline) = 0;
30
31
/// Receives at least one byte from the stream.
32
[[
nodiscard
]]
virtual
size_t
ReadSome
(
void
*
buf
,
size_t
len
,
33
Deadline
deadline
) = 0;
34
35
/// Receives exactly len bytes from the stream.
36
/// @note Can return less than len if stream is closed by peer.
37
[[
nodiscard
]]
virtual
size_t
ReadAll
(
void
*
buf
,
size_t
len
,
38
Deadline
deadline
) = 0;
39
};
40
41
/// IoData for vector send
42
struct
IoData
final
{
43
const
void
* data;
44
size_t len;
45
};
46
47
/// @ingroup userver_base_classes
48
///
49
/// Interface for writable streams
50
class
WritableBase
{
51
public
:
52
virtual
~WritableBase();
53
54
/// Suspends current task until the data is available.
55
[[
nodiscard
]]
virtual
bool
WaitWriteable
(Deadline deadline) = 0;
56
57
/// @brief Sends exactly len bytes of buf.
58
/// @note Can return less than len if stream is closed by peer.
59
[[
nodiscard
]]
virtual
size_t
WriteAll
(
const
void
*
buf
,
size_t
len
,
60
Deadline
deadline
) = 0;
61
62
[[
nodiscard
]]
virtual
size_t WriteAll(std::initializer_list<IoData> list,
63
Deadline deadline) {
64
size_t result{0};
65
for
(
const
auto
& io_data : list) {
66
result += WriteAll(io_data.data, io_data.len, deadline);
67
}
68
return
result;
69
}
70
};
71
72
/// @ingroup userver_base_classes
73
///
74
/// Interface for readable and writable streams
75
// NOLINTNEXTLINE(fuchsia-multiple-inheritance)
76
class
RwBase
:
public
ReadableBase
,
public
WritableBase
{
77
public
:
78
~RwBase()
override
;
79
};
80
81
using
ReadableBasePtr = std::shared_ptr<
ReadableBase
>;
82
83
}
// namespace engine::io
84
85
USERVER_NAMESPACE_END
userver
engine
io
common.hpp
Generated on Thu Sep 21 2023 07:52:11 for userver by
Doxygen
1.9.8