-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
swoole_async_coro.cc
233 lines (209 loc) · 6.71 KB
/
swoole_async_coro.cc
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
/*
+----------------------------------------------------------------------+
| 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. |
+----------------------------------------------------------------------+
| Author: Tianfeng Han <[email protected]> |
+----------------------------------------------------------------------+
*/
#include "php_swoole_cxx.h"
#include "php_streams.h"
#include "php_network.h"
#include "ext/standard/file.h"
#include "ext/standard/basic_functions.h"
#include <string>
#include <vector>
#include <unordered_map>
using swoole::PHPCoroutine;
using swoole::Coroutine;
using swoole::coroutine::Socket;
using std::string;
using std::vector;
typedef struct
{
char address[16];
time_t update_time;
} dns_cache;
typedef struct
{
zval *callback;
pid_t pid;
int fd;
swString *buffer;
} process_stream;
static std::unordered_map<std::string, dns_cache*> request_cache_map;
void php_swoole_async_coro_minit(int module_number)
{
}
void php_swoole_async_coro_rshutdown()
{
for(auto i = request_cache_map.begin(); i != request_cache_map.end(); i++)
{
efree(i->second);
}
}
PHP_FUNCTION(swoole_async_set)
{
if (SwooleTG.reactor)
{
php_swoole_fatal_error(E_ERROR, "eventLoop has already been created. unable to change settings");
RETURN_FALSE;
}
zval *zset = NULL;
HashTable *vht;
zval *ztmp;
ZEND_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_ARRAY(zset)
ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE);
vht = Z_ARRVAL_P(zset);
if (php_swoole_array_get_value(vht, "enable_signalfd", ztmp))
{
SwooleG.enable_signalfd = zval_is_true(ztmp);
}
if (php_swoole_array_get_value(vht, "dns_cache_refresh_time", ztmp))
{
SwooleG.dns_cache_refresh_time = zval_get_double(ztmp);
}
if (php_swoole_array_get_value(vht, "socket_buffer_size", ztmp))
{
SwooleG.socket_buffer_size = zval_get_long(ztmp);
if (SwooleG.socket_buffer_size <= 0 || SwooleG.socket_buffer_size > INT_MAX)
{
SwooleG.socket_buffer_size = INT_MAX;
}
}
if (php_swoole_array_get_value(vht, "socket_send_timeout", ztmp))
{
SwooleG.socket_send_timeout = zval_get_double(ztmp);
if (SwooleG.socket_send_timeout <= 0 || SwooleG.socket_send_timeout > INT_MAX)
{
SwooleG.socket_send_timeout = INT_MAX;
}
}
if (php_swoole_array_get_value(vht, "log_level", ztmp))
{
zend_long level = zval_get_long(ztmp);
SwooleG.log_level = (uint32_t) (level < 0 ? UINT32_MAX : level);
}
if (php_swoole_array_get_value(vht, "thread_num", ztmp) || php_swoole_array_get_value(vht, "min_thread_num", ztmp))
{
zend_long v = zval_get_long(ztmp);
v = SW_MAX(1, SW_MIN(v, UINT32_MAX));
SwooleG.aio_core_worker_num = v;
}
if (php_swoole_array_get_value(vht, "max_thread_num", ztmp))
{
zend_long v = zval_get_long(ztmp);
v = SW_MAX(1, SW_MIN(v, UINT32_MAX));
SwooleG.aio_worker_num= v;
}
if (php_swoole_array_get_value(vht, "display_errors", ztmp))
{
SWOOLE_G(display_errors) = zval_is_true(ztmp);
}
if (php_swoole_array_get_value(vht, "socket_dontwait", ztmp))
{
SwooleG.socket_dontwait = zval_is_true(ztmp);
}
if (php_swoole_array_get_value(vht, "dns_lookup_random", ztmp))
{
SwooleG.dns_lookup_random = zval_is_true(ztmp);
}
if (php_swoole_array_get_value(vht, "dns_server", ztmp))
{
if (SwooleG.dns_server_v4)
{
sw_free(SwooleG.dns_server_v4);
}
SwooleG.dns_server_v4 = zend::string(ztmp).dup();
}
if (php_swoole_array_get_value(vht, "use_async_resolver", ztmp))
{
SwooleG.use_async_resolver = zval_is_true(ztmp);
}
if (php_swoole_array_get_value(vht, "enable_coroutine", ztmp))
{
SwooleG.enable_coroutine = zval_is_true(ztmp);
}
#if defined(HAVE_REUSEPORT) && defined(HAVE_EPOLL)
//reuse port
if (php_swoole_array_get_value(vht, "enable_reuse_port", ztmp))
{
if (zval_is_true(ztmp) && swoole_version_compare(SwooleG.uname.release, "3.9.0") >= 0)
{
SwooleG.reuse_port = 1;
}
else
{
SwooleG.reuse_port = 0;
}
}
#endif
}
PHP_FUNCTION(swoole_async_dns_lookup_coro)
{
Coroutine::get_current_safe();
zval *domain;
double timeout = Socket::default_connect_timeout;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "z|d", &domain, &timeout) == FAILURE)
{
RETURN_FALSE;
}
if (Z_TYPE_P(domain) != IS_STRING)
{
php_swoole_fatal_error(E_WARNING, "invalid domain name");
RETURN_FALSE;
}
if (Z_STRLEN_P(domain) == 0)
{
php_swoole_fatal_error(E_WARNING, "domain name empty");
RETURN_FALSE;
}
//find cache
std::string key(Z_STRVAL_P(domain), Z_STRLEN_P(domain));
dns_cache *cache;
if (request_cache_map.find(key) != request_cache_map.end())
{
cache = request_cache_map[key];
if (cache->update_time > swTimer_get_absolute_msec())
{
RETURN_STRING(cache->address);
}
}
php_swoole_check_reactor();
vector<string> result = swoole::coroutine::dns_lookup(Z_STRVAL_P(domain), timeout);
if (result.empty())
{
SwooleG.error = SW_ERROR_DNSLOOKUP_RESOLVE_FAILED;
RETURN_FALSE;
}
if (SwooleG.dns_lookup_random)
{
RETVAL_STRING(result[rand() % result.size()].c_str());
}
else
{
RETVAL_STRING(result[0].c_str());
}
auto cache_iterator = request_cache_map.find(key);
if (cache_iterator == request_cache_map.end())
{
cache = (dns_cache *) emalloc(sizeof(dns_cache));
bzero(cache, sizeof(dns_cache));
request_cache_map[key] = cache;
}
else
{
cache = cache_iterator->second;
}
memcpy(cache->address, Z_STRVAL_P(return_value), Z_STRLEN_P(return_value));
cache->address[Z_STRLEN_P(return_value)] = '\0';
cache->update_time = swTimer_get_absolute_msec() + (int64_t) (SwooleG.dns_cache_refresh_time * 1000);
}