build: split h/ipp/cpp and update clang-tidy config

This commit is contained in:
Michele Caini 2024-10-21 10:57:36 +02:00
parent f70a0dbc2b
commit 21372c17e8
70 changed files with 2272 additions and 2312 deletions

View File

@ -1,2 +1,5 @@
Checks: >
bugprone-*,
CheckOptions:
- key: bugprone-suspicious-include.HeaderFileExtensions
value: ";h;hpp;ipp"

View File

@ -1,22 +1,2 @@
#ifdef UVW_AS_LIB
# include "async.h"
#endif
#include "config.h"
namespace uvw {
UVW_INLINE void async_handle::send_callback(uv_async_t *hndl) {
async_handle &async = *(static_cast<async_handle *>(hndl->data));
async.publish(async_event{});
}
UVW_INLINE int async_handle::init() {
return leak_if(uv_async_init(parent().raw(), raw(), &send_callback));
}
UVW_INLINE int async_handle::send() {
return uv_async_send(raw());
}
} // namespace uvw
#include "async.h"
#include "async.ipp"

View File

@ -52,7 +52,7 @@ public:
} // namespace uvw
#ifndef UVW_AS_LIB
# include "async.cpp"
# include "async.ipp"
#endif
#endif // UVW_ASYNC_INCLUDE_H

18
src/uvw/async.ipp Normal file
View File

@ -0,0 +1,18 @@
#include "config.h"
namespace uvw {
UVW_INLINE void async_handle::send_callback(uv_async_t *hndl) {
async_handle &async = *(static_cast<async_handle *>(hndl->data));
async.publish(async_event{});
}
UVW_INLINE int async_handle::init() {
return leak_if(uv_async_init(parent().raw(), raw(), &send_callback));
}
UVW_INLINE int async_handle::send() {
return uv_async_send(raw());
}
} // namespace uvw

View File

@ -1,26 +1,2 @@
#ifdef UVW_AS_LIB
# include "check.h"
#endif
#include "config.h"
namespace uvw {
UVW_INLINE void check_handle::start_callback(uv_check_t *hndl) {
check_handle &check = *(static_cast<check_handle *>(hndl->data));
check.publish(check_event{});
}
UVW_INLINE int check_handle::init() {
return leak_if(uv_check_init(parent().raw(), raw()));
}
UVW_INLINE int check_handle::start() {
return uv_check_start(raw(), &start_callback);
}
UVW_INLINE int check_handle::stop() {
return uv_check_stop(raw());
}
} // namespace uvw
#include "check.h"
#include "check.ipp"

View File

@ -50,7 +50,7 @@ public:
} // namespace uvw
#ifndef UVW_AS_LIB
# include "check.cpp"
# include "check.ipp"
#endif
#endif // UVW_CHECK_INCLUDE_H

22
src/uvw/check.ipp Normal file
View File

@ -0,0 +1,22 @@
#include "config.h"
namespace uvw {
UVW_INLINE void check_handle::start_callback(uv_check_t *hndl) {
check_handle &check = *(static_cast<check_handle *>(hndl->data));
check.publish(check_event{});
}
UVW_INLINE int check_handle::init() {
return leak_if(uv_check_init(parent().raw(), raw()));
}
UVW_INLINE int check_handle::start() {
return uv_check_start(raw(), &start_callback);
}
UVW_INLINE int check_handle::stop() {
return uv_check_stop(raw());
}
} // namespace uvw

View File

@ -1,93 +1,2 @@
#ifdef UVW_AS_LIB
# include "dns.h"
#endif
#include "config.h"
namespace uvw {
UVW_INLINE addr_info_event::addr_info_event(std::unique_ptr<addrinfo, deleter> addr)
: data{std::move(addr)} {}
UVW_INLINE name_info_event::name_info_event(const char *host, const char *serv)
: hostname{host}, service{serv} {}
UVW_INLINE void get_addr_info_req::addr_info_callback(uv_getaddrinfo_t *req, int status, addrinfo *res) {
if(auto ptr = reserve(req); status) {
ptr->publish(error_event{status});
} else {
auto data = std::unique_ptr<addrinfo, void (*)(addrinfo *)>{res, [](addrinfo *addr) { uv_freeaddrinfo(addr); }};
ptr->publish(addr_info_event{std::move(data)});
}
}
UVW_INLINE int get_addr_info_req::node_addr_info(const char *node, const char *service, addrinfo *hints) {
return this->leak_if(uv_getaddrinfo(parent().raw(), raw(), &addr_info_callback, node, service, hints));
}
UVW_INLINE auto get_addr_info_req::node_addr_info_sync(const char *node, const char *service, addrinfo *hints) {
auto req = raw();
auto err = uv_getaddrinfo(parent().raw(), req, nullptr, node, service, hints);
auto data = std::unique_ptr<addrinfo, void (*)(addrinfo *)>{req->addrinfo, [](addrinfo *addr) { uv_freeaddrinfo(addr); }};
return std::make_pair(!err, std::move(data));
}
UVW_INLINE int get_addr_info_req::node_addr_info(const std::string &node, addrinfo *hints) {
return node_addr_info(node.data(), nullptr, hints);
}
UVW_INLINE std::pair<bool, std::unique_ptr<addrinfo, get_addr_info_req::deleter>> get_addr_info_req::node_addr_info_sync(const std::string &node, addrinfo *hints) {
return node_addr_info_sync(node.data(), nullptr, hints);
}
UVW_INLINE int get_addr_info_req::service_addr_info(const std::string &service, addrinfo *hints) {
return node_addr_info(nullptr, service.data(), hints);
}
UVW_INLINE std::pair<bool, std::unique_ptr<addrinfo, get_addr_info_req::deleter>> get_addr_info_req::service_addr_info_sync(const std::string &service, addrinfo *hints) {
return node_addr_info_sync(nullptr, service.data(), hints);
}
UVW_INLINE int get_addr_info_req::addr_info(const std::string &node, const std::string &service, addrinfo *hints) {
return node_addr_info(node.data(), service.data(), hints);
}
UVW_INLINE std::pair<bool, std::unique_ptr<addrinfo, get_addr_info_req::deleter>> get_addr_info_req::addr_info_sync(const std::string &node, const std::string &service, addrinfo *hints) {
return node_addr_info_sync(node.data(), service.data(), hints);
}
UVW_INLINE void get_name_info_req::name_info_callback(uv_getnameinfo_t *req, int status, const char *hostname, const char *service) {
if(auto ptr = reserve(req); status) {
ptr->publish(error_event{status});
} else {
ptr->publish(name_info_event{hostname, service});
}
}
UVW_INLINE int get_name_info_req::name_info(const sockaddr &addr, int flags) {
return this->leak_if(uv_getnameinfo(parent().raw(), raw(), &name_info_callback, &addr, flags));
}
UVW_INLINE int get_name_info_req::name_info(const std::string &ip, unsigned int port, int flags) {
return name_info(details::ip_addr(ip.data(), port), flags);
}
UVW_INLINE int get_name_info_req::name_info(socket_address addr, int flags) {
return name_info(std::move(addr.ip), addr.port, flags);
}
UVW_INLINE std::pair<bool, std::pair<const char *, const char *>> get_name_info_req::name_info_sync(const sockaddr &addr, int flags) {
auto req = raw();
auto err = uv_getnameinfo(parent().raw(), req, nullptr, &addr, flags);
return std::make_pair(!err, std::make_pair(req->host, req->service));
}
UVW_INLINE std::pair<bool, std::pair<const char *, const char *>> get_name_info_req::name_info_sync(const std::string &ip, unsigned int port, int flags) {
return name_info_sync(details::ip_addr(ip.data(), port), flags);
}
UVW_INLINE std::pair<bool, std::pair<const char *, const char *>> get_name_info_req::name_info_sync(socket_address addr, int flags) {
return name_info_sync(addr.ip, addr.port, flags);
}
} // namespace uvw
#include "dns.h"
#include "dns.ipp"

View File

@ -220,7 +220,7 @@ public:
} // namespace uvw
#ifndef UVW_AS_LIB
# include "dns.cpp"
# include "dns.ipp"
#endif
#endif // UVW_DNS_INCLUDE_H

89
src/uvw/dns.ipp Normal file
View File

@ -0,0 +1,89 @@
#include "config.h"
namespace uvw {
UVW_INLINE addr_info_event::addr_info_event(std::unique_ptr<addrinfo, deleter> addr)
: data{std::move(addr)} {}
UVW_INLINE name_info_event::name_info_event(const char *host, const char *serv)
: hostname{host}, service{serv} {}
UVW_INLINE void get_addr_info_req::addr_info_callback(uv_getaddrinfo_t *req, int status, addrinfo *res) {
if(auto ptr = reserve(req); status) {
ptr->publish(error_event{status});
} else {
auto data = std::unique_ptr<addrinfo, void (*)(addrinfo *)>{res, [](addrinfo *addr) { uv_freeaddrinfo(addr); }};
ptr->publish(addr_info_event{std::move(data)});
}
}
UVW_INLINE int get_addr_info_req::node_addr_info(const char *node, const char *service, addrinfo *hints) {
return this->leak_if(uv_getaddrinfo(parent().raw(), raw(), &addr_info_callback, node, service, hints));
}
UVW_INLINE auto get_addr_info_req::node_addr_info_sync(const char *node, const char *service, addrinfo *hints) {
auto req = raw();
auto err = uv_getaddrinfo(parent().raw(), req, nullptr, node, service, hints);
auto data = std::unique_ptr<addrinfo, void (*)(addrinfo *)>{req->addrinfo, [](addrinfo *addr) { uv_freeaddrinfo(addr); }};
return std::make_pair(!err, std::move(data));
}
UVW_INLINE int get_addr_info_req::node_addr_info(const std::string &node, addrinfo *hints) {
return node_addr_info(node.data(), nullptr, hints);
}
UVW_INLINE std::pair<bool, std::unique_ptr<addrinfo, get_addr_info_req::deleter>> get_addr_info_req::node_addr_info_sync(const std::string &node, addrinfo *hints) {
return node_addr_info_sync(node.data(), nullptr, hints);
}
UVW_INLINE int get_addr_info_req::service_addr_info(const std::string &service, addrinfo *hints) {
return node_addr_info(nullptr, service.data(), hints);
}
UVW_INLINE std::pair<bool, std::unique_ptr<addrinfo, get_addr_info_req::deleter>> get_addr_info_req::service_addr_info_sync(const std::string &service, addrinfo *hints) {
return node_addr_info_sync(nullptr, service.data(), hints);
}
UVW_INLINE int get_addr_info_req::addr_info(const std::string &node, const std::string &service, addrinfo *hints) {
return node_addr_info(node.data(), service.data(), hints);
}
UVW_INLINE std::pair<bool, std::unique_ptr<addrinfo, get_addr_info_req::deleter>> get_addr_info_req::addr_info_sync(const std::string &node, const std::string &service, addrinfo *hints) {
return node_addr_info_sync(node.data(), service.data(), hints);
}
UVW_INLINE void get_name_info_req::name_info_callback(uv_getnameinfo_t *req, int status, const char *hostname, const char *service) {
if(auto ptr = reserve(req); status) {
ptr->publish(error_event{status});
} else {
ptr->publish(name_info_event{hostname, service});
}
}
UVW_INLINE int get_name_info_req::name_info(const sockaddr &addr, int flags) {
return this->leak_if(uv_getnameinfo(parent().raw(), raw(), &name_info_callback, &addr, flags));
}
UVW_INLINE int get_name_info_req::name_info(const std::string &ip, unsigned int port, int flags) {
return name_info(details::ip_addr(ip.data(), port), flags);
}
UVW_INLINE int get_name_info_req::name_info(socket_address addr, int flags) {
return name_info(std::move(addr.ip), addr.port, flags);
}
UVW_INLINE std::pair<bool, std::pair<const char *, const char *>> get_name_info_req::name_info_sync(const sockaddr &addr, int flags) {
auto req = raw();
auto err = uv_getnameinfo(parent().raw(), req, nullptr, &addr, flags);
return std::make_pair(!err, std::make_pair(req->host, req->service));
}
UVW_INLINE std::pair<bool, std::pair<const char *, const char *>> get_name_info_req::name_info_sync(const std::string &ip, unsigned int port, int flags) {
return name_info_sync(details::ip_addr(ip.data(), port), flags);
}
UVW_INLINE std::pair<bool, std::pair<const char *, const char *>> get_name_info_req::name_info_sync(socket_address addr, int flags) {
return name_info_sync(addr.ip, addr.port, flags);
}
} // namespace uvw

View File

@ -1,29 +1,2 @@
#ifdef UVW_AS_LIB
# include "emitter.h"
#endif
#include "config.h"
namespace uvw {
UVW_INLINE int error_event::translate(int sys) noexcept {
return uv_translate_sys_error(sys);
}
UVW_INLINE const char *error_event::what() const noexcept {
return uv_strerror(ec);
}
UVW_INLINE const char *error_event::name() const noexcept {
return uv_err_name(ec);
}
UVW_INLINE int error_event::code() const noexcept {
return ec;
}
UVW_INLINE error_event::operator bool() const noexcept {
return ec < 0;
}
} // namespace uvw
#include "emitter.h"
#include "emitter.ipp"

View File

@ -153,7 +153,7 @@ private:
} // namespace uvw
#ifndef UVW_AS_LIB
# include "emitter.cpp"
# include "emitter.ipp"
#endif
#endif // UVW_EMITTER_INCLUDE_H

25
src/uvw/emitter.ipp Normal file
View File

@ -0,0 +1,25 @@
#include "config.h"
namespace uvw {
UVW_INLINE int error_event::translate(int sys) noexcept {
return uv_translate_sys_error(sys);
}
UVW_INLINE const char *error_event::what() const noexcept {
return uv_strerror(ec);
}
UVW_INLINE const char *error_event::name() const noexcept {
return uv_err_name(ec);
}
UVW_INLINE int error_event::code() const noexcept {
return ec;
}
UVW_INLINE error_event::operator bool() const noexcept {
return ec < 0;
}
} // namespace uvw

View File

