uvw  1.10.0
handle.hpp
1 #pragma once
2 
3 
4 #include <cstddef>
5 #include <utility>
6 #include <memory>
7 #include <uv.h>
8 #include "resource.hpp"
9 #include "util.hpp"
10 
11 
12 namespace uvw {
13 
14 
20 struct CloseEvent {};
21 
22 
28 template<typename T, typename U>
29 class Handle: public BaseHandle, public Resource<T, U>
30 {
31  static void closeCallback(uv_handle_t *handle) {
32  Handle<T, U> &ref = *(static_cast<T*>(handle->data));
33  auto ptr = ref.shared_from_this();
34  (void)ptr;
35  ref.reset();
36  ref.publish(CloseEvent{});
37  }
38 
39 protected:
40  static void allocCallback(uv_handle_t *, std::size_t suggested, uv_buf_t *buf) {
41  auto size = static_cast<unsigned int>(suggested);
42  *buf = uv_buf_init(new char[size], size);
43  }
44 
45  template<typename F, typename... Args>
46  bool initialize(F &&f, Args&&... args) {
47  if(!this->self()) {
48  auto err = std::forward<F>(f)(this->parent(), this->get(), std::forward<Args>(args)...);
49 
50  if(err) {
51  this->publish(ErrorEvent{err});
52  } else {
53  this->leak();
54  }
55  }
56 
57  return this->self();
58  }
59 
60  template<typename F, typename... Args>
61  void invoke(F &&f, Args&&... args) {
62  auto err = std::forward<F>(f)(std::forward<Args>(args)...);
63  if(err) { Emitter<T>::publish(ErrorEvent{err}); }
64  }
65 
66 public:
68 
78  HandleCategory category() const noexcept override {
79  return HandleCategory{this->template get<uv_handle_t>()->type};
80  }
81 
91  HandleType type() const noexcept override {
92  return Utilities::guessHandle(category());
93  }
94 
114  bool active() const noexcept override {
115  return !(uv_is_active(this->template get<uv_handle_t>()) == 0);
116  }
117 
126  bool closing() const noexcept override {
127  return !(uv_is_closing(this->template get<uv_handle_t>()) == 0);
128  }
129 
139  void close() noexcept override {
140  if(!closing()) {
141  uv_close(this->template get<uv_handle_t>(), &Handle<T, U>::closeCallback);
142  }
143  }
144 
151  void reference() noexcept override {
152  uv_ref(this->template get<uv_handle_t>());
153  }
154 
161  void unreference() noexcept override {
162  uv_unref(this->template get<uv_handle_t>());
163  }
164 
169  bool referenced() const noexcept override {
170  return !(uv_has_ref(this->template get<uv_handle_t>()) == 0);
171  }
172 
177  std::size_t size() const noexcept {
178  return uv_handle_size(this->template get<uv_handle_t>()->type);
179  }
180 
193  int value = 0;
194  auto err = uv_send_buffer_size(this->template get<uv_handle_t>(), &value);
195  return err ? 0 : value;
196  }
197 
209  bool sendBufferSize(int value) {
210  return (0 == uv_send_buffer_size(this->template get<uv_handle_t>(), &value));
211  }
212 
225  int value = 0;
226  auto err = uv_recv_buffer_size(this->template get<uv_handle_t>(), &value);
227  return err ? 0 : value;
228  }
229 
241  bool recvBufferSize(int value) {
242  return (0 == uv_recv_buffer_size(this->template get<uv_handle_t>(), &value));
243  }
244 
268  uv_os_fd_t fd;
269  uv_fileno(this->template get<uv_handle_t>(), &fd);
270  return fd;
271  }
272 };
273 
274 
275 }
Event emitter base class.
Definition: emitter.hpp:87
std::size_t size() const noexcept
Returns the size of the underlying handle type.
Definition: handle.hpp:177
bool recvBufferSize(int value)
Sets the size of the receive buffer used for the socket.
Definition: handle.hpp:241
details::UVTypeWrapper< uv_os_fd_t > OSFileDescriptor
Definition: util.hpp:200
Handle base class.
Definition: handle.hpp:29
bool sendBufferSize(int value)
Sets the size of the send buffer used for the socket.
Definition: handle.hpp:209
HandleType type() const noexcept override
Gets the type of the handle.
Definition: handle.hpp:91
void close() noexcept override
Request handle to be closed.
Definition: handle.hpp:139
details::UVTypeWrapper< uv_handle_type > HandleCategory
Definition: util.hpp:197
CloseEvent event.
Definition: handle.hpp:20
The ErrorEvent event.
Definition: emitter.hpp:23
Common class for almost all the resources available in uvw.
Definition: resource.hpp:19
Untyped handle class.
Definition: loop.hpp:47
bool active() const noexcept override
Checks if the handle is active.
Definition: handle.hpp:114
static HandleType guessHandle(HandleCategory category) noexcept
Gets the type of the handle given a category.
Definition: util.hpp:552
HandleCategory category() const noexcept override
Gets the category of the handle.
Definition: handle.hpp:78
bool closing() const noexcept override
Checks if a handle is closing or closed.
Definition: handle.hpp:126
OSFileDescriptor fileno() const
Gets the platform dependent file descriptor equivalent.
Definition: handle.hpp:267
void unreference() noexcept override
Unreference the given handle.
Definition: handle.hpp:161
bool referenced() const noexcept override
Checks if the given handle referenced.
Definition: handle.hpp:169
int sendBufferSize()
Gets the size of the send buffer used for the socket.
Definition: handle.hpp:192
void reference() noexcept override
Reference the given handle.
Definition: handle.hpp:151
int recvBufferSize()
Gets the size of the receive buffer used for the socket.
Definition: handle.hpp:224
uvw default namespace.
Definition: async.hpp:11