From 7c618546561627d213aa820bf72e4aca4b84781e Mon Sep 17 00:00:00 2001 From: Michele Caini Date: Thu, 28 Jul 2016 13:36:35 +0200 Subject: [PATCH 1/2] updated names + docs --- README.md | 38 +++++++++++++++++++------------------- src/uvw/async.hpp | 8 ++++---- src/uvw/check.hpp | 8 ++++---- src/uvw/fs.hpp | 8 ++++---- src/uvw/fs_event.hpp | 16 ++++++++-------- src/uvw/fs_poll.hpp | 8 ++++---- src/uvw/idle.hpp | 8 ++++---- src/uvw/pipe.hpp | 12 ++++++------ src/uvw/poll.hpp | 8 ++++---- src/uvw/prepare.hpp | 8 ++++---- src/uvw/signal.hpp | 8 ++++---- src/uvw/stream.hpp | 32 ++++++++++++++++---------------- src/uvw/tcp.hpp | 12 ++++++------ src/uvw/timer.hpp | 8 ++++---- src/uvw/tty.hpp | 10 +++++----- src/uvw/udp.hpp | 8 ++++---- src/uvw/work.hpp | 8 ++++---- test/main.cpp | 30 +++++++++++++++--------------- test/uvw/loop.cpp | 6 +++--- 19 files changed, 122 insertions(+), 122 deletions(-) diff --git a/README.md b/README.md index 66272a5d..f2ca101f 100644 --- a/README.md +++ b/README.md @@ -14,13 +14,13 @@ As an example, a *handle* should be initialized before any other operation and c #include void listen(uvw::Loop &loop) { - std::shared_ptr tcp = loop.resource(); + std::shared_ptr tcp = loop.resource(); - tcp->once([](const uvw::ListenEvent &event, uvw::Tcp &srv) mutable { - std::shared_ptr client = srv.loop().resource(); + tcp->once([](const uvw::ListenEvent &event, uvw::TcpHandle &srv) mutable { + std::shared_ptr client = srv.loop().resource(); - client->on([ptr = srv.shared_from_this()](const uvw::CloseEvent &, uvw::Tcp &) mutable { ptr->close(); }); - client->on([](const uvw::EndEvent &, uvw::Tcp &client) { client.close(); }); + client->on([ptr = srv.shared_from_this()](const uvw::CloseEvent &, uvw::TcpHandle &) mutable { ptr->close(); }); + client->on([](const uvw::EndEvent &, uvw::TcpHandle &client) { client.close(); }); srv.accept(*client); client->read(); @@ -31,11 +31,11 @@ void listen(uvw::Loop &loop) { } void conn(uvw::Loop &loop) { - auto tcp = loop.resource(); + auto tcp = loop.resource(); - tcp->on([](const uvw::ErrorEvent &, uvw::Tcp &) { /* handle errors */ }); + tcp->on([](const uvw::ErrorEvent &, uvw::TcpHandle &) { /* handle errors */ }); - tcp->once([](const uvw::ConnectEvent &, uvw::Tcp &tcp) mutable { + tcp->once([](const uvw::ConnectEvent &, uvw::TcpHandle &tcp) mutable { auto dataWrite = std::unique_ptr(new char[2]{ 'b', 'c' }); tcp.write(std::move(dataWrite), 2); tcp.close(); @@ -119,13 +119,13 @@ Loops can be run using the `run`, `runOnce` and `runWait` member methods. Please In order to create a resource and to bind it to the given loop, just do the following: - auto tcp = loop.resource(); + auto tcp = loop.resource(); The line above will create and initialize a tcp handle, thus a shared pointer to that resource will be returned. Users should check if pointers have been correctly initialized: in case of errors, they won't be. Another way to create a resource is: - auto tcp = Tcp::create(loop); + auto tcp = TcpHandle::create(loop); tcp->init(); Pretty annoying indeed. Using a loop is the recommended approach. @@ -160,7 +160,7 @@ There exist two methods to attach an event to a resource: * `resource.once(listener)`: the listener will be automatically removed after the first event of the given type * `resource.on(listener)`: to be used for long-running listeners -Both of them return an object of type `ResourceType::Connection` (as an example, `Tcp::Connection`). +Both of them return an object of type `ResourceType::Connection` (as an example, `TcpHandle::Connection`). A connection object can be used later as an argument to the `erase` member method of the resource to remove the listener. There exists also the `clear` member method to drop all the listeners at once. @@ -168,14 +168,14 @@ The code below shows how to create a simple tcp server using `uvw`: ``` auto loop = uvw::Loop::getDefault(); -auto tcp = loop.resource(); +auto tcp = loop.resource(); -tcp->on([](const uvw::ErrorEvent &, uvw::Tcp &srv) { /* something went wrong */ }); +tcp->on([](const uvw::ErrorEvent &, uvw::TcpHandle &srv) { /* something went wrong */ }); -tcp->on([](const uvw::ListenEvent &event, uvw::Tcp &srv) mutable { - std::shared_ptr client = srv.loop().resource(); - client->once([](const uvw::EndEvent &, uvw::Tcp &client) { client.close(); }); - client->on([](const uvw::DataEvent &, uvw::Tcp &) { /* data received */ }); +tcp->on([](const uvw::ListenEvent &event, uvw::TcpHandle &srv) mutable { + std::shared_ptr client = srv.loop().resource(); + client->once([](const uvw::EndEvent &, uvw::TcpHandle &client) { client.close(); }); + client->on([](const uvw::DataEvent &, uvw::TcpHandle &) { /* data received */ }); srv.accept(*client); client->read(); }); @@ -184,8 +184,8 @@ tcp->bind("127.0.0.1", 4242); tcp->listen(); ``` -Note that `uvw::Tcp` already supports _IPv6_ out-of-the-box. The statement above is equivalent to `tcp->bind("127.0.0.1", 4242)`. -It's suffice to explicitly specify `uvw::Tcp::IPv6` as the underlying protocol to use it. +Note that `uvw::TcpHandle` already supports _IPv6_ out-of-the-box. The statement above is equivalent to `tcp->bind("127.0.0.1", 4242)`. +It's suffice to explicitly specify `uvw::TcpHandle::IPv6` as the underlying protocol to use it. The API reference is the recommended documentation for further details about resources and their methods. diff --git a/src/uvw/async.hpp b/src/uvw/async.hpp index b855cf10..2079d20c 100644 --- a/src/uvw/async.hpp +++ b/src/uvw/async.hpp @@ -15,9 +15,9 @@ namespace uvw { struct AsyncEvent: Event { }; -class Async final: public Handle { +class AsyncHandle final: public Handle { static void sendCallback(uv_async_t *handle) { - Async &async = *(static_cast(handle->data)); + AsyncHandle &async = *(static_cast(handle->data)); async.publish(AsyncEvent{}); } @@ -25,8 +25,8 @@ class Async final: public Handle { public: template - static std::shared_ptr create(Args&&... args) { - return std::shared_ptr{new Async{std::forward(args)...}}; + static std::shared_ptr create(Args&&... args) { + return std::shared_ptr{new AsyncHandle{std::forward(args)...}}; } bool init() { return initialize(&uv_async_init, &sendCallback); } diff --git a/src/uvw/check.hpp b/src/uvw/check.hpp index 694a145d..86c538fa 100644 --- a/src/uvw/check.hpp +++ b/src/uvw/check.hpp @@ -15,9 +15,9 @@ namespace uvw { struct CheckEvent: Event { }; -class Check final: public Handle { +class CheckHandle final: public Handle { static void startCallback(uv_check_t *handle) { - Check &check = *(static_cast(handle->data)); + CheckHandle &check = *(static_cast(handle->data)); check.publish(CheckEvent{}); } @@ -25,8 +25,8 @@ class Check final: public Handle { public: template - static std::shared_ptr create(Args&&... args) { - return std::shared_ptr{new Check{std::forward(args)...}}; + static std::shared_ptr create(Args&&... args) { + return std::shared_ptr{new CheckHandle{std::forward(args)...}}; } bool init() { return initialize(&uv_check_init); } diff --git a/src/uvw/fs.hpp b/src/uvw/fs.hpp index ff8f8eff..10cc3b40 100644 --- a/src/uvw/fs.hpp +++ b/src/uvw/fs.hpp @@ -206,7 +206,7 @@ template using FsEvent = TypedEvent; -class Fs final: public Request { +class FsReq final: public Request { static constexpr int BAD_FD = -1; static void fsCloseCallback(uv_fs_t *req) { @@ -283,11 +283,11 @@ public: using Entry = std::pair; template - static std::shared_ptr create(Args&&... args) { - return std::shared_ptr{new Fs{std::forward(args)...}}; + static std::shared_ptr create(Args&&... args) { + return std::shared_ptr{new FsReq{std::forward(args)...}}; } - ~Fs() { + ~FsReq() { uv_fs_req_cleanup(get()); } diff --git a/src/uvw/fs_event.hpp b/src/uvw/fs_event.hpp index 17827339..6451b3c1 100644 --- a/src/uvw/fs_event.hpp +++ b/src/uvw/fs_event.hpp @@ -32,8 +32,8 @@ enum class UVFsEvent: std::underlying_type_t { } -struct FsMonitorEvent: Event { - FsMonitorEvent(std::string fPath, Flags f) +struct FsEventEvent: Event { + FsEventEvent(std::string fPath, Flags f) : flgs{std::move(f)}, relPath{std::move(fPath)} { } @@ -46,11 +46,11 @@ private: }; -class FsMonitor final: public Handle { +class FsEventHandle final: public Handle { static void startCallback(uv_fs_event_t *handle, const char *filename, int events, int status) { - FsMonitor &fsMonitor = *(static_cast(handle->data)); - if(status) { fsMonitor.publish(ErrorEvent{status}); } - else { fsMonitor.publish(FsMonitorEvent{filename, static_cast>(events)}); } + FsEventHandle &fsEvent = *(static_cast(handle->data)); + if(status) { fsEvent.publish(ErrorEvent{status}); } + else { fsEvent.publish(FsEventEvent{filename, static_cast>(events)}); } } using Handle::Handle; @@ -60,8 +60,8 @@ public: using Event = details::UVFsEventFlags; template - static std::shared_ptr create(Args&&... args) { - return std::shared_ptr{new FsMonitor{std::forward(args)...}}; + static std::shared_ptr create(Args&&... args) { + return std::shared_ptr{new FsEventHandle{std::forward(args)...}}; } bool init() { return initialize(&uv_fs_event_init); } diff --git a/src/uvw/fs_poll.hpp b/src/uvw/fs_poll.hpp index 130f12e0..41c24f18 100644 --- a/src/uvw/fs_poll.hpp +++ b/src/uvw/fs_poll.hpp @@ -27,9 +27,9 @@ private: }; -class FsPoll final: public Handle { +class FsPollHandle final: public Handle { static void startCallback(uv_fs_poll_t *handle, int status, const uv_stat_t *prev, const uv_stat_t *curr) { - FsPoll &fsPoll = *(static_cast(handle->data)); + FsPollHandle &fsPoll = *(static_cast(handle->data)); if(status) { fsPoll.publish(ErrorEvent{status}); } else { fsPoll.publish(FsPollEvent{ *prev, *curr }); } } @@ -38,8 +38,8 @@ class FsPoll final: public Handle { public: template - static std::shared_ptr create(Args&&... args) { - return std::shared_ptr{new FsPoll{std::forward(args)...}}; + static std::shared_ptr create(Args&&... args) { + return std::shared_ptr{new FsPollHandle{std::forward(args)...}}; } bool init() { return initialize(&uv_fs_poll_init); } diff --git a/src/uvw/idle.hpp b/src/uvw/idle.hpp index aab33281..513206da 100644 --- a/src/uvw/idle.hpp +++ b/src/uvw/idle.hpp @@ -15,9 +15,9 @@ namespace uvw { struct IdleEvent: Event { }; -class Idle final: public Handle { +class IdleHandle final: public Handle { static void startCallback(uv_idle_t *handle) { - Idle &idle = *(static_cast(handle->data)); + IdleHandle &idle = *(static_cast(handle->data)); idle.publish(IdleEvent{}); } @@ -25,8 +25,8 @@ class Idle final: public Handle { public: template - static std::shared_ptr create(Args&&... args) { - return std::shared_ptr{new Idle{std::forward(args)...}}; + static std::shared_ptr create(Args&&... args) { + return std::shared_ptr{new IdleHandle{std::forward(args)...}}; } bool init() { return initialize(&uv_idle_init); } diff --git a/src/uvw/pipe.hpp b/src/uvw/pipe.hpp index 80c5d06b..e549ab9c 100644 --- a/src/uvw/pipe.hpp +++ b/src/uvw/pipe.hpp @@ -29,15 +29,15 @@ enum class UVHandleType: std::underlying_type_t { } -class Pipe final: public Stream { - using Stream::Stream; +class PipeHandle final: public StreamHandle { + using StreamHandle::StreamHandle; public: using Pending = details::UVHandleType; template - static std::shared_ptr create(Args&&... args) { - return std::shared_ptr{new Pipe{std::forward(args)...}}; + static std::shared_ptr create(Args&&... args) { + return std::shared_ptr{new PipeHandle{std::forward(args)...}}; } bool init(bool ipc = false) { return initialize(&uv_pipe_init, ipc); } @@ -51,11 +51,11 @@ public: } void connect(std::string name) { - auto listener = [ptr = shared_from_this()](const auto &event, details::Connect &) { + auto listener = [ptr = shared_from_this()](const auto &event, details::ConnectReq &) { ptr->publish(event); }; - auto connect = loop().resource(); + auto connect = loop().resource(); connect->once(listener); connect->once(listener); connect->connect(&uv_pipe_connect, get(), name.data()); diff --git a/src/uvw/poll.hpp b/src/uvw/poll.hpp index ef93b0e8..d0765a46 100644 --- a/src/uvw/poll.hpp +++ b/src/uvw/poll.hpp @@ -38,9 +38,9 @@ private: }; -class Poll final: public Handle { +class PollHandle final: public Handle { static void startCallback(uv_poll_t *handle, int status, int events) { - Poll &poll = *(static_cast(handle->data)); + PollHandle &poll = *(static_cast(handle->data)); if(status) { poll.publish(ErrorEvent{status}); } else { poll.publish(PollEvent{static_cast>(events)}); } } @@ -51,8 +51,8 @@ public: using Event = details::UVPollEvent; template - static std::shared_ptr create(Args&&... args) { - return std::shared_ptr{new Poll{std::forward(args)...}}; + static std::shared_ptr create(Args&&... args) { + return std::shared_ptr{new PollHandle{std::forward(args)...}}; } bool init(int fd) { return initialize(&uv_poll_init, fd); } diff --git a/src/uvw/prepare.hpp b/src/uvw/prepare.hpp index 3a50fb8a..06807ad7 100644 --- a/src/uvw/prepare.hpp +++ b/src/uvw/prepare.hpp @@ -15,9 +15,9 @@ namespace uvw { struct PrepareEvent: Event { }; -class Prepare final: public Handle { +class PrepareHandle final: public Handle { static void startCallback(uv_prepare_t *handle) { - Prepare &prepare = *(static_cast(handle->data)); + PrepareHandle &prepare = *(static_cast(handle->data)); prepare.publish(PrepareEvent{}); } @@ -25,8 +25,8 @@ class Prepare final: public Handle { public: template - static std::shared_ptr create(Args&&... args) { - return std::shared_ptr{new Prepare{std::forward(args)...}}; + static std::shared_ptr create(Args&&... args) { + return std::shared_ptr{new PrepareHandle{std::forward(args)...}}; } bool init() { return initialize(&uv_prepare_init); } diff --git a/src/uvw/signal.hpp b/src/uvw/signal.hpp index b4f736ee..5906d162 100644 --- a/src/uvw/signal.hpp +++ b/src/uvw/signal.hpp @@ -22,9 +22,9 @@ private: }; -class Signal final: public Handle { +class SignalHandle final: public Handle { static void startCallback(uv_signal_t *handle, int signum) { - Signal &signal = *(static_cast(handle->data)); + SignalHandle &signal = *(static_cast(handle->data)); signal.publish(SignalEvent{signum}); } @@ -32,8 +32,8 @@ class Signal final: public Handle { public: template - static std::shared_ptr create(Args&&... args) { - return std::shared_ptr{new Signal{std::forward(args)...}}; + static std::shared_ptr create(Args&&... args) { + return std::shared_ptr{new SignalHandle{std::forward(args)...}}; } bool init() { return initialize(&uv_signal_init); } diff --git a/src/uvw/stream.hpp b/src/uvw/stream.hpp index bd26d1ae..6500d8aa 100644 --- a/src/uvw/stream.hpp +++ b/src/uvw/stream.hpp @@ -39,13 +39,13 @@ private: namespace details { -class Connect final: public Request { +class ConnectReq final: public Request { using Request::Request; public: template - static std::shared_ptr create(Args&&... args) { - return std::shared_ptr{new Connect{std::forward(args)...}}; + static std::shared_ptr create(Args&&... args) { + return std::shared_ptr{new ConnectReq{std::forward(args)...}}; } template @@ -55,13 +55,13 @@ public: }; -class Shutdown final: public Request { +class ShutdownReq final: public Request { using Request::Request; public: template - static std::shared_ptr create(Args&&... args) { - return std::shared_ptr{new Shutdown{std::forward(args)...}}; + static std::shared_ptr create(Args&&... args) { + return std::shared_ptr{new ShutdownReq{std::forward(args)...}}; } void shutdown(uv_stream_t *handle) { @@ -70,13 +70,13 @@ public: }; -class Write final: public Request { +class WriteReq final: public Request { using Request::Request; public: template - static std::shared_ptr create(Args&&... args) { - return std::shared_ptr{new Write{std::forward(args)...}}; + static std::shared_ptr create(Args&&... args) { + return std::shared_ptr{new WriteReq{std::forward(args)...}}; } void write(uv_stream_t *handle, const uv_buf_t bufs[], unsigned int nbufs) { @@ -93,7 +93,7 @@ public: template -class Stream: public Handle { +class StreamHandle: public Handle { static constexpr unsigned int DEFAULT_BACKLOG = 128; static void readCallback(uv_stream_t *handle, ssize_t nread, const uv_buf_t *buf) { @@ -124,11 +124,11 @@ protected: public: void shutdown() { - auto listener = [ptr = this->shared_from_this()](const auto &event, details::Shutdown &) { + auto listener = [ptr = this->shared_from_this()](const auto &event, details::ShutdownReq &) { ptr->publish(event); }; - auto shutdown = this->loop().template resource(); + auto shutdown = this->loop().template resource(); shutdown->template once(listener); shutdown->template once(listener); shutdown->shutdown(this->template get()); @@ -158,11 +158,11 @@ public: void write(std::unique_ptr data, ssize_t len) { uv_buf_t bufs[] = { uv_buf_init(data.get(), len) }; - auto listener = [ptr = this->shared_from_this()](const auto &event, details::Write &) { + auto listener = [ptr = this->shared_from_this()](const auto &event, details::WriteReq &) { ptr->publish(event); }; - auto write = this->loop().template resource(); + auto write = this->loop().template resource(); write->template once(listener); write->template once(listener); write->write(this->template get(), bufs, 1); @@ -172,11 +172,11 @@ public: void write(S &send, std::unique_ptr data, ssize_t len) { uv_buf_t bufs[] = { uv_buf_init(data.get(), len) }; - auto listener = [ptr = this->shared_from_this()](const auto &event, details::Write &) { + auto listener = [ptr = this->shared_from_this()](const auto &event, details::WriteReq &) { ptr->publish(event); }; - auto write = this->loop().template resource(); + auto write = this->loop().template resource(); write->template once(listener); write->template once(listener); write->write(this->template get(), bufs, 1, send.template get()); diff --git a/src/uvw/tcp.hpp b/src/uvw/tcp.hpp index e26a2c98..4e4a800b 100644 --- a/src/uvw/tcp.hpp +++ b/src/uvw/tcp.hpp @@ -27,8 +27,8 @@ enum class UVTcpFlags: std::underlying_type_t { } -class Tcp final: public Stream { - using Stream::Stream; +class TcpHandle final: public StreamHandle { + using StreamHandle::StreamHandle; public: using Time = std::chrono::seconds; @@ -37,8 +37,8 @@ public: using IPv6 = details::IPv6; template - static std::shared_ptr create(Args&&... args) { - return std::shared_ptr{new Tcp{std::forward(args)...}}; + static std::shared_ptr create(Args&&... args) { + return std::shared_ptr{new TcpHandle{std::forward(args)...}}; } bool init() { return initialize(&uv_tcp_init); } @@ -91,11 +91,11 @@ public: typename details::IpTraits::Type addr; details::IpTraits::AddrFunc(ip.data(), port, &addr); - auto listener = [ptr = shared_from_this()](const auto &event, details::Connect &) { + auto listener = [ptr = shared_from_this()](const auto &event, details::ConnectReq &) { ptr->publish(event); }; - auto connect = loop().resource(); + auto connect = loop().resource(); connect->once(listener); connect->once(listener); connect->connect(&uv_tcp_connect, get(), reinterpret_cast(&addr)); diff --git a/src/uvw/timer.hpp b/src/uvw/timer.hpp index 21b72074..c76cf7af 100644 --- a/src/uvw/timer.hpp +++ b/src/uvw/timer.hpp @@ -16,9 +16,9 @@ namespace uvw { struct TimerEvent: Event { }; -class Timer final: public Handle { +class TimerHandle final: public Handle { static void startCallback(uv_timer_t *handle) { - Timer &timer = *(static_cast(handle->data)); + TimerHandle &timer = *(static_cast(handle->data)); timer.publish(TimerEvent{}); } @@ -28,8 +28,8 @@ public: using Time = std::chrono::milliseconds; template - static std::shared_ptr create(Args&&... args) { - return std::shared_ptr{new Timer{std::forward(args)...}}; + static std::shared_ptr create(Args&&... args) { + return std::shared_ptr{new TimerHandle{std::forward(args)...}}; } bool init() { return initialize(&uv_timer_init); } diff --git a/src/uvw/tty.hpp b/src/uvw/tty.hpp index 636e006a..fd9e4e92 100644 --- a/src/uvw/tty.hpp +++ b/src/uvw/tty.hpp @@ -26,19 +26,19 @@ enum class UVTTYModeT: std::underlying_type_t { } -class TTY final: public Stream { - explicit TTY(std::shared_ptr ref, +class TTYHandle final: public StreamHandle { + explicit TTYHandle(std::shared_ptr ref, FileHandle desc, bool readable) - : Stream{std::move(ref)}, fd{desc}, rw{readable} + : StreamHandle{std::move(ref)}, fd{desc}, rw{readable} { } public: using Mode = details::UVTTYModeT; template - static std::shared_ptr create(Args&&... args) { - return std::shared_ptr{new TTY{std::forward(args)...}}; + static std::shared_ptr create(Args&&... args) { + return std::shared_ptr{new TTYHandle{std::forward(args)...}}; } bool init() { return initialize(&uv_tty_init, fd, rw); } diff --git a/src/uvw/udp.hpp b/src/uvw/udp.hpp index c279efaa..0eff5b95 100644 --- a/src/uvw/udp.hpp +++ b/src/uvw/udp.hpp @@ -69,7 +69,7 @@ public: } -class Udp final: public Handle { +class UdpHandle final: public Handle { using Handle::Handle; template @@ -77,7 +77,7 @@ class Udp final: public Handle { typename details::IpTraits::Type *aptr = reinterpret_cast::Type *>(addr); int len = sizeof(*addr); - Udp &udp = *(static_cast(handle->data)); + UdpHandle &udp = *(static_cast(handle->data)); // data will be destroyed no matter of what the value of nread is std::unique_ptr data{buf->base}; @@ -102,8 +102,8 @@ public: using IPv6 = details::IPv6; template - static std::shared_ptr create(Args&&... args) { - return std::shared_ptr{new Udp{std::forward(args)...}}; + static std::shared_ptr create(Args&&... args) { + return std::shared_ptr{new UdpHandle{std::forward(args)...}}; } bool init() { return initialize(&uv_udp_init); } diff --git a/src/uvw/work.hpp b/src/uvw/work.hpp index 6450378b..0b011f88 100644 --- a/src/uvw/work.hpp +++ b/src/uvw/work.hpp @@ -16,9 +16,9 @@ namespace uvw { struct WorkEvent: Event { }; -class Work final: public Request { +class WorkReq final: public Request { static void workCallback(uv_work_t *req) { - static_cast(req->data)->task(); + static_cast(req->data)->task(); } using Request::Request; @@ -27,8 +27,8 @@ public: using Task = std::function; template - static std::shared_ptr create(Args&&... args) { - return std::shared_ptr{new Work{std::forward(args)...}}; + static std::shared_ptr create(Args&&... args) { + return std::shared_ptr{new WorkReq{std::forward(args)...}}; } void queue(Task t) { diff --git a/test/main.cpp b/test/main.cpp index 8858ff23..9267fd70 100644 --- a/test/main.cpp +++ b/test/main.cpp @@ -6,25 +6,25 @@ void listen(uvw::Loop &loop) { - std::shared_ptr tcp = loop.resource(); + std::shared_ptr tcp = loop.resource(); - tcp->on([](const uvw::ErrorEvent &, uvw::Tcp &) { + tcp->on([](const uvw::ErrorEvent &, uvw::TcpHandle &) { std::cout << "error " << std::endl; }); - tcp->once([](const uvw::ListenEvent &event, uvw::Tcp &srv) mutable { + tcp->once([](const uvw::ListenEvent &event, uvw::TcpHandle &srv) mutable { std::cout << "listen" << std::endl; - std::shared_ptr client = srv.loop().resource(); + std::shared_ptr client = srv.loop().resource(); - client->on([](const uvw::ErrorEvent &, uvw::Tcp &) { + client->on([](const uvw::ErrorEvent &, uvw::TcpHandle &) { std::cout << "error " << std::endl; }); - client->on([ptr = srv.shared_from_this()](const uvw::CloseEvent &, uvw::Tcp &) mutable { + client->on([ptr = srv.shared_from_this()](const uvw::CloseEvent &, uvw::TcpHandle &) mutable { std::cout << "close" << std::endl; - uvw::Tcp &srv = *ptr; + uvw::TcpHandle &srv = *ptr; srv.close(); }); @@ -36,12 +36,12 @@ void listen(uvw::Loop &loop) { uvw::Addr remote = client->peer(); std::cout << "remote: " << remote.ip << " " << remote.port << std::endl; - client->on([](const uvw::DataEvent &event, uvw::Tcp &) { + client->on([](const uvw::DataEvent &event, uvw::TcpHandle &) { std::cout.write(event.data(), event.length()) << std::endl; std::cout << "data length: " << event.length() << std::endl; }); - client->on([](const uvw::EndEvent &, uvw::Tcp &client) { + client->on([](const uvw::EndEvent &, uvw::TcpHandle &client) { std::cout << "end" << std::endl; int count = 0; client.loop().walk([&count](uvw::BaseHandle &handle) { ++count; }); @@ -52,7 +52,7 @@ void listen(uvw::Loop &loop) { client->read(); }); - tcp->once([](const uvw::CloseEvent &, uvw::Tcp &) mutable { + tcp->once([](const uvw::CloseEvent &, uvw::TcpHandle &) mutable { std::cout << "close" << std::endl; }); @@ -62,18 +62,18 @@ void listen(uvw::Loop &loop) { void conn(uvw::Loop &loop) { - auto tcp = loop.resource(); + auto tcp = loop.resource(); - tcp->on([](const uvw::ErrorEvent &, uvw::Tcp &) { + tcp->on([](const uvw::ErrorEvent &, uvw::TcpHandle &) { std::cout << "error " << std::endl; }); - tcp->once([](const uvw::WriteEvent &, uvw::Tcp &tcp) mutable { + tcp->once([](const uvw::WriteEvent &, uvw::TcpHandle &tcp) mutable { std::cout << "write" << std::endl; tcp.close(); }); - tcp->once([](const uvw::ConnectEvent &, uvw::Tcp &tcp) mutable { + tcp->once([](const uvw::ConnectEvent &, uvw::TcpHandle &tcp) mutable { std::cout << "connect" << std::endl; auto dataTryWrite = std::unique_ptr(new char[1]{ 'a' }); @@ -84,7 +84,7 @@ void conn(uvw::Loop &loop) { tcp.write(std::move(dataWrite), 2); }); - tcp->once([](const uvw::CloseEvent &, uvw::Tcp &) mutable { + tcp->once([](const uvw::CloseEvent &, uvw::TcpHandle &) mutable { std::cout << "close" << std::endl; }); diff --git a/test/uvw/loop.cpp b/test/uvw/loop.cpp index 1dce00c5..f33462e6 100644 --- a/test/uvw/loop.cpp +++ b/test/uvw/loop.cpp @@ -14,8 +14,8 @@ TEST(Loop, Basics) { def->walk([](uvw::BaseHandle &) { ASSERT_TRUE(false); }); auto loop = uvw::Loop::create(); - auto handle = loop->resource(); - auto req = loop->resource(); + auto handle = loop->resource(); + auto req = loop->resource(); auto err = [](uvw::ErrorEvent, auto &) { ASSERT_TRUE(false); }; @@ -29,7 +29,7 @@ TEST(Loop, Basics) { ASSERT_FALSE(loop->alive()); handle->start(); - handle->on([](uvw::PrepareEvent, uvw::Prepare &handle) { + handle->on([](uvw::PrepareEvent, uvw::PrepareHandle &handle) { handle.loop().walk([](uvw::BaseHandle &) { static bool trigger = true; ASSERT_TRUE(trigger); From ec8e179516b744d62bd537ece1d6c2774d15cf4b Mon Sep 17 00:00:00 2001 From: Michele Caini Date: Thu, 28 Jul 2016 13:49:21 +0200 Subject: [PATCH 2/2] FileHandle is no longer part of a FsReq --- src/uvw/fs.hpp | 94 +++++++++++++++++++++++--------------------------- 1 file changed, 43 insertions(+), 51 deletions(-) diff --git a/src/uvw/fs.hpp b/src/uvw/fs.hpp index 10cc3b40..65df5fc9 100644 --- a/src/uvw/fs.hpp +++ b/src/uvw/fs.hpp @@ -81,6 +81,23 @@ private: }; +template<> +struct TypedEvent + : Event> +{ + TypedEvent(const char *p, uv_file desc) noexcept + : rPath{p}, fd{desc} + { } + + const char * path() const noexcept { return rPath; } + FileHandle descriptor() const noexcept { return fd; } + +private: + const char *rPath; + uv_file fd; +}; + + template<> struct TypedEvent : Event> @@ -207,28 +224,10 @@ using FsEvent = TypedEvent; class FsReq final: public Request { - static constexpr int BAD_FD = -1; - - static void fsCloseCallback(uv_fs_t *req) { - auto ptr = reserve(reinterpret_cast(req)); - - if(req->result) { - ptr->publish(ErrorEvent{req->result}); - } else { - ptr->file = BAD_FD; - ptr->publish(FsEvent{req->path}); - } - } - static void fsOpenCallback(uv_fs_t *req) { auto ptr = reserve(reinterpret_cast(req)); - - if(req->result < 0) { - ptr->publish(ErrorEvent{req->result}); - } else { - ptr->file = req->result; - ptr->publish(FsEvent{req->path}); - } + if(req->result < 0) { ptr->publish(ErrorEvent{req->result}); } + else { ptr->publish(FsEvent{req->path, static_cast(req->result)}); } } template @@ -291,14 +290,13 @@ public: uv_fs_req_cleanup(get()); } - void close() { - cleanupAndInvoke(&uv_fs_close, parent(), get(), file, &fsCloseCallback); + void close(FileHandle file) { + cleanupAndInvoke(&uv_fs_close, parent(), get(), file, &fsGenericCallback); } - auto closeSync() { + auto closeSync(FileHandle file) { auto req = get(); cleanupAndInvokeSync(&uv_fs_close, parent(), req, file, nullptr); - if(!req->result) { file = BAD_FD; } return std::make_pair(ErrorEvent{req->result}, FsEvent{req->path}); } @@ -310,8 +308,7 @@ public: auto req = get(); cleanupAndInvokeSync(&uv_fs_open, parent(), req, path.data(), flags, mode, nullptr); auto fd = req->result; - if(fd >= 0) { file = fd; } - return std::make_pair(ErrorEvent{fd < 0 ? fd : 0}, FsEvent{req->path}); + return std::make_pair(ErrorEvent{fd < 0 ? fd : 0}, FsEvent{req->path, static_cast(fd)}); } void read(int64_t offset, unsigned int len) { @@ -332,12 +329,12 @@ public: return std::make_pair(ErrorEvent{req->result}, FsEvent{req->path}); } - void write(std::unique_ptr data, ssize_t len, int64_t offset) { + void write(FileHandle file, std::unique_ptr data, ssize_t len, int64_t offset) { uv_buf_t bufs[] = { uv_buf_init(data.get(), len) }; cleanupAndInvoke(&uv_fs_write, parent(), get(), file, bufs, 1, offset, &fsResultCallback); } - auto writeSync(std::unique_ptr data, ssize_t len, int64_t offset) { + auto writeSync(FileHandle file, std::unique_ptr data, ssize_t len, int64_t offset) { uv_buf_t bufs[] = { uv_buf_init(data.get(), len) }; auto req = get(); cleanupAndInvokeSync(&uv_fs_write, parent(), get(), file, bufs, 1, offset, nullptr); @@ -410,11 +407,11 @@ public: return std::make_pair(ErrorEvent{req->result}, FsEvent{req->path, req->statbuf}); } - void fstat() { + void fstat(FileHandle file) { cleanupAndInvoke(&uv_fs_fstat, parent(), get(), file, &fsStatCallback); } - auto fstatSync() { + auto fstatSync(FileHandle file) { auto req = get(); cleanupAndInvokeSync(&uv_fs_fstat, parent(), req, file, nullptr); return std::make_pair(ErrorEvent{req->result}, FsEvent{req->path, req->statbuf}); @@ -440,43 +437,43 @@ public: return std::make_pair(ErrorEvent{req->result}, FsEvent{req->path}); } - void fsync() { + void fsync(FileHandle file) { cleanupAndInvoke(&uv_fs_fsync, parent(), get(), file, &fsGenericCallback); } - auto fsyncSync() { + auto fsyncSync(FileHandle file) { auto req = get(); cleanupAndInvokeSync(&uv_fs_fsync, parent(), req, file, nullptr); return std::make_pair(ErrorEvent{req->result}, FsEvent{req->path}); } - void fdatasync() { + void fdatasync(FileHandle file) { cleanupAndInvoke(&uv_fs_fdatasync, parent(), get(), file, &fsGenericCallback); } - auto fdatasyncSync() { + auto fdatasyncSync(FileHandle file) { auto req = get(); cleanupAndInvokeSync(&uv_fs_fdatasync, parent(), req, file, nullptr); return std::make_pair(ErrorEvent{req->result}, FsEvent{req->path}); } - void ftruncate(int64_t offset) { + void ftruncate(FileHandle file, int64_t offset) { cleanupAndInvoke(&uv_fs_ftruncate, parent(), get(), file, offset, &fsGenericCallback); } - auto ftruncateSync(int64_t offset) { + auto ftruncateSync(FileHandle file, int64_t offset) { auto req = get(); cleanupAndInvokeSync(&uv_fs_ftruncate, parent(), req, file, offset, nullptr); return std::make_pair(ErrorEvent{req->result}, FsEvent{req->path}); } - void sendfile(FileHandle out, int64_t offset, size_t length) { - cleanupAndInvoke(&uv_fs_sendfile, parent(), get(), out, file, offset, length, &fsResultCallback); + void sendfile(FileHandle out, FileHandle in, int64_t offset, size_t length) { + cleanupAndInvoke(&uv_fs_sendfile, parent(), get(), out, in, offset, length, &fsResultCallback); } - auto sendfileSync(FileHandle out, int64_t offset, size_t length) { + auto sendfileSync(FileHandle out, FileHandle in, int64_t offset, size_t length) { auto req = get(); - cleanupAndInvokeSync(&uv_fs_sendfile, parent(), req, out, file, offset, length, nullptr); + cleanupAndInvokeSync(&uv_fs_sendfile, parent(), req, out, in, offset, length, nullptr); auto bw = req->result; return std::make_pair(ErrorEvent{bw < 0 ? bw : 0}, FsEvent{req->path, bw}); } @@ -501,11 +498,11 @@ public: return std::make_pair(ErrorEvent{req->result}, FsEvent{req->path}); } - void fchmod(int mode) { + void fchmod(FileHandle file, int mode) { cleanupAndInvoke(&uv_fs_fchmod, parent(), get(), file, mode, &fsGenericCallback); } - auto fchmodSync(int mode) { + auto fchmodSync(FileHandle file, int mode) { auto req = get(); cleanupAndInvokeSync(&uv_fs_fchmod, parent(), req, file, mode, nullptr); return std::make_pair(ErrorEvent{req->result}, FsEvent{req->path}); @@ -521,11 +518,11 @@ public: return std::make_pair(ErrorEvent{req->result}, FsEvent{req->path}); } - void futime(Time atime, Time mtime) { + void futime(FileHandle file, Time atime, Time mtime) { cleanupAndInvoke(&uv_fs_futime, parent(), get(), file, atime.count(), mtime.count(), &fsGenericCallback); } - auto futimeSync(Time atime, Time mtime) { + auto futimeSync(FileHandle file, Time atime, Time mtime) { auto req = get(); cleanupAndInvokeSync(&uv_fs_futime, parent(), req, file, atime.count(), mtime.count(), nullptr); return std::make_pair(ErrorEvent{req->result}, FsEvent{req->path}); @@ -582,20 +579,15 @@ public: return std::make_pair(ErrorEvent{req->result}, FsEvent{req->path}); } - void fchown(Uid uid, Gid gid) { + void fchown(FileHandle file, Uid uid, Gid gid) { cleanupAndInvoke(&uv_fs_fchown, parent(), get(), file, uid, gid, &fsGenericCallback); } - auto fchownSync(Uid uid, Gid gid) { + auto fchownSync(FileHandle file, Uid uid, Gid gid) { auto req = get(); cleanupAndInvokeSync(&uv_fs_fchown, parent(), req, file, uid, gid, nullptr); return std::make_pair(ErrorEvent{req->result}, FsEvent{req->path}); } - - operator FileHandle() const noexcept { return file; } - -private: - uv_file file{BAD_FD}; };