WIP: UDP
This commit is contained in:
parent
896bc01ae7
commit
36112d3884
@ -92,6 +92,7 @@ private:
|
|||||||
struct IdleEvent: Event<IdleEvent> { };
|
struct IdleEvent: Event<IdleEvent> { };
|
||||||
struct ListenEvent: Event<ListenEvent> { };
|
struct ListenEvent: Event<ListenEvent> { };
|
||||||
struct PrepareEvent: Event<PrepareEvent> { };
|
struct PrepareEvent: Event<PrepareEvent> { };
|
||||||
|
struct SendEvent: Event<SendEvent> { };
|
||||||
struct ShutdownEvent: Event<ShutdownEvent> { };
|
struct ShutdownEvent: Event<ShutdownEvent> { };
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -41,6 +41,10 @@ class Handle: public BaseHandle, public Resource<T>
|
|||||||
protected:
|
protected:
|
||||||
using Resource<T>::Resource;
|
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>
|
template<typename U, typename F, typename... Args>
|
||||||
bool initialize(F &&f, Args&&... args) {
|
bool initialize(F &&f, Args&&... args) {
|
||||||
if(!this->self()) {
|
if(!this->self()) {
|
||||||
|
|||||||
@ -15,6 +15,7 @@ struct RequestType;
|
|||||||
|
|
||||||
template<> struct RequestType<uv_connect_t> { };
|
template<> struct RequestType<uv_connect_t> { };
|
||||||
template<> struct RequestType<uv_shutdown_t> { };
|
template<> struct RequestType<uv_shutdown_t> { };
|
||||||
|
template<> struct RequestType<uv_udp_send_t> { };
|
||||||
template<> struct RequestType<uv_work_t> { };
|
template<> struct RequestType<uv_work_t> { };
|
||||||
template<> struct RequestType<uv_write_t> { };
|
template<> struct RequestType<uv_write_t> { };
|
||||||
|
|
||||||
|
|||||||
@ -59,10 +59,6 @@ template<typename T>
|
|||||||
class Stream: public Handle<T> {
|
class Stream: public Handle<T> {
|
||||||
static constexpr unsigned int DEFAULT_BACKLOG = 128;
|
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) {
|
static void readCallback(uv_stream_t *handle, ssize_t nread, const uv_buf_t *buf) {
|
||||||
T &ref = *(static_cast<T*>(handle->data));
|
T &ref = *(static_cast<T*>(handle->data));
|
||||||
// data will be destroyed no matter of what the value of nread is
|
// data will be destroyed no matter of what the value of nread is
|
||||||
@ -138,7 +134,7 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
void read() {
|
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() {
|
void stop() {
|
||||||
|
|||||||
@ -14,6 +14,29 @@
|
|||||||
namespace uvw {
|
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> {
|
class Udp final: public Stream<Udp> {
|
||||||
explicit Udp(std::shared_ptr<Loop> ref)
|
explicit Udp(std::shared_ptr<Loop> ref)
|
||||||
: Stream{HandleType<uv_udp_t>{}, std::move(ref)}
|
: Stream{HandleType<uv_udp_t>{}, std::move(ref)}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user