diff --git a/README.md b/README.md index 3d27369a..a1449ebf 100644 --- a/README.md +++ b/README.md @@ -215,4 +215,4 @@ Docs released under [Creative Commons](https://github.com/skypjack/uvw/blob/mast # Note -This documentation is mostly inspired to the [libuv API documentation](http://docs.libuv.org/en/v1.x/) for obvious reasons. +This documentation is mostly inspired by the official [libuv API documentation](http://docs.libuv.org/en/v1.x/) for obvious reasons. diff --git a/src/uvw/fs_poll.hpp b/src/uvw/fs_poll.hpp index 69e44678..58098d79 100644 --- a/src/uvw/fs_poll.hpp +++ b/src/uvw/fs_poll.hpp @@ -34,7 +34,7 @@ public: void start(std::string f, unsigned int interval) { file = std::move(f); - invoke(&uv_fs_poll_start, get(), &startCallback, file.c_str(), interval); + invoke(&uv_fs_poll_start, get(), &startCallback, file.data(), interval); } void stop() { invoke(&uv_fs_poll_stop, get()); } diff --git a/src/uvw/tcp.hpp b/src/uvw/tcp.hpp index 207d7e46..ac706c20 100644 --- a/src/uvw/tcp.hpp +++ b/src/uvw/tcp.hpp @@ -77,17 +77,17 @@ public: bool init() { return initialize(&uv_tcp_init); } void noDelay(bool value = false) { - invoke(&uv_tcp_nodelay, get(), value ? 1 : 0); + invoke(&uv_tcp_nodelay, get(), value); } void keepAlive(bool enable = false, Time time = Time{0}) { - invoke(&uv_tcp_keepalive, get(), enable ? 1 : 0, time.count()); + invoke(&uv_tcp_keepalive, get(), enable, time.count()); } template> void bind(std::string ip, unsigned int port, Flags flags = Flags{}) { typename Traits::Type addr; - Traits::AddrFunc(ip.c_str(), port, &addr); + Traits::AddrFunc(ip.data(), port, &addr); if(0 == invoke(&uv_tcp_bind, get(), reinterpret_cast(&addr), flags)) { addressF = &tAddress; @@ -106,7 +106,7 @@ public: template> void connect(std::string ip, unsigned int port) { typename Traits::Type addr; - Traits::AddrFunc(ip.c_str(), port, &addr); + Traits::AddrFunc(ip.data(), port, &addr); std::weak_ptr weak = this->shared_from_this(); diff --git a/src/uvw/tty.hpp b/src/uvw/tty.hpp index 66d4ed3f..a5dda816 100644 --- a/src/uvw/tty.hpp +++ b/src/uvw/tty.hpp @@ -19,11 +19,15 @@ class TTY final: public Stream { bool readable) : Stream{HandleType{}, std::move(ref)}, fd{static_cast(desc)}, - rw{readable ? 1 : 0} + rw{readable} { } public: - enum class Mode: unsigned short int { NORMAL, RAW, IO }; + enum class Mode: std::underlying_type_t { + NORMAL = UV_TTY_MODE_NORMAL, + RAW = UV_TTY_MODE_RAW, + IO = UV_TTY_MODE_IO + }; template static std::shared_ptr create(Args&&... args) { @@ -32,23 +36,11 @@ public: bool init() { return initialize(&uv_tty_init, fd, rw); } - void mode(TTY::Mode m) { - // uv_tty_set_mode is inline, cannot be used with invoke directly - auto wrap = [](auto *handle, auto m) { + void mode(Mode m) { + // uv_tty_set_mode is inline, it cannot be used with invoke directly + invoke([](auto *handle, auto m) { return uv_tty_set_mode(handle, m); - }; - - switch(m) { - case TTY::Mode::NORMAL: - invoke(std::move(wrap), get(), UV_TTY_MODE_NORMAL); - break; - case TTY::Mode::RAW: - invoke(std::move(wrap), get(), UV_TTY_MODE_RAW); - break; - case TTY::Mode::IO: - invoke(std::move(wrap), get(), UV_TTY_MODE_IO); - break; - } + }, get(), static_cast>(m)); } void reset() { invoke(&uv_tty_reset_mode); } diff --git a/src/uvw/udp.hpp b/src/uvw/udp.hpp index 151e3714..8782b429 100644 --- a/src/uvw/udp.hpp +++ b/src/uvw/udp.hpp @@ -4,6 +4,7 @@ #include #include #include +#include #include #include "event.hpp" #include "request.hpp" @@ -87,6 +88,11 @@ public: REUSEADDR = UV_UDP_REUSEADDR }; + enum class Membership: std::underlying_type_t { + LEAVE_GROUP = UV_LEAVE_GROUP, + JOIN_GROUP = UV_JOIN_GROUP + }; + template static std::shared_ptr create(Args&&... args) { return std::shared_ptr{new Udp{std::forward(args)...}}; @@ -97,7 +103,7 @@ public: template> void bind(std::string ip, unsigned int port, Flags flags = Flags{}) { typename Traits::Type addr; - Traits::AddrFunc(ip.c_str(), port, &addr); + Traits::AddrFunc(ip.data(), port, &addr); if(0 == invoke(&uv_udp_bind, get(), reinterpret_cast(&addr), flags)) { addressF = &tAddress; @@ -112,18 +118,37 @@ public: Addr address() const noexcept { return addressF(*this); } - // TODO uv_udp_set_membership - // TODO uv_udp_set_multicast_loop - // TODO uv_udp_set_multicast_ttl - // TODO uv_udp_set_multicast_interface + template + void multicastMembership(std::string multicast, std::string interface, Membership membership) { + if(0 == invoke(&uv_udp_set_membership, get(), multicast.data(), interface.data(), static_cast(membership))) { + addressF = &tAddress; + remoteF = &tRemote; + } + } - void broadcast(bool enable = false) { invoke(&uv_udp_set_broadcast, get(), enable ? 1 : 0); } + void multicastLoop(bool enable = true) { + invoke(&uv_udp_set_multicast_loop, get(), enable); + } + + void multicastTttl(int val) { + invoke(&uv_udp_set_multicast_ttl, get(), val > 255 ? 255 : val); + } + + template + void multicastInterface(std::string interface) { + if(0 == invoke(&uv_udp_set_multicast_interface, get(), interface.data())) { + addressF = &tAddress; + remoteF = &tRemote; + } + } + + void broadcast(bool enable = false) { invoke(&uv_udp_set_broadcast, get(), enable); } void ttl(int val) { invoke(&uv_udp_set_ttl, get(), val > 255 ? 255 : val); } template> void send(std::string ip, unsigned int port, char *data, ssize_t len) { typename Traits::Type addr; - Traits::AddrFunc(ip.c_str(), port, &addr); + Traits::AddrFunc(ip.data(), port, &addr); uv_buf_t bufs[] = { uv_buf_init(data, len) }; std::weak_ptr weak = this->shared_from_this(); @@ -155,7 +180,7 @@ public: template> int trySend(std::string ip, unsigned int port, char *data, ssize_t len) { typename Traits::Type addr; - Traits::AddrFunc(ip.c_str(), port, &addr); + Traits::AddrFunc(ip.data(), port, &addr); uv_buf_t bufs[] = { uv_buf_init(data, len) }; auto bw = uv_udp_try_send(get(), bufs, 1, reinterpret_cast(&addr));