Skip to content

Commit f4dd085

Browse files
author
ace411
committed
feat: modify extension internals
- introduce signal handler count global variable - redefine sig_cb as an array of signal handlers - allow for manual override of internal buffer size in invocations of addReadStream
1 parent 5424532 commit f4dd085

File tree

3 files changed

+38
-21
lines changed

3 files changed

+38
-21
lines changed

php_mrloop.c

+5-2
Original file line numberDiff line numberDiff line change
@@ -138,9 +138,12 @@ PHP_RSHUTDOWN_FUNCTION(mrloop)
138138
efree(MRLOOP_G(tcp_cb));
139139
}
140140

141-
if (MRLOOP_G(sig_cb))
141+
if (MRLOOP_G(sigc) > 0)
142142
{
143-
efree(MRLOOP_G(sig_cb));
143+
for (size_t idx = 0; idx < MRLOOP_G(sigc); idx++)
144+
{
145+
efree(MRLOOP_G(sig_cb)[idx]);
146+
}
144147
}
145148

146149
return SUCCESS;

src/loop.c

+30-18
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ static void php_mrloop_readv_cb(void *data, int res)
172172
cb = (php_mrloop_cb_t *)data;
173173
iov = (php_iovec_t *)cb->data;
174174

175-
char next[iov->iov_len];
175+
char next[(size_t)iov->iov_len];
176176
sprintf(next, "%.*s", (int)iov->iov_len, (char *)iov->iov_base);
177177

178178
ZVAL_STRING(&args[0], next);
@@ -434,23 +434,32 @@ static void php_mrloop_parse_http_response(INTERNAL_FUNCTION_PARAMETERS)
434434

435435
static void php_mrloop_signal_cb(int sig)
436436
{
437-
zval result;
438-
int fsignal;
439-
440-
MRLOOP_G(sig_cb)->fci.retval = &result;
441-
MRLOOP_G(sig_cb)->fci.param_count = 0;
442-
MRLOOP_G(sig_cb)->fci.params = NULL;
443-
444-
fsignal = MRLOOP_G(sig_cb)->signal;
445-
446-
if (fsignal == sig)
437+
for (size_t idx = 0; idx < MRLOOP_G(sigc); idx++)
447438
{
448-
if (zend_call_function(&MRLOOP_G(sig_cb)->fci, &MRLOOP_G(sig_cb)->fci_cache) == FAILURE)
439+
if (MRLOOP_G(sig_cb)[idx] == NULL)
449440
{
450-
PHP_MRLOOP_THROW("There is an error in your callback");
441+
break;
451442
}
452443

453-
zval_ptr_dtor(&result);
444+
php_mrloop_cb_t *cb = MRLOOP_G(sig_cb)[idx];
445+
446+
if (cb->signal == sig)
447+
{
448+
zval result;
449+
450+
cb->fci.retval = &result;
451+
cb->fci.param_count = 0;
452+
cb->fci.params = NULL;
453+
454+
if (zend_call_function(&cb->fci, &cb->fci_cache) == FAILURE)
455+
{
456+
PHP_MRLOOP_THROW("There is an error in your callback");
457+
}
458+
459+
zval_ptr_dtor(&result);
460+
461+
break;
462+
}
454463
}
455464

456465
exit(EXIT_SUCCESS);
@@ -466,9 +475,12 @@ static void php_mrloop_add_signal(INTERNAL_FUNCTION_PARAMETERS)
466475
Z_PARAM_FUNC(fci, fci_cache)
467476
ZEND_PARSE_PARAMETERS_END();
468477

469-
MRLOOP_G(sig_cb) = emalloc(sizeof(php_mrloop_cb_t));
470-
PHP_CB_TO_MRLOOP_CB(MRLOOP_G(sig_cb), fci, fci_cache);
471-
MRLOOP_G(sig_cb)->signal = (int)php_signal;
478+
MRLOOP_G(sigc)++;
479+
size_t next = MRLOOP_G(sigc) - 1;
480+
481+
MRLOOP_G(sig_cb)[next] = emalloc(sizeof(php_mrloop_cb_t));
482+
PHP_CB_TO_MRLOOP_CB(MRLOOP_G(sig_cb)[next], fci, fci_cache);
483+
MRLOOP_G(sig_cb)[next]->signal = (int)php_signal;
472484

473485
signal(SIGINT, php_mrloop_signal_cb);
474486
signal(SIGHUP, php_mrloop_signal_cb);
@@ -533,7 +545,7 @@ static void php_mrloop_add_read_stream(INTERNAL_FUNCTION_PARAMETERS)
533545
return;
534546
}
535547

536-
fnbytes = (size_t)(nbytes_null == true ? DEFAULT_STREAM_BUFF_LEN : fnbytes);
548+
fnbytes = (size_t)(nbytes_null == true ? DEFAULT_STREAM_BUFF_LEN : nbytes);
537549

538550
iov = emalloc(sizeof(php_iovec_t));
539551
iov->iov_base = emalloc(fnbytes);

src/loop.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,9 @@ ZEND_BEGIN_MODULE_GLOBALS(mrloop)
9393
/* TCP server callback */
9494
php_mrloop_cb_t *tcp_cb;
9595
/* signal callback */
96-
php_mrloop_cb_t *sig_cb;
96+
php_mrloop_cb_t *sig_cb[3];
97+
/* signal callback count */
98+
size_t sigc;
9799
ZEND_END_MODULE_GLOBALS(mrloop)
98100
/* }}} */
99101

0 commit comments

Comments
 (0)