Merge pull request #20 from cynnyx/master

UDP + minor changes
This commit is contained in:
Michele Caini 2016-07-21 09:29:30 +02:00 committed by GitHub
commit 1abdecf769
5 changed files with 49 additions and 32 deletions

View File

@ -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.

View File

@ -34,7 +34,7 @@ public:
void start(std::string f, unsigned int interval) {
file = std::move(f);
invoke(&uv_fs_poll_start, get<uv_fs_poll_t>(), &startCallback, file.c_str(), interval);
invoke(&uv_fs_poll_start, get<uv_fs_poll_t>(), &startCallback, file.data(), interval);
}
void stop() { invoke(&uv_fs_poll_stop, get<uv_fs_poll_t>()); }

View File

@ -77,17 +77,17 @@ public:
bool init() { return initialize<uv_tcp_t>(&uv_tcp_init); }
void noDelay(bool value = false) {
invoke(&uv_tcp_nodelay, get<uv_tcp_t>(), value ? 1 : 0);
invoke(&uv_tcp_nodelay, get<uv_tcp_t>(), value);
}
void keepAlive(bool enable = false, Time time = Time{0}) {
invoke(&uv_tcp_keepalive, get<uv_tcp_t>(), enable ? 1 : 0, time.count());
invoke(&uv_tcp_keepalive, get<uv_tcp_t>(), enable, time.count());
}
template<typename I, typename..., typename Traits = details::IpTraits<I>>
void bind(std::string ip, unsigned int port, Flags<Bind> flags = Flags<Bind>{}) {
typename Traits::Type addr;
Traits::AddrFunc(ip.c_str(), port, &addr);
Traits::AddrFunc(ip.data(), port, &addr);
if(0 == invoke(&uv_tcp_bind, get<uv_tcp_t>(), reinterpret_cast<const sockaddr *>(&addr), flags)) {
addressF = &tAddress<I>;
@ -106,7 +106,7 @@ public:
template<typename I, typename..., typename Traits = details::IpTraits<I>>
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<Tcp> weak = this->shared_from_this();

View File

@ -19,11 +19,15 @@ class TTY final: public Stream<TTY> {
bool readable)
: Stream{HandleType<uv_tty_t>{}, std::move(ref)},
fd{static_cast<FileDescriptor::Type>(desc)},
rw{readable ? 1 : 0}
rw{readable}
{ }
public:
enum class Mode: unsigned short int { NORMAL, RAW, IO };
enum class Mode: std::underlying_type_t<uv_tty_mode_t> {
NORMAL = UV_TTY_MODE_NORMAL,
RAW = UV_TTY_MODE_RAW,
IO = UV_TTY_MODE_IO
};
template<typename... Args>
static std::shared_ptr<TTY> create(Args&&... args) {
@ -32,23 +36,11 @@ public:
bool init() { return initialize<uv_tty_t>(&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_t>(), UV_TTY_MODE_NORMAL);
break;
case TTY::Mode::RAW:
invoke(std::move(wrap), get<uv_tty_t>(), UV_TTY_MODE_RAW);
break;
case TTY::Mode::IO:
invoke(std::move(wrap), get<uv_tty_t>(), UV_TTY_MODE_IO);
break;
}
}, get<uv_tty_t>(), static_cast<std::underlying_type_t<Mode>>(m));
}
void reset() { invoke(&uv_tty_reset_mode); }

View File

@ -4,6 +4,7 @@
#include <type_traits>
#include <utility>
#include <memory>
#include <string>
#include <uv.h>
#include "event.hpp"
#include "request.hpp"
@ -87,6 +88,11 @@ public:
REUSEADDR = UV_UDP_REUSEADDR
};
enum class Membership: std::underlying_type_t<uv_membership> {
LEAVE_GROUP = UV_LEAVE_GROUP,
JOIN_GROUP = UV_JOIN_GROUP
};
template<typename... Args>
static std::shared_ptr<Udp> create(Args&&... args) {
return std::shared_ptr<Udp>{new Udp{std::forward<Args>(args)...}};
@ -97,7 +103,7 @@ public:
template<typename I, typename..., typename Traits = details::IpTraits<I>>
void bind(std::string ip, unsigned int port, Flags<Bind> flags = Flags<Bind>{}) {
typename Traits::Type addr;
Traits::AddrFunc(ip.c_str(), port, &addr);
Traits::AddrFunc(ip.data(), port, &addr);
if(0 == invoke(&uv_udp_bind, get<uv_udp_t>(), reinterpret_cast<const sockaddr *>(&addr), flags)) {
addressF = &tAddress<I>;
@ -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<typename I>
void multicastMembership(std::string multicast, std::string interface, Membership membership) {
if(0 == invoke(&uv_udp_set_membership, get<uv_udp_t>(), multicast.data(), interface.data(), static_cast<uv_membership>(membership))) {
addressF = &tAddress<I>;
remoteF = &tRemote<I>;
}
}
void broadcast(bool enable = false) { invoke(&uv_udp_set_broadcast, get<uv_udp_t>(), enable ? 1 : 0); }
void multicastLoop(bool enable = true) {
invoke(&uv_udp_set_multicast_loop, get<uv_udp_t>(), enable);
}
void multicastTttl(int val) {
invoke(&uv_udp_set_multicast_ttl, get<uv_udp_t>(), val > 255 ? 255 : val);
}
template<typename I>
void multicastInterface(std::string interface) {
if(0 == invoke(&uv_udp_set_multicast_interface, get<uv_udp_t>(), interface.data())) {
addressF = &tAddress<I>;
remoteF = &tRemote<I>;
}
}
void broadcast(bool enable = false) { invoke(&uv_udp_set_broadcast, get<uv_udp_t>(), enable); }
void ttl(int val) { invoke(&uv_udp_set_ttl, get<uv_udp_t>(), val > 255 ? 255 : val); }
template<typename I, typename..., typename Traits = details::IpTraits<I>>
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<Udp> weak = this->shared_from_this();
@ -155,7 +180,7 @@ public:
template<typename I, typename..., typename Traits = details::IpTraits<I>>
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<uv_udp_t>(), bufs, 1, reinterpret_cast<const sockaddr *>(&addr));