Skip to content
This repository has been archived by the owner on May 4, 2018. It is now read-only.

Commit

Permalink
Move shared c-ares glue code from uv-common to cares.c
Browse files Browse the repository at this point in the history
  • Loading branch information
piscisaureus committed May 22, 2012
1 parent c06edd4 commit 58ba2d8
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 54 deletions.
2 changes: 1 addition & 1 deletion include/uv.h
Expand Up @@ -137,7 +137,6 @@ typedef enum {
#undef UV_ERRNO_GEN

#define UV_HANDLE_TYPE_MAP(XX) \
XX(ARES_TASK, ares_task) \
XX(ASYNC, async) \
XX(CHECK, check) \
XX(FS_EVENT, fs_event) \
Expand Down Expand Up @@ -165,6 +164,7 @@ typedef enum {
#define XX(uc, lc) UV_##uc,
UV_HANDLE_TYPE_MAP(XX)
#undef XX
UV_ARES_TASK,
UV_FILE,
UV_HANDLE_TYPE_PRIVATE
UV_HANDLE_TYPE_MAX
Expand Down
46 changes: 46 additions & 0 deletions src/cares.c
Expand Up @@ -20,6 +20,7 @@
*/

#include "uv.h"
#include "tree.h"
#include "uv-common.h"

#include <assert.h>
Expand All @@ -28,6 +29,51 @@
#include <string.h>


struct uv_ares_task_s {
UV_HANDLE_FIELDS
ares_socket_t sock;
uv_poll_t poll_watcher;
RB_ENTRY(uv_ares_task_s) node;
};


static int cmp_ares_tasks(const uv_ares_task_t* a, const uv_ares_task_t* b) {
if (a->sock < b->sock) return -1;
if (a->sock > b->sock) return 1;
return 0;
}


RB_GENERATE_STATIC(uv__ares_tasks, uv_ares_task_s, node, cmp_ares_tasks)


/* Add ares handle to list. */
static void uv_add_ares_handle(uv_loop_t* loop, uv_ares_task_t* handle) {
assert(loop == handle->loop);
RB_INSERT(uv__ares_tasks, &loop->ares_handles, handle);
}


/* Find matching ares handle in list. */
static uv_ares_task_t* uv_find_ares_handle(uv_loop_t* loop, ares_socket_t sock) {
uv_ares_task_t handle;
handle.sock = sock;
return RB_FIND(uv__ares_tasks, &loop->ares_handles, &handle);
}


/* Remove ares handle from list. */
static void uv_remove_ares_handle(uv_ares_task_t* handle) {
RB_REMOVE(uv__ares_tasks, &handle->loop->ares_handles, handle);
}


/* Returns 1 if the ares_handles list is empty, 0 otherwise. */
static int uv_ares_handles_empty(uv_loop_t* loop) {
return RB_EMPTY(&loop->ares_handles);
}


/* This is called once per second by loop->timer. It is used to constantly */
/* call back into c-ares for possibly processing timeouts. */
static void uv__ares_timeout(uv_timer_t* handle, int status) {
Expand Down
37 changes: 0 additions & 37 deletions src/uv-common.c
Expand Up @@ -199,43 +199,6 @@ int uv_ip6_name(struct sockaddr_in6* src, char* dst, size_t size) {
}


static int cmp_ares_tasks(const uv_ares_task_t* a, const uv_ares_task_t* b) {
if (a->sock < b->sock) return -1;
if (a->sock > b->sock) return 1;
return 0;
}


RB_GENERATE_STATIC(uv__ares_tasks, uv_ares_task_s, node, cmp_ares_tasks)


/* add ares handle to list */
void uv_add_ares_handle(uv_loop_t* loop, uv_ares_task_t* handle) {
assert(loop == handle->loop);
RB_INSERT(uv__ares_tasks, &loop->ares_handles, handle);
}


/* find matching ares handle in list */
uv_ares_task_t* uv_find_ares_handle(uv_loop_t* loop, ares_socket_t sock) {
uv_ares_task_t handle;
handle.sock = sock;
return RB_FIND(uv__ares_tasks, &loop->ares_handles, &handle);
}


/* remove ares handle in list */
void uv_remove_ares_handle(uv_ares_task_t* handle) {
RB_REMOVE(uv__ares_tasks, &handle->loop->ares_handles, handle);
}


/* Returns 1 if the ares_handles list is empty. 0 otherwise. */
int uv_ares_handles_empty(uv_loop_t* loop) {
return RB_EMPTY(&loop->ares_handles);
}


int uv_tcp_bind(uv_tcp_t* handle, struct sockaddr_in addr) {
if (handle->type != UV_TCP || addr.sin_family != AF_INET) {
uv__set_artificial_error(handle->loop, UV_EFAULT);
Expand Down
16 changes: 0 additions & 16 deletions src/uv-common.h
Expand Up @@ -57,22 +57,6 @@ enum {
# define UV__ACTIVE 0x00000040
#endif

struct uv_ares_task_s {
UV_HANDLE_FIELDS
ares_socket_t sock;
uv_poll_t poll_watcher;
RB_ENTRY(uv_ares_task_s) node;
};


void uv_remove_ares_handle(uv_ares_task_t* handle);
uv_ares_task_t* uv_find_ares_handle(uv_loop_t*, ares_socket_t sock);

/* TODO Rename to uv_ares_task_init? */
void uv_add_ares_handle(uv_loop_t* loop, uv_ares_task_t* handle);

int uv_ares_handles_empty(uv_loop_t* loop);

extern const uv_err_t uv_ok_;

uv_err_code uv_translate_sys_error(int sys_errno);
Expand Down

0 comments on commit 58ba2d8

Please sign in to comment.