@ -1,553 +1,2 @@
#ifdef UVW_AS_LIB
# include "fs.h"
#endif
#include "config.h"
namespace uvw {
UVW_INLINE void file_req::fs_open_callback(uv_fs_t *req) {
if(auto ptr = reserve(req); req->result < 0) {
ptr->publish(error_event{req->result});
} else {
ptr->file = static_cast<uv_file>(req->result);
ptr->publish(fs_event{*req});
}
}
UVW_INLINE void file_req::fs_close_callback(uv_fs_t *req) {
if(auto ptr = reserve(req); req->result < 0) {
ptr->publish(error_event{req->result});
} else {
ptr->file = BAD_FD;
ptr->publish(fs_event{*req});
}
}
UVW_INLINE void file_req::fs_read_callback(uv_fs_t *req) {
if(auto ptr = reserve(req); req->result < 0) {
ptr->publish(error_event{req->result});
} else {
ptr->publish(fs_event{*req, std::move(ptr->current)});
}
}
UVW_INLINE file_req::~file_req() noexcept {
uv_fs_req_cleanup(raw());
}
UVW_INLINE void file_req::close() {
uv_fs_req_cleanup(this->raw());
uv_fs_close(parent().raw(), raw(), file, &fs_close_callback);
}
UVW_INLINE bool file_req::close_sync() {
auto req = raw();
uv_fs_req_cleanup(this->raw());
uv_fs_close(parent().raw(), req, file, nullptr);
if(req->result >= 0) {
file = BAD_FD;
}
return !(req->result < 0);
}
UVW_INLINE void file_req::open(const std::string &path, file_open_flags flags, int mode) {
uv_fs_req_cleanup(this->raw());
uv_fs_open(parent().raw(), raw(), path.data(), static_cast<int>(flags), mode, &fs_open_callback);
}
UVW_INLINE bool file_req::open_sync(const std::string &path, file_open_flags flags, int mode) {
auto req = raw();
uv_fs_req_cleanup(this->raw());
uv_fs_open(parent().raw(), req, path.data(), static_cast<int>(flags), mode, nullptr);
if(req->result >= 0) {
file = static_cast<uv_file>(req->result);
}
return !(req->result < 0);
}
UVW_INLINE void file_req::read(int64_t offset, unsigned int len) {
current = std::unique_ptr<char[]>{new char[len]};
buffer = uv_buf_init(current.get(), len);
uv_buf_t bufs[] = {buffer};
uv_fs_req_cleanup(this->raw());
uv_fs_read(parent().raw(), raw(), file, bufs, 1, offset, &fs_read_callback);
}
UVW_INLINE std::pair<bool, std::pair<std::unique_ptr<const char[]>, std::size_t>> file_req::read_sync(int64_t offset, unsigned int len) {
current = std::unique_ptr<char[]>{new char[len]};
buffer = uv_buf_init(current.get(), len);
uv_buf_t bufs[] = {buffer};
auto req = raw();
uv_fs_req_cleanup(this->raw());
uv_fs_read(parent().raw(), req, file, bufs, 1, offset, nullptr);
bool err = req->result < 0;
return std::make_pair(!err, std::make_pair(std::move(current), err ? 0 : std::size_t(req->result)));
}
UVW_INLINE void file_req::write(std::unique_ptr<char[]> buf, unsigned int len, int64_t offset) {
current = std::move(buf);
uv_buf_t bufs[] = {uv_buf_init(current.get(), len)};
uv_fs_req_cleanup(this->raw());
uv_fs_write(parent().raw(), raw(), file, bufs, 1, offset, &fs_request_callback);
}
UVW_INLINE void file_req::write(char *buf, unsigned int len, int64_t offset) {
uv_buf_t bufs[] = {uv_buf_init(buf, len)};
uv_fs_req_cleanup(this->raw());
uv_fs_write(parent().raw(), raw(), file, bufs, 1, offset, &fs_request_callback);
}
UVW_INLINE std::pair<bool, std::size_t> file_req::write_sync(std::unique_ptr<char[]> buf, unsigned int len, int64_t offset) {
current = std::move(buf);
uv_buf_t bufs[] = {uv_buf_init(current.get(), len)};
auto req = raw();
uv_fs_req_cleanup(this->raw());
uv_fs_write(parent().raw(), req, file, bufs, 1, offset, nullptr);
bool err = req->result < 0;
return std::make_pair(!err, err ? 0 : std::size_t(req->result));
}
UVW_INLINE void file_req::stat() {
uv_fs_req_cleanup(this->raw());
uv_fs_fstat(parent().raw(), raw(), file, &fs_request_callback);
}
UVW_INLINE std::pair<bool, file_info> file_req::stat_sync() {
auto req = raw();
uv_fs_req_cleanup(this->raw());
uv_fs_fstat(parent().raw(), req, file, nullptr);
return std::make_pair(!(req->result < 0), req->statbuf);
}
UVW_INLINE void file_req::sync() {
uv_fs_req_cleanup(this->raw());
uv_fs_fsync(parent().raw(), raw(), file, &fs_request_callback);
}
UVW_INLINE bool file_req::sync_sync() {
auto req = raw();
uv_fs_req_cleanup(this->raw());
uv_fs_fsync(parent().raw(), req, file, nullptr);
return !(req->result < 0);
}
UVW_INLINE void file_req::datasync() {
uv_fs_req_cleanup(this->raw());
uv_fs_fdatasync(parent().raw(), raw(), file, &fs_request_callback);
}
UVW_INLINE bool file_req::datasync_sync() {
auto req = raw();
uv_fs_req_cleanup(this->raw());
uv_fs_fdatasync(parent().raw(), req, file, nullptr);
return !(req->result < 0);
}
UVW_INLINE void file_req::truncate(int64_t offset) {
uv_fs_req_cleanup(this->raw());
uv_fs_ftruncate(parent().raw(), raw(), file, offset, &fs_request_callback);
}
UVW_INLINE bool file_req::truncate_sync(int64_t offset) {
auto req = raw();
uv_fs_req_cleanup(this->raw());
uv_fs_ftruncate(parent().raw(), req, file, offset, nullptr);
return !(req->result < 0);
}
UVW_INLINE void file_req::sendfile(file_handle out, int64_t offset, std::size_t length) {
uv_fs_req_cleanup(this->raw());
uv_fs_sendfile(parent().raw(), raw(), out, file, offset, length, &fs_request_callback);
}
UVW_INLINE std::pair<bool, std::size_t> file_req::sendfile_sync(file_handle out, int64_t offset, std::size_t length) {
auto req = raw();
uv_fs_req_cleanup(this->raw());
uv_fs_sendfile(parent().raw(), req, out, file, offset, length, nullptr);
bool err = req->result < 0;
return std::make_pair(!err, err ? 0 : std::size_t(req->result));
}
UVW_INLINE void file_req::chmod(int mode) {
uv_fs_req_cleanup(this->raw());
uv_fs_fchmod(parent().raw(), raw(), file, mode, &fs_request_callback);
}
UVW_INLINE bool file_req::chmod_sync(int mode) {
auto req = raw();
uv_fs_req_cleanup(this->raw());
uv_fs_fchmod(parent().raw(), req, file, mode, nullptr);
return !(req->result < 0);
}
UVW_INLINE void file_req::futime(fs_request::time atime, fs_request::time mtime) {
uv_fs_req_cleanup(this->raw());
uv_fs_futime(parent().raw(), raw(), file, atime.count(), mtime.count(), &fs_request_callback);
}
UVW_INLINE bool file_req::futime_sync(fs_request::time atime, fs_request::time mtime) {
auto req = raw();
uv_fs_req_cleanup(this->raw());
uv_fs_futime(parent().raw(), req, file, atime.count(), mtime.count(), nullptr);
return !(req->result < 0);
}
UVW_INLINE void file_req::chown(uid_type uid, gid_type gid) {
uv_fs_req_cleanup(this->raw());
uv_fs_fchown(parent().raw(), raw(), file, uid, gid, &fs_request_callback);
}
UVW_INLINE bool file_req::chown_sync(uid_type uid, gid_type gid) {
auto req = raw();
uv_fs_req_cleanup(this->raw());
uv_fs_fchown(parent().raw(), req, file, uid, gid, nullptr);
return !(req->result < 0);
}
UVW_INLINE file_req::operator file_handle() const noexcept {
return file;
}
UVW_INLINE fs_req::~fs_req() noexcept {
uv_fs_req_cleanup(raw());
}
UVW_INLINE void fs_req::unlink(const std::string &path) {
uv_fs_req_cleanup(this->raw());
uv_fs_unlink(parent().raw(), raw(), path.data(), &fs_request_callback);
}
UVW_INLINE bool fs_req::unlink_sync(const std::string &path) {
auto req = raw();
uv_fs_req_cleanup(this->raw());
uv_fs_unlink(parent().raw(), req, path.data(), nullptr);
return !(req->result < 0);
}
UVW_INLINE void fs_req::mkdir(const std::string &path, int mode) {
uv_fs_req_cleanup(this->raw());
uv_fs_mkdir(parent().raw(), raw(), path.data(), mode, &fs_request_callback);
}
UVW_INLINE bool fs_req::mkdir_sync(const std::string &path, int mode) {
auto req = raw();
uv_fs_req_cleanup(this->raw());
uv_fs_mkdir(parent().raw(), req, path.data(), mode, nullptr);
return !(req->result < 0);
}
UVW_INLINE void fs_req::mkdtemp(const std::string &tpl) {
uv_fs_req_cleanup(this->raw());
uv_fs_mkdtemp(parent().raw(), raw(), tpl.data(), &fs_request_callback);
}
UVW_INLINE std::pair<bool, const char *> fs_req::mkdtemp_sync(const std::string &tpl) {
auto req = raw();
uv_fs_req_cleanup(this->raw());
uv_fs_mkdtemp(parent().raw(), req, tpl.data(), nullptr);
return std::make_pair(!(req->result < 0), req->path);
}
UVW_INLINE void fs_req::mkstemp(const std::string &tpl) {
uv_fs_req_cleanup(this->raw());
uv_fs_mkstemp(parent().raw(), raw(), tpl.data(), &fs_request_callback);
}
UVW_INLINE std::pair<bool, std::pair<std::string, std::size_t>> fs_req::mkstemp_sync(const std::string &tpl) {
std::pair<bool, std::pair<std::string, std::size_t>> ret{false, {}};
auto req = raw();
uv_fs_req_cleanup(this->raw());
uv_fs_mkstemp(parent().raw(), req, tpl.data(), nullptr);
ret.first = !(req->result < 0);
if(ret.first) {
ret.second.first = req->path;
ret.second.second = static_cast<std::size_t>(req->result);
}
return ret;
}
UVW_INLINE void fs_req::lutime(const std::string &path, time atime, time mtime) {
uv_fs_req_cleanup(this->raw());
uv_fs_lutime(parent().raw(), raw(), path.data(), atime.count(), mtime.count(), &fs_request_callback);
}
UVW_INLINE bool fs_req::lutime_sync(const std::string &path, time atime, time mtime) {
auto req = raw();
uv_fs_req_cleanup(this->raw());
uv_fs_lutime(parent().raw(), req, path.data(), atime.count(), mtime.count(), nullptr);
return !(req->result < 0);
}
UVW_INLINE void fs_req::rmdir(const std::string &path) {
uv_fs_req_cleanup(this->raw());
uv_fs_rmdir(parent().raw(), raw(), path.data(), &fs_request_callback);
}
UVW_INLINE bool fs_req::rmdir_sync(const std::string &path) {
auto req = raw();
uv_fs_req_cleanup(this->raw());
uv_fs_rmdir(parent().raw(), req, path.data(), nullptr);
return !(req->result < 0);
}
UVW_INLINE void fs_req::scandir(const std::string &path, int flags) {
uv_fs_req_cleanup(this->raw());
uv_fs_scandir(parent().raw(), raw(), path.data(), flags, &fs_request_callback);
}
UVW_INLINE std::pair<bool, std::size_t> fs_req::scandir_sync(const std::string &path, int flags) {
auto req = raw();
uv_fs_req_cleanup(this->raw());
uv_fs_scandir(parent().raw(), req, path.data(), flags, nullptr);
bool err = req->result < 0;
return std::make_pair(!err, err ? 0 : std::size_t(req->result));
}
UVW_INLINE std::pair<bool, std::pair<fs_req::entry_type, const char *>> fs_req::scandir_next() {
std::pair<bool, std::pair<entry_type, const char *>> ret{false, {entry_type::UNKNOWN, nullptr}};
uv_fs_req_cleanup(raw());
auto res = uv_fs_scandir_next(raw(), dirents);
if(UV_EOF != res) {
ret.second.first = static_cast<entry_type>(dirents[0].type);
ret.second.second = dirents[0].name;
ret.first = true;
}
return ret;
}
UVW_INLINE void fs_req::stat(const std::string &path) {
uv_fs_req_cleanup(this->raw());
uv_fs_stat(parent().raw(), raw(), path.data(), &fs_request_callback);
}
UVW_INLINE std::pair<bool, file_info> fs_req::stat_sync(const std::string &path) {
auto req = raw();
uv_fs_req_cleanup(this->raw());
uv_fs_stat(parent().raw(), req, path.data(), nullptr);
return std::make_pair(!(req->result < 0), req->statbuf);
}
UVW_INLINE void fs_req::lstat(const std::string &path) {
uv_fs_req_cleanup(this->raw());
uv_fs_lstat(parent().raw(), raw(), path.data(), &fs_request_callback);
}
UVW_INLINE std::pair<bool, file_info> fs_req::lstat_sync(const std::string &path) {
auto req = raw();
uv_fs_req_cleanup(this->raw());
uv_fs_lstat(parent().raw(), req, path.data(), nullptr);
return std::make_pair(!(req->result < 0), req->statbuf);
}
UVW_INLINE void fs_req::statfs(const std::string &path) {
uv_fs_req_cleanup(this->raw());
uv_fs_statfs(parent().raw(), raw(), path.data(), &fs_request_callback);
}
UVW_INLINE std::pair<bool, fs_info> fs_req::statfs_sync(const std::string &path) {
auto req = raw();
uv_fs_req_cleanup(this->raw());
uv_fs_statfs(parent().raw(), req, path.data(), nullptr);
return std::make_pair(!(req->result < 0), *static_cast<uv_statfs_t *>(req->ptr));
}
UVW_INLINE void fs_req::rename(const std::string &old, const std::string &path) {
uv_fs_req_cleanup(this->raw());
uv_fs_rename(parent().raw(), raw(), old.data(), path.data(), &fs_request_callback);
}
UVW_INLINE bool fs_req::rename_sync(const std::string &old, const std::string &path) {
auto req = raw();
uv_fs_req_cleanup(this->raw());
uv_fs_rename(parent().raw(), req, old.data(), path.data(), nullptr);
return !(req->result < 0);
}
UVW_INLINE void fs_req::copyfile(const std::string &old, const std::string &path, copy_file_flags flags) {
uv_fs_req_cleanup(this->raw());
uv_fs_copyfile(parent().raw(), raw(), old.data(), path.data(), static_cast<int>(flags), &fs_request_callback);
}
UVW_INLINE bool fs_req::copyfile_sync(const std::string &old, const std::string &path, copy_file_flags flags) {
auto req = raw();
uv_fs_req_cleanup(this->raw());
uv_fs_copyfile(parent().raw(), raw(), old.data(), path.data(), static_cast<int>(flags), nullptr);
return !(req->result < 0);
}
UVW_INLINE void fs_req::access(const std::string &path, int mode) {
uv_fs_req_cleanup(this->raw());
uv_fs_access(parent().raw(), raw(), path.data(), mode, &fs_request_callback);
}
UVW_INLINE bool fs_req::access_sync(const std::string &path, int mode) {
auto req = raw();
uv_fs_req_cleanup(this->raw());
uv_fs_access(parent().raw(), req, path.data(), mode, nullptr);
return !(req->result < 0);
}
UVW_INLINE void fs_req::chmod(const std::string &path, int mode) {
uv_fs_req_cleanup(this->raw());
uv_fs_chmod(parent().raw(), raw(), path.data(), mode, &fs_request_callback);
}
UVW_INLINE bool fs_req::chmod_sync(const std::string &path, int mode) {
auto req = raw();
uv_fs_req_cleanup(this->raw());
uv_fs_chmod(parent().raw(), req, path.data(), mode, nullptr);
return !(req->result < 0);
}
UVW_INLINE void fs_req::utime(const std::string &path, fs_request::time atime, fs_request::time mtime) {
uv_fs_req_cleanup(this->raw());
uv_fs_utime(parent().raw(), raw(), path.data(), atime.count(), mtime.count(), &fs_request_callback);
}
UVW_INLINE bool fs_req::utime_sync(const std::string &path, fs_request::time atime, fs_request::time mtime) {
auto req = raw();
uv_fs_req_cleanup(this->raw());
uv_fs_utime(parent().raw(), req, path.data(), atime.count(), mtime.count(), nullptr);
return !(req->result < 0);
}
UVW_INLINE void fs_req::link(const std::string &old, const std::string &path) {
uv_fs_req_cleanup(this->raw());
uv_fs_link(parent().raw(), raw(), old.data(), path.data(), &fs_request_callback);
}
UVW_INLINE bool fs_req::link_sync(const std::string &old, const std::string &path) {
auto req = raw();
uv_fs_req_cleanup(this->raw());
uv_fs_link(parent().raw(), req, old.data(), path.data(), nullptr);
return !(req->result < 0);
}
UVW_INLINE void fs_req::symlink(const std::string &old, const std::string &path, symlink_flags flags) {
uv_fs_req_cleanup(this->raw());
uv_fs_symlink(parent().raw(), raw(), old.data(), path.data(), static_cast<int>(flags), &fs_request_callback);
}
UVW_INLINE bool fs_req::symlink_sync(const std::string &old, const std::string &path, symlink_flags flags) {
auto req = raw();
uv_fs_req_cleanup(this->raw());
uv_fs_symlink(parent().raw(), req, old.data(), path.data(), static_cast<int>(flags), nullptr);
return !(req->result < 0);
}
UVW_INLINE void fs_req::readlink(const std::string &path) {
uv_fs_req_cleanup(this->raw());
uv_fs_readlink(parent().raw(), raw(), path.data(), &fs_request_callback);
}
UVW_INLINE std::pair<bool, std::pair<const char *, std::size_t>> fs_req::readlink_sync(const std::string &path) {
auto req = raw();
uv_fs_req_cleanup(this->raw());
uv_fs_readlink(parent().raw(), req, path.data(), nullptr);
bool err = req->result < 0;
return std::make_pair(!err, std::make_pair(static_cast<char *>(req->ptr), err ? 0 : std::size_t(req->result)));
}
UVW_INLINE void fs_req::realpath(const std::string &path) {
uv_fs_req_cleanup(this->raw());
uv_fs_realpath(parent().raw(), raw(), path.data(), &fs_request_callback);
}
UVW_INLINE std::pair<bool, const char *> fs_req::realpath_sync(const std::string &path) {
auto req = raw();
uv_fs_req_cleanup(this->raw());
uv_fs_realpath(parent().raw(), req, path.data(), nullptr);
return std::make_pair(!(req->result < 0), req->path);
}
UVW_INLINE void fs_req::chown(const std::string &path, uid_type uid, gid_type gid) {
uv_fs_req_cleanup(this->raw());
uv_fs_chown(parent().raw(), raw(), path.data(), uid, gid, &fs_request_callback);
}
UVW_INLINE bool fs_req::chown_sync(const std::string &path, uid_type uid, gid_type gid) {
auto req = raw();
uv_fs_req_cleanup(this->raw());
uv_fs_chown(parent().raw(), req, path.data(), uid, gid, nullptr);
return !(req->result < 0);
}
UVW_INLINE void fs_req::lchown(const std::string &path, uid_type uid, gid_type gid) {
uv_fs_req_cleanup(this->raw());
uv_fs_lchown(parent().raw(), raw(), path.data(), uid, gid, &fs_request_callback);
}
UVW_INLINE bool fs_req::lchown_sync(const std::string &path, uid_type uid, gid_type gid) {
auto req = raw();
uv_fs_req_cleanup(this->raw());
uv_fs_lchown(parent().raw(), req, path.data(), uid, gid, nullptr);
return !(req->result < 0);
}
UVW_INLINE void fs_req::opendir(const std::string &path) {
uv_fs_req_cleanup(this->raw());
uv_fs_opendir(parent().raw(), raw(), path.data(), &fs_request_callback);
}
UVW_INLINE bool fs_req::opendir_sync(const std::string &path) {
auto req = raw();
uv_fs_req_cleanup(this->raw());
uv_fs_opendir(parent().raw(), req, path.data(), nullptr);
return !(req->result < 0);
}
UVW_INLINE void fs_req::closedir() {
auto req = raw();
auto *dir = static_cast<uv_dir_t *>(req->ptr);
uv_fs_req_cleanup(this->raw());
uv_fs_closedir(parent().raw(), req, dir, &fs_request_callback);
}
UVW_INLINE bool fs_req::closedir_sync() {
auto req = raw();
auto *dir = static_cast<uv_dir_t *>(req->ptr);
uv_fs_req_cleanup(this->raw());
uv_fs_closedir(parent().raw(), req, dir, nullptr);
return !(req->result < 0);
}
UVW_INLINE void fs_req::readdir() {
auto req = raw();
auto *dir = static_cast<uv_dir_t *>(req->ptr);
dir->dirents = dirents;
dir->nentries = 1;
uv_fs_req_cleanup(this->raw());
uv_fs_readdir(parent().raw(), req, dir, &fs_request_callback);
}
UVW_INLINE std::pair<bool, std::pair<fs_req::entry_type, const char *>> fs_req::readdir_sync() {
auto req = raw();
auto *dir = static_cast<uv_dir_t *>(req->ptr);
dir->dirents = dirents;
dir->nentries = 1;
uv_fs_req_cleanup(this->raw());
uv_fs_readdir(parent().raw(), req, dir, nullptr);
return {req->result != 0, {static_cast<entry_type>(dirents[0].type), dirents[0].name}};
}
UVW_INLINE os_file_descriptor fs_helper::handle(file_handle file) noexcept {
return uv_get_osfhandle(file);
}
UVW_INLINE file_handle fs_helper::open(os_file_descriptor descriptor) noexcept {
return uv_open_osfhandle(descriptor);
}
} // namespace uvw
#include "fs.h"
#include "fs.ipp"

View File

@ -1229,7 +1229,7 @@ struct fs_helper {
} // namespace uvw
#ifndef UVW_AS_LIB
# include "fs.cpp"
# include "fs.ipp"
#endif
#endif // UVW_FS_INCLUDE_H

549
src/uvw/fs.ipp Normal file
View File

