uvw 2.12.1
Loading...
Searching...
No Matches
handle.hpp
1#ifndef UVW_HANDLE_INCLUDE_H
2#define UVW_HANDLE_INCLUDE_H
3
4#include <cstddef>
5#include <memory>
6#include <utility>
7#include <uv.h>
8#include "resource.hpp"
9#include "util.h"
10
11namespace uvw {
12
18struct CloseEvent {};
19
25template<typename T, typename U>
26class Handle: public Resource<T, U> {
27protected:
28 static void closeCallback(uv_handle_t *handle) {
29 Handle<T, U> &ref = *(static_cast<T *>(handle->data));
30 [[maybe_unused]] auto ptr = ref.shared_from_this();
31 ref.reset();
32 ref.publish(CloseEvent{});
33 }
34
35 static void allocCallback(uv_handle_t *, std::size_t suggested, uv_buf_t *buf) {
36 auto size = static_cast<unsigned int>(suggested);
37 *buf = uv_buf_init(new char[size], size);
38 }
39
40 template<typename F, typename... Args>
41 bool initialize(F &&f, Args &&...args) {
42 if(!this->self()) {
43 if(auto err = std::forward<F>(f)(this->parent(), this->get(), std::forward<Args>(args)...); err) {
44 this->publish(ErrorEvent{err});
45 } else {
46 this->leak();
47 }
48 }
49
50 return this->self();
51 }
52
53 template<typename F, typename... Args>
54 void invoke(F &&f, Args &&...args) {
55 auto err = std::forward<F>(f)(std::forward<Args>(args)...);
56 if(err) { Emitter<T>::publish(ErrorEvent{err}); }
57 }
58
59public:
60 using Resource<T, U>::Resource;
61
71 HandleCategory category() const noexcept {
72 return HandleCategory{this->template get<uv_handle_t>()->type};
73 }
74
84 HandleType type() const noexcept {
86 }
87
107 bool active() const noexcept {
108 return !(uv_is_active(this->template get<uv_handle_t>()) == 0);
109 }
110
119 bool closing() const noexcept {
120 return !(uv_is_closing(this->template get<uv_handle_t>()) == 0);
121 }
122
132 void close() noexcept {
133 if(!closing()) {
134 uv_close(this->template get<uv_handle_t>(), &Handle<T, U>::closeCallback);
135 }
136 }
137
144 void reference() noexcept {
145 uv_ref(this->template get<uv_handle_t>());
146 }
147
154 void unreference() noexcept {
155 uv_unref(this->template get<uv_handle_t>());
156 }
157
162 bool referenced() const noexcept {
163 return !(uv_has_ref(this->template get<uv_handle_t>()) == 0);
164 }
165
170 std::size_t size() const noexcept {
171 return uv_handle_size(this->template get<uv_handle_t>()->type);
172 }
173
186 int value = 0;
187 auto err = uv_send_buffer_size(this->template get<uv_handle_t>(), &value);
188 return err ? 0 : value;
189 }
190
202 bool sendBufferSize(int value) {
203 return (0 == uv_send_buffer_size(this->template get<uv_handle_t>(), &value));
204 }
205
218 int value = 0;
219 auto err = uv_recv_buffer_size(this->template get<uv_handle_t>(), &value);
220 return err ? 0 : value;
221 }
222
234 bool recvBufferSize(int value) {
235 return (0 == uv_recv_buffer_size(this->template get<uv_handle_t>(), &value));
236 }
237
261 uv_os_fd_t fd;
262 uv_fileno(this->template get<uv_handle_t>(), &fd);
263 return fd;
264 }
265};
266
267} // namespace uvw
268
269#endif // UVW_HANDLE_INCLUDE_H
Event emitter base class.
Definition emitter.h:83
Handle base class.
Definition handle.hpp:26
bool closing() const noexcept
Checks if a handle is closing or closed.
Definition handle.hpp:119
void reference() noexcept
Reference the given handle.
Definition handle.hpp:144
HandleCategory category() const noexcept
Gets the category of the handle.
Definition handle.hpp:71
int sendBufferSize()
Gets the size of the send buffer used for the socket.
Definition handle.hpp:185
bool recvBufferSize(int value)
Sets the size of the receive buffer used for the socket.
Definition handle.hpp:234
HandleType type() const noexcept
Gets the type of the handle.
Definition handle.hpp:84
bool active() const noexcept
Checks if the handle is active.
Definition handle.hpp:107
void unreference() noexcept
Unreference the given handle.
Definition handle.hpp:154
std::size_t size() const noexcept
Returns the size of the underlying handle type.
Definition handle.hpp:170
void close() noexcept
Request handle to be closed.
Definition handle.hpp:132
bool sendBufferSize(int value)
Sets the size of the send buffer used for the socket.
Definition handle.hpp:202
int recvBufferSize()
Gets the size of the receive buffer used for the socket.
Definition handle.hpp:217
bool referenced() const noexcept
Checks if the given handle referenced.
Definition handle.hpp:162
OSFileDescriptor fd() const
Gets the platform dependent file descriptor equivalent.
Definition handle.hpp:260
Common class for almost all the resources available in uvw.
Definition resource.hpp:17
uvw default namespace.
Definition async.h:8
details::UVTypeWrapper< uv_os_fd_t > OSFileDescriptor
Definition util.h:207
details::UVTypeWrapper< uv_handle_type > HandleCategory
Definition util.h:204
CloseEvent event.
Definition handle.hpp:18
The ErrorEvent event.
Definition emitter.h:23
static HandleType guessHandle(HandleCategory category) noexcept
Gets the type of the handle given a category.