-
Notifications
You must be signed in to change notification settings - Fork 79
/
study_runtime.cc
227 lines (197 loc) · 5.64 KB
/
study_runtime.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
#include "study_runtime.h"
#include "study_coroutine_socket.h"
using study::coroutine::Socket;
ZEND_BEGIN_ARG_INFO_EX(arginfo_study_runtime_void, 0, 0, 0)
ZEND_END_ARG_INFO()
extern PHP_METHOD(study_coroutine_util, sleep);
static void hook_func(const char *name, size_t name_len, zif_handler handler);
static int socket_close(php_stream *stream, int close_handle);
static int socket_set_option(php_stream *stream, int option, int value, void *ptrparam);
struct php_study_netstream_data_t
{
php_netstream_data_t stream;
Socket *socket;
};
static php_stream_ops socket_ops
{
NULL,
NULL,
socket_close,
NULL,
"tcp_socket/coroutine",
NULL, /* seek */
NULL,
NULL,
socket_set_option,
};
enum
{
STREAM_XPORT_OP_BIND,
STREAM_XPORT_OP_CONNECT,
STREAM_XPORT_OP_LISTEN,
STREAM_XPORT_OP_ACCEPT,
STREAM_XPORT_OP_CONNECT_ASYNC,
STREAM_XPORT_OP_GET_NAME,
STREAM_XPORT_OP_GET_PEER_NAME,
STREAM_XPORT_OP_RECV,
STREAM_XPORT_OP_SEND,
STREAM_XPORT_OP_SHUTDOWN,
};
php_stream *socket_create(const char *proto, size_t protolen,
const char *resourcename, size_t resourcenamelen,
const char *persistent_id, int options, int flags,
struct timeval *timeout,
php_stream_context *context STREAMS_DC
)
{
php_stream *stream;
php_study_netstream_data_t *abstract;
Socket *sock;
sock = new Socket(AF_INET, SOCK_STREAM, 0);
abstract = (php_study_netstream_data_t*) ecalloc(1, sizeof(*abstract));
abstract->socket = sock;
abstract->stream.socket = sock->get_fd();
if (timeout)
{
abstract->stream.timeout = *timeout;
}
else
{
abstract->stream.timeout.tv_sec = -1;
}
persistent_id = nullptr;
stream = php_stream_alloc_rel(&socket_ops, abstract, persistent_id, "r+");
if (stream == NULL)
{
delete sock;
}
return stream;
}
static int socket_close(php_stream *stream, int close_handle)
{
php_study_netstream_data_t *abstract = (php_study_netstream_data_t *) stream->abstract;
Socket *sock = (Socket*) abstract->socket;
sock->close();
delete sock;
efree(abstract);
return 0;
}
/**
* copy from php src file: xp_socket.c
*/
static inline char *parse_ip_address_ex(const char *str, size_t str_len, int *portno, int get_err, zend_string **err)
{
char *colon;
char *host = NULL;
#ifdef HAVE_IPV6
char *p;
if (*(str) == '[' && str_len > 1)
{
/* IPV6 notation to specify raw address with port (i.e. [fe80::1]:80) */
p = (char *)memchr(str + 1, ']', str_len - 2);
if (!p || *(p + 1) != ':')
{
if (get_err)
{
*err = strpprintf(0, "Failed to parse IPv6 address \"%s\"", str);
}
return NULL;
}
*portno = atoi(p + 2);
return estrndup(str + 1, p - str - 1);
}
#endif
if (str_len)
{
colon = (char *)memchr(str, ':', str_len - 1);
}
else
{
colon = NULL;
}
if (colon)
{
*portno = atoi(colon + 1);
host = estrndup(str, colon - str);
}
else
{
if (get_err)
{
*err = strpprintf(0, "Failed to parse address \"%s\"", str);
}
return NULL;
}
return host;
}
/**
* copy from php src file: xp_socket.c
*/
static inline char *parse_ip_address(php_stream_xport_param *xparam, int *portno)
{
return parse_ip_address_ex(xparam->inputs.name, xparam->inputs.namelen, portno, xparam->want_errortext, &xparam->outputs.error_text);
}
static int php_study_tcp_sockop_bind(php_stream *stream, php_study_netstream_data_t *abstract, php_stream_xport_param *xparam)
{
char *host = NULL;
int portno;
host = parse_ip_address(xparam, &portno);
if (host == NULL)
{
return -1;
}
int ret = abstract->socket->bind(ST_SOCK_TCP, host, portno);
if (host)
{
efree(host);
}
return ret;
}
static int socket_set_option(php_stream *stream, int option, int value, void *ptrparam)
{
php_study_netstream_data_t *abstract = (php_study_netstream_data_t *) stream->abstract;
php_stream_xport_param *xparam;
switch(option)
{
case PHP_STREAM_OPTION_XPORT_API:
xparam = (php_stream_xport_param *)ptrparam;
switch(xparam->op)
{
case STREAM_XPORT_OP_BIND:
xparam->outputs.returncode = php_study_tcp_sockop_bind(stream, abstract, xparam);
return PHP_STREAM_OPTION_RETURN_OK;
case STREAM_XPORT_OP_LISTEN:
xparam->outputs.returncode = abstract->socket->listen(xparam->inputs.backlog);
return PHP_STREAM_OPTION_RETURN_OK;
default:
/* fall through */
;
}
}
return PHP_STREAM_OPTION_RETURN_OK;
}
static PHP_METHOD(study_runtime, enableCoroutine)
{
hook_func(ZEND_STRL("sleep"), zim_study_coroutine_util_sleep);
php_stream_xport_register("tcp", socket_create);
}
static const zend_function_entry study_runtime_methods[] =
{
PHP_ME(study_runtime, enableCoroutine, arginfo_study_runtime_void, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC)
PHP_FE_END
};
static void hook_func(const char *name, size_t name_len, zif_handler new_handler)
{
zend_function *ori_f = (zend_function *) zend_hash_str_find_ptr(EG(function_table), name, name_len);
ori_f->internal_function.handler = new_handler;
}
/**
* Define zend class entry
*/
zend_class_entry study_runtime_ce;
zend_class_entry *study_runtime_ce_ptr;
void study_runtime_init()
{
INIT_NS_CLASS_ENTRY(study_runtime_ce, "Study", "Runtime", study_runtime_methods);
study_runtime_ce_ptr = zend_register_internal_class(&study_runtime_ce TSRMLS_CC); // Registered in the Zend Engine
}