@ -0,0 +1,549 @@
#include "config.h"
namespace uvw {
UVW_INLINE void file_req::fs_open_callback(uv_fs_t *req) {
if(auto ptr = reserve(req); req->result < 0) {
ptr->publish(error_event{req->result});
} else {
ptr->file = static_cast<uv_file>(req->result);
ptr->publish(fs_event{*req});
}
}
UVW_INLINE void file_req::fs_close_callback(uv_fs_t *req) {
if(auto ptr = reserve(req); req->result < 0) {
ptr->publish(error_event{req->result});
} else {
ptr->file = BAD_FD;
ptr->publish(fs_event{*req});
}
}
UVW_INLINE void file_req::fs_read_callback(uv_fs_t *req) {
if(auto ptr = reserve(req); req->result < 0) {
ptr->publish(error_event{req->result});
} else {
ptr->publish(fs_event{*req, std::move(ptr->current)});
}
}
UVW_INLINE file_req::~file_req() noexcept {
uv_fs_req_cleanup(raw());
}
UVW_INLINE void file_req::close() {
uv_fs_req_cleanup(this->raw());
uv_fs_close(parent().raw(), raw(), file, &fs_close_callback);
}
UVW_INLINE bool file_req::close_sync() {
auto req = raw();
uv_fs_req_cleanup(this->raw());
uv_fs_close(parent().raw(), req, file, nullptr);
if(req->result >= 0) {
file = BAD_FD;
}
return !(req->result < 0);
}
UVW_INLINE void file_req::open(const std::string &path, file_open_flags flags, int mode) {
uv_fs_req_cleanup(this->raw());
uv_fs_open(parent().raw(), raw(), path.data(), static_cast<int>(flags), mode, &fs_open_callback);
}
UVW_INLINE bool file_req::open_sync(const std::string &path, file_open_flags flags, int mode) {
auto req = raw();
uv_fs_req_cleanup(this->raw());
uv_fs_open(parent().raw(), req, path.data(), static_cast<int>(flags), mode, nullptr);
if(req->result >= 0) {
file = static_cast<uv_file>(req->result);
}
return !(req->result < 0);
}
UVW_INLINE void file_req::read(int64_t offset, unsigned int len) {
current = std::unique_ptr<char[]>{new char[len]};
buffer = uv_buf_init(current.get(), len);
uv_buf_t bufs[] = {buffer};
uv_fs_req_cleanup(this->raw());
uv_fs_read(parent().raw(), raw(), file, bufs, 1, offset, &fs_read_callback);
}
UVW_INLINE std::pair<bool, std::pair<std::unique_ptr<const char[]>, std::size_t>> file_req::read_sync(int64_t offset, unsigned int len) {
current = std::unique_ptr<char[]>{new char[len]};
buffer = uv_buf_init(current.get(), len);
uv_buf_t bufs[] = {buffer};
auto req = raw();
uv_fs_req_cleanup(this->raw());
uv_fs_read(parent().raw(), req, file, bufs, 1, offset, nullptr);
bool err = req->result < 0;
return std::make_pair(!err, std::make_pair(std::move(current), err ? 0 : std::size_t(req->result)));
}
UVW_INLINE void file_req::write(std::unique_ptr<char[]> buf, unsigned int len, int64_t offset) {
current = std::move(buf);
uv_buf_t bufs[] = {uv_buf_init(current.get(), len)};
uv_fs_req_cleanup(this->raw());
uv_fs_write(parent().raw(), raw(), file, bufs, 1, offset, &fs_request_callback);
}
UVW_INLINE void file_req::write(char *buf, unsigned int len, int64_t offset) {
uv_buf_t bufs[] = {uv_buf_init(buf, len)};
uv_fs_req_cleanup(this->raw());
uv_fs_write(parent().raw(), raw(), file, bufs, 1, offset, &fs_request_callback);
}
UVW_INLINE std::pair<bool, std::size_t> file_req::write_sync(std::unique_ptr<char[]> buf, unsigned int len, int64_t offset) {
current = std::move(buf);
uv_buf_t bufs[] = {uv_buf_init(current.get(), len)};
auto req = raw();
uv_fs_req_cleanup(this->raw());
uv_fs_write(parent().raw(), req, file, bufs, 1, offset, nullptr);
bool err = req->result < 0;
return std::make_pair(!err, err ? 0 : std::size_t(req->result));
}
UVW_INLINE void file_req::stat() {
uv_fs_req_cleanup(this->raw());
uv_fs_fstat(parent().raw(), raw(), file, &fs_request_callback);
}
UVW_INLINE std::pair<bool, file_info> file_req::stat_sync() {
auto req = raw();
uv_fs_req_cleanup(this->raw());
uv_fs_fstat(parent().raw(), req, file, nullptr);
return std::make_pair(!(req->result < 0), req->statbuf);
}
UVW_INLINE void file_req::sync() {
uv_fs_req_cleanup(this->raw());
uv_fs_fsync(parent().raw(), raw(), file, &fs_request_callback);
}
UVW_INLINE bool file_req::sync_sync() {
auto req = raw();
uv_fs_req_cleanup(this->raw());
uv_fs_fsync(parent().raw(), req, file, nullptr);
return !(req->result < 0);
}
UVW_INLINE void file_req::datasync() {
uv_fs_req_cleanup(this->raw());
uv_fs_fdatasync(parent().raw(), raw(), file, &fs_request_callback);
}
UVW_INLINE bool file_req::datasync_sync() {
auto req = raw();
uv_fs_req_cleanup(this->raw());
uv_fs_fdatasync(parent().raw(), req, file, nullptr);
return !(req->result < 0);
}
UVW_INLINE void file_req::truncate(int64_t offset) {
uv_fs_req_cleanup(this->raw());
uv_fs_ftruncate(parent().raw(), raw(), file, offset, &fs_request_callback);
}
UVW_INLINE bool file_req::truncate_sync(int64_t offset) {
auto req = raw();
uv_fs_req_cleanup(this->raw());
uv_fs_ftruncate(parent().raw(), req, file, offset, nullptr);
return !(req->result < 0);
}
UVW_INLINE void file_req::sendfile(file_handle out, int64_t offset, std::size_t length) {
uv_fs_req_cleanup(this->raw());
uv_fs_sendfile(parent().raw(), raw(), out, file, offset, length, &fs_request_callback);
}
UVW_INLINE std::pair<bool, std::size_t> file_req::sendfile_sync(file_handle out, int64_t offset, std::size_t length) {
auto req = raw();
uv_fs_req_cleanup(this->raw());
uv_fs_sendfile(parent().raw(), req, out, file, offset, length, nullptr);
bool err = req->result < 0;
return std::make_pair(!err, err ? 0 : std::size_t(req->result));
}
UVW_INLINE void file_req::chmod(int mode) {
uv_fs_req_cleanup(this->raw());
uv_fs_fchmod(parent().raw(), raw(), file, mode, &fs_request_callback);
}
UVW_INLINE bool file_req::chmod_sync(int mode) {
auto req = raw();
uv_fs_req_cleanup(this->raw());
uv_fs_fchmod(parent().raw(), req, file, mode, nullptr);
return !(req->result < 0);
}
UVW_INLINE void file_req::futime(fs_request::time atime, fs_request::time mtime) {
uv_fs_req_cleanup(this->raw());
uv_fs_futime(parent().raw(), raw(), file, atime.count(), mtime.count(), &fs_request_callback);
}
UVW_INLINE bool file_req::futime_sync(fs_request::time atime, fs_request::time mtime) {
auto req = raw();
uv_fs_req_cleanup(this->raw());
uv_fs_futime(parent().raw(), req, file, atime.count(), mtime.count(), nullptr);
return !(req->result < 0);
}
UVW_INLINE void file_req::chown(uid_type uid, gid_type gid) {
uv_fs_req_cleanup(this->raw());
uv_fs_fchown(parent().raw(), raw(), file, uid, gid, &fs_request_callback);
}
UVW_INLINE bool file_req::chown_sync(uid_type uid, gid_type gid) {
auto req = raw();
uv_fs_req_cleanup(this->raw());
uv_fs_fchown(parent().raw(), req, file, uid, gid, nullptr);
return !(req->result < 0);
}
UVW_INLINE file_req::operator file_handle() const noexcept {
return file;
}
UVW_INLINE fs_req::~fs_req() noexcept {
uv_fs_req_cleanup(raw());
}
UVW_INLINE void fs_req::unlink(const std::string &path) {
uv_fs_req_cleanup(this->raw());
uv_fs_unlink(parent().raw(), raw(), path.data(), &fs_request_callback);
}
UVW_INLINE bool fs_req::unlink_sync(const std::string &path) {
auto req = raw();
uv_fs_req_cleanup(this->raw());
uv_fs_unlink(parent().raw(), req, path.data(), nullptr);
return !(req->result < 0);
}
UVW_INLINE void fs_req::mkdir(const std::string &path, int mode) {
uv_fs_req_cleanup(this->raw());
uv_fs_mkdir(parent().raw(), raw(), path.data(), mode, &fs_request_callback);
}
UVW_INLINE bool fs_req::mkdir_sync(const std::string &path, int mode) {
auto req = raw();
uv_fs_req_cleanup(this->raw());
uv_fs_mkdir(parent().raw(), req, path.data(), mode, nullptr);
return !(req->result < 0);
}
UVW_INLINE void fs_req::mkdtemp(const std::string &tpl) {
uv_fs_req_cleanup(this->raw());
uv_fs_mkdtemp(parent().raw(), raw(), tpl.data(), &fs_request_callback);
}
UVW_INLINE std::pair<bool, const char *> fs_req::mkdtemp_sync(const std::string &tpl) {
auto req = raw();
uv_fs_req_cleanup(this->raw());
uv_fs_mkdtemp(parent().raw(), req, tpl.data(), nullptr);
return std::make_pair(!(req->result < 0), req->path);
}
UVW_INLINE void fs_req::mkstemp(const std::string &tpl) {
uv_fs_req_cleanup(this->raw());
uv_fs_mkstemp(parent().raw(), raw(), tpl.data(), &fs_request_callback);
}
UVW_INLINE std::pair<bool, std::pair<std::string, std::size_t>> fs_req::mkstemp_sync(const std::string &tpl) {
std::pair<bool, std::pair<std::string, std::size_t>> ret{false, {}};
auto req = raw();
uv_fs_req_cleanup(this->raw());
uv_fs_mkstemp(parent().raw(), req, tpl.data(), nullptr);
ret.first = !(req->result < 0);
if(ret.first) {
ret.second.first = req->path;
ret.second.second = static_cast<std::size_t>(req->result);
}
return ret;
}
UVW_INLINE void fs_req::lutime(const std::string &path, time atime, time mtime) {
uv_fs_req_cleanup(this->raw());
uv_fs_lutime(parent().raw(), raw(), path.data(), atime.count(), mtime.count(), &fs_request_callback);
}
UVW_INLINE bool fs_req::lutime_sync(const std::string &path, time atime, time mtime) {
auto req = raw();
uv_fs_req_cleanup(this->raw());
uv_fs_lutime(parent().raw(), req, path.data(), atime.count(), mtime.count(), nullptr);
return !(req->result < 0);
}
UVW_INLINE void fs_req::rmdir(const std::string &path) {
uv_fs_req_cleanup(this->raw());
uv_fs_rmdir(parent().raw(), raw(), path.data(), &fs_request_callback);
}
UVW_INLINE bool fs_req::rmdir_sync(const std::string &path) {
auto req = raw();
uv_fs_req_cleanup(this->raw());
uv_fs_rmdir(parent().raw(), req, path.data(), nullptr);
return !(req->result < 0);
}
UVW_INLINE void fs_req::scandir(const std::string &path, int flags) {
uv_fs_req_cleanup(this->raw());
uv_fs_scandir(parent().raw(), raw(), path.data(), flags, &fs_request_callback);
}
UVW_INLINE std::pair<bool, std::size_t> fs_req::scandir_sync(const std::string &path, int flags) {
auto req = raw();
uv_fs_req_cleanup(this->raw());
uv_fs_scandir(parent().raw(), req, path.data(), flags, nullptr);
bool err = req->result < 0;
return std::make_pair(!err, err ? 0 : std::size_t(req->result));
}
UVW_INLINE std::pair<bool, std::pair<fs_req::entry_type, const char *>> fs_req::scandir_next() {
std::pair<bool, std::pair<entry_type, const char *>> ret{false, {entry_type::UNKNOWN, nullptr}};
uv_fs_req_cleanup(raw());
auto res = uv_fs_scandir_next(raw(), dirents);
if(UV_EOF != res) {
ret.second.first = static_cast<entry_type>(dirents[0].type);
ret.second.second = dirents[0].name;
ret.first = true;
}
return ret;
}
UVW_INLINE void fs_req::stat(const std::string &path) {
uv_fs_req_cleanup(this->raw());
uv_fs_stat(parent().raw(), raw(), path.data(), &fs_request_callback);
}
UVW_INLINE std::pair<bool, file_info> fs_req::stat_sync(const std::string &path) {
auto req = raw();
uv_fs_req_cleanup(this->raw());
uv_fs_stat(parent().raw(), req, path.data(), nullptr);
return std::make_pair(!(req->result < 0), req->statbuf);
}
UVW_INLINE void fs_req::lstat(const std::string &path) {
uv_fs_req_cleanup(this->raw());
uv_fs_lstat(parent().raw(), raw(), path.data(), &fs_request_callback);
}
UVW_INLINE std::pair<bool, file_info> fs_req::lstat_sync(const std::string &path) {
auto req = raw();
uv_fs_req_cleanup(this->raw());
uv_fs_lstat(parent().raw(), req, path.data(), nullptr);
return std::make_pair(!(req->result < 0), req->statbuf);
}
UVW_INLINE void fs_req::statfs(const std::string &path) {
uv_fs_req_cleanup(this->raw());
uv_fs_statfs(parent().raw(), raw(), path.data(), &fs_request_callback);
}
UVW_INLINE std::pair<bool, fs_info> fs_req::statfs_sync(const std::string &path) {
auto req = raw();
uv_fs_req_cleanup(this->raw());
uv_fs_statfs(parent().raw(), req, path.data(), nullptr);
return std::make_pair(!(req->result < 0), *static_cast<uv_statfs_t *>(req->ptr));
}
UVW_INLINE void fs_req::rename(const std::string &old, const std::string &path) {
uv_fs_req_cleanup(this->raw());
uv_fs_rename(parent().raw(), raw(), old.data(), path.data(), &fs_request_callback);
}
UVW_INLINE bool fs_req::rename_sync(const std::string &old, const std::string &path) {
auto req = raw();
uv_fs_req_cleanup(this->raw());
uv_fs_rename(parent().raw(), req, old.data(), path.data(), nullptr);
return !(req->result < 0);
}
UVW_INLINE void fs_req::copyfile(const std::string &old, const std::string &path, copy_file_flags flags) {
uv_fs_req_cleanup(this->raw());
uv_fs_copyfile(parent().raw(), raw(), old.data(), path.data(), static_cast<int>(flags), &fs_request_callback);
}
UVW_INLINE bool fs_req::copyfile_sync(const std::string &old, const std::string &path, copy_file_flags flags) {
auto req = raw();
uv_fs_req_cleanup(this->raw());
uv_fs_copyfile(parent().raw(), raw(), old.data(), path.data(), static_cast<int>(flags), nullptr);
return !(req->result < 0);
}
UVW_INLINE void fs_req::access(const std::string &path, int mode) {
uv_fs_req_cleanup(this->raw());
uv_fs_access(parent().raw(), raw(), path.data(), mode, &fs_request_callback);
}
UVW_INLINE bool fs_req::access_sync(const std::string &path, int mode) {
auto req = raw();
uv_fs_req_cleanup(this->raw());
uv_fs_access(parent().raw(), req, path.data(), mode, nullptr);
return !(req->result < 0);
}
UVW_INLINE void fs_req::chmod(const std::string &path, int mode) {
uv_fs_req_cleanup(this->raw());
uv_fs_chmod(parent().raw(), raw(), path.data(), mode, &fs_request_callback);
}
UVW_INLINE bool fs_req::chmod_sync(const std::string &path, int mode) {
auto req = raw();
uv_fs_req_cleanup(this->raw());
uv_fs_chmod(parent().raw(), req, path.data(), mode, nullptr);
return !(req->result < 0);
}
UVW_INLINE void fs_req::utime(const std::string &path, fs_request::time atime, fs_request::time mtime) {
uv_fs_req_cleanup(this->raw());
uv_fs_utime(parent().raw(), raw(), path.data(), atime.count(), mtime.count(), &fs_request_callback);
}
UVW_INLINE bool fs_req::utime_sync(const std::string &path, fs_request::time atime, fs_request::time mtime) {
auto req = raw();
uv_fs_req_cleanup(this->raw());
uv_fs_utime(parent().raw(), req, path.data(), atime.count(), mtime.count(), nullptr);
return !(req->result < 0);
}
UVW_INLINE void fs_req::link(const std::string &old, const std::string &path) {
uv_fs_req_cleanup(this->raw());
uv_fs_link(parent().raw(), raw(), old.data(), path.data(), &fs_request_callback);
}
UVW_INLINE bool fs_req::link_sync(const std::string &old, const std::string &path) {
auto req = raw();
uv_fs_req_cleanup(this->raw());
uv_fs_link(parent().raw(), req, old.data(), path.data(), nullptr);
return !(req->result < 0);
}
UVW_INLINE void fs_req::symlink(const std::string &old, const std::string &path, symlink_flags flags) {
uv_fs_req_cleanup(this->raw());
uv_fs_symlink(parent().raw(), raw(), old.data(), path.data(), static_cast<int>(flags), &fs_request_callback);
}
UVW_INLINE bool fs_req::symlink_sync(const std::string &old, const std::string &path, symlink_flags flags) {
auto req = raw();
uv_fs_req_cleanup(this->raw());
uv_fs_symlink(parent().raw(), req, old.data(), path.data(), static_cast<int>(flags), nullptr);
return !(req->result < 0);
}
UVW_INLINE void fs_req::readlink(const std::string &path) {
uv_fs_req_cleanup(this->raw());
uv_fs_readlink(parent().raw(), raw(), path.data(), &fs_request_callback);
}
UVW_INLINE std::pair<bool, std::pair<const char *, std::size_t>> fs_req::readlink_sync(const std::string &path) {
auto req = raw();
uv_fs_req_cleanup(this->raw());
uv_fs_readlink(parent().raw(), req, path.data(), nullptr);
bool err = req->result < 0;
return std::make_pair(!err, std::make_pair(static_cast<char *>(req->ptr), err ? 0 : std::size_t(req->result)));
}
UVW_INLINE void fs_req::realpath(const std::string &path) {
uv_fs_req_cleanup(this->raw());
uv_fs_realpath(parent().raw(), raw(), path.data(), &fs_request_callback);
}
UVW_INLINE std::pair<bool, const char *> fs_req::realpath_sync(const std::string &path) {
auto req = raw();
uv_fs_req_cleanup(this->raw());
uv_fs_realpath(parent().raw(), req, path.data(), nullptr);
return std::make_pair(!(req->result < 0), req->path);
}
UVW_INLINE void fs_req::chown(const std::string &path, uid_type uid, gid_type gid) {
uv_fs_req_cleanup(this->raw());
uv_fs_chown(parent().raw(), raw(), path.data(), uid, gid, &fs_request_callback);
}
UVW_INLINE bool fs_req::chown_sync(const std::string &path, uid_type uid, gid_type gid) {
auto req = raw();
uv_fs_req_cleanup(this->raw());
uv_fs_chown(parent().raw(), req, path.data(), uid, gid, nullptr);
return !(req->result < 0);
}
UVW_INLINE void fs_req::lchown(const std::string &path, uid_type uid, gid_type gid) {
uv_fs_req_cleanup(this->raw());
uv_fs_lchown(parent().raw(), raw(), path.data(), uid, gid, &fs_request_callback);
}
UVW_INLINE bool fs_req::lchown_sync(const std::string &path, uid_type uid, gid_type gid) {
auto req = raw();
uv_fs_req_cleanup(this->raw());
uv_fs_lchown(parent().raw(), req, path.data(), uid, gid, nullptr);
return !(req->result < 0);
}
UVW_INLINE void fs_req::opendir(const std::string &path) {
uv_fs_req_cleanup(this->raw());
uv_fs_opendir(parent().raw(), raw(), path.data(), &fs_request_callback);
}
UVW_INLINE bool fs_req::opendir_sync(const std::string &path) {
auto req = raw();
uv_fs_req_cleanup(this->raw());
uv_fs_opendir(parent().raw(), req, path.data(), nullptr);
return !(req->result < 0);
}
UVW_INLINE void fs_req::closedir() {
auto req = raw();
auto *dir = static_cast<uv_dir_t *>(req->ptr);
uv_fs_req_cleanup(this->raw());
uv_fs_closedir(parent().raw(), req, dir, &fs_request_callback);
}
UVW_INLINE bool fs_req::closedir_sync() {
auto req = raw();
auto *dir = static_cast<uv_dir_t *>(req->ptr);
uv_fs_req_cleanup(this->raw());
uv_fs_closedir(parent().raw(), req, dir, nullptr);
return !(req->result < 0);
}
UVW_INLINE void fs_req::readdir() {
auto req = raw();
auto *dir = static_cast<uv_dir_t *>(req->ptr);
dir->dirents = dirents;
dir->nentries = 1;
uv_fs_req_cleanup(this->raw());
uv_fs_readdir(parent().raw(), req, dir, &fs_request_callback);
}
UVW_INLINE std::pair<bool, std::pair<fs_req::entry_type, const char *>> fs_req::readdir_sync() {
auto req = raw();
auto *dir = static_cast<uv_dir_t *>(req->ptr);
dir->dirents = dirents;
dir->nentries = 1;
uv_fs_req_cleanup(this->raw());
uv_fs_readdir(parent().raw(), req, dir, nullptr);
return {req->result != 0, {static_cast<entry_type>(dirents[0].type), dirents[0].name}};
}
UVW_INLINE os_file_descriptor fs_helper::handle(file_handle file) noexcept {
return uv_get_osfhandle(file);
}
UVW_INLINE file_handle fs_helper::open(os_file_descriptor descriptor) noexcept {
return uv_open_osfhandle(descriptor);
}
} // namespace uvw

View File

@ -1,37 +1,2 @@
#ifdef UVW_AS_LIB
# include "fs_event.h"
#endif
#include <utility>
#include "config.h"
namespace uvw {
UVW_INLINE fs_event_event::fs_event_event(const char *pathname, details::uvw_fs_event events)
: filename{pathname}, flags{std::move(events)} {}
UVW_INLINE void fs_event_handle::start_callback(uv_fs_event_t *hndl, const char *filename, int events, int status) {
if(fs_event_handle &fsEvent = *(static_cast<fs_event_handle *>(hndl->data)); status) {
fsEvent.publish(error_event{status});
} else {
fsEvent.publish(fs_event_event{filename, details::uvw_fs_event(events)});
}
}
UVW_INLINE int fs_event_handle::init() {
return leak_if(uv_fs_event_init(parent().raw(), raw()));
}
UVW_INLINE int fs_event_handle::start(const std::string &path, event_flags flags) {
return uv_fs_event_start(raw(), &start_callback, path.data(), static_cast<uv_fs_event_flags>(flags));
}
UVW_INLINE int fs_event_handle::stop() {
return uv_fs_event_stop(raw());
}
UVW_INLINE std::string fs_event_handle::path() noexcept {
return details::try_read(&uv_fs_event_getpath, raw());
}
} // namespace uvw
#include "fs_event.h"
#include "fs_event.ipp"

View File

@ -114,7 +114,7 @@ public:
} // namespace uvw
#ifndef UVW_AS_LIB
# include "fs_event.cpp"
# include "fs_event.ipp"
#endif
#endif // UVW_FS_EVENT_INCLUDE_H

33
src/uvw/fs_event.ipp Normal file
View File

@ -0,0 +1,33 @@
#include <utility>
#include "config.h"
namespace uvw {
UVW_INLINE fs_event_event::fs_event_event(const char *pathname, details::uvw_fs_event events)
: filename{pathname}, flags{std::move(events)} {}
UVW_INLINE void fs_event_handle::start_callback(uv_fs_event_t *hndl, const char *filename, int events, int status) {
if(fs_event_handle &fsEvent = *(static_cast<fs_event_handle *>(hndl->data)); status) {
fsEvent.publish(error_event{status});
} else {
fsEvent.publish(fs_event_event{filename, details::uvw_fs_event(events)});
}
}
UVW_INLINE int fs_event_handle::init() {
return leak_if(uv_fs_event_init(parent().raw(), raw()));
}
UVW_INLINE int fs_event_handle::start(const std::string &path, event_flags flags) {
return uv_fs_event_start(raw(), &start_callback, path.data(), static_cast<uv_fs_event_flags>(flags));
}
UVW_INLINE int fs_event_handle::stop() {
return uv_fs_event_stop(raw());
}
UVW_INLINE std::string fs_event_handle::path() noexcept {
return details::try_read(&uv_fs_event_getpath, raw());
}
} // namespace uvw

View File

