diff --git a/src/uvw/event.hpp b/src/uvw/event.hpp index 4e4ffbe2..abcec078 100644 --- a/src/uvw/event.hpp +++ b/src/uvw/event.hpp @@ -92,6 +92,7 @@ private: struct IdleEvent: Event { }; struct ListenEvent: Event { }; struct PrepareEvent: Event { }; +struct SendEvent: Event { }; struct ShutdownEvent: Event { }; diff --git a/src/uvw/handle.hpp b/src/uvw/handle.hpp index 28fe9b55..9e7996f8 100644 --- a/src/uvw/handle.hpp +++ b/src/uvw/handle.hpp @@ -41,6 +41,10 @@ class Handle: public BaseHandle, public Resource protected: using Resource::Resource; + static void allocCallback(uv_handle_t *, std::size_t suggested, uv_buf_t *buf) { + *buf = uv_buf_init(new char[suggested], suggested); + } + template bool initialize(F &&f, Args&&... args) { if(!this->self()) { diff --git a/src/uvw/request.hpp b/src/uvw/request.hpp index 7ffec791..5c6a3cd5 100644 --- a/src/uvw/request.hpp +++ b/src/uvw/request.hpp @@ -15,6 +15,7 @@ struct RequestType; template<> struct RequestType { }; template<> struct RequestType { }; +template<> struct RequestType { }; template<> struct RequestType { }; template<> struct RequestType { }; diff --git a/src/uvw/stream.hpp b/src/uvw/stream.hpp index d3a01262..3e439ae4 100644 --- a/src/uvw/stream.hpp +++ b/src/uvw/stream.hpp @@ -59,10 +59,6 @@ template class Stream: public Handle { 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(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(), &allocCallback, &readCallback); + this->invoke(&uv_read_start, this->template get(), &this->allocCallback, &readCallback); } void stop() { diff --git a/src/uvw/udp.hpp b/src/uvw/udp.hpp index 011da6ba..be480b31 100644 --- a/src/uvw/udp.hpp +++ b/src/uvw/udp.hpp @@ -14,6 +14,29 @@ namespace uvw { +namespace details { + + +class Send final: public Request { + explicit Send(std::shared_ptr ref) + : Request{RequestType{}, std::move(ref)} + { } + +public: + template + static std::shared_ptr create(Args&&... args) { + return std::shared_ptr{new Send{std::forward(args)...}}; + } + + void send(uv_udp_t *handle, const uv_buf_t bufs[], unsigned int nbufs, const struct sockaddr* addr) { + exec(&uv_udp_send, get(), handle, bufs, nbufs, addr); + } +}; + + +} + + class Udp final: public Stream { explicit Udp(std::shared_ptr ref) : Stream{HandleType{}, std::move(ref)}