-
Notifications
You must be signed in to change notification settings - Fork 3.2k
/
Copy pathswoole_coroutine_socket.h
728 lines (619 loc) · 21.7 KB
/
swoole_coroutine_socket.h
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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
/*
+----------------------------------------------------------------------+
| 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 |
| license@swoole.com so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Author: Tianfeng Han <rango@swoole.com> |
| Twosee <twose@qq.com> |
+----------------------------------------------------------------------+
*/
#pragma once
#include "swoole.h"
#include "swoole_api.h"
#include "swoole_socket.h"
#include "swoole_coroutine.h"
#include "swoole_protocol.h"
#include "swoole_proxy.h"
#include <vector>
namespace swoole {
namespace coroutine {
//-------------------------------------------------------------------------------
/**
* @return true: continue to wait for events
* @return false: stop event waiting and resume coroutine
*/
using EventBarrier = std::function<bool()>;
class Socket {
public:
int errCode = 0;
const char *errMsg = "";
std::string errString;
bool open_length_check = false;
bool open_eof_check = false;
bool http2 = false;
Protocol protocol = {};
Socks5Proxy *socks5_proxy = nullptr;
HttpProxy *http_proxy = nullptr;
enum TimeoutType {
TIMEOUT_DNS = 1 << 0,
TIMEOUT_CONNECT = 1 << 1,
TIMEOUT_READ = 1 << 2,
TIMEOUT_WRITE = 1 << 3,
TIMEOUT_RDWR = TIMEOUT_READ | TIMEOUT_WRITE,
TIMEOUT_ALL = TIMEOUT_DNS | TIMEOUT_CONNECT | TIMEOUT_RDWR,
};
static enum TimeoutType timeout_type_list[4];
Socket(int domain, int type, int protocol);
Socket(int _fd, int _domain, int _type, int _protocol);
Socket(SocketType type = SW_SOCK_TCP);
Socket(int _fd, SocketType _type);
~Socket();
/**
* If SSL is enabled, an SSL handshake will automatically take place during the connect() method.
* When connect() returns true, it indicates that the TCP connection has been successfully
* established and the SSL handshake has also succeeded.
*/
bool connect(std::string host, int port = 0, int flags = 0);
bool connect(const struct sockaddr *addr, socklen_t addrlen);
bool shutdown(int how = SHUT_RDWR);
bool cancel(const EventType event);
bool close();
bool is_connected() {
return connected && !is_closed();
}
bool is_closed() {
return sock_fd == SW_BAD_SOCKET;
}
bool is_port_required() {
return type <= SW_SOCK_UDP6;
}
bool check_liveness();
ssize_t peek(void *__buf, size_t __n);
ssize_t recv(void *__buf, size_t __n);
ssize_t send(const void *__buf, size_t __n);
ssize_t send(const std::string &buf) {
return send(buf.c_str(), buf.length());
}
ssize_t read(void *__buf, size_t __n);
ssize_t write(const void *__buf, size_t __n);
ssize_t readv(network::IOVector *io_vector);
ssize_t readv_all(network::IOVector *io_vector);
ssize_t writev(network::IOVector *io_vector);
ssize_t writev_all(network::IOVector *io_vector);
ssize_t recvmsg(struct msghdr *msg, int flags);
ssize_t sendmsg(const struct msghdr *msg, int flags);
ssize_t recv_all(void *__buf, size_t __n);
ssize_t send_all(const void *__buf, size_t __n);
ssize_t recv_packet(double timeout = 0);
ssize_t recv_line(void *__buf, size_t maxlen);
ssize_t recv_with_buffer(void *__buf, size_t __n);
char *pop_packet() {
if (read_buffer->offset == 0) {
return nullptr;
} else {
return read_buffer->pop(buffer_init_size);
}
}
bool poll(EventType type, double timeout = 0);
/**
* If the server has SSL enabled, you must explicitly call `ssl_handshake()`,
* as it will not be automatically executed within the `accept()` function.
* This behavior is inconsistent with `connect()`, which internally executes `ssl_handshake()` automatically,
* thus not requiring an explicit call at the application level.
* The reason for this design is that `ssl_handshake()` can typically be performed concurrently within a separate
* client coroutine. If `ssl_handshake()` were to be automatically executed inside the `accept()` function,
* it would block the server's listening coroutine,
* causing the `ssl_handshake()` processes to execute sequentially rather than in parallel.
*/
Socket *accept(double timeout = 0);
bool bind(std::string address, int port = 0);
bool bind(const struct sockaddr *sa, socklen_t len);
bool listen(int backlog = 0);
bool sendfile(const char *filename, off_t offset, size_t length);
ssize_t sendto(const std::string &host, int port, const void *__buf, size_t __n);
ssize_t recvfrom(void *__buf, size_t __n);
ssize_t recvfrom(void *__buf, size_t __n, struct sockaddr *_addr, socklen_t *_socklen);
#ifdef SW_USE_OPENSSL
/**
* Operation sequence:
* 1. enable_ssl_encrypt()
* 2. Set SSL parameters, such as certificate file, key file
* 3. ssl_handshake(), to be executed after connect or accept
*/
bool enable_ssl_encrypt() {
if (ssl_context.get()) {
return false;
}
ssl_context = std::make_shared<SSLContext>();
return true;
}
bool ssl_is_enable() {
return get_ssl_context() != nullptr;
}
SSLContext *get_ssl_context() {
return ssl_context.get();
}
bool ssl_handshake();
bool ssl_verify(bool allow_self_signed);
std::string ssl_get_peer_cert();
bool set_ssl_key_file(const std::string &file) {
return ssl_context->set_key_file(file);
}
bool set_ssl_cert_file(const std::string &file) {
return ssl_context->set_cert_file(file);
}
void set_ssl_cafile(const std::string &file) {
ssl_context->cafile = file;
}
void set_ssl_capath(const std::string &path) {
ssl_context->capath = path;
}
void set_ssl_passphrase(const std::string &str) {
ssl_context->passphrase = str;
}
#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
void set_tls_host_name(const std::string &str) {
ssl_context->tls_host_name = str;
// if user set empty ssl_host_name, disable it, otherwise the underlying may set it automatically
ssl_context->disable_tls_host_name = ssl_context->tls_host_name.empty();
}
#endif
void set_ssl_dhparam(const std::string &file) {
ssl_context->dhparam = file;
}
void set_ssl_ecdh_curve(const std::string &str) {
ssl_context->ecdh_curve = str;
}
void set_ssl_protocols(long protocols) {
ssl_context->protocols = protocols;
}
void set_ssl_disable_compress(bool value) {
ssl_context->disable_compress = value;
}
void set_ssl_verify_peer(bool value) {
ssl_context->verify_peer = value;
}
void set_ssl_allow_self_signed(bool value) {
ssl_context->allow_self_signed = value;
}
void set_ssl_verify_depth(uint8_t value) {
ssl_context->verify_depth = value;
}
void set_ssl_ciphers(const std::string &str) {
ssl_context->ciphers = str;
}
#ifdef OPENSSL_IS_BORINGSSL
void set_ssl_grease(uint8_t value) {
ssl_context->grease = value;
}
#endif
const std::string &get_ssl_cert_file() {
return ssl_context->cert_file;
}
const std::string &get_ssl_key_file() {
return ssl_context->key_file;
}
#endif
static inline void init_reactor(Reactor *reactor) {
reactor->set_handler(SW_FD_CO_SOCKET | SW_EVENT_READ, readable_event_callback);
reactor->set_handler(SW_FD_CO_SOCKET | SW_EVENT_WRITE, writable_event_callback);
reactor->set_handler(SW_FD_CO_SOCKET | SW_EVENT_ERROR, error_event_callback);
}
SocketType get_type() {
return type;
}
FdType get_fd_type() {
return socket->fd_type;
}
int get_sock_domain() {
return sock_domain;
}
int get_sock_type() {
return sock_type;
}
int get_sock_protocol() {
return sock_protocol;
}
int get_fd() {
return sock_fd;
}
int get_bind_port() {
return bind_port;
}
network::Socket *get_socket() {
return socket;
}
bool getsockname(network::Address *sa);
bool getpeername(network::Address *sa);
const char *get_ip() {
return socket->info.get_ip();
}
int get_port() {
return socket->info.get_port();
}
bool has_bound(const EventType event = SW_EVENT_RDWR) {
return get_bound_co(event) != nullptr;
}
Coroutine *get_bound_co(const EventType event) {
if (event & SW_EVENT_READ) {
if (read_co) {
return read_co;
}
}
if (event & SW_EVENT_WRITE) {
if (write_co) {
return write_co;
}
}
return nullptr;
}
long get_bound_cid(const EventType event = SW_EVENT_RDWR) {
Coroutine *co = get_bound_co(event);
return co ? co->get_cid() : 0;
}
const char *get_event_str(const EventType event) {
if (event == SW_EVENT_READ) {
return "reading";
} else if (event == SW_EVENT_WRITE) {
return "writing";
} else {
return read_co && write_co ? "reading or writing" : (read_co ? "reading" : "writing");
}
}
void check_bound_co(const EventType event) {
long cid = get_bound_cid(event);
if (sw_unlikely(cid)) {
swoole_fatal_error(SW_ERROR_CO_HAS_BEEN_BOUND,
"Socket#%d has already been bound to another coroutine#%ld, "
"%s of the same socket in coroutine#%ld at the same time is not allowed",
sock_fd,
cid,
get_event_str(event),
Coroutine::get_current_cid());
}
}
void set_err(int e) {
errCode = errno = e;
swoole_set_last_error(errCode);
errMsg = e ? swoole_strerror(e) : "";
}
void set_err(int e, const char *s) {
errCode = errno = e;
swoole_set_last_error(errCode);
errMsg = s;
}
void set_err(int e, std::string s) {
errCode = errno = e;
swoole_set_last_error(errCode);
errString = s;
errMsg = errString.c_str();
}
/* set connect read write timeout */
void set_timeout(double timeout, int type = TIMEOUT_ALL) {
if (timeout == 0) {
return;
}
if (type & TIMEOUT_DNS) {
dns_timeout = timeout;
}
if (type & TIMEOUT_CONNECT) {
connect_timeout = timeout;
}
if (type & TIMEOUT_READ) {
read_timeout = timeout;
}
if (type & TIMEOUT_WRITE) {
write_timeout = timeout;
}
}
void set_timeout(struct timeval *timeout, int type = TIMEOUT_ALL) {
set_timeout((double) timeout->tv_sec + ((double) timeout->tv_usec / 1000 / 1000), type);
}
double get_timeout(enum TimeoutType type = TIMEOUT_ALL) {
SW_ASSERT_1BYTE(type);
if (type == TIMEOUT_DNS) {
return dns_timeout;
} else if (type == TIMEOUT_CONNECT) {
return connect_timeout;
} else if (type == TIMEOUT_READ) {
return read_timeout;
} else if (type == TIMEOUT_WRITE) {
return write_timeout;
} else {
assert(0);
return -1;
}
}
bool set_option(int level, int optname, int optval) {
if (socket->set_option(level, optname, optval) < 0) {
swoole_sys_warning("setsockopt(%d, %d, %d, %d) failed", sock_fd, level, optname, optval);
return false;
}
return true;
}
String *get_read_buffer() {
if (sw_unlikely(!read_buffer)) {
read_buffer = make_string(SW_BUFFER_SIZE_BIG, buffer_allocator);
if (!read_buffer) {
throw std::bad_alloc();
}
}
return read_buffer;
}
String *get_write_buffer() {
if (sw_unlikely(!write_buffer)) {
write_buffer = make_string(SW_BUFFER_SIZE_BIG, buffer_allocator);
if (!write_buffer) {
throw std::bad_alloc();
}
}
return write_buffer;
}
void set_resolve_context(NameResolver::Context *ctx) {
resolve_context_ = ctx;
}
void set_dtor(const std::function<void(Socket *)> &dtor) {
dtor_ = dtor;
}
String *pop_read_buffer() {
if (sw_unlikely(!read_buffer)) {
return nullptr;
}
auto tmp = read_buffer;
read_buffer = nullptr;
return tmp;
}
String *pop_write_buffer() {
if (sw_unlikely(!write_buffer)) {
return nullptr;
}
auto tmp = write_buffer;
write_buffer = nullptr;
return tmp;
}
void set_zero_copy(bool enable) {
zero_copy = enable;
}
void set_buffer_allocator(const Allocator *allocator) {
buffer_allocator = allocator;
}
void set_buffer_init_size(size_t size) {
if (size == 0) {
return;
}
buffer_init_size = size;
}
int move_fd() {
sock_fd = SW_BAD_SOCKET;
return socket->move_fd();
}
network::Socket *move_socket() {
network::Socket *_socket = socket;
socket = nullptr;
return _socket;
}
#ifdef SW_USE_OPENSSL
bool ssl_is_available() {
return socket && ssl_handshaked;
}
SSL *get_ssl() {
return socket->ssl;
}
bool ssl_shutdown();
#endif
private:
SocketType type;
network::Socket *socket = nullptr;
int sock_domain = 0;
int sock_type = 0;
int sock_protocol = 0;
int sock_fd = -1;
Coroutine *read_co = nullptr;
Coroutine *write_co = nullptr;
#ifdef SW_USE_OPENSSL
EventType want_event = SW_EVENT_NULL;
#endif
std::string connect_host;
int connect_port = 0;
std::string bind_address;
int bind_port = 0;
int backlog = 0;
double dns_timeout = network::Socket::default_dns_timeout;
double connect_timeout = network::Socket::default_connect_timeout;
double read_timeout = network::Socket::default_read_timeout;
double write_timeout = network::Socket::default_write_timeout;
TimerNode *read_timer = nullptr;
TimerNode *write_timer = nullptr;
const Allocator *buffer_allocator = nullptr;
size_t buffer_init_size = SW_BUFFER_SIZE_BIG;
String *read_buffer = nullptr;
String *write_buffer = nullptr;
network::Address bind_address_info = {};
EventBarrier *recv_barrier = nullptr;
EventBarrier *send_barrier = nullptr;
#ifdef SW_USE_OPENSSL
bool ssl_is_server = false;
bool ssl_handshaked = false;
std::shared_ptr<SSLContext> ssl_context = nullptr;
std::string ssl_host_name;
bool ssl_context_create();
bool ssl_create(SSLContext *ssl_context);
#endif
bool connected = false;
bool shutdown_read = false;
bool shutdown_write = false;
bool zero_copy = false;
NameResolver::Context *resolve_context_ = nullptr;
std::function<void(Socket *)> dtor_;
Socket(network::Socket *sock, Socket *socket);
static void timer_callback(Timer *timer, TimerNode *tnode);
static int readable_event_callback(Reactor *reactor, Event *event);
static int writable_event_callback(Reactor *reactor, Event *event);
static int error_event_callback(Reactor *reactor, Event *event);
void init_sock_type(SocketType _type);
bool init_sock();
bool init_reactor_socket(int fd);
void check_return_value(ssize_t retval) {
if (retval >= 0) {
set_err(0);
} else if (errCode == 0) {
set_err(errno);
}
}
void init_options() {
if (type == SW_SOCK_TCP || type == SW_SOCK_TCP6) {
set_option(IPPROTO_TCP, TCP_NODELAY, 1);
}
protocol.package_length_type = 'N';
protocol.package_length_size = 4;
protocol.package_length_offset = 0;
protocol.package_body_offset = 0;
protocol.package_max_length = SW_INPUT_BUFFER_SIZE;
}
bool add_event(const EventType event);
bool wait_event(const EventType event, const void **__buf = nullptr, size_t __n = 0);
bool try_connect();
ssize_t recv_packet_with_length_protocol();
ssize_t recv_packet_with_eof_protocol();
bool is_available(const EventType event) {
if (event != SW_EVENT_NULL) {
check_bound_co(event);
}
if (sw_unlikely(is_closed())) {
set_err(EBADF);
return false;
}
if (sw_unlikely(socket->close_wait)) {
set_err(SW_ERROR_CO_SOCKET_CLOSE_WAIT);
return false;
}
return true;
}
bool socks5_handshake();
bool http_proxy_handshake();
class TimerController {
public:
TimerController(TimerNode **_timer_pp, double _timeout, Socket *_socket, TimerCallback _callback)
: timer_pp(_timer_pp), timeout(_timeout), socket_(_socket), callback(std::move(_callback)) {}
bool start() {
if (timeout != 0 && !*timer_pp) {
enabled = true;
if (timeout > 0) {
*timer_pp = swoole_timer_add(timeout, false, callback, socket_);
return *timer_pp != nullptr;
}
*timer_pp = (TimerNode *) -1;
}
return true;
}
~TimerController() {
if (enabled && *timer_pp) {
if (*timer_pp != (TimerNode *) -1) {
swoole_timer_del(*timer_pp);
}
*timer_pp = nullptr;
}
}
private:
bool enabled = false;
TimerNode **timer_pp;
double timeout;
Socket *socket_;
TimerCallback callback;
};
public:
class TimeoutSetter {
public:
TimeoutSetter(Socket *socket, double _timeout, const enum TimeoutType _type)
: socket_(socket), timeout(_timeout), type(_type) {
if (_timeout == 0) {
return;
}
for (uint8_t i = 0; i < SW_ARRAY_SIZE(timeout_type_list); i++) {
if (_type & timeout_type_list[i]) {
original_timeout[i] = socket->get_timeout(timeout_type_list[i]);
if (_timeout != original_timeout[i]) {
socket->set_timeout(_timeout, timeout_type_list[i]);
}
}
}
}
~TimeoutSetter() {
if (timeout == 0) {
return;
}
for (uint8_t i = 0; i < SW_ARRAY_SIZE(timeout_type_list); i++) {
if (type & timeout_type_list[i]) {
if (timeout != original_timeout[i]) {
socket_->set_timeout(original_timeout[i], timeout_type_list[i]);
}
}
}
}
protected:
Socket *socket_;
double timeout;
enum TimeoutType type;
double original_timeout[sizeof(timeout_type_list)] = {};
};
class TimeoutController : public TimeoutSetter {
public:
TimeoutController(Socket *_socket, double _timeout, const enum TimeoutType _type)
: TimeoutSetter(_socket, _timeout, _type) {}
bool has_timedout(const enum TimeoutType _type) {
SW_ASSERT_1BYTE(_type);
if (timeout > 0) {
if (sw_unlikely(startup_time == 0)) {
startup_time = microtime();
} else {
double used_time = microtime() - startup_time;
if (sw_unlikely(timeout - used_time < SW_TIMER_MIN_SEC)) {
socket_->set_err(ETIMEDOUT);
return true;
}
socket_->set_timeout(timeout - used_time, _type);
}
}
return false;
}
protected:
double startup_time = 0;
};
};
class ProtocolSwitch {
private:
bool ori_open_eof_check;
bool ori_open_length_check;
Protocol ori_protocol;
Socket *socket_;
public:
ProtocolSwitch(Socket *socket) {
ori_open_eof_check = socket->open_eof_check;
ori_open_length_check = socket->open_length_check;
ori_protocol = socket->protocol;
socket_ = socket;
}
~ProtocolSwitch() {
/* revert protocol settings */
socket_->open_eof_check = ori_open_eof_check;
socket_->open_length_check = ori_open_length_check;
socket_->protocol = ori_protocol;
}
};
std::vector<std::string> dns_lookup(const char *domain, int family = AF_INET, double timeout = 2.0);
std::vector<std::string> dns_lookup_impl_with_socket(const char *domain, int family, double timeout);
#ifdef SW_USE_CARES
std::vector<std::string> dns_lookup_impl_with_cares(const char *domain, int family, double timeout);
#endif
std::string get_ip_by_hosts(const std::string &domain);
//-------------------------------------------------------------------------------
} // namespace coroutine
} // namespace swoole
std::shared_ptr<swoole::coroutine::Socket> swoole_coroutine_get_socket_object(int sockfd);