@@ -10,13 +10,11 @@ static const int MAX_LOOP_ITERATIONS = 10000;
10
10
11
11
#define SPIN_WHILE (condition ) \
12
12
{ \
13
- bool timed_out = false ; \
14
- uv_timer_t * timer = start_timer (&timed_out); \
15
- while (((condition)) && !timed_out) { \
13
+ Timeout timeout (&loop); \
14
+ while ((condition) && !timeout.timed_out ) { \
16
15
uv_run (&loop, UV_RUN_NOWAIT); \
17
16
} \
18
17
ASSERT_FALSE ((condition)); \
19
- cleanup_timer (timer); \
20
18
}
21
19
22
20
static bool connected = false ;
@@ -46,32 +44,36 @@ static const char HANDSHAKE_REQ[] = "GET /ws/path HTTP/1.1\r\n"
46
44
" Sec-WebSocket-Key: aaa==\r\n "
47
45
" Sec-WebSocket-Version: 13\r\n\r\n " ;
48
46
49
- static void dispose_handle (uv_handle_t * handle) {
50
- *static_cast <bool *>(handle->data ) = true ;
51
- }
52
-
53
- static void set_timeout_flag (uv_timer_t * timer) {
54
- *(static_cast <bool *>(timer->data )) = true ;
55
- }
47
+ class Timeout {
48
+ public:
49
+ explicit Timeout (uv_loop_t * loop) : timed_out(false ), done_(false ) {
50
+ uv_timer_init (loop, &timer_);
51
+ uv_timer_start (&timer_, Timeout::set_flag, 5000 , 0 );
52
+ }
56
53
57
- static uv_timer_t * start_timer (bool * flag) {
58
- uv_timer_t * timer = new uv_timer_t ();
59
- uv_timer_init (&loop, timer);
60
- timer->data = flag;
61
- uv_timer_start (timer, set_timeout_flag, 5000 , 0 );
62
- return timer;
63
- }
54
+ ~Timeout () {
55
+ uv_timer_stop (&timer_);
56
+ uv_close (reinterpret_cast <uv_handle_t *>(&timer_), mark_done);
57
+ while (!done_) {
58
+ uv_run (&loop, UV_RUN_NOWAIT);
59
+ }
60
+ }
61
+ bool timed_out;
62
+ private:
63
+ static void set_flag (uv_timer_t * timer) {
64
+ Timeout* t = node::ContainerOf (&Timeout::timer_, timer);
65
+ t->timed_out = true ;
66
+ }
64
67
65
- static void cleanup_timer (uv_timer_t * timer) {
66
- bool done = false ;
67
- timer->data = &done;
68
- uv_timer_stop (timer);
69
- uv_close (reinterpret_cast <uv_handle_t *>(timer), dispose_handle);
70
- while (!done) {
71
- uv_run (&loop, UV_RUN_NOWAIT);
68
+ static void mark_done (uv_handle_t * timer) {
69
+ Timeout* t = node::ContainerOf (&Timeout::timer_,
70
+ reinterpret_cast <uv_timer_t *>(timer));
71
+ t->done_ = true ;
72
72
}
73
- delete timer;
74
- }
73
+
74
+ bool done_;
75
+ uv_timer_t timer_;
76
+ };
75
77
76
78
static void stop_if_stop_path (enum inspector_handshake_event state,
77
79
const std::string& path, bool * cont) {
0 commit comments