static void *handle_alloc(uv_loop_t *loop, size_t size)
{
- uv_handle_t *handle = malloc(size);
- if (handle) {
- memset(handle, 0, size);
- }
- return handle;
+ return malloc(size);
}
static void handle_free(uv_handle_t *handle)
return;
}
- uv_stream_t *client = (uv_stream_t *)io_create(master->loop, SOCK_STREAM);
- if (!client || uv_accept(master, client) != 0) {
+ uv_stream_t *client = handle_alloc(master->loop, sizeof(*client));
+ if (!client) {
+ return;
+ }
+ io_create(master->loop, (uv_handle_t *)client, SOCK_STREAM);
+ if (uv_accept(master, client) != 0) {
handle_free((uv_handle_t *)client);
return;
}
uv_close((uv_handle_t *)&ep->tcp, NULL);
}
-uv_handle_t *io_create(uv_loop_t *loop, int type)
+void io_create(uv_loop_t *loop, uv_handle_t *handle, int type)
{
if (type == SOCK_DGRAM) {
- uv_udp_t *handle = handle_alloc(loop, sizeof(*handle));
- if (handle) {
- uv_udp_init(loop, handle);
- }
- return (uv_handle_t *)handle;
+ uv_udp_init(loop, (uv_udp_t *)handle);
} else {
- uv_tcp_t *handle = handle_alloc(loop, sizeof(*handle));
- if (handle) {
- uv_tcp_init(loop, handle);
- }
- return (uv_handle_t *)handle;
+ uv_tcp_init(loop, (uv_tcp_t *)handle);
}
}
-void io_close(uv_handle_t *handle)
-{
- uv_close(handle, (uv_close_cb) handle_free);
-}
-
int io_start_read(uv_handle_t *handle)
{
if (handle->type == UV_UDP) {
void udp_unbind(struct endpoint *ep);
int tcp_bind(struct endpoint *ep, struct sockaddr *addr);
void tcp_unbind(struct endpoint *ep);
-uv_handle_t *io_create(uv_loop_t *loop, int type);
-void io_close(uv_handle_t *handle);
+void io_create(uv_loop_t *loop, uv_handle_t *handle, int type);
int io_start_read(uv_handle_t *handle);
int io_stop_read(uv_handle_t *handle);
\ No newline at end of file
/* @internal IO request entry. */
struct ioreq
{
- union {
- uv_udp_send_t send;
- uv_write_t write;
- uv_connect_t connect;
- } as;
+ union {
+ uv_udp_t udp;
+ uv_tcp_t tcp;
+ uv_udp_send_t send;
+ uv_write_t write;
+ uv_connect_t connect;
+ } as;
};
static inline struct ioreq *ioreq_take(struct worker_ctx *worker)
{
- struct ioreq *req = NULL;
+ struct ioreq *req = NULL;
if (worker->ioreqs.len > 0) {
req = array_tail(worker->ioreqs);
array_pop(worker->ioreqs);
return status;
}
+static void on_close(uv_handle_t *handle)
+{
+ struct qr_task *task = handle->data;
+ ioreq_release(task->worker, (struct ioreq *)handle);
+}
+
static void on_send(uv_udp_send_t *req, int status)
{
struct qr_task *task = req->data;
- qr_task_on_send(task, status);
- ioreq_release(task->worker, (struct ioreq *)req);
+ qr_task_on_send(task, status);
+ ioreq_release(task->worker, (struct ioreq *)req);
}
static void on_write(uv_write_t *req, int status)
{
struct qr_task *task = req->data;
- qr_task_on_send(task, status);
- ioreq_release(task->worker, (struct ioreq *)req);
+ qr_task_on_send(task, status);
+ ioreq_release(task->worker, (struct ioreq *)req);
}
static int qr_task_send(struct qr_task *task, uv_handle_t *handle, struct sockaddr *addr, knot_pkt_t *pkt)
if (task->next_handle) {
if (!uv_is_closing(task->next_handle)) {
io_stop_read(task->next_handle);
- uv_close(task->next_handle, (uv_close_cb) free);
+ uv_close(task->next_handle, on_close);
}
uv_timer_stop(&task->timeout);
task->next_handle = NULL;
}
/* Create connection for iterative query */
- task->next_handle = io_create(task->worker->loop, sock_type);
- if (task->next_handle == NULL) {
+ task->next_handle = (uv_handle_t *)ioreq_take(task->worker);
+ if (!task->next_handle) {
return qr_task_finalize(task, KNOT_STATE_FAIL);
}
/* Connect or issue query datagram */
+ io_create(task->worker->loop, task->next_handle, sock_type);
task->next_handle->data = task;
if (sock_type == SOCK_STREAM) {
struct ioreq *req = ioreq_take(task->worker);