added Stream::listen
This commit is contained in:
parent
d3a2d03fb1
commit
4cf0dfba4d
@ -49,7 +49,7 @@ class Resource: public std::enable_shared_from_this<T> {
|
|||||||
template<typename, typename>
|
template<typename, typename>
|
||||||
friend struct details::UVCallback;
|
friend struct details::UVCallback;
|
||||||
|
|
||||||
static void closeCallback(T &ref, uv_handle_t* h) {
|
static void closeCallback(T &ref, uv_handle_t*) {
|
||||||
Resource<T> &res = ref;
|
Resource<T> &res = ref;
|
||||||
res.closeCb(UVWError{});
|
res.closeCb(UVWError{});
|
||||||
res.closeCb = nullptr;
|
res.closeCb = nullptr;
|
||||||
@ -117,9 +117,10 @@ template<typename T, typename H, typename... Args>
|
|||||||
template<void(*F)(T &, H, Args...)>
|
template<void(*F)(T &, H, Args...)>
|
||||||
void UVCallback<T, void(H, Args...)>::proto(H handle, Args... args) {
|
void UVCallback<T, void(H, Args...)>::proto(H handle, Args... args) {
|
||||||
T &ref = *(static_cast<T*>(details::get(handle)));
|
T &ref = *(static_cast<T*>(details::get(handle)));
|
||||||
F(ref, handle, std::forward<Args>(args)...);
|
auto ptr = ref.leak;
|
||||||
ref.leak.reset();
|
ref.leak.reset();
|
||||||
|
F(ref, handle, std::forward<Args>(args)...);
|
||||||
|
(void)ptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -11,43 +11,27 @@ namespace uvw {
|
|||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
class Stream: public Resource<T> {
|
class Stream: public Resource<T> {
|
||||||
static void protoListen(uv_stream_t* srv, int status) {
|
static void listenCallback(T &ref, uv_stream_t* srv, int status) {
|
||||||
Stream &stream = *(static_cast<Stream*>(srv->data));
|
ref.listenCb(UVWError{status});
|
||||||
|
ref.listenCb = nullptr;
|
||||||
if(status) {
|
|
||||||
stream.listenCallback(UVWError{status});
|
|
||||||
} else {
|
|
||||||
stream.tryAccept();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void tryAccept() const noexcept {
|
|
||||||
Handle<Stream> handle = accept();
|
|
||||||
|
|
||||||
if(handle) {
|
|
||||||
// TODO invoke cb with handle
|
|
||||||
} else {
|
|
||||||
// TODO invoke cb with error
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
virtual Handle<Stream> accept() const noexcept = 0;
|
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
using Resource<T>::Resource;
|
using Resource<T>::Resource;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
using CallbackListen = std::function<void(UVWError)>;
|
|
||||||
|
|
||||||
// TODO shutdown
|
// TODO shutdown
|
||||||
|
|
||||||
void listen(int backlog, CallbackListen cb) noexcept {
|
void listen(int backlog, std::function<void(UVWError)> cb) noexcept {
|
||||||
listenCallback = std::move(cb);
|
using CB = typename Resource<T>::template Callback<void(uv_stream_t*, int)>;
|
||||||
this->template get<uv_stream_t>()->data = this;
|
|
||||||
auto err = uv_listen(this->template get<uv_stream_t>(), backlog, &protoListen);
|
auto func = CB::get<&Stream<T>::listenCallback>(*static_cast<T*>(this));
|
||||||
|
auto err = uv_listen(this->template get<uv_stream_t>(), backlog, func);
|
||||||
|
|
||||||
if(err) {
|
if(err) {
|
||||||
listenCallback(UVWError{err});
|
listenCb(UVWError{err});
|
||||||
|
} else {
|
||||||
|
listenCb = std::move(cb);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -65,7 +49,7 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
CallbackListen listenCallback;
|
std::function<void(UVWError)> listenCb;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -22,16 +22,6 @@ class Tcp final: public Stream<Tcp> {
|
|||||||
tcp->connCb = nullptr;
|
tcp->connCb = nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
Handle<Stream> accept() const noexcept override {
|
|
||||||
auto handle = loop()->handle<Tcp>(get<uv_stream_t>());
|
|
||||||
|
|
||||||
if(!handle || !static_cast<Tcp&>(handle)) {
|
|
||||||
handle = Handle<Tcp>{};
|
|
||||||
}
|
|
||||||
|
|
||||||
return handle;
|
|
||||||
}
|
|
||||||
|
|
||||||
explicit Tcp(std::shared_ptr<Loop> ref)
|
explicit Tcp(std::shared_ptr<Loop> ref)
|
||||||
: Stream{HandleType<uv_tcp_t>{}, std::move(ref)},
|
: Stream{HandleType<uv_tcp_t>{}, std::move(ref)},
|
||||||
conn{std::make_unique<uv_connect_t>()}
|
conn{std::make_unique<uv_connect_t>()}
|
||||||
@ -45,7 +35,6 @@ class Tcp final: public Stream<Tcp> {
|
|||||||
|
|
||||||
public:
|
public:
|
||||||
using Time = std::chrono::duration<uint64_t>;
|
using Time = std::chrono::duration<uint64_t>;
|
||||||
using CallbackConnect = std::function<void(UVWError)>;
|
|
||||||
|
|
||||||
enum { IPv4, IPv6 };
|
enum { IPv4, IPv6 };
|
||||||
|
|
||||||
@ -63,19 +52,19 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
template<int>
|
template<int>
|
||||||
void connect(std::string, int, CallbackConnect) noexcept;
|
void connect(std::string, int, std::function<void(UVWError)>) noexcept;
|
||||||
|
|
||||||
explicit operator bool() { return initialized; }
|
explicit operator bool() { return initialized; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::unique_ptr<uv_connect_t> conn;
|
std::unique_ptr<uv_connect_t> conn;
|
||||||
CallbackConnect connCb;
|
std::function<void(UVWError)> connCb;
|
||||||
bool initialized;
|
bool initialized;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
template<>
|
template<>
|
||||||
void Tcp::connect<Tcp::IPv4>(std::string ip, int port, CallbackConnect cb) noexcept {
|
void Tcp::connect<Tcp::IPv4>(std::string ip, int port, std::function<void(UVWError)> cb) noexcept {
|
||||||
sockaddr_in addr;
|
sockaddr_in addr;
|
||||||
uv_ip4_addr(ip.c_str(), port, &addr);
|
uv_ip4_addr(ip.c_str(), port, &addr);
|
||||||
connCb = std::move(cb);
|
connCb = std::move(cb);
|
||||||
@ -89,7 +78,7 @@ void Tcp::connect<Tcp::IPv4>(std::string ip, int port, CallbackConnect cb) noexc
|
|||||||
|
|
||||||
|
|
||||||
template<>
|
template<>
|
||||||
void Tcp::connect<Tcp::IPv6>(std::string ip, int port, CallbackConnect cb) noexcept {
|
void Tcp::connect<Tcp::IPv6>(std::string ip, int port, std::function<void(UVWError)> cb) noexcept {
|
||||||
sockaddr_in6 addr;
|
sockaddr_in6 addr;
|
||||||
uv_ip6_addr(ip.c_str(), port, &addr);
|
uv_ip6_addr(ip.c_str(), port, &addr);
|
||||||
connCb = std::move(cb);
|
connCb = std::move(cb);
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user