-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkcp.c
429 lines (353 loc) · 10.2 KB
/
kcp.c
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
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "php.h"
#include "php_ini.h"
#include "ext/standard/info.h"
#include "php_kcp.h"
#include "ikcp.h"
static int le_kcp;
typedef struct {
ikcpcb *kcp;
zval output_callback;
} php_kcp_t;
ZEND_BEGIN_ARG_INFO_EX(arginfo_kcp_create, 0, 0, 2)
ZEND_ARG_INFO(0, conv)
ZEND_ARG_INFO(0, user)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_kcp_set_output_callback, 0, 0, 2)
ZEND_ARG_INFO(0, kcp)
ZEND_ARG_INFO(0, callback)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_kcp_release, 0, 0, 1)
ZEND_ARG_INFO(0, kcp)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_kcp_update, 0, 0, 2)
ZEND_ARG_INFO(0, kcp)
ZEND_ARG_INFO(0, current)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_kcp_input, 0, 0, 2)
ZEND_ARG_INFO(0, kcp)
ZEND_ARG_INFO(0, data)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_kcp_send, 0, 0, 2)
ZEND_ARG_INFO(0, kcp)
ZEND_ARG_INFO(0, data)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_kcp_flush, 0, 0, 1)
ZEND_ARG_INFO(0, kcp)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_kcp_recv, 0, 0, 2)
ZEND_ARG_INFO(0, kcp)
ZEND_ARG_INFO(0, maxsize)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_kcp_nodelay, 0, 0, 5)
ZEND_ARG_INFO(0, kcp)
ZEND_ARG_INFO(0, nodelay)
ZEND_ARG_INFO(0, interval)
ZEND_ARG_INFO(0, resend)
ZEND_ARG_INFO(0, nc)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_kcp_wndsize, 0, 0, 3)
ZEND_ARG_INFO(0, kcp)
ZEND_ARG_INFO(0, sndwnd)
ZEND_ARG_INFO(0, rcvwnd)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_kcp_setmtu, 0, 0, 2)
ZEND_ARG_INFO(0, kcp)
ZEND_ARG_INFO(0, mtu)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_kcp_set_rx_minrto, 0, 0, 2)
ZEND_ARG_INFO(0, kcp)
ZEND_ARG_INFO(0, rx_minrto)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_kcp_waitsnd, 0, 0, 1)
ZEND_ARG_INFO(0, kcp)
ZEND_END_ARG_INFO()
PHP_FUNCTION(kcp_create)
{
long conv;
zval *zuser;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "lz", &conv, &zuser) == FAILURE) {
RETURN_FALSE;
}
php_kcp_t *kcp_res;
kcp_res = emalloc(sizeof(php_kcp_t));
kcp_res->kcp = ikcp_create(conv, Z_RES_P(zuser));
zend_resource *res = zend_register_resource(kcp_res, le_kcp);
RETURN_RES(res);
}
// udp_output function to be called by KCP
int udp_output(const char *buf, int len, ikcpcb *kcp, void *user)
{
// Retrieve the custom data structure
php_kcp_t *kcp_res = (php_kcp_t *)user;
if (kcp_res == NULL || &kcp_res->output_callback == NULL) {
return -1; // Error if callback is not set
}
// Prepare arguments to call the PHP callback
zval params[2];
ZVAL_STRINGL(¶ms[0], buf, len); // Convert the buffer to a PHP string
ZVAL_LONG(¶ms[1], len); // Length as a second argument
// Call the PHP callback
zval retval;
if (call_user_function(EG(function_table), NULL, &kcp_res->output_callback, &retval, 2, params) == FAILURE) {
php_error_docref(NULL, E_WARNING, "Failed to call output callback");
return -1;
}
// Clean up
zval_ptr_dtor(¶ms[0]);
zval_ptr_dtor(&retval);
return len; // Return the length of the data sent
}
PHP_FUNCTION(kcp_set_output_callback)
{
zval *zkcp;
zval *callback;
// Parse parameters: zkcp is the KCP resource, callback is the PHP function
if (zend_parse_parameters(ZEND_NUM_ARGS(), "rz", &zkcp, &callback) == FAILURE) {
RETURN_FALSE;
}
// Fetch the KCP resource
php_kcp_t *kcp_res = (php_kcp_t *)zend_fetch_resource(Z_RES_P(zkcp), "KCP Resource", le_kcp);
if (!kcp_res) {
RETURN_FALSE;
}
// Check if the callback is callable
if (!zend_is_callable(callback, 0, NULL)) {
php_error_docref(NULL, E_WARNING, "Invalid callback provided. Must be a callable.");
RETURN_FALSE;
}
// Release the previous callback if it exists
// if (&kcp_res->output_callback) {
// zval_ptr_dtor(&kcp_res->output_callback);
// kcp_res->output_callback = NULL;
// }
// Store the new callback
ZVAL_COPY(&kcp_res->output_callback, callback);
// Set the udp_output function as the output callback for KCP
kcp_res->kcp->output = udp_output;
kcp_res->kcp->user = kcp_res; // Set the user pointer to our custom structure
RETURN_TRUE;
}
PHP_FUNCTION(kcp_release)
{
zval *zkcp;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "r", &zkcp) == FAILURE) {
RETURN_FALSE;
}
php_kcp_t *kcp_res = (php_kcp_t *)zend_fetch_resource(Z_RES_P(zkcp), "KCP Resource", le_kcp);
if (!kcp_res) {
RETURN_FALSE;
}
ikcp_release(kcp_res->kcp);
zend_list_close(Z_RES_P(zkcp));
RETURN_TRUE;
}
PHP_FUNCTION(kcp_update)
{
zval *zkcp;
long current;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "rl", &zkcp, ¤t) == FAILURE) {
RETURN_FALSE;
}
php_kcp_t *kcp_res = (php_kcp_t *)zend_fetch_resource(Z_RES_P(zkcp), "KCP Resource", le_kcp);
if (!kcp_res) {
RETURN_FALSE;
}
ikcp_update(kcp_res->kcp, current);
RETURN_TRUE;
}
PHP_FUNCTION(kcp_input)
{
zval *zkcp;
char *data;
size_t data_len;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "rs", &zkcp, &data, &data_len) == FAILURE) {
RETURN_FALSE;
}
php_kcp_t *kcp_res = (php_kcp_t *)zend_fetch_resource(Z_RES_P(zkcp), "KCP Resource", le_kcp);
if (!kcp_res) {
RETURN_FALSE;
}
ikcp_input(kcp_res->kcp, data, data_len);
RETURN_TRUE;
}
PHP_FUNCTION(kcp_send)
{
zval *zkcp;
char *data;
size_t data_len;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "rs", &zkcp, &data, &data_len) == FAILURE) {
RETURN_FALSE;
}
php_kcp_t *kcp_res = (php_kcp_t *)zend_fetch_resource(Z_RES_P(zkcp), "KCP Resource", le_kcp);
if (!kcp_res) {
RETURN_FALSE;
}
int ret = ikcp_send(kcp_res->kcp, data, data_len);
RETURN_LONG(ret);
}
PHP_FUNCTION(kcp_flush)
{
zval *zkcp;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "r", &zkcp) == FAILURE) {
RETURN_FALSE;
}
php_kcp_t *kcp_res = (php_kcp_t *)zend_fetch_resource(Z_RES_P(zkcp), "KCP Resource", le_kcp);
if (!kcp_res) {
RETURN_FALSE;
}
ikcp_flush(kcp_res->kcp);
RETURN_TRUE;
}
PHP_FUNCTION(kcp_recv)
{
zval *zkcp;
long maxsize;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "rl", &zkcp, &maxsize) == FAILURE) {
RETURN_FALSE;
}
php_kcp_t *kcp_res = (php_kcp_t *)zend_fetch_resource(Z_RES_P(zkcp), "KCP Resource", le_kcp);
if (!kcp_res) {
RETURN_FALSE;
}
char *buffer = emalloc(maxsize);
int ret = ikcp_recv(kcp_res->kcp, buffer, maxsize);
if (ret < 0) {
efree(buffer);
RETURN_FALSE;
}
RETVAL_STRINGL(buffer, ret);
efree(buffer);
}
PHP_FUNCTION(kcp_nodelay)
{
zval *zkcp;
long nodelay;
long interval;
long resend;
long nc;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "rllll", &zkcp, &nodelay, &interval, &resend, &nc) == FAILURE) {
RETURN_FALSE;
}
php_kcp_t *kcp_res = (php_kcp_t *)zend_fetch_resource(Z_RES_P(zkcp), "KCP Resource", le_kcp);
if (!kcp_res) {
RETURN_FALSE;
}
int ret = ikcp_nodelay(kcp_res->kcp, nodelay, interval, resend, nc);
RETURN_LONG(ret);
}
PHP_FUNCTION(kcp_wndsize)
{
zval *zkcp;
long sndwnd;
long rcvwnd;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "rll", &zkcp, &sndwnd, &rcvwnd) == FAILURE) {
RETURN_FALSE;
}
php_kcp_t *kcp_res = (php_kcp_t *)zend_fetch_resource(Z_RES_P(zkcp), "KCP Resource", le_kcp);
if (!kcp_res) {
RETURN_FALSE;
}
int ret = ikcp_wndsize(kcp_res->kcp, sndwnd, rcvwnd);
RETURN_LONG(ret);
}
PHP_FUNCTION(kcp_setmtu)
{
zval *zkcp;
long mtu;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "rl", &zkcp, &mtu) == FAILURE) {
RETURN_FALSE;
}
php_kcp_t *kcp_res = (php_kcp_t *)zend_fetch_resource(Z_RES_P(zkcp), "KCP Resource", le_kcp);
if (!kcp_res) {
RETURN_FALSE;
}
int ret = ikcp_setmtu(kcp_res->kcp, mtu);
RETURN_LONG(ret);
}
PHP_FUNCTION(kcp_set_rx_minrto)
{
zval *zkcp;
long rx_minrto;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "rl", &zkcp, &rx_minrto) == FAILURE) {
RETURN_FALSE;
}
php_kcp_t *kcp_res = (php_kcp_t *)zend_fetch_resource(Z_RES_P(zkcp), "KCP Resource", le_kcp);
if (!kcp_res) {
RETURN_FALSE;
}
kcp_res->kcp->rx_minrto = rx_minrto;
RETURN_TRUE;
}
PHP_FUNCTION(kcp_waitsnd)
{
zval *zkcp;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "r", &zkcp) == FAILURE) {
RETURN_FALSE;
}
php_kcp_t *kcp_res = (php_kcp_t *)zend_fetch_resource(Z_RES_P(zkcp), "KCP Resource", le_kcp);
if (!kcp_res) {
RETURN_FALSE;
}
int ret = ikcp_waitsnd(kcp_res->kcp);
RETURN_LONG(ret);
}
const zend_function_entry kcp_functions[] = {
PHP_FE(kcp_create, arginfo_kcp_create)
PHP_FE(kcp_set_output_callback, arginfo_kcp_set_output_callback)
PHP_FE(kcp_release, arginfo_kcp_release)
PHP_FE(kcp_update, arginfo_kcp_update)
PHP_FE(kcp_input, arginfo_kcp_input)
PHP_FE(kcp_send, arginfo_kcp_send)
PHP_FE(kcp_flush, arginfo_kcp_flush)
PHP_FE(kcp_recv, arginfo_kcp_recv)
PHP_FE(kcp_nodelay, arginfo_kcp_nodelay)
PHP_FE(kcp_wndsize, arginfo_kcp_wndsize)
PHP_FE(kcp_setmtu, arginfo_kcp_setmtu)
PHP_FE(kcp_set_rx_minrto, arginfo_kcp_set_rx_minrto)
PHP_FE(kcp_waitsnd, arginfo_kcp_waitsnd)
PHP_FE_END
};
PHP_MINIT_FUNCTION(kcp)
{
le_kcp = zend_register_list_destructors_ex(NULL, NULL, "KCP Resource", module_number);
return SUCCESS;
}
PHP_MSHUTDOWN_FUNCTION(kcp)
{
return SUCCESS;
}
PHP_RINIT_FUNCTION(kcp)
{
return SUCCESS;
}
PHP_RSHUTDOWN_FUNCTION(kcp)
{
return SUCCESS;
}
PHP_MINFO_FUNCTION(kcp)
{
php_info_print_table_start();
php_info_print_table_header(2, "kcp support", "enabled");
php_info_print_table_end();
}
zend_module_entry kcp_module_entry = {
STANDARD_MODULE_HEADER,
"kcp",
kcp_functions,
PHP_MINIT(kcp),
PHP_MSHUTDOWN(kcp),
PHP_RINIT(kcp),
PHP_RSHUTDOWN(kcp),
PHP_MINFO(kcp),
PHP_KCP_VERSION,
STANDARD_MODULE_PROPERTIES
};
#ifdef COMPILE_DL_KCP
#ifdef ZTS
ZEND_TSRMLS_CACHE_DEFINE()
#endif
ZEND_GET_MODULE(kcp)
#endif