-
Notifications
You must be signed in to change notification settings - Fork 3.2k
/
Copy pathsocket.cpp
382 lines (307 loc) Β· 10.8 KB
/
socket.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
/*
+----------------------------------------------------------------------+
| Swoole |
+----------------------------------------------------------------------+
| This source file is subject to version 2.0 of the Apache license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.apache.org/licenses/LICENSE-2.0.html |
| If you did not receive a copy of the Apache2.0 license and are unable|
| to obtain it through the world-wide-web, please send a note to |
| [email protected] so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| @link https://www.swoole.com/ |
| @contact [email protected] |
| @license https://github.com/swoole/swoole-src/blob/master/LICENSE |
| @Author Tianfeng Han <[email protected]> |
+----------------------------------------------------------------------+
*/
#include "test_core.h"
#include "swoole_file.h"
using namespace std;
using namespace swoole;
const char test_data[] = "hello swoole, hello world, php is best";
TEST(socket, sendto) {
char sock1_path[] = "/tmp/udp_unix1.sock";
char sock2_path[] = "/tmp/udp_unix2.sock";
unlink(sock1_path);
unlink(sock2_path);
auto sock1 = make_socket(SW_SOCK_UNIX_DGRAM, SW_FD_DGRAM_SERVER, 0);
sock1->bind(sock1_path, nullptr);
auto sock2 = make_socket(SW_SOCK_UNIX_DGRAM, SW_FD_DGRAM_SERVER, 0);
sock2->bind(sock2_path, nullptr);
ASSERT_GT(sock1->sendto(sock2_path, 0, test_data, strlen(test_data)), 0);
char buf[1024] = {};
network::Address sa;
sa.type = SW_SOCK_UNIX_DGRAM;
ASSERT_GT(sock2->recvfrom(buf, sizeof(buf), 0, &sa), 0);
ASSERT_STREQ(test_data, buf);
ASSERT_STREQ(sa.get_ip(), sock1_path);
sock1->free();
sock2->free();
unlink(sock1_path);
unlink(sock2_path);
}
static void test_sendto(enum swSocketType sock_type) {
int port1 = 0, port2 = 0;
const char *ip = sock_type == SW_SOCK_UDP ? "127.0.0.1" : "::1";
auto sock1 = make_socket(sock_type, SW_FD_DGRAM_SERVER, 0);
sock1->bind(ip, &port1);
auto sock2 = make_socket(sock_type, SW_FD_DGRAM_SERVER, 0);
sock2->bind(ip, &port2);
ASSERT_GT(sock1->sendto(ip, port2, test_data, strlen(test_data)), 0);
char buf[1024] = {};
network::Address sa;
sa.type = sock_type;
ASSERT_GT(sock2->recvfrom(buf, sizeof(buf), 0, &sa), 0);
ASSERT_STREQ(test_data, buf);
ASSERT_EQ(sa.get_port(), port1);
ASSERT_STREQ(sa.get_ip(), ip);
sock1->free();
sock2->free();
}
TEST(socket, sendto_ipv4) {
test_sendto(SW_SOCK_UDP);
}
TEST(socket, sendto_ipv6) {
test_sendto(SW_SOCK_UDP6);
}
TEST(socket, recv) {
mutex m;
m.lock();
int port = swoole::test::get_random_port();
thread t1([&m, port]() {
auto svr = make_server_socket(SW_SOCK_TCP, TEST_HOST, port);
char buf[1024] = {};
svr->set_block();
m.unlock();
auto client_sock = svr->accept();
client_sock->recv(buf, sizeof(buf), 0);
ASSERT_STREQ(test_data, buf);
svr->free();
});
thread t2([&m, port]() {
m.lock();
auto cli = make_socket(SW_SOCK_TCP, SW_FD_STREAM_CLIENT, 0);
ASSERT_EQ(cli->connect(TEST_HOST, port), SW_OK);
std::this_thread::sleep_for(std::chrono::milliseconds(100));
cli->send(test_data, sizeof(test_data), 0);
cli->free();
});
t1.join();
t2.join();
}
TEST(socket, recvfrom_sync) {
mutex m;
m.lock();
int port = swoole::test::get_random_port();
thread t1([&m, port]() {
auto svr = make_server_socket(SW_SOCK_UDP, TEST_HOST, port);
network::Address addr;
char buf[1024] = {};
svr->set_nonblock();
m.unlock();
svr->recvfrom_sync(buf, sizeof(buf), 0, &addr);
ASSERT_STREQ(test_data, buf);
svr->free();
});
thread t2([&m, port]() {
m.lock();
auto cli = make_socket(SW_SOCK_UDP, SW_FD_STREAM_CLIENT, 0);
network::Address addr;
addr.assign(SW_SOCK_TCP, TEST_HOST, port);
ASSERT_EQ(cli->connect(addr), SW_OK);
std::this_thread::sleep_for(std::chrono::milliseconds(100));
cli->send(test_data, sizeof(test_data), 0);
cli->free();
});
t1.join();
t2.join();
}
TEST(socket, sendfile_sync) {
string file = test::get_root_path() + "/examples/test.jpg";
mutex m;
int port = swoole::test::get_random_port();
m.lock();
auto str = file_get_contents(file);
thread t1([&m, &str, port]() {
auto svr = make_server_socket(SW_SOCK_TCP, TEST_HOST, port);
m.unlock();
auto cli = svr->accept();
int len;
cli->recv_sync(&len, sizeof(len), MSG_WAITALL);
int _len = ntohl(len);
ASSERT_EQ(_len, str->get_length());
ASSERT_LT(_len, 1024 * 1024);
std::unique_ptr<char[]> data(new char[_len]);
cli->recv_sync(data.get(), _len, MSG_WAITALL);
ASSERT_STREQ(data.get(), str->value());
cli->free();
svr->free();
});
thread t2([&m, &file, &str, port]() {
m.lock();
auto cli = make_socket(SW_SOCK_TCP, SW_FD_STREAM_CLIENT, 0);
network::Address addr;
addr.assign(SW_SOCK_TCP, TEST_HOST, port);
ASSERT_EQ(cli->connect(addr), SW_OK);
int len = htonl(str->get_length());
cli->send(&len, sizeof(len), 0);
ASSERT_EQ(cli->sendfile_sync(file.c_str(), 0, 0, -1), SW_OK);
cli->free();
});
t1.join();
t2.join();
}
TEST(socket, peek) {
char sock1_path[] = "/tmp/udp_unix1.sock";
char sock2_path[] = "/tmp/udp_unix2.sock";
unlink(sock1_path);
unlink(sock2_path);
auto sock1 = make_socket(SW_SOCK_UNIX_DGRAM, SW_FD_DGRAM_SERVER, 0);
sock1->bind(sock1_path, nullptr);
auto sock2 = make_socket(SW_SOCK_UNIX_DGRAM, SW_FD_DGRAM_SERVER, 0);
sock2->bind(sock2_path, nullptr);
ASSERT_GT(sock1->sendto(sock2_path, 0, test_data, strlen(test_data)), 0);
char buf[1024] = {};
ASSERT_GT(sock2->peek(buf, sizeof(buf), 0), 0);
ASSERT_STREQ(test_data, buf);
sw_memset_zero(buf, sizeof(buf));
ASSERT_GT(sock2->recv(buf, sizeof(buf), 0), 0);
ASSERT_STREQ(test_data, buf);
sock1->free();
sock2->free();
unlink(sock1_path);
unlink(sock2_path);
}
TEST(socket, sendto_sync) {
char sock1_path[] = "/tmp/udp_unix1.sock";
unlink(sock1_path);
auto sock1 = make_socket(SW_SOCK_UNIX_DGRAM, SW_FD_DGRAM_SERVER, 0);
sock1->bind(sock1_path, nullptr);
sock1->info.assign(SW_SOCK_UNIX_DGRAM, sock1_path, 0);
char sock2_path[] = "/tmp/udp_unix2.sock";
unlink(sock2_path);
auto sock2 = make_socket(SW_SOCK_UNIX_DGRAM, SW_FD_DGRAM_SERVER, 0);
sock2->bind(sock2_path, nullptr);
sock2->info.assign(SW_SOCK_UNIX_DGRAM, sock2_path, 0);
char sendbuf[65536] = {};
swoole_random_string(sendbuf, sizeof(sendbuf) - 1);
thread t1([sock2, sendbuf]() {
std::this_thread::sleep_for(std::chrono::milliseconds(100));
char recvbuf[65536] = {};
while (1) {
auto retval = sock2->recv(recvbuf, sizeof(recvbuf) - 1, 0);
recvbuf[retval] = 0;
if (retval == 3) {
ASSERT_STREQ(recvbuf, "end");
break;
} else {
ASSERT_STREQ(sendbuf, recvbuf);
}
}
});
for (int i = 0; i < 10; i++) {
ASSERT_GT(sock1->sendto_sync(sock2->info, sendbuf, strlen(sendbuf)), 0);
}
ASSERT_GT(sock1->sendto_sync(sock2->info, "end", 3), 0);
t1.join();
sock1->free();
sock2->free();
unlink(sock1_path);
unlink(sock2_path);
}
TEST(socket, clean) {
char sock1_path[] = "/tmp/udp_unix1.sock";
unlink(sock1_path);
auto sock1 = make_socket(SW_SOCK_UNIX_DGRAM, SW_FD_DGRAM_SERVER, 0);
sock1->bind(sock1_path, nullptr);
sock1->info.assign(SW_SOCK_UNIX_DGRAM, sock1_path, 0);
char sock2_path[] = "/tmp/udp_unix2.sock";
unlink(sock2_path);
auto sock2 = make_socket(SW_SOCK_UNIX_DGRAM, SW_FD_DGRAM_SERVER, 0);
sock2->bind(sock2_path, nullptr);
sock2->info.assign(SW_SOCK_UNIX_DGRAM, sock2_path, 0);
char sendbuf[65536] = {};
swoole_random_string(sendbuf, sizeof(sendbuf) - 1);
for (int i = 0; i < 3; i++) {
ASSERT_GT(sock1->sendto_sync(sock2->info, sendbuf, strlen(sendbuf)), 0);
}
sock2->clean();
char recvbuf[1024];
auto retval = sock2->peek(recvbuf, sizeof(recvbuf), MSG_DONTWAIT);
ASSERT_EQ(retval, -1);
sock1->free();
sock2->free();
unlink(sock1_path);
unlink(sock2_path);
}
TEST(socket, check_liveness) {
mutex m;
int svr_port = swoole::test::get_random_port();
m.lock();
thread t1([&m, svr_port]() {
auto svr = make_server_socket(SW_SOCK_TCP, TEST_HOST, svr_port);
m.unlock();
auto cli = svr->accept();
ASSERT_TRUE(cli);
char buf[1024] = {};
cli->recv(buf, sizeof(buf), 0);
ASSERT_STREQ(test_data, buf);
ssize_t n = cli->recv(buf, sizeof(buf), 0);
buf[n] = 0;
ASSERT_STREQ("close", buf);
cli->shutdown(SHUT_RDWR);
cli->free();
svr->free();
});
thread t2([&m, svr_port]() {
m.lock();
auto cli = make_socket(SW_SOCK_TCP, SW_FD_STREAM_CLIENT, 0);
ASSERT_EQ(cli->connect(TEST_HOST, svr_port), SW_OK);
cli->send(test_data, sizeof(test_data), 0);
std::this_thread::sleep_for(std::chrono::milliseconds(1));
ASSERT_TRUE(cli->check_liveness());
cli->send(SW_STRL("close"), 0);
std::this_thread::sleep_for(std::chrono::milliseconds(5));
ASSERT_FALSE(cli->check_liveness());
cli->free();
});
t1.join();
t2.join();
}
#define CRLF "\r\n"
TEST(socket, sync) {
auto sock = make_socket(SW_SOCK_TCP, SW_FD_STREAM, 0);
swoole::network::Address addr;
ASSERT_TRUE(addr.assign("tcp://httpbin.org:80"));
ASSERT_EQ(sock->connect(addr), 0);
const char *req = "GET / HTTP/1.1" CRLF \
"Host: httpbin.org" CRLF \
"User-Agent: curl/7.81.0" CRLF \
"Accept: */*" CRLF \
"Connection: close" CRLF \
CRLF CRLF;
ssize_t n = strlen(req);
ASSERT_EQ(sock->write_sync(req, n), n);
string resp;
SW_LOOP {
char buf[1024];
n = sock->read_sync(buf, sizeof(buf));
if (n == 0) {
break;
}
ASSERT_GT(n, 0);
resp.append(buf, n);
}
ASSERT_GT(resp.length(), 4096);
sock->free();
}
TEST(socket, ipv6_addr) {
auto sock = make_socket(SW_SOCK_TCP6, SW_FD_STREAM, 0);
swoole::network::Address addr;
ASSERT_TRUE(addr.assign("tcp://[::1]:12345"));
ASSERT_EQ(sock->connect(addr), SW_ERR);
ASSERT_EQ(errno, ECONNREFUSED);
sock->free();
}