userver: userver/engine/io/pipe.hpp Source File
⚠️ This is the documentation for an old userver version. Click here to switch to the latest version.
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages Concepts
pipe.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file userver/engine/io/pipe.hpp
4/// @brief @copybrief engine::io::Pipe
5
6#include <userver/engine/deadline.hpp>
7#include <userver/engine/io/common.hpp>
8#include <userver/engine/io/exception.hpp>
9#include <userver/engine/io/fd_control_holder.hpp>
10
11USERVER_NAMESPACE_BEGIN
12
13namespace engine::io {
14
15namespace impl {
16class FdControl;
17} // namespace impl
18
19/// Reading end of an unidirectional pipe
20class PipeReader final : public ReadableBase {
21 public:
22 /// Whether the reading end of the pipe is valid.
23 bool IsValid() const override;
24
25 /// Suspends current task until the pipe has data available.
26 [[nodiscard]] bool WaitReadable(Deadline) override;
27
28 /// Receives at least one bytes from the pipe.
29 [[nodiscard]] size_t ReadSome(void* buf, size_t len,
30 Deadline deadline) override;
31
32 /// Receives exactly len bytes from the pipe.
33 /// @note Can return less than len if pipe is closed by peer.
34 [[nodiscard]] size_t ReadAll(void* buf, size_t len,
35 Deadline deadline) override;
36
37 /// File descriptor corresponding to the read end of the pipe.
38 int Fd() const;
39
40 /// Releases reading end file descriptor and invalidates it.
41 [[nodiscard]] int Release() noexcept;
42
43 /// Closes and invalidates the reading end of the pipe.
44 /// @warning You should not call Close with pending I/O. This may work okay
45 /// sometimes but it's loosely predictable.
46 void Close();
47
48 private:
49 friend class Pipe;
50
51 PipeReader() = default;
52 explicit PipeReader(int fd);
53
54 impl::FdControlHolder fd_control_;
55};
56
57/// Writing end of an unidirectional pipe
58class PipeWriter final : public WritableBase {
59 public:
60 /// Whether the writing end of the pipe is valid.
61 bool IsValid() const;
62
63 /// Suspends current task until the pipe can accept more data.
64 [[nodiscard]] bool WaitWriteable(Deadline) override;
65
66 /// Sends exactly len bytes to the pipe.
67 /// @note Can return less than len if pipe is closed by peer.
68 [[nodiscard]] size_t WriteAll(const void* buf, size_t len,
69 Deadline deadline) override;
70
71 /// File descriptor corresponding to the write end of the pipe.
72 int Fd() const;
73
74 /// Releases writing end file descriptor and invalidates it.
75 [[nodiscard]] int Release() noexcept;
76
77 /// Closes and invalidates the writing end of the pipe.
78 /// @warning You should not call Close with pending I/O. This may work okay
79 /// sometimes but it's loosely predictable.
80 void Close();
81
82 private:
83 friend class Pipe;
84
85 PipeWriter() = default;
86 explicit PipeWriter(int fd);
87
88 impl::FdControlHolder fd_control_;
89};
90
91/// Unidirectional pipe representation
92class Pipe final {
93 public:
94 /// Constructs a unidirectional pipe
96
97 PipeReader reader;
98 PipeWriter writer;
99};
100
101} // namespace engine::io
102
103USERVER_NAMESPACE_END