@ -1,37 +1,2 @@
#ifdef UVW_AS_LIB
# include "fs_poll.h"
#endif
#include <utility>
#include "config.h"
namespace uvw {
UVW_INLINE fs_poll_event::fs_poll_event(file_info previous, file_info current) noexcept
: prev{std::move(previous)}, curr{std::move(current)} {}
UVW_INLINE void fs_poll_handle::start_callback(uv_fs_poll_t *hndl, int status, const uv_stat_t *prev, const uv_stat_t *curr) {
if(fs_poll_handle &fsPoll = *(static_cast<fs_poll_handle *>(hndl->data)); status) {
fsPoll.publish(error_event{status});
} else {
fsPoll.publish(fs_poll_event{*prev, *curr});
}
}
UVW_INLINE int fs_poll_handle::init() {
return leak_if(uv_fs_poll_init(parent().raw(), raw()));
}
UVW_INLINE int fs_poll_handle::start(const std::string &file, fs_poll_handle::time interval) {
return uv_fs_poll_start(raw(), &start_callback, file.data(), interval.count());
}
UVW_INLINE int fs_poll_handle::stop() {
return uv_fs_poll_stop(raw());
}
UVW_INLINE std::string fs_poll_handle::path() noexcept {
return details::try_read(&uv_fs_poll_getpath, raw());
}
} // namespace uvw
#include "fs_poll.h"
#include "fs_poll.ipp"

View File

@ -70,7 +70,7 @@ public:
} // namespace uvw
#ifndef UVW_AS_LIB
# include "fs_poll.cpp"
# include "fs_poll.ipp"
#endif
#endif // UVW_FS_POLL_INCLUDE_H

33
src/uvw/fs_poll.ipp Normal file
View File

@ -0,0 +1,33 @@
#include <utility>
#include "config.h"
namespace uvw {
UVW_INLINE fs_poll_event::fs_poll_event(file_info previous, file_info current) noexcept
: prev{std::move(previous)}, curr{std::move(current)} {}
UVW_INLINE void fs_poll_handle::start_callback(uv_fs_poll_t *hndl, int status, const uv_stat_t *prev, const uv_stat_t *curr) {
if(fs_poll_handle &fsPoll = *(static_cast<fs_poll_handle *>(hndl->data)); status) {
fsPoll.publish(error_event{status});
} else {
fsPoll.publish(fs_poll_event{*prev, *curr});
}
}
UVW_INLINE int fs_poll_handle::init() {
return leak_if(uv_fs_poll_init(parent().raw(), raw()));
}
UVW_INLINE int fs_poll_handle::start(const std::string &file, fs_poll_handle::time interval) {
return uv_fs_poll_start(raw(), &start_callback, file.data(), interval.count());
}
UVW_INLINE int fs_poll_handle::stop() {
return uv_fs_poll_stop(raw());
}
UVW_INLINE std::string fs_poll_handle::path() noexcept {
return details::try_read(&uv_fs_poll_getpath, raw());
}
} // namespace uvw

View File

@ -1,26 +1,2 @@
#ifdef UVW_AS_LIB
# include "idle.h"
#endif
#include "config.h"
namespace uvw {
UVW_INLINE void idle_handle::start_callback(uv_idle_t *hndl) {
idle_handle &idle = *(static_cast<idle_handle *>(hndl->data));
idle.publish(idle_event{});
}
UVW_INLINE int idle_handle::init() {
return leak_if(uv_idle_init(parent().raw(), raw()));
}
UVW_INLINE int idle_handle::start() {
return uv_idle_start(raw(), &start_callback);
}
UVW_INLINE int idle_handle::stop() {
return uv_idle_stop(raw());
}
} // namespace uvw
#include "idle.h"
#include "idle.ipp"

View File

@ -59,7 +59,7 @@ public:
} // namespace uvw
#ifndef UVW_AS_LIB
# include "idle.cpp"
# include "idle.ipp"
#endif
#endif // UVW_IDLE_INCLUDE_H

22
src/uvw/idle.ipp Normal file
View File

@ -0,0 +1,22 @@
#include "config.h"
namespace uvw {
UVW_INLINE void idle_handle::start_callback(uv_idle_t *hndl) {
idle_handle &idle = *(static_cast<idle_handle *>(hndl->data));
idle.publish(idle_event{});
}
UVW_INLINE int idle_handle::init() {
return leak_if(uv_idle_init(parent().raw(), raw()));
}
UVW_INLINE int idle_handle::start() {
return uv_idle_start(raw(), &start_callback);
}
UVW_INLINE int idle_handle::stop() {
return uv_idle_stop(raw());
}
} // namespace uvw

View File

@ -1,27 +1,2 @@
#ifdef UVW_AS_LIB
# include "lib.h"
#endif
#include <utility>
#include "config.h"
namespace uvw {
UVW_INLINE shared_lib::shared_lib(loop::token token, std::shared_ptr<loop> ref, const std::string &filename) noexcept
: uv_type{token, std::move(ref)} {
opened = (0 == uv_dlopen(filename.data(), raw()));
}
UVW_INLINE shared_lib::~shared_lib() noexcept {
uv_dlclose(raw());
}
UVW_INLINE shared_lib::operator bool() const noexcept {
return opened;
}
UVW_INLINE const char *shared_lib::error() const noexcept {
return uv_dlerror(raw());
}
} // namespace uvw
#include "lib.h"
#include "lib.ipp"

View File

@ -43,7 +43,9 @@ public:
static_assert(std::is_function_v<F>);
F *func;
auto err = uv_dlsym(raw(), name.data(), reinterpret_cast<void **>(&func));
if(err) { func = nullptr; }
if(err) {
func = nullptr;
}
return func;
}
@ -60,7 +62,7 @@ private:
} // namespace uvw
#ifndef UVW_AS_LIB
# include "lib.cpp"
# include "lib.ipp"
#endif
#endif // UVW_LIB_INCLUDE_H

23
src/uvw/lib.ipp Normal file
View File

@ -0,0 +1,23 @@
#include <utility>
#include "config.h"
namespace uvw {
UVW_INLINE shared_lib::shared_lib(loop::token token, std::shared_ptr<loop> ref, const std::string &filename) noexcept
: uv_type{token, std::move(ref)} {
opened = (0 == uv_dlopen(filename.data(), raw()));
}
UVW_INLINE shared_lib::~shared_lib() noexcept {
uv_dlclose(raw());
}
UVW_INLINE shared_lib::operator bool() const noexcept {
return opened;
}
UVW_INLINE const char *shared_lib::error() const noexcept {
return uv_dlerror(raw());
}
} // namespace uvw

View File

@ -1,120 +1,2 @@
#ifdef UVW_AS_LIB
# include "loop.h"
#endif
#include "config.h"
namespace uvw {
UVW_INLINE loop::loop(std::unique_ptr<uv_loop_t, deleter> ptr) noexcept
: uv_loop{std::move(ptr)} {}
UVW_INLINE std::shared_ptr<loop> loop::create() {
auto ptr = std::unique_ptr<uv_loop_t, deleter>{new uv_loop_t, [](uv_loop_t *l) { delete l; }};
auto curr = std::shared_ptr<loop>{new loop{std::move(ptr)}};
if(uv_loop_init(curr->uv_loop.get())) {
curr = nullptr;
}
return curr;
}
UVW_INLINE std::shared_ptr<loop> loop::create(uv_loop_t *res) {
auto ptr = std::unique_ptr<uv_loop_t, deleter>{res, [](uv_loop_t *) {}};
return std::shared_ptr<loop>{new loop{std::move(ptr)}};
}
UVW_INLINE std::shared_ptr<loop> loop::get_default() {
static std::weak_ptr<loop> ref;
std::shared_ptr<loop> curr;
if(ref.expired()) {
auto def = uv_default_loop();
if(def) {
auto ptr = std::unique_ptr<uv_loop_t, deleter>(def, [](uv_loop_t *) {});
curr = std::shared_ptr<loop>{new loop{std::move(ptr)}};
}
ref = curr;
} else {
curr = ref.lock();
}
return curr;
}
UVW_INLINE loop::~loop() noexcept {
if(uv_loop) {
close();
}
}
UVW_INLINE int loop::close() {
int ret = 0;
if(uv_loop) {
ret = uv_loop_close(uv_loop.get());
uv_loop.reset();
}
return ret;
}
UVW_INLINE int loop::run(run_mode mode) noexcept {
return uv_run(uv_loop.get(), static_cast<uv_run_mode>(mode));
}
UVW_INLINE bool loop::alive() const noexcept {
return !!uv_loop_alive(uv_loop.get());
}
UVW_INLINE void loop::stop() noexcept {
uv_stop(uv_loop.get());
}
UVW_INLINE int loop::descriptor() const noexcept {
return uv_backend_fd(uv_loop.get());
}
UVW_INLINE std::pair<bool, loop::time> loop::timeout() const noexcept {
auto to = uv_backend_timeout(uv_loop.get());
return std::make_pair(to == -1, time{to});
}
UVW_INLINE loop::time loop::idle_time() const noexcept {
return time{uv_metrics_idle_time(uv_loop.get())};
}
UVW_INLINE metrics_type loop::metrics() const noexcept {
metrics_type res{};
uv_metrics_info(uv_loop.get(), &res);
return res;
}
UVW_INLINE loop::time loop::now() const noexcept {
return time{uv_now(uv_loop.get())};
}
UVW_INLINE void loop::update() const noexcept {
return uv_update_time(uv_loop.get());
}
UVW_INLINE int loop::fork() noexcept {
return uv_loop_fork(uv_loop.get());
}
UVW_INLINE void loop::data(std::shared_ptr<void> ud) {
user_data = std::move(ud);
}
UVW_INLINE const uv_loop_t *loop::raw() const noexcept {
return uv_loop.get();
}
UVW_INLINE uv_loop_t *loop::raw() noexcept {
return const_cast<uv_loop_t *>(const_cast<const loop *>(this)->raw());
}
} // namespace uvw
#include "loop.h"
#include "loop.ipp"

View File

@ -433,7 +433,7 @@ private:
} // namespace uvw
#ifndef UVW_AS_LIB
# include "loop.cpp"
# include "loop.ipp"
#endif
#endif // UVW_LOOP_INCLUDE_H

116
src/uvw/loop.ipp Normal file
View File

@ -0,0 +1,116 @@
#include "config.h"
namespace uvw {
UVW_INLINE loop::loop(std::unique_ptr<uv_loop_t, deleter> ptr) noexcept
: uv_loop{std::move(ptr)} {}
UVW_INLINE std::shared_ptr<loop> loop::create() {
auto ptr = std::unique_ptr<uv_loop_t, deleter>{new uv_loop_t, [](uv_loop_t *l) { delete l; }};
auto curr = std::shared_ptr<loop>{new loop{std::move(ptr)}};
if(uv_loop_init(curr->uv_loop.get())) {
curr = nullptr;
}
return curr;
}
UVW_INLINE std::shared_ptr<loop> loop::create(uv_loop_t *res) {
auto ptr = std::unique_ptr<uv_loop_t, deleter>{res, [](uv_loop_t *) {}};
return std::shared_ptr<loop>{new loop{std::move(ptr)}};
}
UVW_INLINE std::shared_ptr<loop> loop::get_default() {
static std::weak_ptr<loop> ref;
std::shared_ptr<loop> curr;
if(ref.expired()) {
auto def = uv_default_loop();
if(def) {
auto ptr = std::unique_ptr<uv_loop_t, deleter>(def, [](uv_loop_t *) {});
curr = std::shared_ptr<loop>{new loop{std::move(ptr)}};
}
ref = curr;
} else {
curr = ref.lock();
}
return curr;
}
UVW_INLINE loop::~loop() noexcept {
if(uv_loop) {
close();
}
}
UVW_INLINE int loop::close() {
int ret = 0;
if(uv_loop) {
ret = uv_loop_close(uv_loop.get());
uv_loop.reset();
}
return ret;
}
UVW_INLINE int loop::run(run_mode mode) noexcept {
return uv_run(uv_loop.get(), static_cast<uv_run_mode>(mode));
}
UVW_INLINE bool loop::alive() const noexcept {
return !!uv_loop_alive(uv_loop.get());
}
UVW_INLINE void loop::stop() noexcept {
uv_stop(uv_loop.get());
}
UVW_INLINE int loop::descriptor() const noexcept {
return uv_backend_fd(uv_loop.get());
}
UVW_INLINE std::pair<bool, loop::time> loop::timeout() const noexcept {
auto to = uv_backend_timeout(uv_loop.get());
return std::make_pair(to == -1, time{to});
}
UVW_INLINE loop::time loop::idle_time() const noexcept {
return time{uv_metrics_idle_time(uv_loop.get())};
}
UVW_INLINE metrics_type loop::metrics() const noexcept {
metrics_type res{};
uv_metrics_info(uv_loop.get(), &res);
return res;
}
UVW_INLINE loop::time loop::now() const noexcept {
return time{uv_now(uv_loop.get())};
}
UVW_INLINE void loop::update() const noexcept {
return uv_update_time(uv_loop.get());
}
UVW_INLINE int loop::fork() noexcept {
return uv_loop_fork(uv_loop.get());
}
UVW_INLINE void loop::data(std::shared_ptr<void> ud) {
user_data = std::move(ud);
}
UVW_INLINE const uv_loop_t *loop::raw() const noexcept {
return uv_loop.get();
}
UVW_INLINE uv_loop_t *loop::raw() noexcept {
return const_cast<uv_loop_t *>(const_cast<const loop *>(this)->raw());
}
} // namespace uvw

View File

@ -1,63 +1,2 @@
#ifdef UVW_AS_LIB
# include "pipe.h"
#endif
#include <utility>
#include "config.h"
namespace uvw {
UVW_INLINE pipe_handle::pipe_handle(loop::token token, std::shared_ptr<loop> ref, bool pass)
: stream_handle{token, std::move(ref)}, ipc{pass} {}
UVW_INLINE int pipe_handle::init() {
return leak_if(uv_pipe_init(parent().raw(), raw(), ipc));
}
UVW_INLINE int pipe_handle::open(file_handle file) {
return uv_pipe_open(raw(), file);
}
UVW_INLINE int pipe_handle::bind(const std::string &name, const bool no_truncate) {
return uv_pipe_bind2(raw(), name.data(), name.size(), no_truncate * UV_PIPE_NO_TRUNCATE);
}
UVW_INLINE int pipe_handle::connect(const std::string &name, const bool no_truncate) {
auto listener = [ptr = shared_from_this()](const auto &event, const auto &) {
ptr->publish(event);
};
auto connect = parent().resource<details::connect_req>();
connect->on<error_event>(listener);
connect->on<connect_event>(listener);
unsigned int flags = no_truncate * UV_PIPE_NO_TRUNCATE;
return connect->connect(&uv_pipe_connect2, raw(), name.data(), name.size(), flags);
}
UVW_INLINE std::string pipe_handle::sock() const noexcept {
return details::try_read(&uv_pipe_getsockname, raw());
}
UVW_INLINE std::string pipe_handle::peer() const noexcept {
return details::try_read(&uv_pipe_getpeername, raw());
}
UVW_INLINE void pipe_handle::pending(int count) noexcept {
uv_pipe_pending_instances(raw(), count);
}
UVW_INLINE int pipe_handle::pending() noexcept {
return uv_pipe_pending_count(raw());
}
UVW_INLINE handle_type pipe_handle::receive() noexcept {
handle_category category = uv_pipe_pending_type(raw());
return utilities::guess_handle(category);
}
UVW_INLINE int pipe_handle::chmod(chmod_flags flags) noexcept {
return uv_pipe_chmod(raw(), static_cast<uv_poll_event>(flags));
}
} // namespace uvw
#include "pipe.h"
#include "pipe.ipp"

View File

@ -158,7 +158,7 @@ private:
} // namespace uvw
#ifndef UVW_AS_LIB
# include "pipe.cpp"
# include "pipe.ipp"
#endif
#endif // UVW_PIPE_INCLUDE_H

59
src/uvw/pipe.ipp Normal file
View File

@ -0,0 +1,59 @@
#include <utility>
#include "config.h"
namespace uvw {
UVW_INLINE pipe_handle::pipe_handle(loop::token token, std::shared_ptr<loop> ref, bool pass)
: stream_handle{token, std::move(ref)}, ipc{pass} {}
UVW_INLINE int pipe_handle::init() {
return leak_if(uv_pipe_init(parent().raw(), raw(), ipc));
}
UVW_INLINE int pipe_handle::open(file_handle file) {
return uv_pipe_open(raw(), file);
}
UVW_INLINE int pipe_handle::bind(const std::string &name, const bool no_truncate) {
return uv_pipe_bind2(raw(), name.data(), name.size(), no_truncate * UV_PIPE_NO_TRUNCATE);
}
UVW_INLINE int pipe_handle::connect(const std::string &name, const bool no_truncate) {
auto listener = [ptr = shared_from_this()](const auto &event, const auto &) {
ptr->publish(event);
};
auto connect = parent().resource<details::connect_req>();
connect->on<error_event>(listener);
connect->on<connect_event>(listener);
unsigned int flags = no_truncate * UV_PIPE_NO_TRUNCATE;
return connect->connect(&uv_pipe_connect2, raw(), name.data(), name.size(), flags);
}
UVW_INLINE std::string pipe_handle::sock() const noexcept {
return details::try_read(&uv_pipe_getsockname, raw());
}
UVW_INLINE std::string pipe_handle::peer() const noexcept {
return details::try_read(&uv_pipe_getpeername, raw());
}
UVW_INLINE void pipe_handle::pending(int count) noexcept {
uv_pipe_pending_instances(raw(), count);
}
UVW_INLINE int pipe_handle::pending() noexcept {
return uv_pipe_pending_count(raw());
}
UVW_INLINE handle_type pipe_handle::receive() noexcept {
handle_category category = uv_pipe_pending_type(raw());
return utilities::guess_handle(category);
}
UVW_INLINE int pipe_handle::chmod(chmod_flags flags) noexcept {
return uv_pipe_chmod(raw(), static_cast<uv_poll_event>(flags));
}
} // namespace uvw

View File

@ -1,43 +1,2 @@
#ifdef UVW_AS_LIB
# include "poll.h"
#endif
#include <utility>
#include "config.h"
namespace uvw {
UVW_INLINE poll_event::poll_event(details::uvw_poll_event events) noexcept
: flags{std::move(events)} {}
UVW_INLINE poll_handle::poll_handle(loop::token token, std::shared_ptr<loop> ref, int desc)
: handle{token, std::move(ref)}, tag{FD}, file_desc{desc} {}
UVW_INLINE poll_handle::poll_handle(loop::token token, std::shared_ptr<loop> ref, os_socket_handle sock)
: handle{token, std::move(ref)}, tag{SOCKET}, socket{sock} {}
UVW_INLINE void poll_handle::start_callback(uv_poll_t *hndl, int status, int events) {
if(poll_handle &poll = *(static_cast<poll_handle *>(hndl->data)); status) {
poll.publish(error_event{status});
} else {
poll.publish(poll_event{poll_event_flags(events)});
}
}
UVW_INLINE int poll_handle::init() {
if(tag == SOCKET) {
return leak_if(uv_poll_init_socket(parent().raw(), raw(), socket));
} else {
return leak_if(uv_poll_init(parent().raw(), raw(), file_desc));
}
}
UVW_INLINE int poll_handle::start(poll_event_flags flags) {
return uv_poll_start(raw(), static_cast<uv_poll_event>(flags), &start_callback);
}
UVW_INLINE int poll_handle::stop() {
return uv_poll_stop(raw());
}
} // namespace uvw
#include "poll.h"
#include "poll.ipp"

View File

@ -113,7 +113,7 @@ private:
} // namespace uvw
#ifndef UVW_AS_LIB
# include "poll.cpp"
# include "poll.ipp"
#endif
#endif // UVW_POLL_INCLUDE_H

39
src/uvw/poll.ipp Normal file
View File

@ -0,0 +1,39 @@
#include <utility>
#include "config.h"
namespace uvw {
UVW_INLINE poll_event::poll_event(details::uvw_poll_event events) noexcept
: flags{std::move(events)} {}
UVW_INLINE poll_handle::poll_handle(loop::token token, std::shared_ptr<loop> ref, int desc)
: handle{token, std::move(ref)}, tag{FD}, file_desc{desc} {}
UVW_INLINE poll_handle::poll_handle(loop::token token, std::shared_ptr<loop> ref, os_socket_handle sock)
: handle{token, std::move(ref)}, tag{SOCKET}, socket{sock} {}
UVW_INLINE void poll_handle::start_callback(uv_poll_t *hndl, int status, int events) {
if(poll_handle &poll = *(static_cast<poll_handle *>(hndl->data)); status) {
poll.publish(error_event{status});
} else {
poll.publish(poll_event{poll_event_flags(events)});
}
}
UVW_INLINE int poll_handle::init() {
if(tag == SOCKET) {
return leak_if(uv_poll_init_socket(parent().raw(), raw(), socket));
} else {
return leak_if(uv_poll_init(parent().raw(), raw(), file_desc));
}
}
UVW_INLINE int poll_handle::start(poll_event_flags flags) {
return uv_poll_start(raw(), static_cast<uv_poll_event>(flags), &start_callback);
}
UVW_INLINE int poll_handle::stop() {
return uv_poll_stop(raw());
}
} // namespace uvw

