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