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