userver: userver/engine/io/pipe.hpp Source File
Loading...
Searching...
No Matches
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 {
21public:
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, Deadline deadline) override;
30
31 /// Receives exactly len bytes from the pipe.
32 /// @note Can return less than len if pipe is closed by peer.
33 [[nodiscard]] size_t ReadAll(void* buf, size_t len, Deadline deadline) override;
34
35 /// File descriptor corresponding to the read end of the pipe.
36 int Fd() const;
37
38 /// Releases reading end file descriptor and invalidates it.
39 [[nodiscard]] int Release() noexcept;
40
41 /// Closes and invalidates the reading end of the pipe.
42 /// @warning You should not call Close with pending I/O. This may work okay
43 /// sometimes but it's loosely predictable.
44 void Close();
45
46private:
47 friend class Pipe;
48
49 PipeReader() = default;
50 explicit PipeReader(int fd);
51
52 impl::FdControlHolder fd_control_;
53};
54
55/// Writing end of an unidirectional pipe
56class PipeWriter final : public WritableBase {
57public:
58 /// Whether the writing end of the pipe is valid.
59 bool IsValid() const;
60
61 /// Suspends current task until the pipe can accept more data.
62 [[nodiscard]] bool WaitWriteable(Deadline) override;
63
64 /// Sends exactly len bytes to the pipe.
65 /// @note Can return less than len if pipe is closed by peer.
66 [[nodiscard]] size_t WriteAll(const void* buf, size_t len, Deadline deadline) override;
67
68 /// File descriptor corresponding to the write end of the pipe.
69 int Fd() const;
70
71 /// Releases writing end file descriptor and invalidates it.
72 [[nodiscard]] int Release() noexcept;
73
74 /// Closes and invalidates the writing end of the pipe.
75 /// @warning You should not call Close with pending I/O. This may work okay
76 /// sometimes but it's loosely predictable.
77 void Close();
78
79private:
80 friend class Pipe;
81
82 PipeWriter() = default;
83 explicit PipeWriter(int fd);
84
85 impl::FdControlHolder fd_control_;
86};
87
88/// Unidirectional pipe representation
89class Pipe final {
90public:
91 /// Constructs a unidirectional pipe
93
94 PipeReader reader;
95 PipeWriter writer;
96};
97
98} // namespace engine::io
99
100USERVER_NAMESPACE_END