added handle Poll

This commit is contained in:
Michele Caini 2016-07-19 12:38:47 +02:00
parent 286072a3c0
commit 6a78831d65
4 changed files with 63 additions and 4 deletions

View File

@ -4,6 +4,7 @@
#include "uvw/fs_poll.hpp"
#include "uvw/idle.hpp"
#include "uvw/loop.hpp"
#include "uvw/poll.hpp"
#include "uvw/prepare.hpp"
#include "uvw/signal.hpp"
#include "uvw/tcp.hpp"

View File

@ -37,7 +37,7 @@ struct ConnectEvent: Event<ConnectEvent> { };
struct DataEvent: Event<DataEvent> {
explicit DataEvent(std::unique_ptr<const char[]> ptr, ssize_t l)
explicit DataEvent(std::unique_ptr<const char[]> ptr, ssize_t l) noexcept
: dt{std::move(ptr)}, len{l}
{ }
@ -54,7 +54,7 @@ struct EndEvent: Event<EndEvent> { };
struct ErrorEvent: Event<ErrorEvent> {
explicit ErrorEvent(int code = 0): ec(code) { }
explicit ErrorEvent(int code = 0) noexcept: ec(code) { }
operator const char *() const noexcept { return uv_strerror(ec); }
operator int() const noexcept { return ec; }
@ -64,8 +64,19 @@ private:
};
template<typename E>
struct FlagsEvent: Event<FlagsEvent<E>> {
explicit FlagsEvent(Flags<E> f) noexcept: flags{std::move(f)} { }
operator Flags<E>() const noexcept { return flags; }
private:
const Flags<E> flags;
};
struct FsPollEvent: Event<FsPollEvent> {
explicit FsPollEvent(const Stat &p, const Stat &c)
explicit FsPollEvent(const Stat &p, const Stat &c) noexcept
: prev(p), curr(c)
{ }
@ -85,7 +96,7 @@ struct ShutdownEvent: Event<ShutdownEvent> { };
struct SignalEvent: Event<SignalEvent> {
explicit SignalEvent(int sig): signum(sig) { }
explicit SignalEvent(int sig) noexcept: signum(sig) { }
operator int() const noexcept { return signum; }

View File

@ -18,6 +18,7 @@ template<> struct HandleType<uv_async_t> { };
template<> struct HandleType<uv_check_t> { };
template<> struct HandleType<uv_fs_poll_t> { };
template<> struct HandleType<uv_idle_t> { };
template<> struct HandleType<uv_poll_t> { };
template<> struct HandleType<uv_prepare_t> { };
template<> struct HandleType<uv_signal_t> { };
template<> struct HandleType<uv_tcp_t> { };

46
src/uvw/poll.hpp Normal file
View File

@ -0,0 +1,46 @@
#pragma once
#include <utility>
#include <memory>
#include <uv.h>
#include "event.hpp"
#include "handle.hpp"
#include "util.hpp"
namespace uvw {
class Poll final: public Handle<Poll> {
static void startCallback(uv_poll_t *handle, int status, int events) {
Poll &poll = *(static_cast<Poll*>(handle->data));
if(status) { poll.publish(ErrorEvent{status}); }
else { poll.publish(FlagsEvent<Poll::Event>{events}); }
}
explicit Poll(std::shared_ptr<Loop> ref)
: Handle{HandleType<uv_poll_t>{}, std::move(ref)}
{ }
public:
enum class Event {
READABLE = UV_READABLE,
WRITABLE = UV_WRITABLE,
DISCONNECT = UV_DISCONNECT
};
template<typename... Args>
static std::shared_ptr<Poll> create(Args&&... args) {
return std::shared_ptr<Poll>{new Poll{std::forward<Args>(args)...}};
}
bool init(FileDescriptor fd) { return initialize<uv_poll_t>(&uv_poll_init, static_cast<FileDescriptor::Type>(fd)); }
void start(Flags<Event> flags) { invoke(&uv_poll_start, get<uv_poll_t>(), flags, &startCallback); }
void start(Event event) { start(Flags<Event>{event}); }
void stop() { invoke(&uv_poll_stop, get<uv_poll_t>()); }
};
}