View File

@ -1,26 +1,2 @@
#ifdef UVW_AS_LIB
# include "prepare.h"
#endif
#include "config.h"
namespace uvw {
UVW_INLINE void prepare_handle::start_callback(uv_prepare_t *hndl) {
prepare_handle &prepare = *(static_cast<prepare_handle *>(hndl->data));
prepare.publish(prepare_event{});
}
UVW_INLINE int prepare_handle::init() {
return leak_if(uv_prepare_init(parent().raw(), raw()));
}
UVW_INLINE int prepare_handle::start() {
return uv_prepare_start(raw(), &start_callback);
}
UVW_INLINE int prepare_handle::stop() {
return uv_prepare_stop(raw());
}
} // namespace uvw
#include "prepare.h"
#include "prepare.ipp"

View File

@ -52,7 +52,7 @@ public:
} // namespace uvw
#ifndef UVW_AS_LIB
# include "prepare.cpp"
# include "prepare.ipp"
#endif
#endif // UVW_PREPARE_INCLUDE_H

22
src/uvw/prepare.ipp Normal file
View File

@ -0,0 +1,22 @@
#include "config.h"
namespace uvw {
UVW_INLINE void prepare_handle::start_callback(uv_prepare_t *hndl) {
prepare_handle &prepare = *(static_cast<prepare_handle *>(hndl->data));
prepare.publish(prepare_event{});
}
UVW_INLINE int prepare_handle::init() {
return leak_if(uv_prepare_init(parent().raw(), raw()));
}
UVW_INLINE int prepare_handle::start() {
return uv_prepare_start(raw(), &start_callback);
}
UVW_INLINE int prepare_handle::stop() {
return uv_prepare_stop(raw());
}
} // namespace uvw

View File

@ -1,111 +1,2 @@
#ifdef UVW_AS_LIB
# include "process.h"
#endif
#include <algorithm>
#include "config.h"
namespace uvw {
UVW_INLINE exit_event::exit_event(int64_t code, int sig) noexcept
: status{code}, signal{sig} {}
UVW_INLINE void process_handle::exit_callback(uv_process_t *hndl, int64_t exit_status, int term_signal) {
process_handle &process = *(static_cast<process_handle *>(hndl->data));
process.publish(exit_event{exit_status, term_signal});
}
UVW_INLINE process_handle::process_handle(loop::token token, std::shared_ptr<loop> ref)
: handle{token, std::move(ref)} {}
UVW_INLINE void process_handle::disable_stdio_inheritance() noexcept {
uv_disable_stdio_inheritance();
}
UVW_INLINE bool process_handle::kill(int pid, int signum) noexcept {
return (0 == uv_kill(pid, signum));
}
UVW_INLINE int process_handle::init() {
// deferred initialization: libuv initializes process handles only when
// uv_spawn is invoked and uvw stays true to the underlying library
return 0;
}
UVW_INLINE int process_handle::spawn(const char *file, char **args, char **env) {
uv_process_options_t po;
po.exit_cb = &exit_callback;
po.file = file;
po.args = args;
po.env = env;
po.cwd = po_cwd.empty() ? nullptr : po_cwd.data();
po.flags = static_cast<uv_process_flags>(po_flags);
po.uid = po_uid;
po.gid = po_gid;
std::vector<uv_stdio_container_t> poStdio;
poStdio.reserve(po_fd_stdio.size() + po_stream_stdio.size());
poStdio.insert(poStdio.begin(), po_fd_stdio.cbegin(), po_fd_stdio.cend());
poStdio.insert(poStdio.end(), po_stream_stdio.cbegin(), po_stream_stdio.cend());
po.stdio_count = static_cast<decltype(po.stdio_count)>(poStdio.size());
po.stdio = poStdio.data();
// see init member function for more details
leak_if(0);
return uv_spawn(parent().raw(), raw(), &po);
}
UVW_INLINE int process_handle::kill(int signum) {
return uv_process_kill(raw(), signum);
}
UVW_INLINE int process_handle::pid() noexcept {
return raw()->pid;
}
UVW_INLINE process_handle &process_handle::cwd(const std::string &path) noexcept {
po_cwd = path;
return *this;
}
UVW_INLINE process_handle &process_handle::flags(process_flags flags) noexcept {
po_flags = flags;
return *this;
}
UVW_INLINE process_handle &process_handle::stdio(file_handle fd, stdio_flags flags) {
auto fgs = static_cast<uv_stdio_flags>(flags);
auto actual = uvw::file_handle{fd};
auto it = std::find_if(po_fd_stdio.begin(), po_fd_stdio.end(), [actual](auto &&container) {
return static_cast<const uvw::details::uv_type_wrapper<int>>(container.data.fd) == static_cast<const uvw::details::uv_type_wrapper<int>>(actual);
});
if(it == po_fd_stdio.cend()) {
uv_stdio_container_t container;
container.flags = fgs;
container.data.fd = actual;
po_fd_stdio.push_back(std::move(container));
} else {
it->flags = fgs;
it->data.fd = actual;
}
return *this;
}
UVW_INLINE process_handle &process_handle::uid(uid_type id) {
po_uid = id;
return *this;
}
UVW_INLINE process_handle &process_handle::gid(gid_type id) {
po_gid = id;
return *this;
}
} // namespace uvw
#include "process.h"
#include "process.ipp"

View File

@ -239,7 +239,7 @@ private:
} // namespace uvw
#ifndef UVW_AS_LIB
# include "process.cpp"
# include "process.ipp"
#endif
#endif // UVW_PROCESS_INCLUDE_H

107
src/uvw/process.ipp Normal file
View File

@ -0,0 +1,107 @@
#include <algorithm>
#include "config.h"
namespace uvw {
UVW_INLINE exit_event::exit_event(int64_t code, int sig) noexcept
: status{code}, signal{sig} {}
UVW_INLINE void process_handle::exit_callback(uv_process_t *hndl, int64_t exit_status, int term_signal) {
process_handle &process = *(static_cast<process_handle *>(hndl->data));
process.publish(exit_event{exit_status, term_signal});
}
UVW_INLINE process_handle::process_handle(loop::token token, std::shared_ptr<loop> ref)
: handle{token, std::move(ref)} {}
UVW_INLINE void process_handle::disable_stdio_inheritance() noexcept {
uv_disable_stdio_inheritance();
}
UVW_INLINE bool process_handle::kill(int pid, int signum) noexcept {
return (0 == uv_kill(pid, signum));
}
UVW_INLINE int process_handle::init() {
// deferred initialization: libuv initializes process handles only when
// uv_spawn is invoked and uvw stays true to the underlying library
return 0;
}
UVW_INLINE int process_handle::spawn(const char *file, char **args, char **env) {
uv_process_options_t po;
po.exit_cb = &exit_callback;
po.file = file;
po.args = args;
po.env = env;
po.cwd = po_cwd.empty() ? nullptr : po_cwd.data();
po.flags = static_cast<uv_process_flags>(po_flags);
po.uid = po_uid;
po.gid = po_gid;
std::vector<uv_stdio_container_t> poStdio;
poStdio.reserve(po_fd_stdio.size() + po_stream_stdio.size());
poStdio.insert(poStdio.begin(), po_fd_stdio.cbegin(), po_fd_stdio.cend());
poStdio.insert(poStdio.end(), po_stream_stdio.cbegin(), po_stream_stdio.cend());
po.stdio_count = static_cast<decltype(po.stdio_count)>(poStdio.size());
po.stdio = poStdio.data();
// see init member function for more details
leak_if(0);
return uv_spawn(parent().raw(), raw(), &po);
}
UVW_INLINE int process_handle::kill(int signum) {
return uv_process_kill(raw(), signum);
}
UVW_INLINE int process_handle::pid() noexcept {
return raw()->pid;
}
UVW_INLINE process_handle &process_handle::cwd(const std::string &path) noexcept {
po_cwd = path;
return *this;
}
UVW_INLINE process_handle &process_handle::flags(process_flags flags) noexcept {
po_flags = flags;
return *this;
}
UVW_INLINE process_handle &process_handle::stdio(file_handle fd, stdio_flags flags) {
auto fgs = static_cast<uv_stdio_flags>(flags);
auto actual = uvw::file_handle{fd};
auto it = std::find_if(po_fd_stdio.begin(), po_fd_stdio.end(), [actual](auto &&container) {
return static_cast<const uvw::details::uv_type_wrapper<int>>(container.data.fd) == static_cast<const uvw::details::uv_type_wrapper<int>>(actual);
});
if(it == po_fd_stdio.cend()) {
uv_stdio_container_t container;
container.flags = fgs;
container.data.fd = actual;
po_fd_stdio.push_back(std::move(container));
} else {
it->flags = fgs;
it->data.fd = actual;
}
return *this;
}
UVW_INLINE process_handle &process_handle::uid(uid_type id) {
po_uid = id;
return *this;
}
UVW_INLINE process_handle &process_handle::gid(gid_type id) {
po_gid = id;
return *this;
}
} // namespace uvw

View File

@ -1,37 +1,2 @@
#ifdef UVW_AS_LIB
# include "signal.h"
#endif
#include "config.h"
namespace uvw {
UVW_INLINE signal_event::signal_event(int sig) noexcept
: signum{sig} {}
UVW_INLINE void signal_handle::start_callback(uv_signal_t *hndl, int signum) {
signal_handle &signal = *(static_cast<signal_handle *>(hndl->data));
signal.publish(signal_event{signum});
}
UVW_INLINE int signal_handle::init() {
return leak_if(uv_signal_init(parent().raw(), raw()));
}
UVW_INLINE int signal_handle::start(int signum) {
return uv_signal_start(raw(), &start_callback, signum);
}
UVW_INLINE int signal_handle::one_shot(int signum) {
return uv_signal_start_oneshot(raw(), &start_callback, signum);
}
UVW_INLINE int signal_handle::stop() {
return uv_signal_stop(raw());
}
UVW_INLINE int signal_handle::signal() const noexcept {
return raw()->signum;
}
} // namespace uvw
#include "signal.h"
#include "signal.ipp"

View File

@ -77,7 +77,7 @@ public:
} // namespace uvw
#ifndef UVW_AS_LIB
# include "signal.cpp"
# include "signal.ipp"
#endif
#endif // UVW_SIGNAL_INCLUDE_H

33
src/uvw/signal.ipp Normal file
View File

@ -0,0 +1,33 @@
#include "config.h"
namespace uvw {
UVW_INLINE signal_event::signal_event(int sig) noexcept
: signum{sig} {}
UVW_INLINE void signal_handle::start_callback(uv_signal_t *hndl, int signum) {
signal_handle &signal = *(static_cast<signal_handle *>(hndl->data));
signal.publish(signal_event{signum});
}
UVW_INLINE int signal_handle::init() {
return leak_if(uv_signal_init(parent().raw(), raw()));
}
UVW_INLINE int signal_handle::start(int signum) {
return uv_signal_start(raw(), &start_callback, signum);
}
UVW_INLINE int signal_handle::one_shot(int signum) {
return uv_signal_start_oneshot(raw(), &start_callback, signum);
}
UVW_INLINE int signal_handle::stop() {
return uv_signal_stop(raw());
}
UVW_INLINE int signal_handle::signal() const noexcept {
return raw()->signum;
}
} // namespace uvw

View File

@ -1,33 +1,2 @@
#ifdef UVW_AS_LIB
# include "stream.h"
#endif
#include "config.h"
namespace uvw {
UVW_INLINE data_event::data_event(std::unique_ptr<char[]> buf, std::size_t len) noexcept
: data{std::move(buf)},
length{len} {}
UVW_INLINE void details::connect_req::connect_callback(uv_connect_t *req, int status) {
if(auto ptr = reserve(req); status) {
ptr->publish(error_event{status});
} else {
ptr->publish(connect_event{});
}
}
UVW_INLINE void details::shutdown_req::shoutdown_callback(uv_shutdown_t *req, int status) {
if(auto ptr = reserve(req); status) {
ptr->publish(error_event{status});
} else {
ptr->publish(shutdown_event{});
}
}
UVW_INLINE int details::shutdown_req::shutdown(uv_stream_t *hndl) {
return this->leak_if(uv_shutdown(raw(), hndl, &shoutdown_callback));
}
} // namespace uvw
#include "stream.h"
#include "stream.ipp"

View File

@ -475,7 +475,7 @@ public:
} // namespace uvw
#ifndef UVW_AS_LIB
# include "stream.cpp"
# include "stream.ipp"
#endif
#endif // UVW_STREAM_INCLUDE_H

29
src/uvw/stream.ipp Normal file
View File

@ -0,0 +1,29 @@
#include "config.h"
namespace uvw {
UVW_INLINE data_event::data_event(std::unique_ptr<char[]> buf, std::size_t len) noexcept
: data{std::move(buf)},
length{len} {}
UVW_INLINE void details::connect_req::connect_callback(uv_connect_t *req, int status) {
if(auto ptr = reserve(req); status) {
ptr->publish(error_event{status});
} else {
ptr->publish(connect_event{});
}
}
UVW_INLINE void details::shutdown_req::shoutdown_callback(uv_shutdown_t *req, int status) {
if(auto ptr = reserve(req); status) {
ptr->publish(error_event{status});
} else {
ptr->publish(shutdown_event{});
}
}
UVW_INLINE int details::shutdown_req::shutdown(uv_stream_t *hndl) {
return this->leak_if(uv_shutdown(raw(), hndl, &shoutdown_callback));
}
} // namespace uvw

View File

@ -1,86 +1,2 @@
#ifdef UVW_AS_LIB
# include "tcp.h"
#endif
#include "config.h"
namespace uvw {
UVW_INLINE tcp_handle::tcp_handle(loop::token token, std::shared_ptr<loop> ref, unsigned int f)
: stream_handle{token, std::move(ref)}, tag{f ? FLAGS : DEFAULT}, flags{f} {}
UVW_INLINE int tcp_handle::init() {
if(tag == FLAGS) {
return leak_if(uv_tcp_init_ex(parent().raw(), raw(), flags));
} else {
return leak_if(uv_tcp_init(parent().raw(), raw()));
}
}
UVW_INLINE int tcp_handle::open(os_socket_handle socket) {
return uv_tcp_open(raw(), socket);
}
UVW_INLINE bool tcp_handle::no_delay(bool value) {
return (0 == uv_tcp_nodelay(raw(), value));
}
UVW_INLINE bool tcp_handle::keep_alive(bool enable, tcp_handle::time val) {
return (0 == uv_tcp_keepalive(raw(), enable, val.count()));
}
UVW_INLINE bool tcp_handle::simultaneous_accepts(bool enable) {
return (0 == uv_tcp_simultaneous_accepts(raw(), enable));
}
UVW_INLINE int tcp_handle::bind(const sockaddr &addr, tcp_flags opts) {
return uv_tcp_bind(raw(), &addr, static_cast<uv_tcp_flags>(opts));
}
UVW_INLINE int tcp_handle::bind(const std::string &ip, unsigned int port, tcp_flags opts) {
return bind(details::ip_addr(ip.data(), port), opts);
}
UVW_INLINE int tcp_handle::bind(socket_address addr, tcp_flags opts) {
return bind(addr.ip, addr.port, opts);
}
UVW_INLINE socket_address tcp_handle::sock() const noexcept {
sockaddr_storage storage;
int len = sizeof(sockaddr_storage);
uv_tcp_getsockname(raw(), reinterpret_cast<sockaddr *>(&storage), &len);
return details::sock_addr(storage);
}
UVW_INLINE socket_address tcp_handle::peer() const noexcept {
sockaddr_storage storage;
int len = sizeof(sockaddr_storage);
uv_tcp_getpeername(raw(), reinterpret_cast<sockaddr *>(&storage), &len);
return details::sock_addr(storage);
}
UVW_INLINE int tcp_handle::connect(const std::string &ip, unsigned int port) {
return connect(details::ip_addr(ip.data(), port));
}
UVW_INLINE int tcp_handle::connect(socket_address addr) {
return connect(addr.ip, addr.port);
}
UVW_INLINE int tcp_handle::connect(const sockaddr &addr) {
auto listener = [ptr = shared_from_this()](const auto &event, const auto &) {
ptr->publish(event);
};
auto req = parent().resource<details::connect_req>();
req->on<error_event>(listener);
req->on<connect_event>(listener);
return req->connect(&uv_tcp_connect, raw(), &addr);
}
UVW_INLINE int tcp_handle::close_reset() {
return uv_tcp_close_reset(raw(), &this->close_callback);
}
} // namespace uvw
#include "tcp.h"
#include "tcp.ipp"

View File

@ -227,7 +227,7 @@ private:
} // namespace uvw
#ifndef UVW_AS_LIB
# include "tcp.cpp"
# include "tcp.ipp"
#endif
#endif // UVW_TCP_INCLUDE_H

82
src/uvw/tcp.ipp Normal file
View File

@ -0,0 +1,82 @@
#include "config.h"
namespace uvw {
UVW_INLINE tcp_handle::tcp_handle(loop::token token, std::shared_ptr<loop> ref, unsigned int f)
: stream_handle{token, std::move(ref)}, tag{f ? FLAGS : DEFAULT}, flags{f} {}
UVW_INLINE int tcp_handle::init() {
if(tag == FLAGS) {
return leak_if(uv_tcp_init_ex(parent().raw(), raw(), flags));
} else {
return leak_if(uv_tcp_init(parent().raw(), raw()));
}
}
UVW_INLINE int tcp_handle::open(os_socket_handle socket) {
return uv_tcp_open(raw(), socket);
}
UVW_INLINE bool tcp_handle::no_delay(bool value) {
return (0 == uv_tcp_nodelay(raw(), value));
}
UVW_INLINE bool tcp_handle::keep_alive(bool enable, tcp_handle::time val) {
return (0 == uv_tcp_keepalive(raw(), enable, val.count()));
}
UVW_INLINE bool tcp_handle::simultaneous_accepts(bool enable) {
return (0 == uv_tcp_simultaneous_accepts(raw(), enable));
}
UVW_INLINE int tcp_handle::bind(const sockaddr &addr, tcp_flags opts) {
return uv_tcp_bind(raw(), &addr, static_cast<uv_tcp_flags>(opts));
}
UVW_INLINE int tcp_handle::bind(const std::string &ip, unsigned int port, tcp_flags opts) {
return bind(details::ip_addr(ip.data(), port), opts);
}
UVW_INLINE int tcp_handle::bind(socket_address addr, tcp_flags opts) {
return bind(addr.ip, addr.port, opts);
}
UVW_INLINE socket_address tcp_handle::sock() const noexcept {
sockaddr_storage storage;
int len = sizeof(sockaddr_storage);
uv_tcp_getsockname(raw(), reinterpret_cast<sockaddr *>(&storage), &len);
return details::sock_addr(storage);
}
UVW_INLINE socket_address tcp_handle::peer() const noexcept {
sockaddr_storage storage;
int len = sizeof(sockaddr_storage);
uv_tcp_getpeername(raw(), reinterpret_cast<sockaddr *>(&storage), &len);
return details::sock_addr(storage);
}
UVW_INLINE int tcp_handle::connect(const std::string &ip, unsigned int port) {
return connect(details::ip_addr(ip.data(), port));
}
UVW_INLINE int tcp_handle::connect(socket_address addr) {
return connect(addr.ip, addr.port);
}
UVW_INLINE int tcp_handle::connect(const sockaddr &addr) {
auto listener = [ptr = shared_from_this()](const auto &event, const auto &) {
ptr->publish(event);
};
auto req = parent().resource<details::connect_req>();
req->on<error_event>(listener);
req->on<connect_event>(listener);
return req->connect(&uv_tcp_connect, raw(), &addr);
}
UVW_INLINE int tcp_handle::close_reset() {
return uv_tcp_close_reset(raw(), &this->close_callback);
}
} // namespace uvw

View File

