|
160
|
1 /* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
|
|
|
2 *
|
|
|
3 * Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
4 * of this software and associated documentation files (the "Software"), to
|
|
|
5 * deal in the Software without restriction, including without limitation the
|
|
|
6 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
|
|
7 * sell copies of the Software, and to permit persons to whom the Software is
|
|
|
8 * furnished to do so, subject to the following conditions:
|
|
|
9 *
|
|
|
10 * The above copyright notice and this permission notice shall be included in
|
|
|
11 * all copies or substantial portions of the Software.
|
|
|
12 *
|
|
|
13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
14 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
15 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
16 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
17 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
18 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
|
|
19 * IN THE SOFTWARE.
|
|
|
20 */
|
|
|
21
|
|
|
22 #include "uv.h"
|
|
|
23 #include "task.h"
|
|
|
24 #include <stdio.h>
|
|
|
25 #include <stdlib.h>
|
|
|
26
|
|
|
27 static int connection_cb_called = 0;
|
|
|
28 static int do_accept_called = 0;
|
|
|
29 static int close_cb_called = 0;
|
|
|
30 static int connect_cb_called = 0;
|
|
|
31
|
|
|
32
|
|
|
33 static void alloc_cb(uv_handle_t* handle, size_t size, uv_buf_t* buf) {
|
|
|
34 buf->base = malloc(size);
|
|
|
35 buf->len = size;
|
|
|
36 }
|
|
|
37
|
|
|
38
|
|
|
39 static void close_cb(uv_handle_t* handle) {
|
|
|
40 ASSERT_NOT_NULL(handle);
|
|
|
41
|
|
|
42 free(handle);
|
|
|
43
|
|
|
44 close_cb_called++;
|
|
|
45 }
|
|
|
46
|
|
|
47
|
|
|
48 static void do_accept(uv_timer_t* timer_handle) {
|
|
|
49 uv_tcp_t* server;
|
|
|
50 uv_tcp_t* accepted_handle = (uv_tcp_t*)malloc(sizeof *accepted_handle);
|
|
|
51 int r;
|
|
|
52
|
|
|
53 ASSERT_NOT_NULL(timer_handle);
|
|
|
54 ASSERT_NOT_NULL(accepted_handle);
|
|
|
55
|
|
|
56 r = uv_tcp_init(uv_default_loop(), accepted_handle);
|
|
|
57 ASSERT_OK(r);
|
|
|
58
|
|
|
59 server = (uv_tcp_t*)timer_handle->data;
|
|
|
60 r = uv_accept((uv_stream_t*)server, (uv_stream_t*)accepted_handle);
|
|
|
61 ASSERT_OK(r);
|
|
|
62
|
|
|
63 do_accept_called++;
|
|
|
64
|
|
|
65 /* Immediately close the accepted handle. */
|
|
|
66 uv_close((uv_handle_t*)accepted_handle, close_cb);
|
|
|
67
|
|
|
68 /* After accepting the two clients close the server handle */
|
|
|
69 if (do_accept_called == 2) {
|
|
|
70 uv_close((uv_handle_t*)server, close_cb);
|
|
|
71 }
|
|
|
72
|
|
|
73 /* Dispose the timer. */
|
|
|
74 uv_close((uv_handle_t*)timer_handle, close_cb);
|
|
|
75 }
|
|
|
76
|
|
|
77
|
|
|
78 static void connection_cb(uv_stream_t* tcp, int status) {
|
|
|
79 int r;
|
|
|
80 uv_timer_t* timer_handle;
|
|
|
81
|
|
|
82 ASSERT_OK(status);
|
|
|
83
|
|
|
84 timer_handle = (uv_timer_t*)malloc(sizeof *timer_handle);
|
|
|
85 ASSERT_NOT_NULL(timer_handle);
|
|
|
86
|
|
|
87 /* Accept the client after 1 second */
|
|
|
88 r = uv_timer_init(uv_default_loop(), timer_handle);
|
|
|
89 ASSERT_OK(r);
|
|
|
90
|
|
|
91 timer_handle->data = tcp;
|
|
|
92
|
|
|
93 r = uv_timer_start(timer_handle, do_accept, 1000, 0);
|
|
|
94 ASSERT_OK(r);
|
|
|
95
|
|
|
96 connection_cb_called++;
|
|
|
97 }
|
|
|
98
|
|
|
99
|
|
|
100 static void start_server(void) {
|
|
|
101 struct sockaddr_in addr;
|
|
|
102 uv_tcp_t* server = (uv_tcp_t*)malloc(sizeof *server);
|
|
|
103 int r;
|
|
|
104
|
|
|
105 ASSERT_OK(uv_ip4_addr("0.0.0.0", TEST_PORT, &addr));
|
|
|
106 ASSERT_NOT_NULL(server);
|
|
|
107
|
|
|
108 r = uv_tcp_init(uv_default_loop(), server);
|
|
|
109 ASSERT_OK(r);
|
|
|
110 r = uv_tcp_bind(server, (const struct sockaddr*) &addr, 0);
|
|
|
111 ASSERT_OK(r);
|
|
|
112
|
|
|
113 r = uv_listen((uv_stream_t*)server, 128, connection_cb);
|
|
|
114 ASSERT_OK(r);
|
|
|
115 }
|
|
|
116
|
|
|
117
|
|
|
118 static void read_cb(uv_stream_t* tcp, ssize_t nread, const uv_buf_t* buf) {
|
|
|
119 /* The server will not send anything, it should close gracefully. */
|
|
|
120
|
|
|
121 if (buf->base) {
|
|
|
122 free(buf->base);
|
|
|
123 }
|
|
|
124
|
|
|
125 if (nread >= 0) {
|
|
|
126 ASSERT_OK(nread);
|
|
|
127 } else {
|
|
|
128 ASSERT_NOT_NULL(tcp);
|
|
|
129 ASSERT_EQ(nread, UV_EOF);
|
|
|
130 uv_close((uv_handle_t*)tcp, close_cb);
|
|
|
131 }
|
|
|
132 }
|
|
|
133
|
|
|
134
|
|
|
135 static void connect_cb(uv_connect_t* req, int status) {
|
|
|
136 int r;
|
|
|
137
|
|
|
138 ASSERT_NOT_NULL(req);
|
|
|
139 ASSERT_OK(status);
|
|
|
140
|
|
|
141 /* Not that the server will send anything, but otherwise we'll never know
|
|
|
142 * when the server closes the connection. */
|
|
|
143 r = uv_read_start((uv_stream_t*)(req->handle), alloc_cb, read_cb);
|
|
|
144 ASSERT_OK(r);
|
|
|
145
|
|
|
146 connect_cb_called++;
|
|
|
147
|
|
|
148 free(req);
|
|
|
149 }
|
|
|
150
|
|
|
151
|
|
|
152 static void client_connect(void) {
|
|
|
153 struct sockaddr_in addr;
|
|
|
154 uv_tcp_t* client = (uv_tcp_t*)malloc(sizeof *client);
|
|
|
155 uv_connect_t* connect_req = malloc(sizeof *connect_req);
|
|
|
156 int r;
|
|
|
157
|
|
|
158 ASSERT_OK(uv_ip4_addr("127.0.0.1", TEST_PORT, &addr));
|
|
|
159 ASSERT_NOT_NULL(client);
|
|
|
160 ASSERT_NOT_NULL(connect_req);
|
|
|
161
|
|
|
162 r = uv_tcp_init(uv_default_loop(), client);
|
|
|
163 ASSERT_OK(r);
|
|
|
164
|
|
|
165 r = uv_tcp_connect(connect_req,
|
|
|
166 client,
|
|
|
167 (const struct sockaddr*) &addr,
|
|
|
168 connect_cb);
|
|
|
169 ASSERT_OK(r);
|
|
|
170 }
|
|
|
171
|
|
|
172
|
|
|
173
|
|
|
174 TEST_IMPL(delayed_accept) {
|
|
|
175 start_server();
|
|
|
176
|
|
|
177 client_connect();
|
|
|
178 client_connect();
|
|
|
179
|
|
|
180 uv_run(uv_default_loop(), UV_RUN_DEFAULT);
|
|
|
181
|
|
|
182 ASSERT_EQ(2, connection_cb_called);
|
|
|
183 ASSERT_EQ(2, do_accept_called);
|
|
|
184 ASSERT_EQ(2, connect_cb_called);
|
|
|
185 ASSERT_EQ(7, close_cb_called);
|
|
|
186
|
|
|
187 MAKE_VALGRIND_HAPPY(uv_default_loop());
|
|
|
188 return 0;
|
|
|
189 }
|