2012-05-22 19:43:11 +02:00
|
|
|
/*
|
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
| PHP Version 5 |
|
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
| Copyright (c) 1997-2011 The PHP Group |
|
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
| This source file is subject to version 3.01 of the PHP 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.php.net/license/3_01.txt |
|
|
|
|
| If you did not receive a copy of the PHP license and are unable to |
|
|
|
|
| obtain it through the world-wide-web, please send a note to |
|
|
|
|
| license@php.net so we can mail you a copy immediately. |
|
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
| Authors: Shuhei Tanuma <chobieee@gmail.com> |
|
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include "php_uv.h"
|
|
|
|
|
|
|
|
extern void php_uv_init(TSRMLS_D);
|
|
|
|
extern zend_class_entry *uv_class_entry;
|
|
|
|
|
2012-05-23 06:35:28 +02:00
|
|
|
static int uv_resource_handle;
|
2012-05-24 06:07:52 +02:00
|
|
|
static int uv_connect_handle;
|
2012-05-23 06:35:28 +02:00
|
|
|
|
2012-05-22 19:43:11 +02:00
|
|
|
void php_uv_init(TSRMLS_D);
|
|
|
|
|
2012-05-27 15:28:26 +02:00
|
|
|
static void php_uv_close_cb(uv_handle_t *handle);
|
|
|
|
|
2012-05-27 16:13:09 +02:00
|
|
|
typedef struct {
|
|
|
|
uv_write_t req;
|
|
|
|
uv_buf_t buf;
|
|
|
|
} write_req_t;
|
2012-05-27 15:28:26 +02:00
|
|
|
|
2012-05-23 06:35:28 +02:00
|
|
|
void static destruct_uv(zend_rsrc_list_entry *rsrc TSRMLS_DC)
|
|
|
|
{
|
2012-05-27 08:36:03 +02:00
|
|
|
int base_id = -1;
|
2012-05-27 16:27:17 +02:00
|
|
|
//fprintf(stderr, "will be free\n");
|
2012-05-23 06:35:28 +02:00
|
|
|
php_uv_t *obj = (php_uv_t *)rsrc->ptr;
|
2012-05-27 16:27:17 +02:00
|
|
|
|
2012-05-27 08:36:03 +02:00
|
|
|
if (obj->in_free) {
|
2012-05-27 16:27:17 +02:00
|
|
|
/* TODO: why other php_uv_t has already set this? */
|
|
|
|
//fprintf(stderr, "resource_id: %d is freeing", obj->resource_id);
|
|
|
|
//return;
|
2012-05-27 08:36:03 +02:00
|
|
|
}
|
2012-05-27 16:27:17 +02:00
|
|
|
|
2012-05-27 08:36:03 +02:00
|
|
|
obj->in_free = 1;
|
2012-05-23 06:35:28 +02:00
|
|
|
|
2012-05-27 07:43:27 +02:00
|
|
|
if (obj->read_cb) {
|
2012-05-27 11:17:06 +02:00
|
|
|
//fprintf(stderr, "readcb: %d\n", Z_REFCOUNT_P(obj->read_cb));
|
2012-05-27 07:43:27 +02:00
|
|
|
zval_ptr_dtor(&obj->read_cb);
|
|
|
|
obj->read_cb = NULL;
|
|
|
|
}
|
|
|
|
if (obj->write_cb) {
|
2012-05-27 11:17:06 +02:00
|
|
|
//fprintf(stderr, "writecb: %d\n", Z_REFCOUNT_P(obj->write_cb));
|
2012-05-27 07:43:27 +02:00
|
|
|
zval_ptr_dtor(&obj->write_cb);
|
|
|
|
obj->write_cb = NULL;
|
|
|
|
}
|
|
|
|
if (obj->close_cb) {
|
2012-05-27 11:17:06 +02:00
|
|
|
//fprintf(stderr, "closecb: %d\n", Z_REFCOUNT_P(obj->close_cb));
|
2012-05-27 07:43:27 +02:00
|
|
|
zval_ptr_dtor(&obj->close_cb);
|
|
|
|
obj->close_cb = NULL;
|
|
|
|
}
|
|
|
|
if (obj->listen_cb) {
|
2012-05-27 11:17:06 +02:00
|
|
|
//fprintf(stderr, "listencb: %d\n", Z_REFCOUNT_P(obj->listen_cb));
|
2012-05-27 07:43:27 +02:00
|
|
|
zval_ptr_dtor(&obj->listen_cb);
|
|
|
|
obj->listen_cb = NULL;
|
|
|
|
}
|
2012-05-27 08:36:03 +02:00
|
|
|
if (obj->resource_id) {
|
|
|
|
base_id = obj->resource_id;
|
|
|
|
obj->resource_id = NULL;
|
|
|
|
}
|
|
|
|
|
2012-05-27 11:17:06 +02:00
|
|
|
if (obj != NULL) {
|
2012-05-27 07:43:27 +02:00
|
|
|
efree(obj);
|
2012-05-27 08:36:03 +02:00
|
|
|
obj = NULL;
|
|
|
|
}
|
2012-05-27 09:53:30 +02:00
|
|
|
|
2012-05-27 08:36:03 +02:00
|
|
|
if (base_id) {
|
2012-05-27 09:53:30 +02:00
|
|
|
/* basically, this block always fail */
|
2012-05-27 08:36:03 +02:00
|
|
|
zend_list_delete(base_id);
|
2012-05-27 07:43:27 +02:00
|
|
|
}
|
2012-05-27 08:36:03 +02:00
|
|
|
|
2012-05-23 06:35:28 +02:00
|
|
|
}
|
|
|
|
|
2012-05-22 19:43:11 +02:00
|
|
|
|
|
|
|
PHP_MINIT_FUNCTION(uv) {
|
|
|
|
php_uv_init(TSRMLS_C);
|
2012-05-23 06:35:28 +02:00
|
|
|
uv_resource_handle = zend_register_list_destructors_ex(destruct_uv, NULL, PHP_UV_RESOURCE_NAME, module_number);
|
2012-05-24 06:07:52 +02:00
|
|
|
uv_connect_handle = zend_register_list_destructors_ex(destruct_uv, NULL, PHP_UV_CONNECT_RESOURCE_NAME, module_number);
|
2012-05-22 19:43:11 +02:00
|
|
|
|
|
|
|
return SUCCESS;
|
|
|
|
}
|
|
|
|
|
2012-05-23 06:35:28 +02:00
|
|
|
ZEND_BEGIN_ARG_INFO_EX(arginfo_uv_run, 0, 0, 1)
|
|
|
|
ZEND_ARG_INFO(0, loop)
|
|
|
|
ZEND_END_ARG_INFO()
|
|
|
|
|
|
|
|
ZEND_BEGIN_ARG_INFO_EX(arginfo_uv_tcp_connect, 0, 0, 2)
|
|
|
|
ZEND_ARG_INFO(0, resource)
|
|
|
|
ZEND_ARG_INFO(0, callback)
|
|
|
|
ZEND_END_ARG_INFO()
|
|
|
|
|
|
|
|
|
2012-05-23 13:41:04 +02:00
|
|
|
ZEND_BEGIN_ARG_INFO_EX(arginfo_uv_tcp_init, 0, 0, 0)
|
2012-05-23 06:35:28 +02:00
|
|
|
ZEND_ARG_INFO(0, loop)
|
|
|
|
ZEND_END_ARG_INFO()
|
|
|
|
|
|
|
|
|
2012-05-23 13:41:04 +02:00
|
|
|
ZEND_BEGIN_ARG_INFO_EX(arginfo_uv_listen, 0, 0, 3)
|
|
|
|
ZEND_ARG_INFO(0, resource)
|
|
|
|
ZEND_ARG_INFO(0, backlog)
|
|
|
|
ZEND_ARG_INFO(0, callback)
|
|
|
|
ZEND_END_ARG_INFO()
|
|
|
|
|
2012-05-23 19:31:16 +02:00
|
|
|
ZEND_BEGIN_ARG_INFO_EX(arginfo_uv_accept, 0, 0, 2)
|
|
|
|
ZEND_ARG_INFO(0, server)
|
|
|
|
ZEND_ARG_INFO(0, client)
|
|
|
|
ZEND_END_ARG_INFO()
|
2012-05-23 13:41:04 +02:00
|
|
|
|
2012-05-24 06:07:52 +02:00
|
|
|
ZEND_BEGIN_ARG_INFO_EX(arginfo_uv_read_start, 0, 0, 2)
|
|
|
|
ZEND_ARG_INFO(0, server)
|
|
|
|
ZEND_ARG_INFO(0, callback)
|
|
|
|
ZEND_END_ARG_INFO()
|
|
|
|
|
2012-05-26 10:29:04 +02:00
|
|
|
ZEND_BEGIN_ARG_INFO_EX(arginfo_uv_write, 0, 0, 2)
|
|
|
|
ZEND_ARG_INFO(0, client)
|
|
|
|
ZEND_ARG_INFO(0, data)
|
|
|
|
ZEND_END_ARG_INFO()
|
|
|
|
|
2012-05-26 15:54:43 +02:00
|
|
|
ZEND_BEGIN_ARG_INFO_EX(arginfo_uv_last_error, 0, 0, 1)
|
|
|
|
ZEND_ARG_INFO(0, loop)
|
|
|
|
ZEND_END_ARG_INFO()
|
|
|
|
|
2012-05-26 16:26:29 +02:00
|
|
|
ZEND_BEGIN_ARG_INFO_EX(arginfo_uv_timer_init, 0, 0, 1)
|
|
|
|
ZEND_ARG_INFO(0, loop)
|
|
|
|
ZEND_END_ARG_INFO()
|
|
|
|
|
|
|
|
ZEND_BEGIN_ARG_INFO_EX(arginfo_uv_timer_start, 0, 0, 4)
|
|
|
|
ZEND_ARG_INFO(0, timer)
|
|
|
|
ZEND_ARG_INFO(0, timeout)
|
|
|
|
ZEND_ARG_INFO(0, repeat)
|
|
|
|
ZEND_ARG_INFO(0, callback)
|
|
|
|
ZEND_END_ARG_INFO()
|
|
|
|
|
2012-05-24 06:07:52 +02:00
|
|
|
|
2012-05-23 13:41:04 +02:00
|
|
|
ZEND_BEGIN_ARG_INFO_EX(arginfo_uv_tcp_bind, 0, 0, 1)
|
|
|
|
ZEND_ARG_INFO(0, resource)
|
|
|
|
ZEND_ARG_INFO(0, address)
|
|
|
|
ZEND_ARG_INFO(0, port)
|
|
|
|
ZEND_END_ARG_INFO()
|
|
|
|
|
2012-05-26 15:54:43 +02:00
|
|
|
ZEND_BEGIN_ARG_INFO_EX(arginfo_uv_close, 0, 0, 1)
|
|
|
|
ZEND_ARG_INFO(0, stream)
|
|
|
|
ZEND_END_ARG_INFO()
|
|
|
|
|
2012-05-23 13:41:04 +02:00
|
|
|
|
2012-05-23 06:35:28 +02:00
|
|
|
PHP_FUNCTION(uv_run)
|
|
|
|
{
|
|
|
|
uv_run(uv_default_loop());
|
|
|
|
}
|
|
|
|
|
|
|
|
static void php_uv_tcp_connect_cb(uv_connect_t *conn_req, int status)
|
|
|
|
{
|
2012-05-23 13:41:04 +02:00
|
|
|
fprintf(stderr,"status: %d\n", status);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
PHP_FUNCTION(uv_tcp_bind)
|
|
|
|
{
|
|
|
|
zval *resource;
|
|
|
|
char *address;
|
|
|
|
int address_len;
|
|
|
|
long port = 8080;
|
2012-05-26 16:26:29 +02:00
|
|
|
struct sockaddr_in addr;
|
2012-05-23 13:41:04 +02:00
|
|
|
php_uv_t *uv;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
|
|
|
|
"zsl",&resource, &address, &address_len, &port) == FAILURE) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ZEND_FETCH_RESOURCE(uv, php_uv_t *, &resource, -1, PHP_UV_RESOURCE_NAME, uv_resource_handle);
|
2012-05-26 16:26:29 +02:00
|
|
|
addr = uv_ip4_addr(address, port);
|
2012-05-23 13:41:04 +02:00
|
|
|
|
2012-05-26 16:26:29 +02:00
|
|
|
r = uv_tcp_bind((uv_tcp_t*)&uv->uv.tcp, addr);
|
2012-05-23 13:41:04 +02:00
|
|
|
if (r) {
|
|
|
|
fprintf(stderr,"bind error %d\n", r);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-26 10:29:04 +02:00
|
|
|
static void php_uv_write_cb(uv_write_t* req, int status)
|
|
|
|
{
|
2012-05-26 15:19:54 +02:00
|
|
|
TSRMLS_FETCH();
|
2012-05-27 16:13:09 +02:00
|
|
|
write_req_t* wr;
|
2012-05-26 15:19:54 +02:00
|
|
|
zval *retval_ptr, *stat, *client= NULL;
|
|
|
|
zval **params[2];
|
|
|
|
zend_fcall_info fci;
|
|
|
|
zend_fcall_info_cache fcc;
|
|
|
|
char *is_callable_error = NULL;
|
|
|
|
|
2012-05-27 16:13:09 +02:00
|
|
|
wr = (write_req_t*) req;
|
2012-05-26 15:19:54 +02:00
|
|
|
php_uv_t *uv = (php_uv_t*)req->data;
|
|
|
|
|
|
|
|
if(zend_fcall_info_init(uv->write_cb, 0, &fci,&fcc,NULL,&is_callable_error TSRMLS_CC) == SUCCESS) {
|
|
|
|
if (is_callable_error) {
|
|
|
|
fprintf(stderr,"to be a valid callback\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* for now */
|
|
|
|
fci.retval_ptr_ptr = &retval_ptr;
|
|
|
|
|
|
|
|
MAKE_STD_ZVAL(stat);
|
|
|
|
ZVAL_LONG(stat, status);
|
2012-05-27 08:12:13 +02:00
|
|
|
|
2012-05-26 15:19:54 +02:00
|
|
|
MAKE_STD_ZVAL(client);
|
2012-05-27 08:36:03 +02:00
|
|
|
ZVAL_RESOURCE(client, uv->resource_id);
|
2012-05-27 09:53:30 +02:00
|
|
|
zend_list_addref(uv->resource_id);
|
2012-05-26 15:25:15 +02:00
|
|
|
|
2012-05-26 15:19:54 +02:00
|
|
|
params[0] = &stat;
|
|
|
|
params[1] = &client;
|
|
|
|
|
|
|
|
fci.params = params;
|
|
|
|
fci.param_count = 2;
|
|
|
|
|
|
|
|
zend_call_function(&fci, &fcc TSRMLS_CC);
|
|
|
|
zval_ptr_dtor(&retval_ptr);
|
2012-05-27 08:12:13 +02:00
|
|
|
zval_ptr_dtor(&stat);
|
|
|
|
zval_ptr_dtor(&client);
|
2012-05-27 16:13:09 +02:00
|
|
|
|
|
|
|
if (wr->buf.base) {
|
|
|
|
//free(wr->buf.base);
|
|
|
|
}
|
|
|
|
efree(wr);
|
2012-05-26 10:29:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
PHP_FUNCTION(uv_write)
|
|
|
|
{
|
2012-05-26 15:19:54 +02:00
|
|
|
zval *z_cli,*callback;
|
2012-05-26 10:29:04 +02:00
|
|
|
char *data;
|
|
|
|
int data_len = 0;
|
|
|
|
php_uv_t *client;
|
2012-05-27 16:13:09 +02:00
|
|
|
write_req_t *w;
|
2012-05-26 10:29:04 +02:00
|
|
|
|
|
|
|
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
|
2012-05-26 15:19:54 +02:00
|
|
|
"zsz",&z_cli, &data, &data_len,&callback) == FAILURE) {
|
2012-05-26 10:29:04 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ZEND_FETCH_RESOURCE(client, php_uv_t *, &z_cli, -1, PHP_UV_RESOURCE_NAME, uv_resource_handle);
|
2012-05-27 09:53:30 +02:00
|
|
|
zend_list_addref(client->resource_id);
|
2012-05-26 15:19:54 +02:00
|
|
|
Z_ADDREF_P(callback);
|
|
|
|
client->write_cb = callback;
|
|
|
|
|
2012-05-27 16:13:09 +02:00
|
|
|
w = emalloc(sizeof(write_req_t));
|
|
|
|
w->req.data = client;
|
|
|
|
w->buf = uv_buf_init(data, data_len);
|
|
|
|
uv_write(&w->req, &client->uv.tcp, &w->buf, 1, php_uv_write_cb);
|
2012-05-26 10:29:04 +02:00
|
|
|
}
|
|
|
|
|
2012-05-23 19:31:16 +02:00
|
|
|
PHP_FUNCTION(uv_accept)
|
2012-05-23 13:41:04 +02:00
|
|
|
{
|
2012-05-23 19:31:16 +02:00
|
|
|
zval *z_svr,*z_cli;
|
2012-05-26 09:49:50 +02:00
|
|
|
php_uv_t *server, *client;
|
2012-05-27 06:48:10 +02:00
|
|
|
int r;
|
2012-05-23 19:31:16 +02:00
|
|
|
|
|
|
|
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
|
|
|
|
"zz",&z_svr, &z_cli) == FAILURE) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-05-26 09:49:50 +02:00
|
|
|
ZEND_FETCH_RESOURCE(server, php_uv_t *, &z_svr, -1, PHP_UV_RESOURCE_NAME, uv_resource_handle);
|
2012-05-23 19:31:16 +02:00
|
|
|
ZEND_FETCH_RESOURCE(client, php_uv_t *, &z_cli, -1, PHP_UV_RESOURCE_NAME, uv_resource_handle);
|
|
|
|
|
2012-05-27 06:48:10 +02:00
|
|
|
r = uv_accept((uv_stream_t *)&server->uv.tcp, (uv_stream_t *)&client->uv.tcp);
|
|
|
|
if (r) {
|
|
|
|
fprintf(stderr, "failed");
|
|
|
|
}
|
2012-05-23 13:41:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-05-26 09:49:50 +02:00
|
|
|
static void php_uv_listen_cb(uv_stream_t* server, int status)
|
2012-05-23 19:31:16 +02:00
|
|
|
{
|
|
|
|
TSRMLS_FETCH();
|
2012-05-26 15:08:21 +02:00
|
|
|
zval *retval_ptr, *svr= NULL;
|
|
|
|
zval **params[1];
|
2012-05-24 06:07:52 +02:00
|
|
|
zend_fcall_info fci;
|
|
|
|
zend_fcall_info_cache fcc;
|
|
|
|
char *is_callable_error = NULL;
|
|
|
|
|
2012-05-26 09:49:50 +02:00
|
|
|
php_uv_t *uv = (php_uv_t*)server->data;
|
2012-05-24 06:07:52 +02:00
|
|
|
|
|
|
|
if(zend_fcall_info_init(uv->listen_cb, 0, &fci,&fcc,NULL,&is_callable_error TSRMLS_CC) == SUCCESS) {
|
|
|
|
if (is_callable_error) {
|
|
|
|
fprintf(stderr,"to be a valid callback\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* for now */
|
|
|
|
fci.retval_ptr_ptr = &retval_ptr;
|
2012-05-23 19:31:16 +02:00
|
|
|
|
2012-05-26 15:08:21 +02:00
|
|
|
MAKE_STD_ZVAL(svr);
|
2012-05-27 08:36:03 +02:00
|
|
|
ZVAL_RESOURCE(svr, uv->resource_id);
|
2012-05-27 09:53:30 +02:00
|
|
|
zend_list_addref(uv->resource_id);
|
2012-05-26 15:25:15 +02:00
|
|
|
|
2012-05-26 15:08:21 +02:00
|
|
|
params[0] = &svr;
|
|
|
|
|
|
|
|
fci.params = params;
|
|
|
|
fci.param_count = 1;
|
2012-05-24 06:07:52 +02:00
|
|
|
|
|
|
|
zend_call_function(&fci, &fcc TSRMLS_CC);
|
|
|
|
zval_ptr_dtor(&retval_ptr);
|
2012-05-27 08:12:13 +02:00
|
|
|
zval_ptr_dtor(&svr);
|
2012-05-24 06:07:52 +02:00
|
|
|
}
|
2012-05-23 19:31:16 +02:00
|
|
|
|
2012-05-27 16:53:59 +02:00
|
|
|
static void php_uv_close_cb2(uv_handle_t *handle)
|
|
|
|
{
|
|
|
|
/* FIXME */
|
|
|
|
//efree(handle);
|
|
|
|
}
|
|
|
|
|
2012-05-27 15:28:26 +02:00
|
|
|
static void php_uv_shutdown_cb(uv_shutdown_t* req, int status) {
|
2012-05-27 16:53:59 +02:00
|
|
|
uv_close((uv_handle_t*)req->handle, php_uv_close_cb2);
|
2012-05-27 15:28:26 +02:00
|
|
|
}
|
|
|
|
|
2012-05-26 10:29:04 +02:00
|
|
|
static void php_uv_read_cb(uv_stream_t* handle, ssize_t nread, uv_buf_t buf)
|
2012-05-24 06:07:52 +02:00
|
|
|
{
|
2012-05-25 01:07:12 +02:00
|
|
|
TSRMLS_FETCH();
|
2012-05-26 15:03:21 +02:00
|
|
|
zval *retval_ptr = NULL;
|
|
|
|
zval **params[2];
|
2012-05-25 01:07:12 +02:00
|
|
|
zval *buffer;
|
|
|
|
zend_fcall_info fci;
|
|
|
|
zend_fcall_info_cache fcc;
|
|
|
|
char *is_callable_error = NULL;
|
2012-05-27 15:28:26 +02:00
|
|
|
|
|
|
|
if (nread < 0) {
|
|
|
|
uv_shutdown_t* req;
|
2012-05-27 16:27:17 +02:00
|
|
|
|
2012-05-27 15:28:26 +02:00
|
|
|
/* Error or EOF */
|
2012-05-27 16:53:59 +02:00
|
|
|
assert(uv_last_error(uv_default_loop()).code == UV_EOF);
|
2012-05-27 16:27:17 +02:00
|
|
|
|
2012-05-27 15:28:26 +02:00
|
|
|
if (buf.base) {
|
|
|
|
efree(buf.base);
|
|
|
|
}
|
2012-05-27 16:53:59 +02:00
|
|
|
|
2012-05-27 15:28:26 +02:00
|
|
|
fprintf(stderr,"eof or error\n");
|
2012-05-27 16:53:59 +02:00
|
|
|
/* FIXME: can't free in callback function
|
|
|
|
eof or error
|
|
|
|
[Sun May 27 23:52:40 2012] Script: 'examples/example.php'
|
|
|
|
---------------------------------------
|
|
|
|
/Users/chobie/src/php-uv/php_uv.c(322) : Block 0x102e8de58 status: // efree on php_uv_close_cb2.
|
|
|
|
Invalid pointer: ((thread_id=0x02BCD480) != (expected=0x74917960))
|
|
|
|
Invalid pointer: ((size=0x00000071) != (next.prev=0x00000000))
|
|
|
|
*/
|
2012-05-27 15:28:26 +02:00
|
|
|
req = (uv_shutdown_t*) emalloc(sizeof *req);
|
|
|
|
uv_shutdown(req, handle, php_uv_shutdown_cb);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (nread == 0) {
|
|
|
|
/* Everything OK, but nothing read. */
|
|
|
|
efree(buf.base);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-05-25 01:07:12 +02:00
|
|
|
php_uv_t *uv = (php_uv_t*)handle->data;
|
|
|
|
if(zend_fcall_info_init(uv->read_cb, 0, &fci, &fcc, NULL, &is_callable_error TSRMLS_CC) == SUCCESS) {
|
|
|
|
if (is_callable_error) {
|
|
|
|
fprintf(stderr,"to be a valid callback\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-26 09:49:50 +02:00
|
|
|
// for now
|
2012-05-25 01:07:12 +02:00
|
|
|
fci.retval_ptr_ptr = &retval_ptr;
|
|
|
|
|
|
|
|
MAKE_STD_ZVAL(buffer);
|
2012-05-27 16:13:09 +02:00
|
|
|
ZVAL_STRINGL(buffer,buf.base,nread, 1);
|
2012-05-25 01:07:12 +02:00
|
|
|
|
2012-05-26 15:03:21 +02:00
|
|
|
zval *rsc;
|
|
|
|
MAKE_STD_ZVAL(rsc);
|
2012-05-27 08:36:03 +02:00
|
|
|
ZVAL_RESOURCE(rsc, uv->resource_id);
|
2012-05-27 09:53:30 +02:00
|
|
|
//zend_list_addref(uv->resource_id);
|
|
|
|
|
2012-05-26 15:03:21 +02:00
|
|
|
params[0] = &buffer;
|
|
|
|
params[1] = &rsc;
|
|
|
|
|
|
|
|
fci.params = params;
|
|
|
|
fci.param_count = 2;
|
|
|
|
|
|
|
|
//zend_fcall_info_args(&fci, *params TSRMLS_CC);
|
|
|
|
zend_call_function(&fci, &fcc TSRMLS_CC);
|
2012-05-25 01:07:12 +02:00
|
|
|
//zend_fcall_info_args_clear(&fcc, 1);
|
2012-05-27 08:12:13 +02:00
|
|
|
zval_ptr_dtor(&buffer);
|
|
|
|
zval_ptr_dtor(&rsc);
|
2012-05-25 01:07:12 +02:00
|
|
|
zval_ptr_dtor(&retval_ptr);
|
2012-05-27 16:13:09 +02:00
|
|
|
|
|
|
|
if (buf.base) {
|
|
|
|
efree(buf.base);
|
|
|
|
}
|
2012-05-24 06:07:52 +02:00
|
|
|
}
|
2012-05-23 19:31:16 +02:00
|
|
|
|
2012-05-24 16:29:54 +02:00
|
|
|
static uv_buf_t php_uv_read_alloc(uv_handle_t* handle, size_t suggested_size)
|
|
|
|
{
|
2012-05-27 15:28:26 +02:00
|
|
|
return uv_buf_init(emalloc(suggested_size), suggested_size);
|
2012-05-24 06:07:52 +02:00
|
|
|
}
|
|
|
|
|
2012-05-26 15:54:43 +02:00
|
|
|
|
|
|
|
static void php_uv_close_cb(uv_handle_t *handle)
|
|
|
|
{
|
|
|
|
TSRMLS_FETCH();
|
|
|
|
zval *retval_ptr = NULL;
|
|
|
|
zval **params[1];
|
|
|
|
zval *h;
|
|
|
|
zend_fcall_info fci;
|
|
|
|
zend_fcall_info_cache fcc;
|
|
|
|
char *is_callable_error = NULL;
|
|
|
|
|
|
|
|
php_uv_t *uv = (php_uv_t*)handle->data;
|
|
|
|
if(zend_fcall_info_init(uv->close_cb, 0, &fci, &fcc, NULL, &is_callable_error TSRMLS_CC) == SUCCESS) {
|
|
|
|
if (is_callable_error) {
|
|
|
|
fprintf(stderr,"to be a valid callback\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// for now
|
|
|
|
fci.retval_ptr_ptr = &retval_ptr;
|
|
|
|
|
|
|
|
MAKE_STD_ZVAL(h);
|
2012-05-27 08:36:03 +02:00
|
|
|
ZVAL_RESOURCE(h, uv->resource_id);
|
2012-05-27 09:53:30 +02:00
|
|
|
|
2012-05-26 15:54:43 +02:00
|
|
|
params[0] = &h;
|
|
|
|
|
|
|
|
fci.params = params;
|
|
|
|
fci.param_count = 1;
|
|
|
|
|
|
|
|
//zend_fcall_info_args(&fci, *params TSRMLS_CC);
|
|
|
|
zend_call_function(&fci, &fcc TSRMLS_CC);
|
|
|
|
//zend_fcall_info_args_clear(&fcc, 1);
|
|
|
|
zval_ptr_dtor(&retval_ptr);
|
2012-05-27 10:05:25 +02:00
|
|
|
/* for testing resource ref count.
|
2012-05-27 09:53:30 +02:00
|
|
|
{
|
|
|
|
zend_rsrc_list_entry *le;
|
|
|
|
if (zend_hash_index_find(&EG(regular_list), uv->resource_id, (void **) &le)==SUCCESS) {
|
|
|
|
printf("del(%d): %d->%d\n", uv->resource_id, le->refcount, le->refcount-1);
|
2012-05-27 16:27:17 +02:00
|
|
|
zend_list_delete(uv->resource_id);
|
2012-05-27 09:53:30 +02:00
|
|
|
} else {
|
|
|
|
printf("can't find");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*/
|
2012-05-27 16:27:17 +02:00
|
|
|
zval_ptr_dtor(&h); /* call destruct_uv */
|
2012-05-26 15:54:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
PHP_FUNCTION(uv_close)
|
|
|
|
{
|
|
|
|
zval *client, *callback;
|
|
|
|
php_uv_t *uv;
|
|
|
|
|
|
|
|
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
|
|
|
|
"rz",&client, &callback) == FAILURE) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ZEND_FETCH_RESOURCE(uv, php_uv_t *, &client, -1, PHP_UV_RESOURCE_NAME, uv_resource_handle);
|
|
|
|
Z_ADDREF_P(callback);
|
|
|
|
|
|
|
|
uv->close_cb = callback;
|
2012-05-26 15:59:59 +02:00
|
|
|
uv_close((uv_stream_t*)&uv->uv.tcp, php_uv_close_cb);
|
2012-05-26 15:54:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-05-24 06:07:52 +02:00
|
|
|
PHP_FUNCTION(uv_read_start)
|
|
|
|
{
|
|
|
|
zval *client, *callback;
|
2012-05-24 16:29:54 +02:00
|
|
|
php_uv_t *uv;
|
2012-05-24 06:07:52 +02:00
|
|
|
|
|
|
|
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
|
2012-05-25 01:07:12 +02:00
|
|
|
"rz",&client, &callback) == FAILURE) {
|
2012-05-24 06:07:52 +02:00
|
|
|
return;
|
|
|
|
}
|
2012-05-26 09:49:50 +02:00
|
|
|
|
2012-05-24 16:29:54 +02:00
|
|
|
ZEND_FETCH_RESOURCE(uv, php_uv_t *, &client, -1, PHP_UV_RESOURCE_NAME, uv_resource_handle);
|
2012-05-26 07:05:17 +02:00
|
|
|
Z_ADDREF_P(callback);
|
2012-05-27 09:53:30 +02:00
|
|
|
zend_list_addref(uv->resource_id);
|
2012-05-26 09:49:50 +02:00
|
|
|
|
2012-05-26 07:05:17 +02:00
|
|
|
uv->read_cb = callback;
|
2012-05-26 15:59:59 +02:00
|
|
|
uv->uv.tcp.data = uv;
|
2012-05-26 09:49:50 +02:00
|
|
|
|
2012-05-26 15:59:59 +02:00
|
|
|
uv_read_start((uv_stream_t*)&uv->uv.tcp, php_uv_read_alloc, php_uv_read_cb);
|
2012-05-23 19:31:16 +02:00
|
|
|
}
|
|
|
|
|
2012-05-23 13:41:04 +02:00
|
|
|
PHP_FUNCTION(uv_listen)
|
|
|
|
{
|
2012-05-24 06:07:52 +02:00
|
|
|
zval *resource, *callback;
|
2012-05-23 13:41:04 +02:00
|
|
|
long backlog = SOMAXCONN;
|
|
|
|
php_uv_t *uv;
|
2012-05-27 06:48:10 +02:00
|
|
|
int r;
|
2012-05-23 13:41:04 +02:00
|
|
|
|
|
|
|
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
|
2012-05-24 06:07:52 +02:00
|
|
|
"zlz",&resource, &backlog, &callback) == FAILURE) {
|
2012-05-23 13:41:04 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ZEND_FETCH_RESOURCE(uv, php_uv_t *, &resource, -1, PHP_UV_RESOURCE_NAME, uv_resource_handle);
|
2012-05-26 07:05:17 +02:00
|
|
|
Z_ADDREF_P(callback);
|
|
|
|
uv->listen_cb = callback;
|
2012-05-27 09:53:30 +02:00
|
|
|
zend_list_addref(uv->resource_id);
|
2012-05-27 08:12:13 +02:00
|
|
|
|
2012-05-27 06:48:10 +02:00
|
|
|
r = uv_listen((uv_stream_t*)&uv->uv.tcp, backlog, php_uv_listen_cb);
|
|
|
|
if (r) {
|
|
|
|
fprintf(stderr, "damepo");
|
|
|
|
}
|
2012-05-23 06:35:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
PHP_FUNCTION(uv_tcp_connect)
|
|
|
|
{
|
|
|
|
zval *resource;
|
|
|
|
php_uv_t *uv;
|
|
|
|
zend_fcall_info fci = {
|
|
|
|
0,NULL,NULL,NULL,NULL,0,NULL,NULL
|
|
|
|
};
|
|
|
|
zend_fcall_info_cache fci_cache;
|
|
|
|
|
|
|
|
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
|
|
|
|
"zf",&resource,&fci,&fci_cache) == FAILURE) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ZEND_FETCH_RESOURCE(uv, php_uv_t *, &resource, -1, PHP_UV_RESOURCE_NAME, uv_resource_handle);
|
2012-05-26 07:05:17 +02:00
|
|
|
//uv->fci_connect = fci;
|
|
|
|
//uv->fcc_connect = fci_cache;
|
2012-05-23 13:41:04 +02:00
|
|
|
|
2012-05-26 07:05:17 +02:00
|
|
|
//uv_tcp_connect(uv->connect, uv->socket, uv->addr, php_uv_tcp_connect_cb);
|
2012-05-23 06:35:28 +02:00
|
|
|
}
|
|
|
|
|
2012-05-26 16:26:29 +02:00
|
|
|
PHP_FUNCTION(uv_timer_init)
|
|
|
|
{
|
|
|
|
int r;
|
|
|
|
/* TODO */
|
|
|
|
zval *loop;
|
|
|
|
php_uv_t *uv;
|
|
|
|
|
|
|
|
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
|
|
|
|
"|z",&loop) == FAILURE) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
uv = (php_uv_t *)emalloc(sizeof(php_uv_t));
|
|
|
|
|
|
|
|
r = uv_timer_init(uv_default_loop(), &uv->uv.timer);
|
|
|
|
if (r) {
|
|
|
|
fprintf(stderr, "Socket creation error\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
uv->uv.timer.data = uv;
|
2012-05-27 11:17:06 +02:00
|
|
|
uv->listen_cb = NULL;
|
|
|
|
uv->read_cb = NULL;
|
|
|
|
uv->write_cb = NULL;
|
|
|
|
uv->close_cb = NULL;
|
|
|
|
uv->timer_cb = NULL;
|
2012-05-26 16:26:29 +02:00
|
|
|
|
|
|
|
ZEND_REGISTER_RESOURCE(return_value, uv, uv_resource_handle);
|
|
|
|
uv->resource_id = Z_LVAL_P(return_value);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void php_uv_timer_cb(uv_timer_t *handle, int status)
|
|
|
|
{
|
|
|
|
TSRMLS_FETCH();
|
|
|
|
zval *retval_ptr, *stat, *client= NULL;
|
|
|
|
zval **params[2];
|
|
|
|
zend_fcall_info fci;
|
|
|
|
zend_fcall_info_cache fcc;
|
|
|
|
char *is_callable_error = NULL;
|
|
|
|
|
|
|
|
php_uv_t *uv = (php_uv_t*)handle->data;
|
|
|
|
|
|
|
|
if(zend_fcall_info_init(uv->timer_cb, 0, &fci,&fcc,NULL,&is_callable_error TSRMLS_CC) == SUCCESS) {
|
|
|
|
if (is_callable_error) {
|
|
|
|
fprintf(stderr,"to be a valid callback\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* for now */
|
|
|
|
fci.retval_ptr_ptr = &retval_ptr;
|
|
|
|
|
|
|
|
MAKE_STD_ZVAL(stat);
|
|
|
|
ZVAL_LONG(stat, status);
|
|
|
|
MAKE_STD_ZVAL(client);
|
2012-05-27 08:36:03 +02:00
|
|
|
ZVAL_RESOURCE(client, uv->resource_id);
|
2012-05-27 09:53:30 +02:00
|
|
|
zend_list_addref(uv->resource_id);
|
2012-05-26 16:26:29 +02:00
|
|
|
|
|
|
|
params[0] = &stat;
|
|
|
|
params[1] = &client;
|
|
|
|
|
|
|
|
fci.params = params;
|
|
|
|
fci.param_count = 2;
|
|
|
|
|
|
|
|
zend_call_function(&fci, &fcc TSRMLS_CC);
|
2012-05-27 08:14:56 +02:00
|
|
|
|
2012-05-27 10:05:25 +02:00
|
|
|
zval_ptr_dtor(&retval_ptr);
|
2012-05-27 08:14:56 +02:00
|
|
|
zval_ptr_dtor(&stat);
|
|
|
|
zval_ptr_dtor(&client);
|
2012-05-26 16:26:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
PHP_FUNCTION(uv_timer_start)
|
|
|
|
{
|
|
|
|
//int uv_timer_start(uv_timer_t* handle, uv_timer_cb timer_cb, int64_t timeout,int64_t repeat) {
|
|
|
|
zval *timer, *callback;
|
|
|
|
php_uv_t *uv;
|
|
|
|
long timeout, repeat = 0;
|
|
|
|
|
|
|
|
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
|
|
|
|
"rllz",&timer, &timeout, &repeat, &callback) == FAILURE) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ZEND_FETCH_RESOURCE(uv, php_uv_t *, &timer, -1, PHP_UV_RESOURCE_NAME, uv_resource_handle);
|
|
|
|
Z_ADDREF_P(callback);
|
|
|
|
|
|
|
|
uv->timer_cb = callback;
|
|
|
|
uv_timer_start((uv_timer_t*)&uv->uv.timer, php_uv_timer_cb, timeout, repeat);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-05-23 06:35:28 +02:00
|
|
|
PHP_FUNCTION(uv_tcp_init)
|
|
|
|
{
|
2012-05-23 13:41:04 +02:00
|
|
|
int r;
|
|
|
|
/* TODO */
|
2012-05-23 06:35:28 +02:00
|
|
|
zval *loop;
|
|
|
|
php_uv_t *uv;
|
|
|
|
|
|
|
|
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
|
2012-05-23 13:41:04 +02:00
|
|
|
"|z",&loop) == FAILURE) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-05-24 16:29:54 +02:00
|
|
|
uv = (php_uv_t *)emalloc(sizeof(php_uv_t));
|
2012-05-27 07:43:27 +02:00
|
|
|
if (!uv) {
|
|
|
|
fprintf(stderr, "emalloc error\n");
|
|
|
|
return;
|
|
|
|
}
|
2012-05-24 16:29:54 +02:00
|
|
|
|
2012-05-26 15:59:59 +02:00
|
|
|
r = uv_tcp_init(uv_default_loop(), &uv->uv.tcp);
|
2012-05-23 13:41:04 +02:00
|
|
|
if (r) {
|
|
|
|
fprintf(stderr, "Socket creation error\n");
|
2012-05-23 06:35:28 +02:00
|
|
|
return;
|
|
|
|
}
|
2012-05-27 07:43:27 +02:00
|
|
|
|
2012-05-26 15:59:59 +02:00
|
|
|
uv->uv.tcp.data = uv;
|
2012-05-27 07:43:27 +02:00
|
|
|
uv->listen_cb = NULL;
|
|
|
|
uv->read_cb = NULL;
|
|
|
|
uv->write_cb = NULL;
|
|
|
|
uv->close_cb = NULL;
|
|
|
|
uv->timer_cb = NULL;
|
2012-05-23 06:35:28 +02:00
|
|
|
|
|
|
|
ZEND_REGISTER_RESOURCE(return_value, uv, uv_resource_handle);
|
2012-05-26 15:25:15 +02:00
|
|
|
uv->resource_id = Z_LVAL_P(return_value);
|
2012-05-23 06:35:28 +02:00
|
|
|
}
|
|
|
|
|
2012-05-26 15:54:43 +02:00
|
|
|
PHP_FUNCTION(uv_last_error)
|
|
|
|
{
|
|
|
|
/* TODO */
|
|
|
|
uv_loop_t *loop;
|
|
|
|
uv_err_t err;
|
|
|
|
|
|
|
|
loop = uv_default_loop();
|
|
|
|
err = uv_last_error(loop);
|
|
|
|
|
|
|
|
RETVAL_LONG(err.code);
|
|
|
|
}
|
|
|
|
|
2012-05-23 06:35:28 +02:00
|
|
|
static zend_function_entry uv_functions[] = {
|
|
|
|
PHP_FE(uv_run, arginfo_uv_run)
|
2012-05-26 16:26:29 +02:00
|
|
|
PHP_FE(uv_timer_init, arginfo_uv_timer_init)
|
|
|
|
PHP_FE(uv_timer_start, arginfo_uv_timer_start)
|
2012-05-23 06:35:28 +02:00
|
|
|
PHP_FE(uv_tcp_init, arginfo_uv_tcp_init)
|
2012-05-23 13:41:04 +02:00
|
|
|
PHP_FE(uv_tcp_bind, arginfo_uv_tcp_bind)
|
|
|
|
PHP_FE(uv_listen, arginfo_uv_listen)
|
2012-05-23 19:31:16 +02:00
|
|
|
PHP_FE(uv_accept, arginfo_uv_accept)
|
2012-05-26 10:29:04 +02:00
|
|
|
PHP_FE(uv_write, arginfo_uv_write)
|
2012-05-26 15:54:43 +02:00
|
|
|
PHP_FE(uv_close, arginfo_uv_close)
|
2012-05-24 06:07:52 +02:00
|
|
|
PHP_FE(uv_read_start, arginfo_uv_read_start)
|
2012-05-23 06:35:28 +02:00
|
|
|
PHP_FE(uv_tcp_connect, arginfo_uv_tcp_connect)
|
2012-05-26 15:54:43 +02:00
|
|
|
PHP_FE(uv_last_error, arginfo_uv_last_error)
|
2012-05-23 06:35:28 +02:00
|
|
|
{NULL, NULL, NULL}
|
|
|
|
};
|
|
|
|
|
2012-05-22 19:43:11 +02:00
|
|
|
|
|
|
|
PHP_MINFO_FUNCTION(uv)
|
|
|
|
{
|
|
|
|
php_printf("PHP libuv Extension\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
zend_module_entry uv_module_entry = {
|
|
|
|
#if ZEND_MODULE_API_NO >= 20010901
|
|
|
|
STANDARD_MODULE_HEADER,
|
|
|
|
#endif
|
|
|
|
"uv",
|
2012-05-23 06:35:28 +02:00
|
|
|
uv_functions, /* Functions */
|
2012-05-22 19:43:11 +02:00
|
|
|
PHP_MINIT(uv), /* MINIT */
|
|
|
|
NULL, /* MSHUTDOWN */
|
|
|
|
NULL, /* RINIT */
|
|
|
|
NULL, /* RSHUTDOWN */
|
|
|
|
PHP_MINFO(uv), /* MINFO */
|
|
|
|
#if ZEND_MODULE_API_NO >= 20010901
|
|
|
|
PHP_UV_EXTVER,
|
|
|
|
#endif
|
|
|
|
STANDARD_MODULE_PROPERTIES
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef COMPILE_DL_UV
|
|
|
|
ZEND_GET_MODULE(uv)
|
|
|
|
#endif
|