@ -1,189 +1,2 @@
#ifdef UVW_AS_LIB
# include "thread.h"
#endif
#include "config.h"
namespace uvw {
UVW_INLINE thread::thread(loop::token token, std::shared_ptr<loop> ref, task t, std::shared_ptr<void> d) noexcept
: uv_type{token, std::move(ref)},
data{std::move(d)},
func{std::move(t)} {}
UVW_INLINE void thread::create_callback(void *arg) {
thread &curr = *(static_cast<thread *>(arg));
curr.func(curr.data);
}
UVW_INLINE thread::type thread::self() noexcept {
return uv_thread_self();
}
UVW_INLINE int thread::getcpu() noexcept {
return uv_thread_getcpu();
}
UVW_INLINE bool thread::equal(const thread &tl, const thread &tr) noexcept {
return !(0 == uv_thread_equal(tl.raw(), tr.raw()));
}
UVW_INLINE bool thread::priority(const thread &tl, thread_priority val) noexcept {
return (uv_thread_setpriority(*tl.raw(), static_cast<std::underlying_type_t<thread_priority>>(val)) == 0);
}
UVW_INLINE std::pair<bool, thread::thread_priority> thread::priority(const thread &tl) noexcept {
int prio{};
const bool res = (uv_thread_getpriority(*tl.raw(), &prio) == 0);
return {res, thread_priority{static_cast<std::underlying_type_t<thread_priority>>(prio)}};
}
UVW_INLINE thread::~thread() noexcept {
join();
}
UVW_INLINE bool thread::run() noexcept {
return (0 == uv_thread_create(raw(), &create_callback, this));
}
UVW_INLINE bool thread::run(create_flags opts, std::size_t stack) noexcept {
uv_thread_options_t params{static_cast<unsigned int>(opts), stack};
return (0 == uv_thread_create_ex(raw(), &params, &create_callback, this));
}
UVW_INLINE bool thread::join() noexcept {
return (0 == uv_thread_join(raw()));
}
UVW_INLINE thread_local_storage::thread_local_storage(loop::token token, std::shared_ptr<loop> ref) noexcept
: uv_type{token, std::move(ref)} {
uv_key_create(uv_type::raw());
}
UVW_INLINE thread_local_storage::~thread_local_storage() noexcept {
uv_key_delete(uv_type::raw());
}
UVW_INLINE uv_once_t *once::guard() noexcept {
static uv_once_t once = UV_ONCE_INIT;
return &once;
}
UVW_INLINE mutex::mutex(loop::token token, std::shared_ptr<loop> ref, bool recursive) noexcept
: uv_type{token, std::move(ref)} {
if(recursive) {
uv_mutex_init_recursive(raw());
} else {
uv_mutex_init(raw());
}
}
UVW_INLINE mutex::~mutex() noexcept {
uv_mutex_destroy(raw());
}
UVW_INLINE void mutex::lock() noexcept {
uv_mutex_lock(raw());
}
UVW_INLINE bool mutex::try_lock() noexcept {
return (0 == uv_mutex_trylock(raw()));
}
UVW_INLINE void mutex::unlock() noexcept {
uv_mutex_unlock(raw());
}
UVW_INLINE rwlock::rwlock(loop::token token, std::shared_ptr<loop> ref) noexcept
: uv_type{token, std::move(ref)} {
uv_rwlock_init(raw());
}
UVW_INLINE rwlock::~rwlock() noexcept {
uv_rwlock_destroy(raw());
}
UVW_INLINE void rwlock::rdlock() noexcept {
uv_rwlock_rdlock(raw());
}
UVW_INLINE bool rwlock::try_rdlock() noexcept {
return (0 == uv_rwlock_tryrdlock(raw()));
}
UVW_INLINE void rwlock::rdunlock() noexcept {
uv_rwlock_rdunlock(raw());
}
UVW_INLINE void rwlock::wrlock() noexcept {
uv_rwlock_wrlock(raw());
}
UVW_INLINE bool rwlock::try_wrlock() noexcept {
return (0 == uv_rwlock_trywrlock(raw()));
}
UVW_INLINE void rwlock::wrunlock() noexcept {
uv_rwlock_wrunlock(raw());
}
UVW_INLINE semaphore::semaphore(loop::token token, std::shared_ptr<loop> ref, unsigned int value) noexcept
: uv_type{token, std::move(ref)} {
uv_sem_init(raw(), value);
}
UVW_INLINE semaphore::~semaphore() noexcept {
uv_sem_destroy(raw());
}
UVW_INLINE void semaphore::post() noexcept {
uv_sem_post(raw());
}
UVW_INLINE void semaphore::wait() noexcept {
uv_sem_wait(raw());
}
UVW_INLINE bool semaphore::try_wait() noexcept {
return (0 == uv_sem_trywait(raw()));
}
UVW_INLINE condition::condition(loop::token token, std::shared_ptr<loop> ref) noexcept
: uv_type{token, std::move(ref)} {
uv_cond_init(raw());
}
UVW_INLINE condition::~condition() noexcept {
uv_cond_destroy(raw());
}
UVW_INLINE void condition::signal() noexcept {
uv_cond_signal(raw());
}
UVW_INLINE void condition::broadcast() noexcept {
uv_cond_broadcast(raw());
}
UVW_INLINE void condition::wait(mutex &mtx) noexcept {
uv_cond_wait(raw(), mtx.raw());
}
UVW_INLINE bool condition::timed_wait(mutex &mtx, uint64_t timeout) noexcept {
return (0 == uv_cond_timedwait(raw(), mtx.raw(), timeout));
}
UVW_INLINE barrier::barrier(loop::token token, std::shared_ptr<loop> ref, unsigned int count) noexcept
: uv_type{token, std::move(ref)} {
uv_barrier_init(raw(), count);
}
UVW_INLINE barrier::~barrier() noexcept {
uv_barrier_destroy(raw());
}
UVW_INLINE bool barrier::wait() noexcept {
return (0 == uv_barrier_wait(raw()));
}
} // namespace uvw
#include "thread.h"
#include "thread.ipp"

View File

@ -381,7 +381,7 @@ public:
} // namespace uvw
#ifndef UVW_AS_LIB
# include "thread.cpp"
# include "thread.ipp"
#endif
#endif // UVW_THREAD_INCLUDE_H

185
src/uvw/thread.ipp Normal file
View File

@ -0,0 +1,185 @@
#include "config.h"
namespace uvw {
UVW_INLINE thread::thread(loop::token token, std::shared_ptr<loop> ref, task t, std::shared_ptr<void> d) noexcept
: uv_type{token, std::move(ref)},
data{std::move(d)},
func{std::move(t)} {}
UVW_INLINE void thread::create_callback(void *arg) {
thread &curr = *(static_cast<thread *>(arg));
curr.func(curr.data);
}
UVW_INLINE thread::type thread::self() noexcept {
return uv_thread_self();
}
UVW_INLINE int thread::getcpu() noexcept {
return uv_thread_getcpu();
}
UVW_INLINE bool thread::equal(const thread &tl, const thread &tr) noexcept {
return !(0 == uv_thread_equal(tl.raw(), tr.raw()));
}
UVW_INLINE bool thread::priority(const thread &tl, thread_priority val) noexcept {
return (uv_thread_setpriority(*tl.raw(), static_cast<std::underlying_type_t<thread_priority>>(val)) == 0);
}
UVW_INLINE std::pair<bool, thread::thread_priority> thread::priority(const thread &tl) noexcept {
int prio{};
const bool res = (uv_thread_getpriority(*tl.raw(), &prio) == 0);
return {res, thread_priority{static_cast<std::underlying_type_t<thread_priority>>(prio)}};
}
UVW_INLINE thread::~thread() noexcept {
join();
}
UVW_INLINE bool thread::run() noexcept {
return (0 == uv_thread_create(raw(), &create_callback, this));
}
UVW_INLINE bool thread::run(create_flags opts, std::size_t stack) noexcept {
uv_thread_options_t params{static_cast<unsigned int>(opts), stack};
return (0 == uv_thread_create_ex(raw(), &params, &create_callback, this));
}
UVW_INLINE bool thread::join() noexcept {
return (0 == uv_thread_join(raw()));
}
UVW_INLINE thread_local_storage::thread_local_storage(loop::token token, std::shared_ptr<loop> ref) noexcept
: uv_type{token, std::move(ref)} {
uv_key_create(uv_type::raw());
}
UVW_INLINE thread_local_storage::~thread_local_storage() noexcept {
uv_key_delete(uv_type::raw());
}
UVW_INLINE uv_once_t *once::guard() noexcept {
static uv_once_t once = UV_ONCE_INIT;
return &once;
}
UVW_INLINE mutex::mutex(loop::token token, std::shared_ptr<loop> ref, bool recursive) noexcept
: uv_type{token, std::move(ref)} {
if(recursive) {
uv_mutex_init_recursive(raw());
} else {
uv_mutex_init(raw());
}
}
UVW_INLINE mutex::~mutex() noexcept {
uv_mutex_destroy(raw());
}
UVW_INLINE void mutex::lock() noexcept {
uv_mutex_lock(raw());
}
UVW_INLINE bool mutex::try_lock() noexcept {
return (0 == uv_mutex_trylock(raw()));
}
UVW_INLINE void mutex::unlock() noexcept {
uv_mutex_unlock(raw());
}
UVW_INLINE rwlock::rwlock(loop::token token, std::shared_ptr<loop> ref) noexcept
: uv_type{token, std::move(ref)} {
uv_rwlock_init(raw());
}
UVW_INLINE rwlock::~rwlock() noexcept {
uv_rwlock_destroy(raw());
}
UVW_INLINE void rwlock::rdlock() noexcept {
uv_rwlock_rdlock(raw());
}
UVW_INLINE bool rwlock::try_rdlock() noexcept {
return (0 == uv_rwlock_tryrdlock(raw()));
}
UVW_INLINE void rwlock::rdunlock() noexcept {
uv_rwlock_rdunlock(raw());
}
UVW_INLINE void rwlock::wrlock() noexcept {
uv_rwlock_wrlock(raw());
}
UVW_INLINE bool rwlock::try_wrlock() noexcept {
return (0 == uv_rwlock_trywrlock(raw()));
}
UVW_INLINE void rwlock::wrunlock() noexcept {
uv_rwlock_wrunlock(raw());
}
UVW_INLINE semaphore::semaphore(loop::token token, std::shared_ptr<loop> ref, unsigned int value) noexcept
: uv_type{token, std::move(ref)} {
uv_sem_init(raw(), value);
}
UVW_INLINE semaphore::~semaphore() noexcept {
uv_sem_destroy(raw());
}
UVW_INLINE void semaphore::post() noexcept {
uv_sem_post(raw());
}
UVW_INLINE void semaphore::wait() noexcept {
uv_sem_wait(raw());
}
UVW_INLINE bool semaphore::try_wait() noexcept {
return (0 == uv_sem_trywait(raw()));
}
UVW_INLINE condition::condition(loop::token token, std::shared_ptr<loop> ref) noexcept
: uv_type{token, std::move(ref)} {
uv_cond_init(raw());
}
UVW_INLINE condition::~condition() noexcept {
uv_cond_destroy(raw());
}
UVW_INLINE void condition::signal() noexcept {
uv_cond_signal(raw());
}
UVW_INLINE void condition::broadcast() noexcept {
uv_cond_broadcast(raw());
}
UVW_INLINE void condition::wait(mutex &mtx) noexcept {
uv_cond_wait(raw(), mtx.raw());
}
UVW_INLINE bool condition::timed_wait(mutex &mtx, uint64_t timeout) noexcept {
return (0 == uv_cond_timedwait(raw(), mtx.raw(), timeout));
}
UVW_INLINE barrier::barrier(loop::token token, std::shared_ptr<loop> ref, unsigned int count) noexcept
: uv_type{token, std::move(ref)} {
uv_barrier_init(raw(), count);
}
UVW_INLINE barrier::~barrier() noexcept {
uv_barrier_destroy(raw());
}
UVW_INLINE bool barrier::wait() noexcept {
return (0 == uv_barrier_wait(raw()));
}
} // namespace uvw

View File

@ -1,42 +1,2 @@
#ifdef UVW_AS_LIB
# include "timer.h"
#endif
#include "config.h"
namespace uvw {
UVW_INLINE void timer_handle::start_callback(uv_timer_t *hndl) {
timer_handle &timer = *(static_cast<timer_handle *>(hndl->data));
timer.publish(timer_event{});
}
UVW_INLINE int timer_handle::init() {
return leak_if(uv_timer_init(parent().raw(), raw()));
}
UVW_INLINE int timer_handle::start(timer_handle::time timeout, timer_handle::time repeat) {
return uv_timer_start(raw(), &start_callback, timeout.count(), repeat.count());
}
UVW_INLINE int timer_handle::stop() {
return uv_timer_stop(raw());
}
UVW_INLINE int timer_handle::again() {
return uv_timer_again(raw());
}
UVW_INLINE void timer_handle::repeat(timer_handle::time repeat) {
uv_timer_set_repeat(raw(), repeat.count());
}
UVW_INLINE timer_handle::time timer_handle::repeat() {
return time{uv_timer_get_repeat(raw())};
}
UVW_INLINE timer_handle::time timer_handle::due_in() {
return time{uv_timer_get_due_in(raw())};
}
} // namespace uvw
#include "timer.h"
#include "timer.ipp"

View File

@ -1,8 +1,8 @@
#ifndef UVW_TIMER_INCLUDE_H
#define UVW_TIMER_INCLUDE_H
#include <cstdint>
#include <chrono>
#include <cstdint>
#include <uv.h>
#include "handle.hpp"
#include "loop.h"
@ -105,7 +105,7 @@ public:
} // namespace uvw
#ifndef UVW_AS_LIB
# include "timer.cpp"
# include "timer.ipp"
#endif
#endif // UVW_TIMER_INCLUDE_H

38
src/uvw/timer.ipp Normal file
View File

@ -0,0 +1,38 @@
#include "config.h"
namespace uvw {
UVW_INLINE void timer_handle::start_callback(uv_timer_t *hndl) {
timer_handle &timer = *(static_cast<timer_handle *>(hndl->data));
timer.publish(timer_event{});
}
UVW_INLINE int timer_handle::init() {
return leak_if(uv_timer_init(parent().raw(), raw()));
}
UVW_INLINE int timer_handle::start(timer_handle::time timeout, timer_handle::time repeat) {
return uv_timer_start(raw(), &start_callback, timeout.count(), repeat.count());
}
UVW_INLINE int timer_handle::stop() {
return uv_timer_stop(raw());
}
UVW_INLINE int timer_handle::again() {
return uv_timer_again(raw());
}
UVW_INLINE void timer_handle::repeat(timer_handle::time repeat) {
uv_timer_set_repeat(raw(), repeat.count());
}
UVW_INLINE timer_handle::time timer_handle::repeat() {
return time{uv_timer_get_repeat(raw())};
}
UVW_INLINE timer_handle::time timer_handle::due_in() {
return time{uv_timer_get_due_in(raw())};
}
} // namespace uvw

View File

@ -1,67 +1,2 @@
#ifdef UVW_AS_LIB
# include "tty.h"
#endif
#include <utility>
#include "config.h"
namespace uvw {
UVW_INLINE details::reset_mode_memo::~reset_mode_memo() {
uv_tty_reset_mode();
}
UVW_INLINE tty_handle::tty_handle(loop::token token, std::shared_ptr<loop> ref, file_handle desc, bool readable)
: stream_handle{token, std::move(ref)},
memo{mode_memo_handler()},
fd{desc},
rw{readable} {}
UVW_INLINE std::shared_ptr<details::reset_mode_memo> tty_handle::mode_memo_handler() {
static std::weak_ptr<details::reset_mode_memo> weak;
auto shared = weak.lock();
if(!shared) { weak = shared = std::make_shared<details::reset_mode_memo>(); }
return shared;
}
UVW_INLINE int tty_handle::init() {
return leak_if(uv_tty_init(parent().raw(), raw(), fd, rw));
}
UVW_INLINE bool tty_handle::mode(tty_handle::tty_mode m) {
return (0 == uv_tty_set_mode(raw(), static_cast<uv_tty_mode_t>(m)));
}
UVW_INLINE bool tty_handle::reset_mode() noexcept {
return (0 == uv_tty_reset_mode());
}
UVW_INLINE win_size tty_handle::get_win_size() {
win_size size;
if(0 != uv_tty_get_winsize(raw(), &size.width, &size.height)) {
size.width = -1;
size.height = -1;
}
return size;
}
UVW_INLINE void tty_handle::vterm_state(tty_handle::tty_vtermstate s) const noexcept {
switch(s) {
case tty_vtermstate::SUPPORTED:
uv_tty_set_vterm_state(uv_tty_vtermstate_t::UV_TTY_SUPPORTED);
break;
case tty_vtermstate::UNSUPPORTED:
uv_tty_set_vterm_state(uv_tty_vtermstate_t::UV_TTY_UNSUPPORTED);
break;
}
}
UVW_INLINE tty_handle::tty_vtermstate tty_handle::vterm_state() const noexcept {
uv_tty_vtermstate_t state;
uv_tty_get_vterm_state(&state);
return tty_vtermstate{state};
}
} // namespace uvw
#include "tty.h"
#include "tty.ipp"

View File

@ -140,7 +140,7 @@ private:
} // namespace uvw
#ifndef UVW_AS_LIB
# include "tty.cpp"
# include "tty.ipp"
#endif
#endif // UVW_TTY_INCLUDE_H

65
src/uvw/tty.ipp Normal file
View File

@ -0,0 +1,65 @@
#include <utility>
#include "config.h"
namespace uvw {
UVW_INLINE details::reset_mode_memo::~reset_mode_memo() {
uv_tty_reset_mode();
}
UVW_INLINE tty_handle::tty_handle(loop::token token, std::shared_ptr<loop> ref, file_handle desc, bool readable)
: stream_handle{token, std::move(ref)},
memo{mode_memo_handler()},
fd{desc},
rw{readable} {}
UVW_INLINE std::shared_ptr<details::reset_mode_memo> tty_handle::mode_memo_handler() {
static std::weak_ptr<details::reset_mode_memo> weak;
auto shared = weak.lock();
if(!shared) {
weak = shared = std::make_shared<details::reset_mode_memo>();
}
return shared;
}
UVW_INLINE int tty_handle::init() {
return leak_if(uv_tty_init(parent().raw(), raw(), fd, rw));
}
UVW_INLINE bool tty_handle::mode(tty_handle::tty_mode m) {
return (0 == uv_tty_set_mode(raw(), static_cast<uv_tty_mode_t>(m)));
}
UVW_INLINE bool tty_handle::reset_mode() noexcept {
return (0 == uv_tty_reset_mode());
}
UVW_INLINE win_size tty_handle::get_win_size() {
win_size size;
if(0 != uv_tty_get_winsize(raw(), &size.width, &size.height)) {
size.width = -1;
size.height = -1;
}
return size;
}
UVW_INLINE void tty_handle::vterm_state(tty_handle::tty_vtermstate s) const noexcept {
switch(s) {
case tty_vtermstate::SUPPORTED:
uv_tty_set_vterm_state(uv_tty_vtermstate_t::UV_TTY_SUPPORTED);
break;
case tty_vtermstate::UNSUPPORTED:
uv_tty_set_vterm_state(uv_tty_vtermstate_t::UV_TTY_UNSUPPORTED);
break;
}
}
UVW_INLINE tty_handle::tty_vtermstate tty_handle::vterm_state() const noexcept {
uv_tty_vtermstate_t state;
uv_tty_get_vterm_state(&state);
return tty_vtermstate{state};
}
} // namespace uvw

View File

