diff --git a/src/uvw/resource.hpp b/src/uvw/resource.hpp index 708c6383..ad8667bf 100644 --- a/src/uvw/resource.hpp +++ b/src/uvw/resource.hpp @@ -49,7 +49,7 @@ class Resource: public std::enable_shared_from_this { template friend struct details::UVCallback; - static void closeCallback(T &ref, uv_handle_t* h) { + static void closeCallback(T &ref, uv_handle_t*) { Resource &res = ref; res.closeCb(UVWError{}); res.closeCb = nullptr; @@ -117,9 +117,10 @@ template template void UVCallback::proto(H handle, Args... args) { T &ref = *(static_cast(details::get(handle))); - F(ref, handle, std::forward(args)...); + auto ptr = ref.leak; ref.leak.reset(); - + F(ref, handle, std::forward(args)...); + (void)ptr; } diff --git a/src/uvw/stream.hpp b/src/uvw/stream.hpp index 01b11648..c307d3dd 100644 --- a/src/uvw/stream.hpp +++ b/src/uvw/stream.hpp @@ -11,43 +11,27 @@ namespace uvw { template class Stream: public Resource { - static void protoListen(uv_stream_t* srv, int status) { - Stream &stream = *(static_cast(srv->data)); - - if(status) { - stream.listenCallback(UVWError{status}); - } else { - stream.tryAccept(); - } + static void listenCallback(T &ref, uv_stream_t* srv, int status) { + ref.listenCb(UVWError{status}); + ref.listenCb = nullptr; } - void tryAccept() const noexcept { - Handle handle = accept(); - - if(handle) { - // TODO invoke cb with handle - } else { - // TODO invoke cb with error - } - } - - virtual Handle accept() const noexcept = 0; - protected: using Resource::Resource; public: - using CallbackListen = std::function; - // TODO shutdown - void listen(int backlog, CallbackListen cb) noexcept { - listenCallback = std::move(cb); - this->template get()->data = this; - auto err = uv_listen(this->template get(), backlog, &protoListen); + void listen(int backlog, std::function cb) noexcept { + using CB = typename Resource::template Callback; + + auto func = CB::get<&Stream::listenCallback>(*static_cast(this)); + auto err = uv_listen(this->template get(), backlog, func); if(err) { - listenCallback(UVWError{err}); + listenCb(UVWError{err}); + } else { + listenCb = std::move(cb); } } @@ -65,7 +49,7 @@ public: } private: - CallbackListen listenCallback; + std::function listenCb; }; diff --git a/src/uvw/tcp.hpp b/src/uvw/tcp.hpp index f09b53e3..48cf2871 100644 --- a/src/uvw/tcp.hpp +++ b/src/uvw/tcp.hpp @@ -22,16 +22,6 @@ class Tcp final: public Stream { tcp->connCb = nullptr; } - Handle accept() const noexcept override { - auto handle = loop()->handle(get()); - - if(!handle || !static_cast(handle)) { - handle = Handle{}; - } - - return handle; - } - explicit Tcp(std::shared_ptr ref) : Stream{HandleType{}, std::move(ref)}, conn{std::make_unique()} @@ -45,7 +35,6 @@ class Tcp final: public Stream { public: using Time = std::chrono::duration; - using CallbackConnect = std::function; enum { IPv4, IPv6 }; @@ -63,19 +52,19 @@ public: } template - void connect(std::string, int, CallbackConnect) noexcept; + void connect(std::string, int, std::function) noexcept; explicit operator bool() { return initialized; } private: std::unique_ptr conn; - CallbackConnect connCb; + std::function connCb; bool initialized; }; template<> -void Tcp::connect(std::string ip, int port, CallbackConnect cb) noexcept { +void Tcp::connect(std::string ip, int port, std::function cb) noexcept { sockaddr_in addr; uv_ip4_addr(ip.c_str(), port, &addr); connCb = std::move(cb); @@ -89,7 +78,7 @@ void Tcp::connect(std::string ip, int port, CallbackConnect cb) noexc template<> -void Tcp::connect(std::string ip, int port, CallbackConnect cb) noexcept { +void Tcp::connect(std::string ip, int port, std::function cb) noexcept { sockaddr_in6 addr; uv_ip6_addr(ip.c_str(), port, &addr); connCb = std::move(cb);