Merge pull request #22 from cynnyx/master

WIP: Pipe
This commit is contained in:
Michele Caini 2016-07-21 16:23:32 +02:00 committed by GitHub
commit 6978fc2d1a
6 changed files with 31 additions and 39 deletions

View File

@ -4,6 +4,7 @@
#include "uvw/fs_poll.hpp"
#include "uvw/idle.hpp"
#include "uvw/loop.hpp"
#include "uvw/pipe.hpp"
#include "uvw/poll.hpp"
#include "uvw/prepare.hpp"
#include "uvw/signal.hpp"

View File

@ -26,18 +26,15 @@ public:
return std::shared_ptr<Pipe>{new Pipe{std::forward<Args>(args)...}};
}
bool init(bool ipc = false) { return initialize<uv_pipe_t>(&uv_ipc_init, ipc); }
bool init(bool ipc = false) { return initialize<uv_pipe_t>(&uv_pipe_init, ipc); }
void bind(std::string name) {
invoke(&uv_pipe_bind, get<uv_pipe_t>(), name.data());
}
void connect(std::string name) {
std::weak_ptr<Pipe> weak = this->shared_from_this();
auto listener = [weak](const auto &event, details::Connect &) {
auto ptr = weak.lock();
if(ptr) { ptr->publish(event); }
auto listener = [ptr = this->shared_from_this()](const auto &event, details::Connect &) {
ptr->publish(event);
};
auto connect = loop().resource<details::Connect>();
@ -49,8 +46,9 @@ public:
std::string sock() const noexcept { return details::path(&uv_pipe_getsockname, get<uv_pipe_t>()); }
std::string peer() const noexcept { return details::path(&uv_pipe_getpeername, get<uv_pipe_t>()); }
// TODO uv_pipe_pending_instances
// TODO uv_pipe_pending_count
void pending(int count) noexcept { uv_pipe_pending_instances(get<uv_pipe_t>(), count); }
int pending() noexcept { return uv_pipe_pending_count(get<uv_pipe_t>()); }
// TODO uv_pipe_pending_type
};

View File

@ -1,6 +1,7 @@
#pragma once
#include <type_traits>
#include <utility>
#include <memory>
#include <uv.h>
@ -41,13 +42,21 @@ class Request: public Resource<T> {
protected:
using Resource<T>::Resource;
template<typename R, typename E, typename... A>
auto exec(A&&... args) {
auto ret = this->invoke(std::forward<A>(args)..., &execCallback<R, E>);
template<typename R, typename E, typename F, typename... A>
auto exec(F &&f, A&&... args)
-> std::enable_if_t<not std::is_void<std::result_of_t<F(A..., decltype(&execCallback<R, E>))>>::value, int> {
auto ret = this->invoke(std::forward<F>(f), std::forward<A>(args)..., &execCallback<R, E>);
if(0 == ret) { this->leak(); }
return ret;
}
template<typename R, typename E, typename F, typename... A>
auto exec(F &&f, A&&... args)
-> std::enable_if_t<std::is_void<std::result_of_t<F(A..., decltype(&execCallback<R, E>))>>::value> {
std::forward<F>(f)(std::forward<A>(args)..., &execCallback<R, E>);
this->leak();
}
public:
void cancel() {
this->invoke(&uv_cancel, this->template get<uv_req_t>());

View File

@ -108,11 +108,8 @@ protected:
public:
void shutdown() {
std::weak_ptr<T> weak = this->shared_from_this();
auto listener = [weak](const auto &event, details::Shutdown &) {
auto ptr = weak.lock();
if(ptr) { ptr->publish(event); }
auto listener = [ptr = this->shared_from_this()](const auto &event, details::Shutdown &) {
ptr->publish(event);
};
auto shutdown = this->loop().template resource<details::Shutdown>();
@ -141,11 +138,9 @@ public:
void write(char *data, ssize_t len) {
uv_buf_t bufs[] = { uv_buf_init(data, len) };
std::weak_ptr<T> weak = this->shared_from_this();
auto listener = [weak](const auto &event, details::Write &) {
auto ptr = weak.lock();
if(ptr) { ptr->publish(event); }
auto listener = [ptr = this->shared_from_this()](const auto &event, details::Write &) {
ptr->publish(event);
};
auto write = this->loop().template resource<details::Write>();

View File

@ -85,16 +85,10 @@ public:
typename Traits::Type addr;
Traits::AddrFunc(ip.data(), port, &addr);
std::weak_ptr<Tcp> weak = this->shared_from_this();
auto listener = [weak](const auto &event, details::Connect &) {
auto ptr = weak.lock();
if(ptr) {
ptr->sockF = &tSock<I>;
ptr->peerF = &tPeer<I>;
ptr->publish(event);
}
auto listener = [ptr = this->shared_from_this()](const auto &event, details::Connect &) {
ptr->sockF = &tSock<I>;
ptr->peerF = &tPeer<I>;
ptr->publish(event);
};
auto connect = loop().resource<details::Connect>();

View File

@ -151,16 +151,11 @@ public:
Traits::AddrFunc(ip.data(), port, &addr);
uv_buf_t bufs[] = { uv_buf_init(data, len) };
std::weak_ptr<Udp> weak = this->shared_from_this();
auto listener = [weak](const auto &event, details::Send &) {
auto ptr = weak.lock();
if(ptr) {
ptr->sockF = &tSock<I>;
ptr->peerF = &tPeer<I>;
ptr->publish(event);
}
auto listener = [ptr = this->shared_from_this()](const auto &event, details::Send &) {
ptr->sockF = &tSock<I>;
ptr->peerF = &tPeer<I>;
ptr->publish(event);
};
auto send = this->loop().resource<details::Send>();