28
28
#include " node.h"
29
29
#include " req-wrap.h"
30
30
#include " req-wrap-inl.h"
31
- #include " tree.h"
32
31
#include " util.h"
33
32
#include " util-inl.h"
34
33
#include " uv.h"
37
36
#include < stdlib.h>
38
37
#include < string.h>
39
38
#include < vector>
39
+ #include < unordered_set>
40
40
41
41
#if defined(__ANDROID__) || \
42
42
defined (__MINGW32__) || \
@@ -122,10 +122,22 @@ struct node_ares_task {
122
122
ChannelWrap* channel;
123
123
ares_socket_t sock;
124
124
uv_poll_t poll_watcher;
125
- RB_ENTRY (node_ares_task) node;
126
125
};
127
126
128
- RB_HEAD (node_ares_task_list, node_ares_task);
127
+ struct TaskHash {
128
+ size_t operator ()(node_ares_task* a) const {
129
+ return std::hash<ares_socket_t >()(a->sock );
130
+ }
131
+ };
132
+
133
+ struct TaskEqual {
134
+ inline bool operator ()(node_ares_task* a, node_ares_task* b) const {
135
+ return a->sock == b->sock ;
136
+ }
137
+ };
138
+
139
+ using node_ares_task_list =
140
+ std::unordered_set<node_ares_task*, TaskHash, TaskEqual>;
129
141
130
142
class ChannelWrap : public AsyncWrap {
131
143
public:
@@ -169,8 +181,6 @@ ChannelWrap::ChannelWrap(Environment* env,
169
181
query_last_ok_(true ),
170
182
is_servers_default_(true ),
171
183
library_inited_(false ) {
172
- RB_INIT (&task_list_);
173
-
174
184
MakeWeak<ChannelWrap>(this );
175
185
176
186
Setup ();
@@ -222,25 +232,12 @@ GetNameInfoReqWrap::~GetNameInfoReqWrap() {
222
232
}
223
233
224
234
225
- int cmp_ares_tasks (const node_ares_task* a, const node_ares_task* b) {
226
- if (a->sock < b->sock )
227
- return -1 ;
228
- if (a->sock > b->sock )
229
- return 1 ;
230
- return 0 ;
231
- }
232
-
233
-
234
- RB_GENERATE_STATIC (node_ares_task_list, node_ares_task, node, cmp_ares_tasks)
235
-
236
-
237
-
238
235
/* This is called once per second by loop->timer. It is used to constantly */
239
236
/* call back into c-ares for possibly processing timeouts. */
240
237
void ChannelWrap::AresTimeout (uv_timer_t * handle) {
241
238
ChannelWrap* channel = static_cast <ChannelWrap*>(handle->data );
242
239
CHECK_EQ (channel->timer_handle (), handle);
243
- CHECK_EQ (false , RB_EMPTY ( channel->task_list ()));
240
+ CHECK_EQ (false , channel->task_list ()-> empty ( ));
244
241
ares_process_fd (channel->cares_channel (), ARES_SOCKET_BAD, ARES_SOCKET_BAD);
245
242
}
246
243
@@ -306,7 +303,9 @@ void ares_sockstate_cb(void* data,
306
303
307
304
node_ares_task lookup_task;
308
305
lookup_task.sock = sock;
309
- task = RB_FIND (node_ares_task_list, channel->task_list (), &lookup_task);
306
+ auto it = channel->task_list ()->find (&lookup_task);
307
+
308
+ task = (it == channel->task_list ()->end ()) ? nullptr : *it;
310
309
311
310
if (read || write ) {
312
311
if (!task) {
@@ -315,7 +314,7 @@ void ares_sockstate_cb(void* data,
315
314
/* If this is the first socket then start the timer. */
316
315
uv_timer_t * timer_handle = channel->timer_handle ();
317
316
if (!uv_is_active (reinterpret_cast <uv_handle_t *>(timer_handle))) {
318
- CHECK (RB_EMPTY ( channel->task_list ()));
317
+ CHECK (channel->task_list ()-> empty ( ));
319
318
uv_timer_start (timer_handle, ChannelWrap::AresTimeout, 1000 , 1000 );
320
319
}
321
320
@@ -327,7 +326,7 @@ void ares_sockstate_cb(void* data,
327
326
return ;
328
327
}
329
328
330
- RB_INSERT (node_ares_task_list, channel->task_list (), task);
329
+ channel->task_list ()-> insert ( task);
331
330
}
332
331
333
332
/* This should never fail. If it fails anyway, the query will eventually */
@@ -343,11 +342,11 @@ void ares_sockstate_cb(void* data,
343
342
CHECK (task &&
344
343
" When an ares socket is closed we should have a handle for it" );
345
344
346
- RB_REMOVE (node_ares_task_list, channel->task_list (), task );
345
+ channel->task_list ()-> erase (it );
347
346
uv_close (reinterpret_cast <uv_handle_t *>(&task->poll_watcher ),
348
347
ares_poll_close_cb);
349
348
350
- if (RB_EMPTY ( channel->task_list ())) {
349
+ if (channel->task_list ()-> empty ( )) {
351
350
uv_timer_stop (channel->timer_handle ());
352
351
}
353
352
}
0 commit comments