@ -1,216 +1,2 @@
#ifdef UVW_AS_LIB
# include "udp.h"
#endif
#include "config.h"
namespace uvw {
UVW_INLINE udp_data_event::udp_data_event(socket_address sndr, std::unique_ptr<char[]> buf, std::size_t len, bool part) noexcept
: data{std::move(buf)},
length{len},
sender{std::move(sndr)},
partial{part} {}
UVW_INLINE void details::send_req::udp_send_callback(uv_udp_send_t *req, int status) {
if(auto ptr = reserve(req); status) {
ptr->publish(error_event{status});
} else {
ptr->publish(send_event{});
}
}
UVW_INLINE details::send_req::send_req(loop::token token, std::shared_ptr<loop> parent, std::unique_ptr<char[], deleter> dt, unsigned int len)
: request{token, std::move(parent)},
data{std::move(dt)},
buf{uv_buf_init(data.get(), len)} {}
UVW_INLINE int details::send_req::send(uv_udp_t *hndl, const struct sockaddr *addr) {
return this->leak_if(uv_udp_send(raw(), hndl, &buf, 1, addr, &udp_send_callback));
}
UVW_INLINE void udp_handle::recv_callback(uv_udp_t *hndl, ssize_t nread, const uv_buf_t *buf, const sockaddr *addr, unsigned flags) {
udp_handle &udp = *(static_cast<udp_handle *>(hndl->data));
// data will be destroyed no matter of what the value of nread is
std::unique_ptr<char[]> data{buf->base};
if(nread > 0) {
// data available (can be truncated)
udp.publish(udp_data_event{details::sock_addr(*addr), std::move(data), static_cast<std::size_t>(nread), !(0 == (flags & UV_UDP_PARTIAL))});
} else if(nread == 0 && addr == nullptr) {
// no more data to be read, doing nothing is fine
} else if(nread == 0 && addr != nullptr) {
// empty udp packet
udp.publish(udp_data_event{details::sock_addr(*addr), std::move(data), static_cast<std::size_t>(nread), false});
} else {
// transmission error
udp.publish(error_event(nread));
}
}
UVW_INLINE udp_handle::udp_handle(loop::token token, std::shared_ptr<loop> ref, unsigned int f)
: handle{token, std::move(ref)}, tag{FLAGS}, flags{f} {}
UVW_INLINE int udp_handle::init() {
if(tag == FLAGS) {
return leak_if(uv_udp_init_ex(parent().raw(), raw(), flags));
} else {
return leak_if(uv_udp_init(parent().raw(), raw()));
}
}
UVW_INLINE int udp_handle::open(os_socket_handle socket) {
return uv_udp_open(raw(), socket);
}
UVW_INLINE int udp_handle::connect(const sockaddr &addr) {
return uv_udp_connect(raw(), &addr);
}
UVW_INLINE int udp_handle::connect(const std::string &ip, unsigned int port) {
return connect(details::ip_addr(ip.data(), port));
}
UVW_INLINE int udp_handle::connect(socket_address addr) {
return connect(addr.ip, addr.port);
}
UVW_INLINE int udp_handle::disconnect() {
return uv_udp_connect(raw(), nullptr);
}
UVW_INLINE socket_address udp_handle::peer() const noexcept {
sockaddr_storage storage;
int len = sizeof(sockaddr_storage);
uv_udp_getpeername(raw(), reinterpret_cast<sockaddr *>(&storage), &len);
return details::sock_addr(storage);
}
UVW_INLINE int udp_handle::bind(const sockaddr &addr, udp_handle::udp_flags opts) {
return uv_udp_bind(raw(), &addr, static_cast<uv_udp_flags>(opts));
}
UVW_INLINE int udp_handle::bind(const std::string &ip, unsigned int port, udp_flags opts) {
return bind(details::ip_addr(ip.data(), port), opts);
}
UVW_INLINE int udp_handle::bind(socket_address addr, udp_flags opts) {
return bind(addr.ip, addr.port, opts);
}
UVW_INLINE socket_address udp_handle::sock() const noexcept {
sockaddr_storage storage;
int len = sizeof(sockaddr_storage);
uv_udp_getsockname(raw(), reinterpret_cast<sockaddr *>(&storage), &len);
return details::sock_addr(storage);
}
UVW_INLINE bool udp_handle::multicast_membership(const std::string &multicast, const std::string &iface, membership ms) {
return (0 == uv_udp_set_membership(raw(), multicast.data(), iface.data(), static_cast<uv_membership>(ms)));
}
UVW_INLINE bool udp_handle::multicast_loop(bool enable) {
return (0 == uv_udp_set_multicast_loop(raw(), enable));
}
UVW_INLINE bool udp_handle::multicast_ttl(int val) {
return (0 == uv_udp_set_multicast_ttl(raw(), val > 255 ? 255 : val));
}
UVW_INLINE bool udp_handle::multicast_interface(const std::string &iface) {
return (0 == uv_udp_set_multicast_interface(raw(), iface.data()));
}
UVW_INLINE bool udp_handle::broadcast(bool enable) {
return (0 == uv_udp_set_broadcast(raw(), enable));
}
UVW_INLINE bool udp_handle::ttl(int val) {
return (0 == uv_udp_set_ttl(raw(), val > 255 ? 255 : val));
}
UVW_INLINE int udp_handle::send(const sockaddr &addr, std::unique_ptr<char[]> data, unsigned int len) {
auto req = parent().resource<details::send_req>(std::unique_ptr<char[], details::send_req::deleter>{data.release(), [](char *ptr) { delete[] ptr; }}, len);
auto listener = [ptr = shared_from_this()](const auto &event, const auto &) {
ptr->publish(event);
};
req->on<error_event>(listener);
req->on<send_event>(listener);
return req->send(raw(), &addr);
}
UVW_INLINE int udp_handle::send(const std::string &ip, unsigned int port, std::unique_ptr<char[]> data, unsigned int len) {
return send(details::ip_addr(ip.data(), port), std::move(data), len);
}
UVW_INLINE int udp_handle::send(socket_address addr, std::unique_ptr<char[]> data, unsigned int len) {
return send(addr.ip, addr.port, std::move(data), len);
}
UVW_INLINE int udp_handle::send(const sockaddr &addr, char *data, unsigned int len) {
auto req = parent().resource<details::send_req>(std::unique_ptr<char[], details::send_req::deleter>{data, [](char *) {}}, len);
auto listener = [ptr = shared_from_this()](const auto &event, const auto &) {
ptr->publish(event);
};
req->on<error_event>(listener);
req->on<send_event>(listener);
return req->send(raw(), &addr);
}
UVW_INLINE int udp_handle::send(const std::string &ip, unsigned int port, char *data, unsigned int len) {
return send(details::ip_addr(ip.data(), port), data, len);
}
UVW_INLINE int udp_handle::send(socket_address addr, char *data, unsigned int len) {
return send(addr.ip, addr.port, data, len);
}
UVW_INLINE int udp_handle::try_send(const sockaddr &addr, std::unique_ptr<char[]> data, unsigned int len) {
uv_buf_t bufs[] = {uv_buf_init(data.get(), len)};
return uv_udp_try_send(raw(), bufs, 1, &addr);
}
UVW_INLINE int udp_handle::try_send(const std::string &ip, unsigned int port, std::unique_ptr<char[]> data, unsigned int len) {
return try_send(details::ip_addr(ip.data(), port), std::move(data), len);
}
UVW_INLINE int udp_handle::try_send(socket_address addr, std::unique_ptr<char[]> data, unsigned int len) {
return try_send(addr.ip, addr.port, std::move(data), len);
}
UVW_INLINE int udp_handle::try_send(const sockaddr &addr, char *data, unsigned int len) {
uv_buf_t bufs[] = {uv_buf_init(data, len)};
return uv_udp_try_send(raw(), bufs, 1, &addr);
}
UVW_INLINE int udp_handle::try_send(const std::string &ip, unsigned int port, char *data, unsigned int len) {
return try_send(details::ip_addr(ip.data(), port), data, len);
}
UVW_INLINE int udp_handle::try_send(socket_address addr, char *data, unsigned int len) {
return try_send(addr.ip, addr.port, data, len);
}
UVW_INLINE int udp_handle::recv() {
return uv_udp_recv_start(raw(), &details::common_alloc_callback, &recv_callback);
}
UVW_INLINE int udp_handle::stop() {
return uv_udp_recv_stop(raw());
}
UVW_INLINE size_t udp_handle::send_queue_size() const noexcept {
return uv_udp_get_send_queue_size(raw());
}
UVW_INLINE size_t udp_handle::send_queue_count() const noexcept {
return uv_udp_get_send_queue_count(raw());
}
} // namespace uvw
#include "udp.h"
#include "udp.ipp"

View File

@ -556,7 +556,7 @@ private:
} // namespace uvw
#ifndef UVW_AS_LIB
# include "udp.cpp"
# include "udp.ipp"
#endif
#endif // UVW_UDP_INCLUDE_H

212
src/uvw/udp.ipp Normal file
View File

@ -0,0 +1,212 @@
#include "config.h"
namespace uvw {
UVW_INLINE udp_data_event::udp_data_event(socket_address sndr, std::unique_ptr<char[]> buf, std::size_t len, bool part) noexcept
: data{std::move(buf)},
length{len},
sender{std::move(sndr)},
partial{part} {}
UVW_INLINE void details::send_req::udp_send_callback(uv_udp_send_t *req, int status) {
if(auto ptr = reserve(req); status) {
ptr->publish(error_event{status});
} else {
ptr->publish(send_event{});
}
}
UVW_INLINE details::send_req::send_req(loop::token token, std::shared_ptr<loop> parent, std::unique_ptr<char[], deleter> dt, unsigned int len)
: request{token, std::move(parent)},
data{std::move(dt)},
buf{uv_buf_init(data.get(), len)} {}
UVW_INLINE int details::send_req::send(uv_udp_t *hndl, const struct sockaddr *addr) {
return this->leak_if(uv_udp_send(raw(), hndl, &buf, 1, addr, &udp_send_callback));
}
UVW_INLINE void udp_handle::recv_callback(uv_udp_t *hndl, ssize_t nread, const uv_buf_t *buf, const sockaddr *addr, unsigned flags) {
udp_handle &udp = *(static_cast<udp_handle *>(hndl->data));
// data will be destroyed no matter of what the value of nread is
std::unique_ptr<char[]> data{buf->base};
if(nread > 0) {
// data available (can be truncated)
udp.publish(udp_data_event{details::sock_addr(*addr), std::move(data), static_cast<std::size_t>(nread), !(0 == (flags & UV_UDP_PARTIAL))});
} else if(nread == 0 && addr == nullptr) {
// no more data to be read, doing nothing is fine
} else if(nread == 0 && addr != nullptr) {
// empty udp packet
udp.publish(udp_data_event{details::sock_addr(*addr), std::move(data), static_cast<std::size_t>(nread), false});
} else {
// transmission error
udp.publish(error_event(nread));
}
}
UVW_INLINE udp_handle::udp_handle(loop::token token, std::shared_ptr<loop> ref, unsigned int f)
: handle{token, std::move(ref)}, tag{FLAGS}, flags{f} {}
UVW_INLINE int udp_handle::init() {
if(tag == FLAGS) {
return leak_if(uv_udp_init_ex(parent().raw(), raw(), flags));
} else {
return leak_if(uv_udp_init(parent().raw(), raw()));
}
}
UVW_INLINE int udp_handle::open(os_socket_handle socket) {
return uv_udp_open(raw(), socket);
}
UVW_INLINE int udp_handle::connect(const sockaddr &addr) {
return uv_udp_connect(raw(), &addr);
}
UVW_INLINE int udp_handle::connect(const std::string &ip, unsigned int port) {
return connect(details::ip_addr(ip.data(), port));
}
UVW_INLINE int udp_handle::connect(socket_address addr) {
return connect(addr.ip, addr.port);
}
UVW_INLINE int udp_handle::disconnect() {
return uv_udp_connect(raw(), nullptr);
}
UVW_INLINE socket_address udp_handle::peer() const noexcept {
sockaddr_storage storage;
int len = sizeof(sockaddr_storage);
uv_udp_getpeername(raw(), reinterpret_cast<sockaddr *>(&storage), &len);
return details::sock_addr(storage);
}
UVW_INLINE int udp_handle::bind(const sockaddr &addr, udp_handle::udp_flags opts) {
return uv_udp_bind(raw(), &addr, static_cast<uv_udp_flags>(opts));
}
UVW_INLINE int udp_handle::bind(const std::string &ip, unsigned int port, udp_flags opts) {
return bind(details::ip_addr(ip.data(), port), opts);
}
UVW_INLINE int udp_handle::bind(socket_address addr, udp_flags opts) {
return bind(addr.ip, addr.port, opts);
}
UVW_INLINE socket_address udp_handle::sock() const noexcept {
sockaddr_storage storage;
int len = sizeof(sockaddr_storage);
uv_udp_getsockname(raw(), reinterpret_cast<sockaddr *>(&storage), &len);
return details::sock_addr(storage);
}
UVW_INLINE bool udp_handle::multicast_membership(const std::string &multicast, const std::string &iface, membership ms) {
return (0 == uv_udp_set_membership(raw(), multicast.data(), iface.data(), static_cast<uv_membership>(ms)));
}
UVW_INLINE bool udp_handle::multicast_loop(bool enable) {
return (0 == uv_udp_set_multicast_loop(raw(), enable));
}
UVW_INLINE bool udp_handle::multicast_ttl(int val) {
return (0 == uv_udp_set_multicast_ttl(raw(), val > 255 ? 255 : val));
}
UVW_INLINE bool udp_handle::multicast_interface(const std::string &iface) {
return (0 == uv_udp_set_multicast_interface(raw(), iface.data()));
}
UVW_INLINE bool udp_handle::broadcast(bool enable) {
return (0 == uv_udp_set_broadcast(raw(), enable));
}
UVW_INLINE bool udp_handle::ttl(int val) {
return (0 == uv_udp_set_ttl(raw(), val > 255 ? 255 : val));
}
UVW_INLINE int udp_handle::send(const sockaddr &addr, std::unique_ptr<char[]> data, unsigned int len) {
auto req = parent().resource<details::send_req>(std::unique_ptr<char[], details::send_req::deleter>{data.release(), [](char *ptr) { delete[] ptr; }}, len);
auto listener = [ptr = shared_from_this()](const auto &event, const auto &) {
ptr->publish(event);
};
req->on<error_event>(listener);
req->on<send_event>(listener);
return req->send(raw(), &addr);
}
UVW_INLINE int udp_handle::send(const std::string &ip, unsigned int port, std::unique_ptr<char[]> data, unsigned int len) {
return send(details::ip_addr(ip.data(), port), std::move(data), len);
}
UVW_INLINE int udp_handle::send(socket_address addr, std::unique_ptr<char[]> data, unsigned int len) {
return send(addr.ip, addr.port, std::move(data), len);
}
UVW_INLINE int udp_handle::send(const sockaddr &addr, char *data, unsigned int len) {
auto req = parent().resource<details::send_req>(std::unique_ptr<char[], details::send_req::deleter>{data, [](char *) {}}, len);
auto listener = [ptr = shared_from_this()](const auto &event, const auto &) {
ptr->publish(event);
};
req->on<error_event>(listener);
req->on<send_event>(listener);
return req->send(raw(), &addr);
}
UVW_INLINE int udp_handle::send(const std::string &ip, unsigned int port, char *data, unsigned int len) {
return send(details::ip_addr(ip.data(), port), data, len);
}
UVW_INLINE int udp_handle::send(socket_address addr, char *data, unsigned int len) {
return send(addr.ip, addr.port, data, len);
}
UVW_INLINE int udp_handle::try_send(const sockaddr &addr, std::unique_ptr<char[]> data, unsigned int len) {
uv_buf_t bufs[] = {uv_buf_init(data.get(), len)};
return uv_udp_try_send(raw(), bufs, 1, &addr);
}
UVW_INLINE int udp_handle::try_send(const std::string &ip, unsigned int port, std::unique_ptr<char[]> data, unsigned int len) {
return try_send(details::ip_addr(ip.data(), port), std::move(data), len);
}
UVW_INLINE int udp_handle::try_send(socket_address addr, std::unique_ptr<char[]> data, unsigned int len) {
return try_send(addr.ip, addr.port, std::move(data), len);
}
UVW_INLINE int udp_handle::try_send(const sockaddr &addr, char *data, unsigned int len) {
uv_buf_t bufs[] = {uv_buf_init(data, len)};
return uv_udp_try_send(raw(), bufs, 1, &addr);
}
UVW_INLINE int udp_handle::try_send(const std::string &ip, unsigned int port, char *data, unsigned int len) {
return try_send(details::ip_addr(ip.data(), port), data, len);
}
UVW_INLINE int udp_handle::try_send(socket_address addr, char *data, unsigned int len) {
return try_send(addr.ip, addr.port, data, len);
}
UVW_INLINE int udp_handle::recv() {
return uv_udp_recv_start(raw(), &details::common_alloc_callback, &recv_callback);
}
UVW_INLINE int udp_handle::stop() {
return uv_udp_recv_stop(raw());
}
UVW_INLINE size_t udp_handle::send_queue_size() const noexcept {
return uv_udp_get_send_queue_size(raw());
}
UVW_INLINE size_t udp_handle::send_queue_count() const noexcept {
return uv_udp_get_send_queue_count(raw());
}
} // namespace uvw

View File

