forked from swoole/swoole-src
-
Notifications
You must be signed in to change notification settings - Fork 0
/
php_swoole_cxx.h
329 lines (292 loc) · 7.51 KB
/
php_swoole_cxx.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
/*
+----------------------------------------------------------------------+
| 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]> |
+----------------------------------------------------------------------+
*/
#pragma once
#include "php_swoole.h"
#include "swoole_cxx.h"
#include "swoole_coroutine.h"
#include <string>
#define SW_SET_CLASS_CREATE_WITH_ITS_OWN_HANDLERS(module) \
module##_ce->create_object = [](zend_class_entry *ce) { return sw_zend_create_object(ce, &module##_handlers); }
SW_API bool php_swoole_export_socket(zval *zobject, swoole::coroutine::Socket *_socket);
SW_API zend_object* php_swoole_dup_socket(int fd, enum swSocket_type type);
SW_API void php_swoole_init_socket_object(zval *zobject, swoole::coroutine::Socket *socket);
SW_API swoole::coroutine::Socket* php_swoole_get_socket(zval *zobject);
#ifdef SW_USE_OPENSSL
SW_API bool php_swoole_socket_set_ssl(swoole::coroutine::Socket *sock, zval *zset);
#endif
SW_API bool php_swoole_socket_set_protocol(swoole::coroutine::Socket *sock, zval *zset);
SW_API bool php_swoole_client_set(swoole::coroutine::Socket *cli, zval *zset);
php_stream *php_swoole_create_stream_from_socket(php_socket_t _fd, int domain, int type, int protocol STREAMS_DC);
namespace zend {
//-----------------------------------namespace begin--------------------------------------------
class string
{
public:
string()
{
str = nullptr;
}
string(zval *v)
{
str = zval_get_string(v);
}
string(zend_string *&v)
{
str = zend_string_copy(v);
}
string(zend_string *&&v)
{
str = v;
}
void operator =(zval* v)
{
if (str)
{
zend_string_release(str);
}
str = zval_get_string(v);
}
inline char* val()
{
return ZSTR_VAL(str);
}
inline size_t len()
{
return ZSTR_LEN(str);
}
zend_string* get()
{
return str;
}
std::string to_std_string()
{
return std::string(val(), len());
}
char* dup()
{
return sw_likely(len() > 0) ? sw_strndup(val(), len()) : nullptr;
}
char* edup()
{
return sw_likely(len() > 0) ? estrndup(val(), len()) : nullptr;
}
~string()
{
if (str)
{
zend_string_release(str);
}
}
private:
zend_string *str;
};
class string_ptr
{
public:
string_ptr(zend_string *str) :
str(str)
{
}
string_ptr(string_ptr &&o)
{
str = o.str;
o.str = nullptr;
}
~string_ptr()
{
if (str)
{
zend_string_release(str);
}
}
private:
zend_string *str;
};
class key_value
{
public:
zend_ulong index;
zend_string *key;
zval zvalue;
key_value(zend_ulong _index, zend_string *_key, zval *_zvalue)
{
index = _index;
key = _key ? zend_string_copy(_key) : nullptr;
ZVAL_DEREF(_zvalue);
zvalue = *_zvalue;
Z_TRY_ADDREF(zvalue);
}
inline void add_to(zval *zarray)
{
HashTable *ht = Z_ARRVAL_P(zarray);
zval *dest_elem = !key ? zend_hash_index_update(ht, index, &zvalue) : zend_hash_update(ht, key, &zvalue);
Z_TRY_ADDREF_P(dest_elem);
}
~key_value()
{
if (key)
{
zend_string_release(key);
}
zval_ptr_dtor(&zvalue);
}
};
class ArrayIterator
{
public:
ArrayIterator(Bucket *p)
{
_ptr = p;
_key = _ptr->key;
_val = &_ptr->val;
_index = _ptr->h;
pe = p;
}
ArrayIterator(Bucket *p, Bucket *_pe)
{
_ptr = p;
_key = _ptr->key;
_val = &_ptr->val;
_index = _ptr->h;
pe = _pe;
skipUndefBucket();
}
void operator ++(int i)
{
++_ptr;
skipUndefBucket();
}
bool operator !=(ArrayIterator b)
{
return b.ptr() != _ptr;
}
std::string key()
{
return std::string(_key->val, _key->len);
}
zend_ulong index()
{
return _index;
}
zval* value()
{
return _val;
}
Bucket *ptr()
{
return _ptr;
}
private:
void skipUndefBucket()
{
while (_ptr != pe)
{
_val = &_ptr->val;
if (_val && Z_TYPE_P(_val) == IS_INDIRECT)
{
_val = Z_INDIRECT_P(_val);
}
if (UNEXPECTED(Z_TYPE_P(_val) == IS_UNDEF))
{
++_ptr;
continue;
}
if (_ptr->key)
{
_key = _ptr->key;
_index = 0;
}
else
{
_index = _ptr->h;
_key = NULL;
}
break;
}
}
zval *_val;
zend_string *_key;
Bucket *_ptr;
Bucket *pe;
zend_ulong _index;
};
class Array
{
public:
zval *arr;
Array(zval *_arr)
{
assert(Z_TYPE_P(_arr) == IS_ARRAY);
arr = _arr;
}
inline size_t count()
{
return zend_hash_num_elements(Z_ARRVAL_P(arr));
}
inline bool set(zend_ulong index, zval *value)
{
return add_index_zval(arr, index, value) == SUCCESS;
}
inline bool append(zval *value)
{
return add_next_index_zval(arr, value) == SUCCESS;
}
inline bool set(zend_ulong index, zend_resource *res)
{
zval tmp;
ZVAL_RES(&tmp, res);
return set(index, &tmp);
}
ArrayIterator begin()
{
return ArrayIterator(Z_ARRVAL_P(arr)->arData, Z_ARRVAL_P(arr)->arData + Z_ARRVAL_P(arr)->nNumUsed);
}
ArrayIterator end()
{
return ArrayIterator(Z_ARRVAL_P(arr)->arData + Z_ARRVAL_P(arr)->nNumUsed);
}
};
namespace function
{
/* must use this API to call event callbacks to ensure that exceptions are handled correctly */
inline bool call(zend_fcall_info_cache *fci_cache, uint32_t argc, zval *argv, zval *retval, const bool enable_coroutine)
{
bool success;
if (enable_coroutine)
{
if (retval)
{
/* the coroutine has no return value */
ZVAL_NULL(retval);
}
success = swoole::PHPCoroutine::create(fci_cache, argc, argv) >= 0;
}
else
{
success = sw_zend_call_function_ex(NULL, fci_cache, argc, argv, retval) == SUCCESS;
}
/* we have no chance to return to ZendVM to check the exception */
if (UNEXPECTED(EG(exception)))
{
zend_exception_error(EG(exception), E_ERROR);
}
return success;
}
}
bool include(std::string file);
bool eval(std::string code, std::string filename = "");
//-----------------------------------namespace end--------------------------------------------
}