commit
d309872bdc
@ -19,6 +19,7 @@ template<> struct ResourceType<uv_async_t> { };
|
||||
template<> struct ResourceType<uv_check_t> { };
|
||||
template<> struct ResourceType<uv_idle_t> { };
|
||||
template<> struct ResourceType<uv_prepare_t> { };
|
||||
template<> struct ResourceType<uv_shutdown_t> { };
|
||||
template<> struct ResourceType<uv_signal_t> { };
|
||||
template<> struct ResourceType<uv_tcp_t> { };
|
||||
template<> struct ResourceType<uv_timer_t> { };
|
||||
@ -28,6 +29,9 @@ template<> struct ResourceType<uv_work_t> { };
|
||||
|
||||
template<typename T>
|
||||
class Resource: public Emitter<T>, public Self<T> {
|
||||
template<typename U>
|
||||
friend class Resource;
|
||||
|
||||
struct BaseWrapper {
|
||||
virtual ~BaseWrapper() = default;
|
||||
virtual void * get() const noexcept = 0;
|
||||
|
||||
43
src/uvw/shutdown.hpp
Normal file
43
src/uvw/shutdown.hpp
Normal file
@ -0,0 +1,43 @@
|
||||
#pragma once
|
||||
|
||||
|
||||
#include <utility>
|
||||
#include <memory>
|
||||
#include <uv.h>
|
||||
#include "event.hpp"
|
||||
#include "request.hpp"
|
||||
#include "util.hpp"
|
||||
|
||||
|
||||
namespace uvw {
|
||||
|
||||
|
||||
class Shutdown final: public Request<Shutdown> {
|
||||
static void shutdownCallback(uv_shutdown_t *req, int status) {
|
||||
Shutdown &shutdown = *(static_cast<Shutdown*>(req->data));
|
||||
auto ptr = shutdown.shared_from_this();
|
||||
ptr->reset();
|
||||
if(status) { shutdown.publish(ErrorEvent{status}); }
|
||||
else { shutdown.publish(ShutdownEvent{}); }
|
||||
}
|
||||
|
||||
explicit Shutdown(std::shared_ptr<Loop> ref)
|
||||
: Request{ResourceType<uv_shutdown_t>{}, std::move(ref)}
|
||||
{ }
|
||||
|
||||
public:
|
||||
template<typename... Args>
|
||||
static std::shared_ptr<Shutdown> create(Args&&... args) {
|
||||
return std::shared_ptr<Shutdown>{new Shutdown{std::forward<Args>(args)...}};
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void shutdown(Resource<T> &res) noexcept {
|
||||
if(0 == invoke(&uv_shutdown, get<uv_shutdown_t>(), res.template get<uv_stream_t>(), &shutdownCallback)) {
|
||||
leak();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
}
|
||||
@ -9,6 +9,7 @@
|
||||
#include "event.hpp"
|
||||
#include "handle.hpp"
|
||||
#include "util.hpp"
|
||||
#include "shutdown.hpp"
|
||||
|
||||
|
||||
namespace uvw {
|
||||
@ -44,13 +45,6 @@ class Stream: public Handle<T> {
|
||||
delete req;
|
||||
}
|
||||
|
||||
static void shutdownCallback(uv_shutdown_t *req, int status) {
|
||||
// TODO migrate to Request (see request.hpp for further details)
|
||||
T &ref = *(static_cast<T*>(req->handle->data));
|
||||
if(status) { ref.publish(ErrorEvent{status}); }
|
||||
else { ref.publish(ShutdownEvent{}); }
|
||||
}
|
||||
|
||||
static void listenCallback(uv_stream_t *handle, int status) {
|
||||
T &ref = *(static_cast<T*>(handle->data));
|
||||
if(status) ref.publish(ErrorEvent{status});
|
||||
@ -60,14 +54,16 @@ class Stream: public Handle<T> {
|
||||
protected:
|
||||
template<typename U>
|
||||
Stream(ResourceType<U> rt, std::shared_ptr<Loop> ref)
|
||||
: Handle<T>{std::move(rt), std::move(ref)},
|
||||
// TODO migrate to Request (see request.hpp for further details)
|
||||
sdown{std::make_unique<uv_shutdown_t>()}
|
||||
: Handle<T>{std::move(rt), std::move(ref)}
|
||||
{ }
|
||||
|
||||
public:
|
||||
void shutdown() noexcept {
|
||||
this->invoke(&uv_shutdown, sdown.get(), this->template get<uv_stream_t>(), &shutdownCallback);
|
||||
auto listener = [this](const auto &event, Shutdown &) { publish(event); };
|
||||
auto shutdown = this->loop()->template resource<Shutdown>();
|
||||
shutdown->template once<ErrorEvent>(listener);
|
||||
shutdown->template once<ShutdownEvent>(listener);
|
||||
shutdown->shutdown(*this);
|
||||
}
|
||||
|
||||
void listen(int backlog) noexcept {
|
||||
@ -126,10 +122,6 @@ public:
|
||||
bool writable() const noexcept {
|
||||
return (uv_is_writable(this->template get<uv_stream_t>()) == 1);
|
||||
}
|
||||
|
||||
private:
|
||||
// TODO migrate to Request (see request.hpp for further details)
|
||||
std::unique_ptr<uv_shutdown_t> sdown;
|
||||
};
|
||||
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user