uvw 3.1.0
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 "config.h"
9#include "resource.hpp"
10#include "util.h"
11
12namespace uvw {
13
15struct close_event {};
16
22template<typename T, typename U, typename... E>
23class handle: public resource<T, U, close_event, E...> {
24protected:
25 static void close_callback(uv_handle_t *hndl) {
26 handle<T, U, E...> &ref = *(static_cast<T *>(hndl->data));
27 [[maybe_unused]] auto ptr = ref.shared_from_this();
28 ref.self_reset();
29 ref.publish(close_event{});
30 }
31
32 uv_handle_t *as_uv_handle() {
33 return reinterpret_cast<uv_handle_t *>(this->raw());
34 }
35
36 const uv_handle_t *as_uv_handle() const {
37 return reinterpret_cast<const uv_handle_t *>(this->raw());
38 }
39
40public:
41 using resource<T, U, close_event, E...>::resource;
42
52 handle_category category() const noexcept {
53 return handle_category{as_uv_handle()->type};
54 }
55
65 handle_type type() const noexcept {
67 }
68
88 bool active() const noexcept {
89 return !!uv_is_active(as_uv_handle());
90 }
91
100 bool closing() const noexcept {
101 return !!uv_is_closing(as_uv_handle());
102 }
103
112 void close() noexcept {
113 if(!closing()) {
114 uv_close(as_uv_handle(), &handle<T, U, E...>::close_callback);
115 }
116 }
117
124 void reference() noexcept {
125 uv_ref(as_uv_handle());
126 }
127
134 void unreference() noexcept {
135 uv_unref(as_uv_handle());
136 }
137
142 bool referenced() const noexcept {
143 return !!uv_has_ref(as_uv_handle());
144 }
145
150 std::size_t size() const noexcept {
151 return uv_handle_size(as_uv_handle()->type);
152 }
153
167 int value = 0;
168 auto err = uv_send_buffer_size(as_uv_handle(), &value);
169 return err ? err : value;
170 }
171
183 int send_buffer_size(int value) {
184 return uv_send_buffer_size(as_uv_handle(), &value);
185 }
186
200 int value = 0;
201 auto err = uv_recv_buffer_size(as_uv_handle(), &value);
202 return err ? err : value;
203 }
204
216 int recv_buffer_size(int value) {
217 return uv_recv_buffer_size(as_uv_handle(), &value);
218 }
219
242 uv_os_fd_t fd;
243 uv_fileno(as_uv_handle(), &fd);
244 return fd;
245 }
246};
247
248} // namespace uvw
249
250#endif // UVW_HANDLE_INCLUDE_H
Handle base class.
Definition: handle.hpp:23
os_file_descriptor fd() const
Gets the platform dependent file descriptor equivalent.
Definition: handle.hpp:241
bool referenced() const noexcept
Checks if the given handle referenced.
Definition: handle.hpp:142
void reference() noexcept
Reference the given handle.
Definition: handle.hpp:124
void close() noexcept
Request handle to be closed.
Definition: handle.hpp:112
std::size_t size() const noexcept
Returns the size of the underlying handle type.
Definition: handle.hpp:150
int recv_buffer_size()
Gets the size of the receive buffer used for the socket.
Definition: handle.hpp:199
handle_category category() const noexcept
Gets the category of the handle.
Definition: handle.hpp:52
void unreference() noexcept
Unreference the given handle.
Definition: handle.hpp:134
handle_type type() const noexcept
Gets the type of the handle.
Definition: handle.hpp:65
int send_buffer_size(int value)
Sets the size of the send buffer used for the socket.
Definition: handle.hpp:183
bool active() const noexcept
Checks if the handle is active.
Definition: handle.hpp:88
int send_buffer_size()
Gets the size of the send buffer used for the socket.
Definition: handle.hpp:166
int recv_buffer_size(int value)
Sets the size of the receive buffer used for the socket.
Definition: handle.hpp:216
bool closing() const noexcept
Checks if a handle is closing or closed.
Definition: handle.hpp:100
Common class for almost all the resources available in uvw.
Definition: resource.hpp:18
uvw default namespace.
Definition: async.h:8
details::uv_type_wrapper< uv_os_fd_t > os_file_descriptor
Definition: util.h:87
details::uv_type_wrapper< uv_handle_type > handle_category
Definition: util.h:84
Close event.
Definition: handle.hpp:15
static handle_type guess_handle(handle_category category) noexcept
Gets the type of the handle given a category.
const U * raw() const noexcept
Gets the underlying raw data structure.
Definition: uv_type.hpp:59