uvw  2.2.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 protected:
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  static void allocCallback(uv_handle_t *, std::size_t suggested, uv_buf_t *buf) {
40  auto size = static_cast<unsigned int>(suggested);
41  *buf = uv_buf_init(new char[size], size);
42  }
43 
44  template<typename F, typename... Args>
45  bool initialize(F &&f, Args&&... args) {
46  if(!this->self()) {
47  auto err = std::forward<F>(f)(this->parent(), this->get(), std::forward<Args>(args)...);
48 
49  if(err) {
50  this->publish(ErrorEvent{err});
51  } else {
52  this->leak();
53  }
54  }
55 
56  return this->self();
57  }
58 
59  template<typename F, typename... Args>
60  void invoke(F &&f, Args&&... args) {
61  auto err = std::forward<F>(f)(std::forward<Args>(args)...);
62  if(err) { Emitter<T>::publish(ErrorEvent{err}); }
63  }
64 
65 public:
67 
77  HandleCategory category() const noexcept override {
78  return HandleCategory{this->template get<uv_handle_t>()->type};
79  }
80 
90  HandleType type() const noexcept override {
91  return Utilities::guessHandle(category());
92  }
93 
113  bool active() const noexcept override {
114  return !(uv_is_active(this->template get<uv_handle_t>()) == 0);
115  }
116 
125  bool closing() const noexcept override {
126  return !(uv_is_closing(this->template get<uv_handle_t>()) == 0);
127  }
128 
138  void close() noexcept override {
139  if(!closing()) {
140  uv_close(this->template get<uv_handle_t>(), &Handle<T, U>::closeCallback);
141  }
142  }
143 
150  void reference() noexcept override {
151  uv_ref(this->template get<uv_handle_t>());
152  }
153 
160  void unreference() noexcept override {
161  uv_unref(this->template get<uv_handle_t>());
162  }
163 
168  bool referenced() const noexcept override {
169  return !(uv_has_ref(this->template get<uv_handle_t>()) == 0);
170  }
171 
176  std::size_t size() const noexcept {
177  return uv_handle_size(this->template get<uv_handle_t>()->type);
178  }
179 
192  int value = 0;
193  auto err = uv_send_buffer_size(this->template get<uv_handle_t>(), &value);
194  return err ? 0 : value;
195  }
196 
208  bool sendBufferSize(int value) {
209  return (0 == uv_send_buffer_size(this->template get<uv_handle_t>(), &value));
210  }
211 
224  int value = 0;
225  auto err = uv_recv_buffer_size(this->template get<uv_handle_t>(), &value);
226  return err ? 0 : value;
227  }
228 
240  bool recvBufferSize(int value) {
241  return (0 == uv_recv_buffer_size(this->template get<uv_handle_t>(), &value));
242  }
243 
267  uv_os_fd_t fd;
268  uv_fileno(this->template get<uv_handle_t>(), &fd);
269  return fd;
270  }
271 };
272 
273 
274 }
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:176
bool recvBufferSize(int value)
Sets the size of the receive buffer used for the socket.
Definition: handle.hpp:240
details::UVTypeWrapper< uv_os_fd_t > OSFileDescriptor
Definition: util.hpp:192
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:208
HandleType type() const noexcept override
Gets the type of the handle.
Definition: handle.hpp:90
void close() noexcept override
Request handle to be closed.
Definition: handle.hpp:138
details::UVTypeWrapper< uv_handle_type > HandleCategory
Definition: util.hpp:189
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:113
static HandleType guessHandle(HandleCategory category) noexcept
Gets the type of the handle given a category.
Definition: util.hpp:684
HandleCategory category() const noexcept override
Gets the category of the handle.
Definition: handle.hpp:77
bool closing() const noexcept override
Checks if a handle is closing or closed.
Definition: handle.hpp:125
OSFileDescriptor fileno() const
Gets the platform dependent file descriptor equivalent.
Definition: handle.hpp:266
void unreference() noexcept override
Unreference the given handle.
Definition: handle.hpp:160
bool referenced() const noexcept override
Checks if the given handle referenced.
Definition: handle.hpp:168
int sendBufferSize()
Gets the size of the send buffer used for the socket.
Definition: handle.hpp:191
void reference() noexcept override
Reference the given handle.
Definition: handle.hpp:150
int recvBufferSize()
Gets the size of the receive buffer used for the socket.
Definition: handle.hpp:223
uvw default namespace.
Definition: async.hpp:11