diff --git a/src/uvw.hpp b/src/uvw.hpp index ef612b0e..be032195 100644 --- a/src/uvw.hpp +++ b/src/uvw.hpp @@ -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" diff --git a/src/uvw/event.hpp b/src/uvw/event.hpp index 5fe3cc09..4e4ffbe2 100644 --- a/src/uvw/event.hpp +++ b/src/uvw/event.hpp @@ -37,7 +37,7 @@ struct ConnectEvent: Event { }; struct DataEvent: Event { - explicit DataEvent(std::unique_ptr ptr, ssize_t l) + explicit DataEvent(std::unique_ptr ptr, ssize_t l) noexcept : dt{std::move(ptr)}, len{l} { } @@ -54,7 +54,7 @@ struct EndEvent: Event { }; struct ErrorEvent: Event { - 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 +struct FlagsEvent: Event> { + explicit FlagsEvent(Flags f) noexcept: flags{std::move(f)} { } + + operator Flags() const noexcept { return flags; } + +private: + const Flags flags; +}; + + struct FsPollEvent: Event { - 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 { }; struct SignalEvent: Event { - explicit SignalEvent(int sig): signum(sig) { } + explicit SignalEvent(int sig) noexcept: signum(sig) { } operator int() const noexcept { return signum; } diff --git a/src/uvw/handle.hpp b/src/uvw/handle.hpp index 316ca828..3bb8e874 100644 --- a/src/uvw/handle.hpp +++ b/src/uvw/handle.hpp @@ -18,6 +18,7 @@ template<> struct HandleType { }; template<> struct HandleType { }; template<> struct HandleType { }; template<> struct HandleType { }; +template<> struct HandleType { }; template<> struct HandleType { }; template<> struct HandleType { }; template<> struct HandleType { }; diff --git a/src/uvw/poll.hpp b/src/uvw/poll.hpp new file mode 100644 index 00000000..45319432 --- /dev/null +++ b/src/uvw/poll.hpp @@ -0,0 +1,46 @@ +#pragma once + + +#include +#include +#include +#include "event.hpp" +#include "handle.hpp" +#include "util.hpp" + + +namespace uvw { + + +class Poll final: public Handle { + static void startCallback(uv_poll_t *handle, int status, int events) { + Poll &poll = *(static_cast(handle->data)); + if(status) { poll.publish(ErrorEvent{status}); } + else { poll.publish(FlagsEvent{events}); } + } + + explicit Poll(std::shared_ptr ref) + : Handle{HandleType{}, std::move(ref)} + { } + +public: + enum class Event { + READABLE = UV_READABLE, + WRITABLE = UV_WRITABLE, + DISCONNECT = UV_DISCONNECT + }; + + template + static std::shared_ptr create(Args&&... args) { + return std::shared_ptr{new Poll{std::forward(args)...}}; + } + + bool init(FileDescriptor fd) { return initialize(&uv_poll_init, static_cast(fd)); } + + void start(Flags flags) { invoke(&uv_poll_start, get(), flags, &startCallback); } + void start(Event event) { start(Flags{event}); } + void stop() { invoke(&uv_poll_stop, get()); } +}; + + +}