@ -1,374 +1,2 @@
#ifdef UVW_AS_LIB
# include "util.h"
#endif
#include "config.h"
namespace uvw {
UVW_INLINE passwd_info::passwd_info(std::shared_ptr<uv_passwd_t> pwd)
: value{pwd} {}
UVW_INLINE std::string passwd_info::username() const noexcept {
return ((value && value->username) ? value->username : "");
}
UVW_INLINE decltype(uv_passwd_t::uid) passwd_info::uid() const noexcept {
return (value ? value->uid : decltype(uv_passwd_t::uid){});
}
UVW_INLINE decltype(uv_passwd_t::gid) passwd_info::gid() const noexcept {
return (value ? value->gid : decltype(uv_passwd_t::gid){});
}
UVW_INLINE std::string passwd_info::shell() const noexcept {
return ((value && value->shell) ? value->shell : "");
}
UVW_INLINE std::string passwd_info::homedir() const noexcept {
return ((value && value->homedir) ? value->homedir : "");
}
UVW_INLINE passwd_info::operator bool() const noexcept {
return static_cast<bool>(value);
}
UVW_INLINE uts_name::uts_name(std::shared_ptr<uv_utsname_t> init)
: uname{init} {}
UVW_INLINE std::string uts_name::sysname() const noexcept {
return uname ? uname->sysname : "";
}
UVW_INLINE std::string uts_name::release() const noexcept {
return uname ? uname->release : "";
}
UVW_INLINE std::string uts_name::version() const noexcept {
return uname ? uname->version : "";
}
UVW_INLINE std::string uts_name::machine() const noexcept {
return uname ? uname->machine : "";
}
namespace details {
UVW_INLINE void common_alloc_callback(uv_handle_t *, std::size_t suggested, uv_buf_t *buf) {
auto size = static_cast<unsigned int>(suggested);
*buf = uv_buf_init(new char[size], size);
}
UVW_INLINE sockaddr ip_addr(const char *addr, unsigned int port) {
// explicitly cast to avoid `-Wsign-conversion` warnings
// libuv internally just casts to an `unsigned short` anyway
auto signed_port = static_cast<int>(port);
if(sockaddr_in addr_in; uv_ip4_addr(addr, signed_port, &addr_in) == 0) {
return reinterpret_cast<const sockaddr &>(addr_in);
} else if(sockaddr_in6 addr_in6; uv_ip6_addr(addr, signed_port, &addr_in6) == 0) {
return reinterpret_cast<const sockaddr &>(addr_in6);
}
return {};
}
UVW_INLINE socket_address sock_addr(const sockaddr_in &addr) {
if(char name[details::DEFAULT_SIZE]; uv_ip4_name(&addr, name, details::DEFAULT_SIZE) == 0) {
return socket_address{std::string{name}, ntohs(addr.sin_port)};
}
return socket_address{};
}
UVW_INLINE socket_address sock_addr(const sockaddr_in6 &addr) {
if(char name[details::DEFAULT_SIZE]; uv_ip6_name(&addr, name, details::DEFAULT_SIZE) == 0) {
return socket_address{std::string{name}, ntohs(addr.sin6_port)};
}
return socket_address{};
}
UVW_INLINE socket_address sock_addr(const sockaddr &addr) {
if(addr.sa_family == AF_INET) {
return sock_addr(reinterpret_cast<const sockaddr_in &>(addr));
} else if(addr.sa_family == AF_INET6) {
return sock_addr(reinterpret_cast<const sockaddr_in6 &>(addr));
}
return socket_address{};
}
UVW_INLINE socket_address sock_addr(const sockaddr_storage &storage) {
if(storage.ss_family == AF_INET) {
return sock_addr(reinterpret_cast<const sockaddr_in &>(storage));
} else if(storage.ss_family == AF_INET6) {
return sock_addr(reinterpret_cast<const sockaddr_in6 &>(storage));
}
return socket_address{};
}
} // namespace details
UVW_INLINE pid_type utilities::os::pid() noexcept {
return uv_os_getpid();
}
UVW_INLINE pid_type utilities::os::ppid() noexcept {
return uv_os_getppid();
}
UVW_INLINE std::string utilities::os::homedir() noexcept {
return details::try_read(&uv_os_homedir);
}
UVW_INLINE std::string utilities::os::tmpdir() noexcept {
return details::try_read(&uv_os_tmpdir);
}
UVW_INLINE std::string utilities::os::env(const std::string &name) noexcept {
return details::try_read(&uv_os_getenv, name.c_str());
}
UVW_INLINE bool utilities::os::env(const std::string &name, const std::string &value) noexcept {
return (0 == (value.empty() ? uv_os_unsetenv(name.c_str()) : uv_os_setenv(name.c_str(), value.c_str())));
}
UVW_INLINE std::string utilities::os::hostname() noexcept {
return details::try_read(&uv_os_gethostname);
}
UVW_INLINE uts_name utilities::os::uname() noexcept {
auto ptr = std::make_shared<uv_utsname_t>();
uv_os_uname(ptr.get());
return ptr;
}
UVW_INLINE passwd_info utilities::os::passwd() noexcept {
auto deleter = [](uv_passwd_t *passwd) {
uv_os_free_passwd(passwd);
delete passwd;
};
std::shared_ptr<uv_passwd_t> ptr{new uv_passwd_t, std::move(deleter)};
uv_os_get_passwd(ptr.get());
return ptr;
}
UVW_INLINE int utilities::os::priority(pid_type pid) {
int prio = 0;
if(uv_os_getpriority(pid, &prio)) {
prio = UV_PRIORITY_LOW + 1;
}
return prio;
}
UVW_INLINE bool utilities::os::priority(pid_type pid, int prio) {
return 0 == uv_os_setpriority(pid, prio);
}
UVW_INLINE handle_type utilities::guess_handle(handle_category category) noexcept {
switch(category) {
case UV_ASYNC:
return handle_type::ASYNC;
case UV_CHECK:
return handle_type::CHECK;
case UV_FS_EVENT:
return handle_type::FS_EVENT;
case UV_FS_POLL:
return handle_type::FS_POLL;
case UV_HANDLE:
return handle_type::HANDLE;
case UV_IDLE:
return handle_type::IDLE;
case UV_NAMED_PIPE:
return handle_type::PIPE;
case UV_POLL:
return handle_type::POLL;
case UV_PREPARE:
return handle_type::PREPARE;
case UV_PROCESS:
return handle_type::PROCESS;
case UV_STREAM:
return handle_type::STREAM;
case UV_TCP:
return handle_type::TCP;
case UV_TIMER:
return handle_type::TIMER;
case UV_TTY:
return handle_type::TTY;
case UV_UDP:
return handle_type::UDP;
case UV_SIGNAL:
return handle_type::SIGNAL;
case UV_FILE:
return handle_type::FILE;
default:
return handle_type::UNKNOWN;
}
}
UVW_INLINE handle_type utilities::guess_handle(file_handle file) noexcept {
handle_category category = uv_guess_handle(file);
return guess_handle(category);
}
UVW_INLINE std::vector<cpu_info> utilities::cpu() noexcept {
std::vector<cpu_info> cpuinfos;
uv_cpu_info_t *infos;
int count;
if(0 == uv_cpu_info(&infos, &count)) {
for(int next = 0; next < count; ++next) {
cpuinfos.push_back({infos[next].model, infos[next].speed, infos[next].cpu_times});
}
uv_free_cpu_info(infos, count);
}
return cpuinfos;
}
UVW_INLINE std::vector<interface_address> utilities::interface_addresses() noexcept {
std::vector<interface_address> interfaces;
uv_interface_address_t *ifaces{nullptr};
int count{0};
if(0 == uv_interface_addresses(&ifaces, &count)) {
for(int next = 0; next < count; ++next) {
interface_address iface_addr;
iface_addr.name = ifaces[next].name;
std::copy(ifaces[next].phys_addr, (ifaces[next].phys_addr + 6), iface_addr.physical);
iface_addr.internal = ifaces[next].is_internal == 0 ? false : true;
if(ifaces[next].address.address4.sin_family == AF_INET) {
iface_addr.address = details::sock_addr(ifaces[next].address.address4);
iface_addr.netmask = details::sock_addr(ifaces[next].netmask.netmask4);
} else if(ifaces[next].address.address4.sin_family == AF_INET6) {
iface_addr.address = details::sock_addr(ifaces[next].address.address6);
iface_addr.netmask = details::sock_addr(ifaces[next].netmask.netmask6);
}
interfaces.push_back(std::move(iface_addr));
}
uv_free_interface_addresses(ifaces, count);
}
return interfaces;
}
UVW_INLINE std::string utilities::index_to_name(unsigned int index) noexcept {
return details::try_read(&uv_if_indextoname, index);
}
UVW_INLINE std::string utilities::index_to_iid(unsigned int index) noexcept {
return details::try_read(&uv_if_indextoiid, index);
}
UVW_INLINE bool utilities::replace_allocator(malloc_func_type malloc_func, realloc_func_type realloc_func, calloc_func_type calloc_func, free_func_type free_func) noexcept {
return (0 == uv_replace_allocator(malloc_func, realloc_func, calloc_func, free_func));
}
UVW_INLINE std::array<double, 3> utilities::load_average() noexcept {
std::array<double, 3> avg;
uv_loadavg(avg.data());
return avg;
}
UVW_INLINE char **utilities::setup_args(int argc, char **argv) {
return uv_setup_args(argc, argv);
}
UVW_INLINE std::string utilities::process_title() {
std::size_t size = details::DEFAULT_SIZE;
char buf[details::DEFAULT_SIZE];
std::string str{};
if(0 == uv_get_process_title(buf, size)) {
str.assign(buf, size);
}
return str;
}
UVW_INLINE bool utilities::process_title(const std::string &title) {
return (0 == uv_set_process_title(title.c_str()));
}
UVW_INLINE uint64_t utilities::total_memory() noexcept {
return uv_get_total_memory();
}
UVW_INLINE uint64_t utilities::constrained_memory() noexcept {
return uv_get_constrained_memory();
}
UVW_INLINE uint64_t utilities::available_memory() noexcept {
return uv_get_available_memory();
}
UVW_INLINE int64_t utilities::resident_set_memory() noexcept {
size_t res{};
const auto err = uv_resident_set_memory(&res);
return (err == 0) ? static_cast<int64_t>(res) : static_cast<int64_t>(err);
}
UVW_INLINE double utilities::uptime() noexcept {
double ret;
if(0 != uv_uptime(&ret)) {
ret = 0;
}
return ret;
}
UVW_INLINE resource_usage utilities::rusage() noexcept {
resource_usage ru;
auto err = uv_getrusage(&ru);
return err ? resource_usage{} : ru;
}
UVW_INLINE timespec64 utilities::gettime(clock_id source) noexcept {
timespec64 ts;
auto err = uv_clock_gettime(static_cast<uv_clock_id>(source), &ts);
return err ? timespec64{} : ts;
}
UVW_INLINE uint64_t utilities::hrtime() noexcept {
return uv_hrtime();
}
UVW_INLINE std::string utilities::path() noexcept {
return details::try_read(&uv_exepath);
}
UVW_INLINE std::string utilities::cwd() noexcept {
return details::try_read(&uv_cwd);
}
UVW_INLINE bool utilities::chdir(const std::string &dir) noexcept {
return (0 == uv_chdir(dir.data()));
}
UVW_INLINE timeval64 utilities::time_of_day() noexcept {
uv_timeval64_t ret;
uv_gettimeofday(&ret);
return ret;
}
UVW_INLINE void utilities::sleep(unsigned int msec) noexcept {
uv_sleep(msec);
}
UVW_INLINE unsigned int utilities::available_parallelism() noexcept {
return uv_available_parallelism();
}
} // namespace uvw
#include "util.h"
#include "util.ipp"

View File

@ -707,7 +707,7 @@ overloaded(Func...) -> overloaded<Func...>;
} // namespace uvw
#ifndef UVW_AS_LIB
# include "util.cpp"
# include "util.ipp"
#endif
#endif // UVW_UTIL_INCLUDE_H

370
src/uvw/util.ipp Normal file
View File

@ -0,0 +1,370 @@
#include "config.h"
namespace uvw {
UVW_INLINE passwd_info::passwd_info(std::shared_ptr<uv_passwd_t> pwd)
: value{pwd} {}
UVW_INLINE std::string passwd_info::username() const noexcept {
return ((value && value->username) ? value->username : "");
}
UVW_INLINE decltype(uv_passwd_t::uid) passwd_info::uid() const noexcept {
return (value ? value->uid : decltype(uv_passwd_t::uid){});
}
UVW_INLINE decltype(uv_passwd_t::gid) passwd_info::gid() const noexcept {
return (value ? value->gid : decltype(uv_passwd_t::gid){});
}
UVW_INLINE std::string passwd_info::shell() const noexcept {
return ((value && value->shell) ? value->shell : "");
}
UVW_INLINE std::string passwd_info::homedir() const noexcept {
return ((value && value->homedir) ? value->homedir : "");
}
UVW_INLINE passwd_info::operator bool() const noexcept {
return static_cast<bool>(value);
}
UVW_INLINE uts_name::uts_name(std::shared_ptr<uv_utsname_t> init)
: uname{init} {}
UVW_INLINE std::string uts_name::sysname() const noexcept {
return uname ? uname->sysname : "";
}
UVW_INLINE std::string uts_name::release() const noexcept {
return uname ? uname->release : "";
}
UVW_INLINE std::string uts_name::version() const noexcept {
return uname ? uname->version : "";
}
UVW_INLINE std::string uts_name::machine() const noexcept {
return uname ? uname->machine : "";
}
namespace details {
UVW_INLINE void common_alloc_callback(uv_handle_t *, std::size_t suggested, uv_buf_t *buf) {
auto size = static_cast<unsigned int>(suggested);
*buf = uv_buf_init(new char[size], size);
}
UVW_INLINE sockaddr ip_addr(const char *addr, unsigned int port) {
// explicitly cast to avoid `-Wsign-conversion` warnings
// libuv internally just casts to an `unsigned short` anyway
auto signed_port = static_cast<int>(port);
if(sockaddr_in addr_in; uv_ip4_addr(addr, signed_port, &addr_in) == 0) {
return reinterpret_cast<const sockaddr &>(addr_in);
} else if(sockaddr_in6 addr_in6; uv_ip6_addr(addr, signed_port, &addr_in6) == 0) {
return reinterpret_cast<const sockaddr &>(addr_in6);
}
return {};
}
UVW_INLINE socket_address sock_addr(const sockaddr_in &addr) {
if(char name[details::DEFAULT_SIZE]; uv_ip4_name(&addr, name, details::DEFAULT_SIZE) == 0) {
return socket_address{std::string{name}, ntohs(addr.sin_port)};
}
return socket_address{};
}
UVW_INLINE socket_address sock_addr(const sockaddr_in6 &addr) {
if(char name[details::DEFAULT_SIZE]; uv_ip6_name(&addr, name, details::DEFAULT_SIZE) == 0) {
return socket_address{std::string{name}, ntohs(addr.sin6_port)};
}
return socket_address{};
}
UVW_INLINE socket_address sock_addr(const sockaddr &addr) {
if(addr.sa_family == AF_INET) {
return sock_addr(reinterpret_cast<const sockaddr_in &>(addr));
} else if(addr.sa_family == AF_INET6) {
return sock_addr(reinterpret_cast<const sockaddr_in6 &>(addr));
}
return socket_address{};
}
UVW_INLINE socket_address sock_addr(const sockaddr_storage &storage) {
if(storage.ss_family == AF_INET) {
return sock_addr(reinterpret_cast<const sockaddr_in &>(storage));
} else if(storage.ss_family == AF_INET6) {
return sock_addr(reinterpret_cast<const sockaddr_in6 &>(storage));
}
return socket_address{};
}
} // namespace details
UVW_INLINE pid_type utilities::os::pid() noexcept {
return uv_os_getpid();
}
UVW_INLINE pid_type utilities::os::ppid() noexcept {
return uv_os_getppid();
}
UVW_INLINE std::string utilities::os::homedir() noexcept {
return details::try_read(&uv_os_homedir);
}
UVW_INLINE std::string utilities::os::tmpdir() noexcept {
return details::try_read(&uv_os_tmpdir);
}
UVW_INLINE std::string utilities::os::env(const std::string &name) noexcept {
return details::try_read(&uv_os_getenv, name.c_str());
}
UVW_INLINE bool utilities::os::env(const std::string &name, const std::string &value) noexcept {
return (0 == (value.empty() ? uv_os_unsetenv(name.c_str()) : uv_os_setenv(name.c_str(), value.c_str())));
}
UVW_INLINE std::string utilities::os::hostname() noexcept {
return details::try_read(&uv_os_gethostname);
}
UVW_INLINE uts_name utilities::os::uname() noexcept {
auto ptr = std::make_shared<uv_utsname_t>();
uv_os_uname(ptr.get());
return ptr;
}
UVW_INLINE passwd_info utilities::os::passwd() noexcept {
auto deleter = [](uv_passwd_t *passwd) {
uv_os_free_passwd(passwd);
delete passwd;
};
std::shared_ptr<uv_passwd_t> ptr{new uv_passwd_t, std::move(deleter)};
uv_os_get_passwd(ptr.get());
return ptr;
}
UVW_INLINE int utilities::os::priority(pid_type pid) {
int prio = 0;
if(uv_os_getpriority(pid, &prio)) {
prio = UV_PRIORITY_LOW + 1;
}
return prio;
}
UVW_INLINE bool utilities::os::priority(pid_type pid, int prio) {
return 0 == uv_os_setpriority(pid, prio);
}
UVW_INLINE handle_type utilities::guess_handle(handle_category category) noexcept {
switch(category) {
case UV_ASYNC:
return handle_type::ASYNC;
case UV_CHECK:
return handle_type::CHECK;
case UV_FS_EVENT:
return handle_type::FS_EVENT;
case UV_FS_POLL:
return handle_type::FS_POLL;
case UV_HANDLE:
return handle_type::HANDLE;
case UV_IDLE:
return handle_type::IDLE;
case UV_NAMED_PIPE:
return handle_type::PIPE;
case UV_POLL:
return handle_type::POLL;
case UV_PREPARE:
return handle_type::PREPARE;
case UV_PROCESS:
return handle_type::PROCESS;
case UV_STREAM:
return handle_type::STREAM;
case UV_TCP:
return handle_type::TCP;
case UV_TIMER:
return handle_type::TIMER;
case UV_TTY:
return handle_type::TTY;
case UV_UDP:
return handle_type::UDP;
case UV_SIGNAL:
return handle_type::SIGNAL;
case UV_FILE:
return handle_type::FILE;
default:
return handle_type::UNKNOWN;
}
}
UVW_INLINE handle_type utilities::guess_handle(file_handle file) noexcept {
handle_category category = uv_guess_handle(file);
return guess_handle(category);
}
UVW_INLINE std::vector<cpu_info> utilities::cpu() noexcept {
std::vector<cpu_info> cpuinfos;
uv_cpu_info_t *infos;
int count;
if(0 == uv_cpu_info(&infos, &count)) {
for(int next = 0; next < count; ++next) {
cpuinfos.push_back({infos[next].model, infos[next].speed, infos[next].cpu_times});
}
uv_free_cpu_info(infos, count);
}
return cpuinfos;
}
UVW_INLINE std::vector<interface_address> utilities::interface_addresses() noexcept {
std::vector<interface_address> interfaces;
uv_interface_address_t *ifaces{nullptr};
int count{0};
if(0 == uv_interface_addresses(&ifaces, &count)) {
for(int next = 0; next < count; ++next) {
interface_address iface_addr;
iface_addr.name = ifaces[next].name;
std::copy(ifaces[next].phys_addr, (ifaces[next].phys_addr + 6), iface_addr.physical);
iface_addr.internal = ifaces[next].is_internal == 0 ? false : true;
if(ifaces[next].address.address4.sin_family == AF_INET) {
iface_addr.address = details::sock_addr(ifaces[next].address.address4);
iface_addr.netmask = details::sock_addr(ifaces[next].netmask.netmask4);
} else if(ifaces[next].address.address4.sin_family == AF_INET6) {
iface_addr.address = details::sock_addr(ifaces[next].address.address6);
iface_addr.netmask = details::sock_addr(ifaces[next].netmask.netmask6);
}
interfaces.push_back(std::move(iface_addr));
}
uv_free_interface_addresses(ifaces, count);
}
return interfaces;
}
UVW_INLINE std::string utilities::index_to_name(unsigned int index) noexcept {
return details::try_read(&uv_if_indextoname, index);
}
UVW_INLINE std::string utilities::index_to_iid(unsigned int index) noexcept {
return details::try_read(&uv_if_indextoiid, index);
}
UVW_INLINE bool utilities::replace_allocator(malloc_func_type malloc_func, realloc_func_type realloc_func, calloc_func_type calloc_func, free_func_type free_func) noexcept {
return (0 == uv_replace_allocator(malloc_func, realloc_func, calloc_func, free_func));
}
UVW_INLINE std::array<double, 3> utilities::load_average() noexcept {
std::array<double, 3> avg;
uv_loadavg(avg.data());
return avg;
}
UVW_INLINE char **utilities::setup_args(int argc, char **argv) {
return uv_setup_args(argc, argv);
}
UVW_INLINE std::string utilities::process_title() {
std::size_t size = details::DEFAULT_SIZE;
char buf[details::DEFAULT_SIZE];
std::string str{};
if(0 == uv_get_process_title(buf, size)) {
str.assign(buf, size);
}
return str;
}
UVW_INLINE bool utilities::process_title(const std::string &title) {
return (0 == uv_set_process_title(title.c_str()));
}
UVW_INLINE uint64_t utilities::total_memory() noexcept {
return uv_get_total_memory();
}
UVW_INLINE uint64_t utilities::constrained_memory() noexcept {
return uv_get_constrained_memory();
}
UVW_INLINE uint64_t utilities::available_memory() noexcept {
return uv_get_available_memory();
}
UVW_INLINE int64_t utilities::resident_set_memory() noexcept {
size_t res{};
const auto err = uv_resident_set_memory(&res);
return (err == 0) ? static_cast<int64_t>(res) : static_cast<int64_t>(err);
}
UVW_INLINE double utilities::uptime() noexcept {
double ret;
if(0 != uv_uptime(&ret)) {
ret = 0;
}
return ret;
}
UVW_INLINE resource_usage utilities::rusage() noexcept {
resource_usage ru;
auto err = uv_getrusage(&ru);
return err ? resource_usage{} : ru;
}
UVW_INLINE timespec64 utilities::gettime(clock_id source) noexcept {
timespec64 ts;
auto err = uv_clock_gettime(static_cast<uv_clock_id>(source), &ts);
return err ? timespec64{} : ts;
}
UVW_INLINE uint64_t utilities::hrtime() noexcept {
return uv_hrtime();
}
UVW_INLINE std::string utilities::path() noexcept {
return details::try_read(&uv_exepath);
}
UVW_INLINE std::string utilities::cwd() noexcept {
return details::try_read(&uv_cwd);
}
UVW_INLINE bool utilities::chdir(const std::string &dir) noexcept {
return (0 == uv_chdir(dir.data()));
}
UVW_INLINE timeval64 utilities::time_of_day() noexcept {
uv_timeval64_t ret;
uv_gettimeofday(&ret);
return ret;
}
UVW_INLINE void utilities::sleep(unsigned int msec) noexcept {
uv_sleep(msec);
}
UVW_INLINE unsigned int utilities::available_parallelism() noexcept {
return uv_available_parallelism();
}
} // namespace uvw

View File

@ -1,30 +1,2 @@
#ifdef UVW_AS_LIB
# include "work.h"
#endif
#include <utility>
#include "config.h"
namespace uvw {
UVW_INLINE work_req::work_req(loop::token token, std::shared_ptr<loop> ref, task t)
: request{token, std::move(ref)}, func{t} {}
UVW_INLINE void work_req::work_callback(uv_work_t *req) {
static_cast<work_req *>(req->data)->func();
}
UVW_INLINE void work_req::after_work_callback(uv_work_t* req, int status) {
if(auto ptr = reserve(req); status) {
ptr->publish(error_event{status});
} else {
ptr->publish(work_event{});
}
}
UVW_INLINE int work_req::queue() {
return this->leak_if(uv_queue_work(parent().raw(), raw(), &work_callback, &after_work_callback));
}
} // namespace uvw
#include "work.h"
#include "work.ipp"

View File

@ -53,7 +53,7 @@ private:
} // namespace uvw
#ifndef UVW_AS_LIB
# include "work.cpp"
# include "work.ipp"
#endif
#endif // UVW_WORK_INCLUDE_H

25
src/uvw/work.ipp Normal file
View File

@ -0,0 +1,25 @@
#include <utility>
#include "config.h"
namespace uvw {
UVW_INLINE work_req::work_req(loop::token token, std::shared_ptr<loop> ref, task t)
: request{token, std::move(ref)}, func{t} {}
UVW_INLINE void work_req::work_callback(uv_work_t *req) {
static_cast<work_req *>(req->data)->func();
}
UVW_INLINE void work_req::after_work_callback(uv_work_t *req, int status) {
if(auto ptr = reserve(req); status) {
ptr->publish(error_event{status});
} else {
ptr->publish(work_event{});
}
}
UVW_INLINE int work_req::queue() {
return this->leak_if(uv_queue_work(parent().raw(), raw(), &work_callback, &after_work_callback));
}
} // namespace uvw