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