WPILibC++  2020.3.2
Pipe.h
1 /*----------------------------------------------------------------------------*/
2 /* Copyright (c) 2018-2019 FIRST. All Rights Reserved. */
3 /* Open Source Software - may be modified and shared by FRC teams. The code */
4 /* must be accompanied by the FIRST BSD license file in the root directory of */
5 /* the project. */
6 /*----------------------------------------------------------------------------*/
7 
8 #ifndef WPIUTIL_WPI_UV_PIPE_H_
9 #define WPIUTIL_WPI_UV_PIPE_H_
10 
11 #include <uv.h>
12 
13 #include <functional>
14 #include <memory>
15 #include <string>
16 
17 #include "wpi/Twine.h"
18 #include "wpi/uv/NetworkStream.h"
19 
20 namespace wpi {
21 namespace uv {
22 
23 class Loop;
24 class PipeConnectReq;
25 
31 class Pipe final : public NetworkStreamImpl<Pipe, uv_pipe_t> {
32  struct private_init {};
33 
34  public:
35  explicit Pipe(const private_init&) {}
36  ~Pipe() noexcept override = default;
37 
45  static std::shared_ptr<Pipe> Create(Loop& loop, bool ipc = false);
46 
54  static std::shared_ptr<Pipe> Create(const std::shared_ptr<Loop>& loop,
55  bool ipc = false) {
56  return Create(*loop, ipc);
57  }
58 
69  void Reuse(std::function<void()> callback, bool ipc = false);
70 
86  std::shared_ptr<Pipe> Accept();
87 
104  bool Accept(const std::shared_ptr<Pipe>& client) {
105  return NetworkStream::Accept(client);
106  }
107 
116  void Open(uv_file file) { Invoke(&uv_pipe_open, GetRaw(), file); }
117 
126  void Bind(const Twine& name);
127 
142  void Connect(const Twine& name, const std::shared_ptr<PipeConnectReq>& req);
143 
156  void Connect(const Twine& name, std::function<void()> callback);
157 
162  std::string GetSock();
163 
169  std::string GetPeer();
170 
178  uv_pipe_pending_instances(GetRaw(), count);
179  }
180 
187  void Chmod(int flags) { Invoke(&uv_pipe_chmod, GetRaw(), flags); }
188 
189  private:
190  Pipe* DoAccept() override;
191 
192  struct ReuseData {
193  std::function<void()> callback;
194  bool ipc;
195  };
196  std::unique_ptr<ReuseData> m_reuseData;
197 };
198 
202 class PipeConnectReq : public ConnectReq {
203  public:
204  Pipe& GetStream() const {
205  return *static_cast<Pipe*>(&ConnectReq::GetStream());
206  }
207 };
208 
209 } // namespace uv
210 } // namespace wpi
211 
212 #endif // WPIUTIL_WPI_UV_PIPE_H_
wpi::uv::Pipe::Open
void Open(uv_file file)
Open an existing file descriptor or HANDLE as a pipe.
Definition: Pipe.h:116
wpi::uv::Pipe::Chmod
void Chmod(int flags)
Alters pipe permissions, allowing it to be accessed from processes run by different users.
Definition: Pipe.h:187
wpi::uv::ConnectReq
Connection request.
Definition: NetworkStream.h:27
wpi::uv::Pipe::Create
static std::shared_ptr< Pipe > Create(const std::shared_ptr< Loop > &loop, bool ipc=false)
Create a pipe handle.
Definition: Pipe.h:54
wpi::uv::Pipe::Create
static std::shared_ptr< Pipe > Create(Loop &loop, bool ipc=false)
Create a pipe handle.
wpi::uv::PipeConnectReq
Pipe connection request.
Definition: Pipe.h:202
wpi::uv::NetworkStreamImpl
Definition: NetworkStream.h:130
wpi
WPILib C++ utilities (wpiutil) namespace.
Definition: EventLoopRunner.h:17
wpi::uv::NetworkStream::Accept
std::shared_ptr< NetworkStream > Accept()
Accept incoming connection.
Definition: NetworkStream.h:94
wpi::uv::Pipe::Connect
void Connect(const Twine &name, const std::shared_ptr< PipeConnectReq > &req)
Connect to the Unix domain socket or the named pipe.
wpi::uv::Pipe::GetSock
std::string GetSock()
Get the name of the Unix domain socket or the named pipe.
wpi::uv::Pipe::GetPeer
std::string GetPeer()
Get the name of the Unix domain socket or the named pipe to which the handle is connected.
wpi::uv::Pipe::Reuse
void Reuse(std::function< void()> callback, bool ipc=false)
Reuse this handle.
wpi::uv::NetworkStreamImpl< Pipe, uv_pipe_t >::GetRaw
uv_pipe_t * GetRaw() const noexcept
Get the underlying handle data structure.
Definition: NetworkStream.h:145
wpi::uv::Pipe
Pipe handle.
Definition: Pipe.h:31
wpi::uv::Pipe::SetPendingInstances
void SetPendingInstances(int count)
Set the number of pending pipe instance handles when the pipe server is waiting for connections.
Definition: Pipe.h:177
wpi::uv::Pipe::Accept
std::shared_ptr< Pipe > Accept()
Accept incoming connection.
wpi::uv::Pipe::Bind
void Bind(const Twine &name)
Bind the pipe to a file path (Unix) or a name (Windows).
wpi::uv::Loop
Event loop.
Definition: Loop.h:39
wpi::count
auto count(R &&Range, const E &Element) -> typename std::iterator_traits< decltype(adl_begin(Range))>::difference_type
Wrapper function around std::count to count the number of times an element Element occurs in the give...
Definition: STLExtras.h:1092
wpi::uv::Pipe::Accept
bool Accept(const std::shared_ptr< Pipe > &client)
Accept incoming connection.
Definition: Pipe.h:104
wpi::Twine
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:85