WPILibC++ 2024.3.2
Pipe.h
Go to the documentation of this file.
1// Copyright (c) FIRST and other WPILib contributors.
2// Open Source Software; you can modify and/or share it under the terms of
3// the WPILib BSD license file in the root directory of this project.
4
5#ifndef WPINET_UV_PIPE_H_
6#define WPINET_UV_PIPE_H_
7
8#include <uv.h>
9
10#include <functional>
11#include <memory>
12#include <string>
13#include <string_view>
14
16
17namespace wpi::uv {
18
19class Loop;
20class PipeConnectReq;
21
22/**
23 * Pipe handle.
24 * Pipe handles provide an abstraction over local domain sockets on Unix and
25 * named pipes on Windows.
26 */
27class Pipe final : public NetworkStreamImpl<Pipe, uv_pipe_t> {
28 struct private_init {};
29
30 public:
31 explicit Pipe(const private_init&) {}
32 ~Pipe() noexcept override = default;
33
34 /**
35 * Create a pipe handle.
36 *
37 * @param loop Loop object where this handle runs.
38 * @param ipc Indicates if this pipe will be used for handle passing between
39 * processes.
40 */
41 static std::shared_ptr<Pipe> Create(Loop& loop, bool ipc = false);
42
43 /**
44 * Create a pipe handle.
45 *
46 * @param loop Loop object where this handle runs.
47 * @param ipc Indicates if this pipe will be used for handle passing between
48 * processes.
49 */
50 static std::shared_ptr<Pipe> Create(const std::shared_ptr<Loop>& loop,
51 bool ipc = false) {
52 return Create(*loop, ipc);
53 }
54
55 /**
56 * Reuse this handle. This closes the handle, and after the close completes,
57 * reinitializes it (identically to Create) and calls the provided callback.
58 * Unlike Close(), it does NOT emit the closed signal, however, IsClosing()
59 * will return true until the callback is called. This does nothing if
60 * IsClosing() is true (e.g. if Close() was called).
61 *
62 * @param ipc IPC
63 * @param callback Callback
64 */
65 void Reuse(std::function<void()> callback, bool ipc = false);
66
67 /**
68 * Accept incoming connection.
69 *
70 * This call is used in conjunction with `Listen()` to accept incoming
71 * connections. Call this function after receiving a ListenEvent event to
72 * accept the connection.
73 * An error signal will be emitted in case of errors.
74 *
75 * When the connection signal is emitted it is guaranteed that this
76 * function will complete successfully the first time. If you attempt to use
77 * it more than once, it may fail.
78 * It is suggested to only call this function once per connection signal.
79 *
80 * @return The stream handle for the accepted connection, or nullptr on error.
81 */
82 std::shared_ptr<Pipe> Accept();
83
84 /**
85 * Accept incoming connection.
86 *
87 * This call is used in conjunction with `Listen()` to accept incoming
88 * connections. Call this function after receiving a connection signal to
89 * accept the connection.
90 * An error signal will be emitted in case of errors.
91 *
92 * When the connection signal is emitted it is guaranteed that this
93 * function will complete successfully the first time. If you attempt to use
94 * it more than once, it may fail.
95 * It is suggested to only call this function once per connection signal.
96 *
97 * @param client Client stream object.
98 * @return False on error.
99 */
100 bool Accept(const std::shared_ptr<Pipe>& client) {
101 return NetworkStream::Accept(client);
102 }
103
104 /**
105 * Open an existing file descriptor or HANDLE as a pipe.
106 *
107 * @note The passed file descriptor or HANDLE is not checked for its type, but
108 * it's required that it represents a valid pipe.
109 *
110 * @param file A valid file handle (either a file descriptor or a HANDLE).
111 */
113
114 /**
115 * Bind the pipe to a file path (Unix) or a name (Windows).
116 *
117 * @note Paths on Unix get truncated to `sizeof(sockaddr_un.sun_path)` bytes,
118 * typically between 92 and 108 bytes.
119 *
120 * @param name File path (Unix) or name (Windows).
121 */
123
124 /**
125 * Connect to the Unix domain socket or the named pipe.
126 *
127 * @note Paths on Unix get truncated to `sizeof(sockaddr_un.sun_path)` bytes,
128 * typically between 92 and 108 bytes.
129 *
130 * HandleConnected() is called on the request when the connection has been
131 * established.
132 * HandleError() is called on the request in case of errors during the
133 * connection.
134 *
135 * @param name File path (Unix) or name (Windows).
136 * @param req connection request
137 */
139 const std::shared_ptr<PipeConnectReq>& req);
140
141 /**
142 * Connect to the Unix domain socket or the named pipe.
143 *
144 * @note Paths on Unix get truncated to `sizeof(sockaddr_un.sun_path)` bytes,
145 * typically between 92 and 108 bytes.
146 *
147 * The callback is called when the connection has been established. Errors
148 * are reported to the stream error handler.
149 *
150 * @param name File path (Unix) or name (Windows).
151 * @param callback Callback function to call when connection established
152 */
153 void Connect(std::string_view name, std::function<void()> callback);
154
155 /**
156 * Get the name of the Unix domain socket or the named pipe.
157 * @return The name (will be empty if an error occurred).
158 */
159 std::string GetSock();
160
161 /**
162 * Get the name of the Unix domain socket or the named pipe to which the
163 * handle is connected.
164 * @return The name (will be empty if an error occurred).
165 */
166 std::string GetPeer();
167
168 /**
169 * Set the number of pending pipe instance handles when the pipe server is
170 * waiting for connections.
171 * @note This setting applies to Windows only.
172 * @param count Number of pending handles.
173 */
176 }
177
178 /**
179 * Alters pipe permissions, allowing it to be accessed from processes run
180 * by different users. Makes the pipe writable or readable by all users.
181 * Mode can be UV_WRITABLE, UV_READABLE, or both. This function is blocking.
182 * @param flags chmod flags
183 */
185
186 private:
187 Pipe* DoAccept() override;
188
189 struct ReuseData {
190 std::function<void()> callback;
191 bool ipc;
192 };
193 std::unique_ptr<ReuseData> m_reuseData;
194};
195
196/**
197 * Pipe connection request.
198 */
200 public:
201 Pipe& GetStream() const {
202 return *static_cast<Pipe*>(&ConnectReq::GetStream());
203 }
204};
205
206} // namespace wpi::uv
207
208#endif // WPINET_UV_PIPE_H_
then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file
Definition: ThirdPartyNotices.txt:192
Connection request.
Definition: NetworkStream.h:25
NetworkStream & GetStream() const
Definition: NetworkStream.h:29
bool Invoke(F &&f, Args &&... args) const
Definition: Handle.h:267
Event loop.
Definition: Loop.h:37
std::shared_ptr< NetworkStream > Accept()
Accept incoming connection.
Definition: NetworkStream.h:92
Definition: NetworkStream.h:128
uv_pipe_t * GetRaw() const noexcept
Get the underlying handle data structure.
Definition: NetworkStream.h:143
Pipe connection request.
Definition: Pipe.h:199
Pipe & GetStream() const
Definition: Pipe.h:201
Pipe handle.
Definition: Pipe.h:27
void Open(uv_file file)
Open an existing file descriptor or HANDLE as a pipe.
Definition: Pipe.h:112
std::string GetSock()
Get the name of the Unix domain socket or the named pipe.
static std::shared_ptr< Pipe > Create(Loop &loop, bool ipc=false)
Create a pipe handle.
std::string GetPeer()
Get the name of the Unix domain socket or the named pipe to which the handle is connected.
Pipe(const private_init &)
Definition: Pipe.h:31
void Connect(std::string_view name, const std::shared_ptr< PipeConnectReq > &req)
Connect to the Unix domain socket or the named pipe.
~Pipe() noexcept override=default
void SetPendingInstances(int count)
Set the number of pending pipe instance handles when the pipe server is waiting for connections.
Definition: Pipe.h:174
void Bind(std::string_view name)
Bind the pipe to a file path (Unix) or a name (Windows).
std::shared_ptr< Pipe > Accept()
Accept incoming connection.
void Reuse(std::function< void()> callback, bool ipc=false)
Reuse this handle.
void Chmod(int flags)
Alters pipe permissions, allowing it to be accessed from processes run by different users.
Definition: Pipe.h:184
void Connect(std::string_view name, std::function< void()> callback)
Connect to the Unix domain socket or the named pipe.
bool Accept(const std::shared_ptr< Pipe > &client)
Accept incoming connection.
Definition: Pipe.h:100
basic_string_view< char > string_view
Definition: core.h:501
constexpr auto count() -> size_t
Definition: core.h:1203
Definition: array.h:89
Definition: WebSocket.h:27
flags
Definition: http_parser.h:206
int uv_file
Definition: unix.h:117
UV_EXTERN void uv_pipe_pending_instances(uv_pipe_t *handle, int count)
UV_EXTERN int uv_pipe_open(uv_pipe_t *, uv_file file)
UV_EXTERN int uv_pipe_chmod(uv_pipe_t *handle, int flags)