This commit is contained in:
Michele Caini 2016-07-19 15:36:55 +02:00
parent 896bc01ae7
commit 36112d3884
5 changed files with 30 additions and 5 deletions

View File

@ -92,6 +92,7 @@ private:
struct IdleEvent: Event<IdleEvent> { };
struct ListenEvent: Event<ListenEvent> { };
struct PrepareEvent: Event<PrepareEvent> { };
struct SendEvent: Event<SendEvent> { };
struct ShutdownEvent: Event<ShutdownEvent> { };

View File

@ -41,6 +41,10 @@ class Handle: public BaseHandle, public Resource<T>
protected:
using Resource<T>::Resource;
static void allocCallback(uv_handle_t *, std::size_t suggested, uv_buf_t *buf) {
*buf = uv_buf_init(new char[suggested], suggested);
}
template<typename U, typename F, typename... Args>
bool initialize(F &&f, Args&&... args) {
if(!this->self()) {

View File

@ -15,6 +15,7 @@ struct RequestType;
template<> struct RequestType<uv_connect_t> { };
template<> struct RequestType<uv_shutdown_t> { };
template<> struct RequestType<uv_udp_send_t> { };
template<> struct RequestType<uv_work_t> { };
template<> struct RequestType<uv_write_t> { };

View File

@ -59,10 +59,6 @@ template<typename T>
class Stream: public Handle<T> {
static constexpr unsigned int DEFAULT_BACKLOG = 128;
static void allocCallback(uv_handle_t *, std::size_t suggested, uv_buf_t *buf) {
*buf = uv_buf_init(new char[suggested], suggested);
}
static void readCallback(uv_stream_t *handle, ssize_t nread, const uv_buf_t *buf) {
T &ref = *(static_cast<T*>(handle->data));
// data will be destroyed no matter of what the value of nread is
@ -138,7 +134,7 @@ public:
}
void read() {
this->invoke(&uv_read_start, this->template get<uv_stream_t>(), &allocCallback, &readCallback);
this->invoke(&uv_read_start, this->template get<uv_stream_t>(), &this->allocCallback, &readCallback);
}
void stop() {

View File

@ -14,6 +14,29 @@
namespace uvw {
namespace details {
class Send final: public Request<Send> {
explicit Send(std::shared_ptr<Loop> ref)
: Request{RequestType<uv_udp_send_t>{}, std::move(ref)}
{ }
public:
template<typename... Args>
static std::shared_ptr<Send> create(Args&&... args) {
return std::shared_ptr<Send>{new Send{std::forward<Args>(args)...}};
}
void send(uv_udp_t *handle, const uv_buf_t bufs[], unsigned int nbufs, const struct sockaddr* addr) {
exec<uv_udp_send_t, SendEvent>(&uv_udp_send, get<uv_udp_send_t>(), handle, bufs, nbufs, addr);
}
};
}
class Udp final: public Stream<Udp> {
explicit Udp(std::shared_ptr<Loop> ref)
: Stream{HandleType<uv_udp_t>{}, std::move(ref)}