userver: userver/engine/io/common.hpp Source File
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages Concepts
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#include <optional>
9
10#include <userver/engine/deadline.hpp>
11#include <userver/utils/assert.hpp>
12
13USERVER_NAMESPACE_BEGIN
14
15namespace engine::impl {
16class ContextAccessor;
17}
18
19namespace engine::io {
20
21/// File descriptor of an invalid pipe end.
22inline constexpr int kInvalidFd = -1;
23
24/// @ingroup userver_base_classes
25///
26/// Interface for readable streams
28public:
29 virtual ~ReadableBase();
30
31 /// Whether the stream is valid.
32 virtual bool IsValid() const = 0;
33
34 /// Suspends current task until the stream has data available.
35 [[nodiscard]] virtual bool WaitReadable(Deadline) = 0;
36
37 /// Receives up to len (including zero) bytes from the stream.
38 /// @returns filled-in optional on data presence (e.g. 0, 1, 2... bytes)
39 /// empty optional otherwise
40 [[nodiscard]] virtual std::optional<size_t> ReadNoblock(void* buf, size_t len) {
41 (void)buf;
42 (void)len;
43 UINVARIANT(false, "not implemented yet");
44 return {};
45 }
46
47 /// Receives at least one byte from the stream.
48 [[nodiscard]] virtual size_t ReadSome(void* buf, size_t len, Deadline deadline) = 0;
49
50 /// Receives exactly len bytes from the stream.
51 /// @note Can return less than len if stream is closed by peer.
52 [[nodiscard]] virtual size_t ReadAll(void* buf, size_t len, Deadline deadline) = 0;
53
54 /// For internal use only
55 impl::ContextAccessor* TryGetContextAccessor() { return ca_; }
56
57protected:
58 void SetReadableContextAccessor(impl::ContextAccessor* ca) { ca_ = ca; }
59
60private:
61 impl::ContextAccessor* ca_{nullptr};
62};
63
64/// IoData for vector send
65struct IoData final {
66 const void* data;
67 size_t len;
68};
69
70/// @ingroup userver_base_classes
71///
72/// Interface for writable streams
74public:
75 virtual ~WritableBase();
76
77 /// Suspends current task until the data is available.
78 [[nodiscard]] virtual bool WaitWriteable(Deadline deadline) = 0;
79
80 /// @brief Sends exactly len bytes of buf.
81 /// @note Can return less than len if stream is closed by peer.
82 [[nodiscard]] virtual size_t WriteAll(const void* buf, size_t len, Deadline deadline) = 0;
83
84 [[nodiscard]] virtual size_t WriteAll(std::initializer_list<IoData> list, Deadline deadline) {
85 size_t result{0};
86 for (const auto& io_data : list) {
87 result += WriteAll(io_data.data, io_data.len, deadline);
88 }
89 return result;
90 }
91
92 /// For internal use only
93 impl::ContextAccessor* TryGetContextAccessor() { return ca_; }
94
95protected:
96 void SetWritableContextAccessor(impl::ContextAccessor* ca) { ca_ = ca; }
97
98private:
99 impl::ContextAccessor* ca_{nullptr};
100};
101
102/// @ingroup userver_base_classes
103///
104/// Interface for readable and writable streams
105// NOLINTNEXTLINE(fuchsia-multiple-inheritance)
106class RwBase : public ReadableBase, public WritableBase {
107public:
108 ~RwBase() override;
109
110 ReadableBase& GetReadableBase() { return *this; }
111
112 WritableBase& GetWritableBase() { return *this; }
113};
114
115using ReadableBasePtr = std::shared_ptr<ReadableBase>;
116
117} // namespace engine::io
118
119USERVER_NAMESPACE_END