uvw  1.3.0
pipe.hpp
1 #pragma once
2 
3 
4 #include <type_traits>
5 #include <utility>
6 #include <memory>
7 #include <string>
8 #include <uv.h>
9 #include "request.hpp"
10 #include "stream.hpp"
11 #include "util.hpp"
12 #include "loop.hpp"
13 
14 
15 namespace uvw {
16 
17 
29 class PipeHandle final: public StreamHandle<PipeHandle, uv_pipe_t> {
30 public:
31  explicit PipeHandle(ConstructorAccess ca, std::shared_ptr<Loop> ref, bool pass = false)
32  : StreamHandle{ca, std::move(ref)}, ipc{pass}
33  {}
34 
39  bool init() {
40  return initialize(&uv_pipe_init, ipc);
41  }
42 
52  void open(FileHandle file) {
53  invoke(&uv_pipe_open, get(), file);
54  }
55 
64  void bind(std::string name) {
65  invoke(&uv_pipe_bind, get(), name.data());
66  }
67 
78  void connect(std::string name) {
79  auto listener = [ptr = shared_from_this()](const auto &event, const auto &) {
80  ptr->publish(event);
81  };
82 
83  auto connect = loop().resource<details::ConnectReq>();
84  connect->once<ErrorEvent>(listener);
85  connect->once<ConnectEvent>(listener);
86  connect->connect(&uv_pipe_connect, get(), name.data());
87  }
88 
94  std::string sock() const noexcept {
95  return details::tryRead(&uv_pipe_getsockname, get());
96  }
97 
104  std::string peer() const noexcept {
105  return details::tryRead(&uv_pipe_getpeername, get());
106  }
107 
117  void pending(int count) noexcept {
118  uv_pipe_pending_instances(get(), count);
119  }
120 
125  int pending() noexcept {
126  return uv_pipe_pending_count(get());
127  }
128 
145  HandleType receive() noexcept {
146  HandleCategory category = uv_pipe_pending_type(get());
147  return Utilities::guessHandle(category);
148  }
149 
150 private:
151  bool ipc;
152 };
153 
154 
155 }
std::enable_if_t< std::is_base_of< BaseHandle, R >::value, std::shared_ptr< R > > resource(Args &&... args)
Creates resources of handles&#39; types.
Definition: loop.hpp:250
std::string sock() const noexcept
Gets the name of the Unix domain socket or the named pipe.
Definition: pipe.hpp:94
details::UVTypeWrapper< uv_file > FileHandle
Definition: util.hpp:186
int pending() noexcept
Gets the number of pending pipe this instance can handle.
Definition: pipe.hpp:125
ConnectEvent event.
Definition: stream.hpp:23
void bind(std::string name)
bind Binds the pipe to a file path (Unix) or a name (Windows).
Definition: pipe.hpp:64
The StreamHandle handle.
Definition: stream.hpp:130
details::UVTypeWrapper< uv_handle_type > HandleCategory
Definition: util.hpp:185
HandleType receive() noexcept
Used to receive handles over IPC pipes.
Definition: pipe.hpp:145
void pending(int count) noexcept
Sets the number of pending pipe this instance can handle.
Definition: pipe.hpp:117
The PipeHandle handle.
Definition: pipe.hpp:29
The ErrorEvent event.
Definition: emitter.hpp:23
static HandleType guessHandle(HandleCategory category) noexcept
Gets the type of the handle given a category.
Definition: util.hpp:513
std::string peer() const noexcept
Gets the name of the Unix domain socket or the named pipe to which the handle is connected.
Definition: pipe.hpp:104
HandleCategory category() const noexcept override
Gets the category of the handle.
Definition: handle.hpp:78
bool init()
Initializes the handle.
Definition: pipe.hpp:39
void open(FileHandle file)
Opens an existing file descriptor or HANDLE as a pipe.
Definition: pipe.hpp:52
Loop & loop() const noexcept
Gets the loop from which the resource was originated.
void connect(std::string name)
Connects to the Unix domain socket or the named pipe.
Definition: pipe.hpp:78
uvw default namespace.
Definition: async.hpp:11