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