ext-uv/php_uv.c

1597 lines
36 KiB
C
Raw Normal View History

2012-05-22 19:43:11 +02:00
/*
+----------------------------------------------------------------------+
| PHP Version 5 |
+----------------------------------------------------------------------+
2012-05-28 05:10:49 +02:00
| Copyright (c) 1997-2012 The PHP Group |
2012-05-22 19:43:11 +02:00
+----------------------------------------------------------------------+
| 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. |
+----------------------------------------------------------------------+
2012-05-28 05:10:49 +02:00
| Authors: Shuhei Tanuma <chobieeee@php.net> |
2012-05-22 19:43:11 +02:00
+----------------------------------------------------------------------+
*/
#include "php_uv.h"
extern void php_uv_init(TSRMLS_D);
extern zend_class_entry *uv_class_entry;
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-29 18:47:30 +02:00
typedef struct {
uv_udp_send_t req;
uv_buf_t buf;
} send_req_t;
2012-05-28 18:12:09 +02:00
#define PHP_UV_INIT_ZVALS(uv) \
2012-05-28 05:29:15 +02:00
{ \
2012-05-28 18:12:09 +02:00
uv->address = NULL; \
2012-05-28 05:29:15 +02:00
uv->listen_cb = NULL; \
uv->read_cb = NULL; \
uv->write_cb = NULL; \
uv->close_cb = NULL; \
uv->timer_cb = NULL; \
uv->idle_cb = NULL; \
2012-05-28 18:12:09 +02:00
uv->connect_cb = NULL; \
2012-05-29 18:47:30 +02:00
uv->getaddr_cb = NULL; \
uv->udp_recv_cb = NULL; \
uv->udp_send_cb = NULL; \
2012-05-28 05:29:15 +02:00
}
2012-05-28 18:12:09 +02:00
/* static variables */
static uv_loop_t *_php_uv_default_loop;
/* resources */
2012-05-28 05:29:15 +02:00
2012-05-28 14:52:09 +02:00
static int uv_resource_handle;
static int uv_connect_handle;
static int uv_loop_handle;
2012-05-28 18:12:09 +02:00
static int uv_sockaddr_handle;
2012-05-28 18:12:09 +02:00
/* declarations */
2012-05-28 14:52:09 +02:00
void php_uv_init(TSRMLS_D);
2012-05-29 05:37:08 +02:00
/**
* execute callback
*
* @param zval** retval_ptr non-initialized pointer. this will be allocate from zend_call_function
* @param zval* callback callable object
* @param zval** params parameters.
* @param int param_count
* @return int (maybe..)
*/
2012-05-30 15:18:50 +02:00
static int php_uv_do_callback(zval **retval_ptr, zval *callback, zval ***params, int param_count TSRMLS_DC);
2012-05-29 05:37:08 +02:00
2012-05-28 14:52:09 +02:00
static void php_uv_close_cb(uv_handle_t *handle);
void static destruct_uv(zend_rsrc_list_entry *rsrc TSRMLS_DC);
static void php_uv_tcp_connect_cb(uv_connect_t *conn_req, int status);
static void php_uv_write_cb(uv_write_t* req, int status);
static void php_uv_listen_cb(uv_stream_t* server, int status);
static void php_uv_close_cb2(uv_handle_t *handle);
static void php_uv_shutdown_cb(uv_shutdown_t* req, int status);
static void php_uv_read_cb(uv_stream_t* handle, ssize_t nread, uv_buf_t buf);
static uv_buf_t php_uv_read_alloc(uv_handle_t* handle, size_t suggested_size);
static void php_uv_close_cb(uv_handle_t *handle);
static void php_uv_timer_cb(uv_timer_t *handle, int status);
static void php_uv_idle_cb(uv_timer_t *handle, int status);
2012-05-28 18:12:09 +02:00
/* destructor */
void static destruct_uv_loop(zend_rsrc_list_entry *rsrc TSRMLS_DC)
{
2012-05-30 15:32:11 +02:00
uv_loop_t *loop = (uv_loop_t *)rsrc->ptr;
if (loop != _php_uv_default_loop) {
uv_loop_delete(loop);
}
}
2012-05-28 18:12:09 +02:00
void static destruct_uv_sockaddr(zend_rsrc_list_entry *rsrc TSRMLS_DC)
{
php_uv_sockaddr_t *addr = (php_uv_sockaddr_t *)rsrc->ptr;
efree(addr);
}
static uv_loop_t *php_uv_default_loop()
{
if (_php_uv_default_loop == NULL) {
_php_uv_default_loop = uv_default_loop();
}
return _php_uv_default_loop;
}
2012-05-28 14:52:09 +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;
//fprintf(stderr, "will be free\n");
php_uv_t *obj = (php_uv_t *)rsrc->ptr;
2012-05-27 08:36:03 +02:00
if (obj->in_free) {
/* 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 08:36:03 +02:00
obj->in_free = 1;
2012-05-28 18:12:09 +02:00
if (obj->address) {
2012-05-30 06:06:37 +02:00
//fprintf(stderr, "address: %d\n", Z_REFCOUNT_P(obj->read_cb));
2012-05-28 18:12:09 +02:00
zval_ptr_dtor(&obj->address);
obj->address = NULL;
}
2012-05-27 07:43:27 +02:00
if (obj->read_cb) {
//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) {
//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) {
//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-30 06:06:37 +02:00
//fprintf(stderr, "listen_cb: %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-28 05:29:15 +02:00
if (obj->idle_cb) {
2012-05-30 06:06:37 +02:00
//fprintf(stderr, "idle_cb: %d\n", Z_REFCOUNT_P(obj->listen_cb));
2012-05-28 05:29:15 +02:00
zval_ptr_dtor(&obj->idle_cb);
obj->idle_cb = NULL;
}
2012-05-28 18:12:09 +02:00
if (obj->connect_cb) {
2012-05-30 06:06:37 +02:00
//fprintf(stderr, "connect_cb: %d\n", Z_REFCOUNT_P(obj->listen_cb));
2012-05-28 18:12:09 +02:00
zval_ptr_dtor(&obj->connect_cb);
obj->connect_cb = NULL;
}
2012-05-29 18:47:30 +02:00
if (obj->udp_recv_cb) {
2012-05-30 06:06:37 +02:00
//fprintf(stderr, "udp_recv_cb: %d\n", Z_REFCOUNT_P(obj->listen_cb));
2012-05-29 18:47:30 +02:00
zval_ptr_dtor(&obj->udp_recv_cb);
obj->udp_recv_cb = NULL;
}
if (obj->udp_send_cb) {
2012-05-30 06:06:37 +02:00
//fprintf(stderr, "udp_send_cb: %d\n", Z_REFCOUNT_P(obj->listen_cb));
2012-05-29 18:47:30 +02:00
zval_ptr_dtor(&obj->udp_send_cb);
obj->udp_send_cb = NULL;
}
2012-05-28 05:29:15 +02:00
2012-05-27 08:36:03 +02:00
if (obj->resource_id) {
base_id = obj->resource_id;
2012-05-30 15:32:11 +02:00
obj->resource_id = 0;
2012-05-27 08:36:03 +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-28 18:12:09 +02:00
if (base_id) {
/* basically, this block always fail */
zend_list_delete(base_id);
}
}
/* callback */
2012-05-30 15:18:50 +02:00
static int php_uv_do_callback(zval **retval_ptr, zval *callback, zval ***params, int param_count TSRMLS_DC)
2012-05-28 18:12:09 +02:00
{
zend_fcall_info fci;
zend_fcall_info_cache fcc;
char *is_callable_error = NULL;
2012-05-29 05:37:08 +02:00
int error;
2012-05-28 18:12:09 +02:00
2012-05-29 05:37:08 +02:00
if(zend_fcall_info_init(callback, 0, &fci, &fcc, NULL, &is_callable_error TSRMLS_CC) == SUCCESS) {
2012-05-28 18:12:09 +02:00
if (is_callable_error) {
php_error_docref(NULL TSRMLS_CC, E_ERROR, "to be a valid callback");
2012-05-28 18:12:09 +02:00
}
}
2012-05-29 05:37:08 +02:00
fci.retval_ptr_ptr = retval_ptr;
fci.params = params;
fci.param_count = param_count;
error = zend_call_function(&fci, &fcc TSRMLS_CC);
return error;
}
static void php_uv_tcp_connect_cb(uv_connect_t *req, int status)
{
TSRMLS_FETCH();
zval *retval_ptr, *stat, *client= NULL;
zval **params[2];
php_uv_t *uv = (php_uv_t*)req->data;
2012-05-28 18:12:09 +02:00
MAKE_STD_ZVAL(stat);
ZVAL_LONG(stat, status);
MAKE_STD_ZVAL(client);
ZVAL_RESOURCE(client, uv->resource_id);
zend_list_addref(uv->resource_id);
params[0] = &stat;
params[1] = &client;
2012-05-29 05:37:08 +02:00
php_uv_do_callback(&retval_ptr, uv->connect_cb, params, 2 TSRMLS_CC);
2012-05-28 18:12:09 +02:00
zval_ptr_dtor(&retval_ptr);
zval_ptr_dtor(&stat);
zval_ptr_dtor(&client);
efree(req);
}
static void php_uv_write_cb(uv_write_t* req, int status)
{
TSRMLS_FETCH();
write_req_t* wr;
zval *retval_ptr, *stat, *client= NULL;
zval **params[2];
wr = (write_req_t*) req;
php_uv_t *uv = (php_uv_t*)req->data;
MAKE_STD_ZVAL(stat);
ZVAL_LONG(stat, status);
MAKE_STD_ZVAL(client);
ZVAL_RESOURCE(client, uv->resource_id);
zend_list_addref(uv->resource_id);
params[0] = &stat;
params[1] = &client;
2012-05-29 05:37:08 +02:00
php_uv_do_callback(&retval_ptr, uv->write_cb, params, 2 TSRMLS_CC);
2012-05-28 18:12:09 +02:00
zval_ptr_dtor(&retval_ptr);
zval_ptr_dtor(&stat);
zval_ptr_dtor(&client);
if (wr->buf.base) {
//free(wr->buf.base);
}
efree(wr);
}
2012-05-29 18:47:30 +02:00
static void php_uv_udp_send_cb(uv_udp_send_t* req, int status)
{
TSRMLS_FETCH();
send_req_t* wr;
zval *retval_ptr, *stat, *client= NULL;
zval **params[2];
wr = (send_req_t*) req;
php_uv_t *uv = (php_uv_t*)req->data;
MAKE_STD_ZVAL(stat);
ZVAL_LONG(stat, status);
MAKE_STD_ZVAL(client);
ZVAL_RESOURCE(client, uv->resource_id);
zend_list_addref(uv->resource_id);
params[0] = &stat;
params[1] = &client;
php_uv_do_callback(&retval_ptr, uv->udp_send_cb, params, 2 TSRMLS_CC);
zval_ptr_dtor(&retval_ptr);
zval_ptr_dtor(&stat);
zval_ptr_dtor(&client);
if (wr->buf.base) {
efree(wr->buf.base);
}
efree(wr);
}
2012-05-28 18:12:09 +02:00
static void php_uv_listen_cb(uv_stream_t* server, int status)
{
TSRMLS_FETCH();
zval *retval_ptr, *svr= NULL;
zval **params[1];
php_uv_t *uv = (php_uv_t*)server->data;
MAKE_STD_ZVAL(svr);
ZVAL_RESOURCE(svr, uv->resource_id);
zend_list_addref(uv->resource_id);
params[0] = &svr;
2012-05-29 05:37:08 +02:00
php_uv_do_callback(&retval_ptr, uv->listen_cb, params, 1 TSRMLS_CC);
2012-05-28 18:12:09 +02:00
zval_ptr_dtor(&retval_ptr);
zval_ptr_dtor(&svr);
}
static void php_uv_close_cb2(uv_handle_t *handle)
{
/* what should I do here? */
}
static void php_uv_shutdown_cb(uv_shutdown_t* req, int status)
{
uv_close((uv_handle_t*)req->handle, php_uv_close_cb2);
efree(req);
}
static void php_uv_read_cb(uv_stream_t* handle, ssize_t nread, uv_buf_t buf)
{
TSRMLS_FETCH();
zval *retval_ptr = NULL;
zval **params[2];
zval *buffer;
if (nread < 0) {
2012-05-29 05:37:08 +02:00
/* does this should be in user-land ? */
2012-05-28 18:12:09 +02:00
uv_shutdown_t* req;
/* Error or EOF */
assert(uv_last_error(uv_default_loop()).code == UV_EOF);
if (buf.base) {
efree(buf.base);
}
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;
}
php_uv_t *uv = (php_uv_t*)handle->data;
MAKE_STD_ZVAL(buffer);
ZVAL_STRINGL(buffer,buf.base,nread, 1);
zval *rsc;
MAKE_STD_ZVAL(rsc);
ZVAL_RESOURCE(rsc, uv->resource_id);
//zend_list_addref(uv->resource_id);
params[0] = &buffer;
params[1] = &rsc;
2012-05-29 05:37:08 +02:00
php_uv_do_callback(&retval_ptr, uv->read_cb, params, 2 TSRMLS_CC);
2012-05-28 18:12:09 +02:00
zval_ptr_dtor(&buffer);
zval_ptr_dtor(&rsc);
zval_ptr_dtor(&retval_ptr);
if (buf.base) {
efree(buf.base);
}
}
2012-05-29 18:47:30 +02:00
2012-05-30 15:32:11 +02:00
static void php_uv_udp_recv_cb(uv_udp_t* handle, ssize_t nread, uv_buf_t buf, struct sockaddr* addr, unsigned flags)
2012-05-29 18:47:30 +02:00
{
2012-05-30 06:07:37 +02:00
/* TODO: is this implment correct? */
2012-05-29 18:47:30 +02:00
TSRMLS_FETCH();
zval *retval_ptr = NULL;
zval **params[2];
zval *buffer;
if (nread < 0) {
/* does this should be in user-land ? */
uv_shutdown_t* req;
/* Error or EOF */
assert(uv_last_error(uv_default_loop()).code == UV_EOF);
if (buf.base) {
efree(buf.base);
}
req = (uv_shutdown_t*) emalloc(sizeof *req);
2012-05-30 15:32:11 +02:00
uv_shutdown(req, (uv_stream_t*)handle, php_uv_shutdown_cb);
2012-05-29 18:47:30 +02:00
return;
}
if (nread == 0) {
/* Everything OK, but nothing read. */
efree(buf.base);
return;
}
php_uv_t *uv = (php_uv_t*)handle->data;
MAKE_STD_ZVAL(buffer);
ZVAL_STRINGL(buffer,buf.base,nread, 1);
zval *rsc;
MAKE_STD_ZVAL(rsc);
ZVAL_RESOURCE(rsc, uv->resource_id);
zend_list_addref(uv->resource_id);
params[0] = &buffer;
params[1] = &rsc;
php_uv_do_callback(&retval_ptr, uv->udp_recv_cb, params, 2 TSRMLS_CC);
zval_ptr_dtor(&buffer);
zval_ptr_dtor(&rsc);
zval_ptr_dtor(&retval_ptr);
if (buf.base) {
efree(buf.base);
}
}
2012-05-28 18:12:09 +02:00
static uv_buf_t php_uv_read_alloc(uv_handle_t* handle, size_t suggested_size)
{
return uv_buf_init(emalloc(suggested_size), suggested_size);
}
static void php_uv_close_cb(uv_handle_t *handle)
{
TSRMLS_FETCH();
zval *retval_ptr = NULL;
zval **params[1];
zval *h;
php_uv_t *uv = (php_uv_t*)handle->data;
2012-05-29 01:00:17 +02:00
if (uv->close_cb != NULL) {
MAKE_STD_ZVAL(h);
ZVAL_RESOURCE(h, uv->resource_id);
params[0] = &h;
2012-05-29 05:37:08 +02:00
php_uv_do_callback(&retval_ptr, uv->close_cb, params, 1 TSRMLS_CC);
2012-05-29 01:00:17 +02:00
zval_ptr_dtor(&retval_ptr);
/* for testing resource ref count.
{
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);
zend_list_delete(uv->resource_id);
} else {
printf("can't find");
}
2012-05-28 18:12:09 +02:00
}
2012-05-29 01:00:17 +02:00
*/
zval_ptr_dtor(&h); /* call destruct_uv */
} else {
zend_list_delete(uv->resource_id);
2012-05-28 18:12:09 +02:00
}
}
static void php_uv_idle_cb(uv_timer_t *handle, int status)
{
TSRMLS_FETCH();
zval *retval_ptr, *stat = NULL;
zval **params[1];
php_uv_t *uv = (php_uv_t*)handle->data;
MAKE_STD_ZVAL(stat);
ZVAL_LONG(stat, status);
params[0] = &stat;
2012-05-29 05:37:08 +02:00
php_uv_do_callback(&retval_ptr, uv->idle_cb, params, 1 TSRMLS_CC);
2012-05-28 18:12:09 +02:00
zval_ptr_dtor(&retval_ptr);
zval_ptr_dtor(&stat);
}
static void php_uv_getaddrinfo_cb(uv_getaddrinfo_t* handle, int status, struct addrinfo* res)
{
/* TODO */
efree(handle);
uv_freeaddrinfo(res);
}
static void php_uv_timer_cb(uv_timer_t *handle, int status)
{
TSRMLS_FETCH();
zval *retval_ptr, *stat, *client= NULL;
zval **params[2];
php_uv_t *uv = (php_uv_t*)handle->data;
MAKE_STD_ZVAL(stat);
ZVAL_LONG(stat, status);
MAKE_STD_ZVAL(client);
ZVAL_RESOURCE(client, uv->resource_id);
zend_list_addref(uv->resource_id);
params[0] = &stat;
params[1] = &client;
2012-05-29 05:37:08 +02:00
php_uv_do_callback(&retval_ptr, uv->timer_cb, params, 2 TSRMLS_CC);
2012-05-27 08:36:03 +02:00
2012-05-28 18:12:09 +02:00
zval_ptr_dtor(&retval_ptr);
zval_ptr_dtor(&stat);
zval_ptr_dtor(&client);
}
2012-05-28 18:12:09 +02:00
/* zend */
2012-05-22 19:43:11 +02:00
PHP_MINIT_FUNCTION(uv) {
php_uv_init(TSRMLS_C);
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);
uv_loop_handle = zend_register_list_destructors_ex(destruct_uv_loop, NULL, PHP_UV_LOOP_RESOURCE_NAME, module_number);
2012-05-28 18:12:09 +02:00
uv_sockaddr_handle = zend_register_list_destructors_ex(destruct_uv_sockaddr, NULL, PHP_UV_SOCKADDR_RESOURCE_NAME, module_number);
2012-05-22 19:43:11 +02:00
return SUCCESS;
}
ZEND_BEGIN_ARG_INFO_EX(arginfo_uv_run_once, 0, 0, 1)
ZEND_ARG_INFO(0, loop)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_uv_run, 0, 0, 1)
ZEND_ARG_INFO(0, loop)
ZEND_END_ARG_INFO()
2012-05-30 23:36:11 +02:00
ZEND_BEGIN_ARG_INFO_EX(arginfo_uv_loop_delete, 0, 0, 1)
ZEND_ARG_INFO(0, loop)
ZEND_END_ARG_INFO()
2012-05-30 23:39:58 +02:00
ZEND_BEGIN_ARG_INFO_EX(arginfo_uv_now, 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)
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-29 01:00:17 +02:00
ZEND_BEGIN_ARG_INFO_EX(arginfo_uv_strerror, 0, 0, 1)
ZEND_ARG_INFO(0, error)
ZEND_END_ARG_INFO()
2012-05-30 06:17:01 +02:00
ZEND_BEGIN_ARG_INFO_EX(arginfo_uv_err_name, 0, 0, 1)
ZEND_ARG_INFO(0, error)
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()
2012-05-28 05:37:56 +02:00
ZEND_BEGIN_ARG_INFO_EX(arginfo_uv_idle_stop, 0, 0, 1)
ZEND_ARG_INFO(0, idle)
ZEND_END_ARG_INFO()
2012-05-26 16:26:29 +02:00
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-28 05:29:15 +02:00
ZEND_BEGIN_ARG_INFO_EX(arginfo_uv_idle_start, 0, 0, 2)
ZEND_ARG_INFO(0, timer)
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_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-28 05:29:15 +02:00
ZEND_BEGIN_ARG_INFO_EX(arginfo_uv_idle_init, 0, 0, 0)
ZEND_ARG_INFO(0, loop)
ZEND_END_ARG_INFO()
2012-05-28 15:24:10 +02:00
ZEND_BEGIN_ARG_INFO_EX(arginfo_uv_loop_refcount, 0, 0, 1)
ZEND_ARG_INFO(0, loop)
ZEND_END_ARG_INFO()
2012-05-30 06:13:10 +02:00
ZEND_BEGIN_ARG_INFO_EX(arginfo_uv_update_time, 0, 0, 1)
ZEND_ARG_INFO(0, loop)
ZEND_END_ARG_INFO()
2012-05-28 15:24:10 +02:00
ZEND_BEGIN_ARG_INFO_EX(arginfo_uv_ref, 0, 0, 1)
ZEND_ARG_INFO(0, loop)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_uv_unref, 0, 0, 1)
ZEND_ARG_INFO(0, loop)
ZEND_END_ARG_INFO()
2012-05-28 15:54:54 +02:00
ZEND_BEGIN_ARG_INFO_EX(arginfo_uv_tcp_nodelay, 0, 0, 2)
2012-05-28 15:44:54 +02:00
ZEND_ARG_INFO(0, tcp)
2012-05-28 15:54:54 +02:00
ZEND_ARG_INFO(0, enabled)
2012-05-28 15:44:54 +02:00
ZEND_END_ARG_INFO()
2012-05-28 18:12:09 +02:00
ZEND_BEGIN_ARG_INFO_EX(arginfo_uv_ip4_addr, 0, 0, 2)
ZEND_ARG_INFO(0, address)
ZEND_ARG_INFO(0, port)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_ares_gethostbyname, 0, 0, 2)
ZEND_ARG_INFO(0, address)
ZEND_ARG_INFO(0, port)
ZEND_END_ARG_INFO()
2012-05-29 18:47:30 +02:00
ZEND_BEGIN_ARG_INFO_EX(arginfo_uv_udp_init, 0, 0, 1)
ZEND_ARG_INFO(0, loop)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_uv_udp_bind, 0, 0, 3)
ZEND_ARG_INFO(0, resource)
ZEND_ARG_INFO(0, address)
ZEND_ARG_INFO(0, port)
ZEND_ARG_INFO(0, flags)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_uv_udp_recv_start, 0, 0, 2)
ZEND_ARG_INFO(0, server)
ZEND_ARG_INFO(0, callback)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_uv_udp_recv_stop, 0, 0, 1)
ZEND_ARG_INFO(0, server)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_uv_udp_set_multicast_loop, 0, 0, 2)
ZEND_ARG_INFO(0, server)
ZEND_ARG_INFO(0, enabled)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_uv_udp_set_multicast_ttl, 0, 0, 2)
ZEND_ARG_INFO(0, server)
ZEND_ARG_INFO(0, ttl)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_uv_udp_set_broadcast, 0, 0, 2)
ZEND_ARG_INFO(0, server)
ZEND_ARG_INFO(0, enabled)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_uv_udp_send, 0, 0, 4)
ZEND_ARG_INFO(0, server)
ZEND_ARG_INFO(0, buffer)
ZEND_ARG_INFO(0, address)
ZEND_ARG_INFO(0, callback)
ZEND_END_ARG_INFO()
2012-05-28 18:12:09 +02:00
/* PHP Functions */
2012-05-29 00:35:55 +02:00
/* {{{ */
2012-05-28 15:24:10 +02:00
PHP_FUNCTION(uv_unref)
{
zval *z_loop = NULL;
uv_loop_t *loop;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
"z",&z_loop) == FAILURE) {
return;
}
ZEND_FETCH_RESOURCE(loop, uv_loop_t *, &z_loop, -1, PHP_UV_LOOP_RESOURCE_NAME, uv_loop_handle);
uv_unref(loop);
}
2012-05-29 00:35:55 +02:00
/* }}} */
2012-05-28 15:24:10 +02:00
2012-05-29 00:35:55 +02:00
/* {{{ */
2012-05-28 18:12:09 +02:00
PHP_FUNCTION(uv_last_error)
{
uv_loop_t *loop;
uv_err_t err;
zval *z_loop = NULL;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
"z",&z_loop) == FAILURE) {
return;
}
ZEND_FETCH_RESOURCE(loop, uv_loop_t *, &z_loop, -1, PHP_UV_LOOP_RESOURCE_NAME, uv_loop_handle);
err = uv_last_error(loop);
RETVAL_LONG(err.code);
}
2012-05-29 00:35:55 +02:00
/* }}} */
2012-05-28 15:24:10 +02:00
2012-05-30 06:17:01 +02:00
/* {{{ */
PHP_FUNCTION(uv_err_name)
{
long error_code;
2012-05-30 15:32:11 +02:00
const char *error_msg;
2012-05-30 06:17:01 +02:00
uv_err_t error;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
"l",&error_code) == FAILURE) {
return;
}
error.code = error_code;
error_msg = uv_err_name(error);
RETVAL_STRING(error_msg,1);
}
/* }}} */
2012-05-29 01:00:17 +02:00
/* {{{ */
PHP_FUNCTION(uv_strerror)
{
2012-05-30 06:09:47 +02:00
long error_code;
2012-05-30 15:32:11 +02:00
const char *error_msg;
2012-05-30 06:09:47 +02:00
uv_err_t error;
2012-05-29 01:00:17 +02:00
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
2012-05-30 06:09:47 +02:00
"l",&error_code) == FAILURE) {
2012-05-29 01:00:17 +02:00
return;
}
2012-05-30 06:09:47 +02:00
error.code = error_code;
2012-05-29 01:00:17 +02:00
2012-05-30 15:32:11 +02:00
error_msg = uv_strerror(error);
2012-05-30 06:09:47 +02:00
RETVAL_STRING(error_msg,1);
2012-05-29 01:00:17 +02:00
}
/* }}} */
2012-05-30 06:13:10 +02:00
/* {{{ */
PHP_FUNCTION(uv_update_time)
{
zval *z_loop = NULL;
uv_loop_t *loop;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
"z",&z_loop) == FAILURE) {
return;
}
ZEND_FETCH_RESOURCE(loop, uv_loop_t *, &z_loop, -1, PHP_UV_LOOP_RESOURCE_NAME, uv_loop_handle);
2012-05-30 15:32:11 +02:00
uv_update_time(loop);
2012-05-30 06:13:10 +02:00
}
/* }}} */
2012-05-29 00:35:55 +02:00
/* {{{ */
2012-05-28 15:24:10 +02:00
PHP_FUNCTION(uv_ref)
{
zval *z_loop = NULL;
uv_loop_t *loop;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
"z",&z_loop) == FAILURE) {
return;
}
ZEND_FETCH_RESOURCE(loop, uv_loop_t *, &z_loop, -1, PHP_UV_LOOP_RESOURCE_NAME, uv_loop_handle);
uv_ref(loop);
}
2012-05-29 00:35:55 +02:00
/* }}} */
2012-05-28 15:24:10 +02:00
2012-05-29 00:35:55 +02:00
/* {{{ */
2012-05-28 15:24:10 +02:00
PHP_FUNCTION(uv_loop_refcount)
{
zval *z_loop = NULL;
uv_loop_t *loop;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
"z",&z_loop) == FAILURE) {
return;
}
ZEND_FETCH_RESOURCE(loop, uv_loop_t *, &z_loop, -1, PHP_UV_LOOP_RESOURCE_NAME, uv_loop_handle);
RETURN_LONG(uv_loop_refcount(loop));
}
2012-05-29 00:35:55 +02:00
/* }}} */
2012-05-28 15:24:10 +02:00
2012-05-29 00:35:55 +02:00
/* {{{ */
PHP_FUNCTION(uv_run)
{
zval *z_loop = NULL;
uv_loop_t *loop;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
"|z",&z_loop) == FAILURE) {
return;
}
if (z_loop != NULL) {
ZEND_FETCH_RESOURCE(loop, uv_loop_t *, &z_loop, -1, PHP_UV_LOOP_RESOURCE_NAME, uv_loop_handle);
} else {
loop = php_uv_default_loop();
}
uv_run(loop);
}
2012-05-29 00:35:55 +02:00
/* }}} */
2012-05-29 00:35:55 +02:00
/* {{{ */
PHP_FUNCTION(uv_run_once)
{
zval *z_loop = NULL;
uv_loop_t *loop;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
"|z",&z_loop) == FAILURE) {
return;
}
if (z_loop != NULL) {
ZEND_FETCH_RESOURCE(loop, uv_loop_t *, &z_loop, -1, PHP_UV_LOOP_RESOURCE_NAME, uv_loop_handle);
} else {
loop = php_uv_default_loop();
}
2012-05-28 18:12:09 +02:00
uv_run_once(loop);
}
2012-05-29 00:35:55 +02:00
/* }}} */
2012-05-28 18:12:09 +02:00
2012-05-30 23:36:11 +02:00
/* {{{ */
PHP_FUNCTION(uv_loop_delete)
{
zval *z_loop = NULL;
uv_loop_t *loop;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
"z",&z_loop) == FAILURE) {
return;
}
if (z_loop != NULL) {
ZEND_FETCH_RESOURCE(loop, uv_loop_t *, &z_loop, -1, PHP_UV_LOOP_RESOURCE_NAME, uv_loop_handle);
uv_loop_delete(loop);
}
}
/* }}} */
2012-05-30 23:39:58 +02:00
/* {{{ */
PHP_FUNCTION(uv_now)
{
zval *z_loop = NULL;
uv_loop_t *loop;
int64_t now;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
"z",&z_loop) == FAILURE) {
return;
}
if (z_loop != NULL) {
ZEND_FETCH_RESOURCE(loop, uv_loop_t *, &z_loop, -1, PHP_UV_LOOP_RESOURCE_NAME, uv_loop_handle);
now = uv_now(loop);
RETURN_LONG((long)now);
}
}
/* }}} */
2012-05-30 23:36:11 +02:00
2012-05-29 00:35:55 +02:00
/* {{{ */
2012-05-28 18:12:09 +02:00
PHP_FUNCTION(uv_tcp_bind)
{
zval *resource;
char *address;
int address_len;
long port = 8080;
struct sockaddr_in addr;
php_uv_t *uv;
int r;
2012-05-27 15:28:26 +02:00
2012-05-28 18:12:09 +02:00
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
"zsl",&resource, &address, &address_len, &port) == FAILURE) {
2012-05-27 15:28:26 +02:00
return;
}
2012-05-28 18:12:09 +02:00
ZEND_FETCH_RESOURCE(uv, php_uv_t *, &resource, -1, PHP_UV_RESOURCE_NAME, uv_resource_handle);
addr = uv_ip4_addr(address, port);
2012-05-25 01:07:12 +02:00
2012-05-28 18:12:09 +02:00
r = uv_tcp_bind((uv_tcp_t*)&uv->uv.tcp, addr);
if (r) {
php_error_docref(NULL TSRMLS_CC, E_ERROR, "bind failed");
2012-05-28 18:12:09 +02:00
}
}
2012-05-29 00:35:55 +02:00
/* }}} */
2012-05-29 00:35:55 +02:00
/* {{{ */
2012-05-28 18:12:09 +02:00
PHP_FUNCTION(uv_write)
{
zval *z_cli,*callback;
char *data;
int data_len = 0;
php_uv_t *client;
write_req_t *w;
2012-05-26 15:03:21 +02:00
2012-05-28 18:12:09 +02:00
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
"zsz",&z_cli, &data, &data_len,&callback) == FAILURE) {
return;
}
2012-05-26 15:03:21 +02:00
2012-05-28 18:12:09 +02:00
ZEND_FETCH_RESOURCE(client, php_uv_t *, &z_cli, -1, PHP_UV_RESOURCE_NAME, uv_resource_handle);
zend_list_addref(client->resource_id);
Z_ADDREF_P(callback);
client->write_cb = callback;
2012-05-27 16:13:09 +02:00
2012-05-28 18:12:09 +02:00
w = emalloc(sizeof(write_req_t));
w->req.data = client;
w->buf = uv_buf_init(data, data_len);
2012-05-30 15:32:11 +02:00
uv_write(&w->req, (uv_stream_t*)&client->uv.tcp, &w->buf, 1, php_uv_write_cb);
2012-05-24 06:07:52 +02:00
}
2012-05-29 00:35:55 +02:00
/* }}} */
2012-05-23 19:31:16 +02:00
2012-05-29 00:35:55 +02:00
/* {{{ */
2012-05-28 18:12:09 +02:00
PHP_FUNCTION(uv_tcp_nodelay)
2012-05-24 16:29:54 +02:00
{
2012-05-28 18:12:09 +02:00
zval *z_cli;
php_uv_t *client;
long bval = 1;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
"zl",&z_cli, &bval) == FAILURE) {
return;
}
ZEND_FETCH_RESOURCE(client, php_uv_t *, &z_cli, -1, PHP_UV_RESOURCE_NAME, uv_resource_handle);
2012-05-30 15:32:11 +02:00
uv_tcp_nodelay(&client->uv.tcp, bval);
2012-05-24 06:07:52 +02:00
}
2012-05-29 00:35:55 +02:00
/* }}} */
2012-05-24 06:07:52 +02:00
2012-05-29 00:35:55 +02:00
/* {{{ */
2012-05-28 18:12:09 +02:00
PHP_FUNCTION(uv_accept)
2012-05-26 15:54:43 +02:00
{
2012-05-28 18:12:09 +02:00
zval *z_svr,*z_cli;
php_uv_t *server, *client;
int r;
2012-05-26 15:54:43 +02:00
2012-05-28 18:12:09 +02:00
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
"zz",&z_svr, &z_cli) == FAILURE) {
return;
}
2012-05-26 15:54:43 +02:00
2012-05-28 18:12:09 +02:00
ZEND_FETCH_RESOURCE(server, php_uv_t *, &z_svr, -1, PHP_UV_RESOURCE_NAME, uv_resource_handle);
ZEND_FETCH_RESOURCE(client, php_uv_t *, &z_cli, -1, PHP_UV_RESOURCE_NAME, uv_resource_handle);
2012-05-26 15:54:43 +02:00
2012-05-28 18:12:09 +02:00
r = uv_accept((uv_stream_t *)&server->uv.tcp, (uv_stream_t *)&client->uv.tcp);
if (r) {
php_error_docref(NULL TSRMLS_CC, E_NOTICE, "accept");
}
2012-05-26 15:54:43 +02:00
}
2012-05-29 00:35:55 +02:00
/* }}} */
2012-05-26 15:54:43 +02:00
2012-05-29 00:35:55 +02:00
/* {{{ */
2012-05-26 15:54:43 +02:00
PHP_FUNCTION(uv_close)
{
2012-05-29 01:00:17 +02:00
zval *client, *callback = NULL;
2012-05-26 15:54:43 +02:00
php_uv_t *uv;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
2012-05-29 01:00:17 +02:00
"r|z",&client, &callback) == FAILURE) {
2012-05-26 15:54:43 +02:00
return;
}
ZEND_FETCH_RESOURCE(uv, php_uv_t *, &client, -1, PHP_UV_RESOURCE_NAME, uv_resource_handle);
2012-05-29 01:00:17 +02:00
if (callback != NULL) {
Z_ADDREF_P(callback);
uv->close_cb = callback;
}
2012-05-29 18:47:30 +02:00
if (uv->type == IS_UV_TCP) {
2012-05-30 15:32:11 +02:00
uv_close((uv_handle_t*)&uv->uv.tcp, (uv_close_cb)php_uv_close_cb);
2012-05-29 18:47:30 +02:00
} else if(uv->type == IS_UV_UDP) {
2012-05-30 15:32:11 +02:00
uv_close((uv_handle_t*)&uv->uv.udp, (uv_close_cb)php_uv_close_cb);
2012-05-29 18:47:30 +02:00
}
2012-05-26 15:54:43 +02:00
}
2012-05-29 00:35:55 +02:00
/* }}} */
2012-05-26 15:54:43 +02:00
2012-05-29 00:35:55 +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;
int r;
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-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);
zend_list_addref(uv->resource_id);
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;
r = uv_read_start((uv_stream_t*)&uv->uv.tcp, php_uv_read_alloc, php_uv_read_cb);
if (r) {
php_error_docref(NULL TSRMLS_CC, E_NOTICE, "read failed");
}
2012-05-23 19:31:16 +02:00
}
2012-05-29 00:35:55 +02:00
/* }}} */
2012-05-23 19:31:16 +02:00
2012-05-29 00:35:55 +02:00
/* {{{ */
2012-05-28 18:12:09 +02:00
PHP_FUNCTION(uv_ip4_addr)
{
char *address;
int address_len = 0;
long port = 0;
php_uv_sockaddr_t *sockaddr;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
"sl",&address, &address_len, &port) == FAILURE) {
return;
}
sockaddr = (php_uv_sockaddr_t*)emalloc(sizeof(php_uv_sockaddr_t));
sockaddr->is_ipv4 = 1;
sockaddr->addr.ipv4 = uv_ip4_addr(address, port);
ZEND_REGISTER_RESOURCE(return_value, sockaddr, uv_sockaddr_handle);
sockaddr->resource_id = Z_LVAL_P(return_value);
}
2012-05-29 00:35:55 +02:00
/* }}} */
2012-05-28 18:12:09 +02:00
2012-05-29 00:35:55 +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;
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) {
php_error_docref(NULL TSRMLS_CC, E_ERROR, "listen failed");
2012-05-27 06:48:10 +02:00
}
}
2012-05-29 00:35:55 +02:00
/* }}} */
2012-05-29 00:35:55 +02:00
/* {{{ */
PHP_FUNCTION(uv_tcp_connect)
{
2012-05-28 18:12:09 +02:00
zval *resource,*address, *callback;
php_uv_t *uv;
2012-05-28 18:12:09 +02:00
php_uv_sockaddr_t *addr;
uv_connect_t *req;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
2012-05-28 18:12:09 +02:00
"zzz",&resource,&address, &callback) == FAILURE) {
return;
}
ZEND_FETCH_RESOURCE(uv, php_uv_t *, &resource, -1, PHP_UV_RESOURCE_NAME, uv_resource_handle);
2012-05-28 18:12:09 +02:00
ZEND_FETCH_RESOURCE(addr, php_uv_sockaddr_t *, &address, -1, PHP_UV_SOCKADDR_RESOURCE_NAME, uv_sockaddr_handle);
2012-05-28 15:55:58 +02:00
Z_ADDREF_P(callback);
2012-05-28 18:12:09 +02:00
Z_ADDREF_P(address);
2012-05-28 15:55:58 +02:00
2012-05-28 18:12:09 +02:00
req = (uv_connect_t*)emalloc(sizeof(uv_connect_t));
2012-05-23 13:41:04 +02:00
2012-05-28 18:12:09 +02:00
req->data = uv;
uv->address = address;
uv->connect_cb = callback;
uv_tcp_connect(req, &uv->uv.tcp, addr->addr.ipv4, php_uv_tcp_connect_cb);
}
2012-05-29 00:35:55 +02:00
/* }}} */
2012-05-29 00:35:55 +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) {
php_error_docref(NULL TSRMLS_CC, E_ERROR, "uv_timer_init failed");
2012-05-26 16:26:29 +02:00
return;
}
uv->uv.timer.data = uv;
2012-05-28 18:12:09 +02:00
PHP_UV_INIT_ZVALS(uv)
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);
}
2012-05-29 00:35:55 +02:00
/* }}} */
2012-05-26 16:26:29 +02:00
2012-05-29 00:35:55 +02:00
/* {{{ */
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-29 00:35:55 +02:00
/* }}} */
2012-05-26 16:26:29 +02:00
2012-05-29 00:35:55 +02:00
/* {{{ */
2012-05-28 05:29:15 +02:00
PHP_FUNCTION(uv_idle_start)
{
2012-05-28 05:37:56 +02:00
zval *idle, *callback;
2012-05-28 05:29:15 +02:00
php_uv_t *uv;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
2012-05-28 05:37:56 +02:00
"rz",&idle, &callback) == FAILURE) {
2012-05-28 05:29:15 +02:00
return;
}
2012-05-28 05:37:56 +02:00
ZEND_FETCH_RESOURCE(uv, php_uv_t *, &idle, -1, PHP_UV_RESOURCE_NAME, uv_resource_handle);
2012-05-28 05:29:15 +02:00
Z_ADDREF_P(callback);
2012-05-28 05:37:56 +02:00
zend_list_addref(uv->resource_id);
2012-05-29 13:10:46 +02:00
if (uv->idle_cb) {
zval_ptr_dtor(&uv->idle_cb);
uv->idle_cb = NULL;
}
2012-05-28 05:29:15 +02:00
uv->idle_cb = callback;
2012-05-30 15:32:11 +02:00
uv_idle_start((uv_idle_t*)&uv->uv.idle, (uv_idle_cb)php_uv_idle_cb);
2012-05-28 05:29:15 +02:00
}
2012-05-29 00:35:55 +02:00
/* }}} */
2012-05-28 05:29:15 +02:00
2012-05-29 00:35:55 +02:00
/* {{{ */
2012-05-28 18:12:09 +02:00
PHP_FUNCTION(uv_getaddrinfo)
{
zval *z_loop, *callback = NULL;
uv_loop_t *loop;
php_uv_t *uv;
uv_getaddrinfo_t *handle = (uv_getaddrinfo_t*)emalloc(sizeof(uv_getaddrinfo_t));
/* FIXME: hints */
char *node, *service, *hints;
int node_len, service_len, hints_len = 0;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
"zzsss",&z_loop, &callback, &node, &node_len, &service, &service_len, &hints, &hints_len) == FAILURE) {
return;
}
ZEND_FETCH_RESOURCE(loop, uv_loop_t *, &z_loop, -1, PHP_UV_LOOP_RESOURCE_NAME, uv_loop_handle);
Z_ADDREF_P(callback);
uv->getaddr_cb = callback;
uv_getaddrinfo(loop, handle, php_uv_getaddrinfo_cb, node, service, NULL);
}
2012-05-29 00:35:55 +02:00
/* }}} */
2012-05-28 05:29:15 +02:00
2012-05-29 00:35:55 +02:00
/* {{{ */
2012-05-28 05:37:56 +02:00
PHP_FUNCTION(uv_idle_stop)
{
zval *idle;
php_uv_t *uv;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
"r", &idle) == FAILURE) {
return;
}
ZEND_FETCH_RESOURCE(uv, php_uv_t *, &idle, -1, PHP_UV_RESOURCE_NAME, uv_resource_handle);
2012-05-30 15:32:11 +02:00
uv_idle_stop((uv_idle_t*)&uv->uv.idle);
2012-05-30 15:34:14 +02:00
zend_list_delete(uv->resource_id);
2012-05-28 05:37:56 +02:00
}
2012-05-29 00:35:55 +02:00
/* }}} */
2012-05-28 05:37:56 +02:00
2012-05-29 00:35:55 +02:00
/* {{{ */
PHP_FUNCTION(uv_tcp_init)
{
2012-05-23 13:41:04 +02:00
int r;
/* TODO */
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) {
php_error_docref(NULL TSRMLS_CC, E_ERROR, "uv_tcp_init emalloc failed");
2012-05-27 07:43:27 +02:00
return;
}
2012-05-24 16:29:54 +02:00
2012-05-29 18:47:30 +02:00
uv->type = IS_UV_TCP;
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) {
php_error_docref(NULL TSRMLS_CC, E_ERROR, "uv_tcp_init failed");
return;
}
2012-05-27 07:43:27 +02:00
2012-05-26 15:59:59 +02:00
uv->uv.tcp.data = uv;
2012-05-28 18:12:09 +02:00
PHP_UV_INIT_ZVALS(uv)
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-29 00:35:55 +02:00
/* }}} */
/* {{{ */
2012-05-28 05:29:15 +02:00
PHP_FUNCTION(uv_idle_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_idle_init(uv_default_loop(), &uv->uv.idle);
if (r) {
php_error_docref(NULL TSRMLS_CC, E_ERROR, "uv_idle_init failed");
2012-05-28 05:29:15 +02:00
return;
}
uv->uv.timer.data = uv;
2012-05-28 18:12:09 +02:00
PHP_UV_INIT_ZVALS(uv)
2012-05-28 05:29:15 +02:00
ZEND_REGISTER_RESOURCE(return_value, uv, uv_resource_handle);
uv->resource_id = Z_LVAL_P(return_value);
}
2012-05-29 00:35:55 +02:00
/* }}} */
2012-05-28 05:29:15 +02:00
2012-05-29 00:35:55 +02:00
/* {{{ */
PHP_FUNCTION(uv_default_loop)
{
ZEND_REGISTER_RESOURCE(return_value, php_uv_default_loop(), uv_loop_handle);
}
2012-05-29 00:35:55 +02:00
/* }}} */
2012-05-29 18:47:30 +02:00
/* {{{ */
PHP_FUNCTION(uv_udp_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));
if (!uv) {
php_error_docref(NULL TSRMLS_CC, E_ERROR, "uv_udp_init emalloc failed");
return;
}
uv->type = IS_UV_UDP;
r = uv_udp_init(uv_default_loop(), &uv->uv.udp);
if (r) {
php_error_docref(NULL TSRMLS_CC, E_ERROR, "uv_udp_init failed");
return;
}
uv->uv.udp.data = uv;
PHP_UV_INIT_ZVALS(uv)
ZEND_REGISTER_RESOURCE(return_value, uv, uv_resource_handle);
uv->resource_id = Z_LVAL_P(return_value);
}
/* }}} */
/* {{{ */
PHP_FUNCTION(uv_udp_bind)
{
zval *resource;
char *address;
int address_len;
long port = 0;
long flags = 0;
struct sockaddr_in addr;
php_uv_t *uv;
int r;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
"zsl|l",&resource, &address, &address_len, &port, &flags) == FAILURE) {
return;
}
ZEND_FETCH_RESOURCE(uv, php_uv_t *, &resource, -1, PHP_UV_RESOURCE_NAME, uv_resource_handle);
addr = uv_ip4_addr(address, port);
2012-05-30 15:32:11 +02:00
r = uv_udp_bind((uv_udp_t*)&uv->uv.udp, addr, flags);
2012-05-29 18:47:30 +02:00
if (r) {
php_error_docref(NULL TSRMLS_CC, E_ERROR, "uv_udp_bind failed");
}
}
/* }}} */
/* {{{ */
PHP_FUNCTION(uv_udp_recv_start)
{
zval *client, *callback;
php_uv_t *uv;
int r;
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);
zend_list_addref(uv->resource_id);
uv->udp_recv_cb = callback;
uv->uv.udp.data = uv;
2012-05-30 15:32:11 +02:00
r = uv_udp_recv_start((uv_udp_t*)&uv->uv.udp, php_uv_read_alloc, php_uv_udp_recv_cb);
2012-05-29 18:47:30 +02:00
if (r) {
php_error_docref(NULL TSRMLS_CC, E_NOTICE, "read failed");
}
}
/* }}} */
/* {{{ */
PHP_FUNCTION(uv_udp_recv_stop)
{
zval *client;
php_uv_t *uv;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
"r", &client) == FAILURE) {
return;
}
ZEND_FETCH_RESOURCE(uv, php_uv_t *, &client, -1, PHP_UV_RESOURCE_NAME, uv_resource_handle);
zend_list_delete(uv->resource_id);
2012-05-30 15:32:11 +02:00
uv_udp_recv_stop((uv_udp_t*)&uv->uv.udp);
2012-05-29 18:47:30 +02:00
}
/* }}} */
/* {{{ */
PHP_FUNCTION(uv_udp_set_multicast_loop)
{
zval *client;
php_uv_t *uv;
long enabled = 0;
int r;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
"rl",&client, &enabled) == FAILURE) {
return;
}
ZEND_FETCH_RESOURCE(uv, php_uv_t *, &client, -1, PHP_UV_RESOURCE_NAME, uv_resource_handle);
2012-05-30 15:32:11 +02:00
r = uv_udp_set_multicast_loop((uv_udp_t*)&uv->uv.udp, enabled);
2012-05-29 18:47:30 +02:00
if (r) {
php_error_docref(NULL TSRMLS_CC, E_NOTICE, "uv_udp_set_muticast_loop failed");
}
}
/* }}} */
/* {{{ */
PHP_FUNCTION(uv_udp_set_multicast_ttl)
{
zval *client;
php_uv_t *uv;
long ttl = 0; /* 1 through 255 */
int r;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
"rl",&client, &ttl) == FAILURE) {
return;
}
ZEND_FETCH_RESOURCE(uv, php_uv_t *, &client, -1, PHP_UV_RESOURCE_NAME, uv_resource_handle);
2012-05-30 15:32:11 +02:00
r = uv_udp_set_multicast_ttl((uv_udp_t*)&uv->uv.udp, ttl);
2012-05-29 18:47:30 +02:00
if (r) {
php_error_docref(NULL TSRMLS_CC, E_NOTICE, "uv_udp_set_muticast_ttl failed");
}
}
/* }}} */
/* {{{ */
PHP_FUNCTION(uv_udp_set_broadcast)
{
zval *client;
php_uv_t *uv;
long enabled = 0;
int r;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
"rl",&client, &enabled) == FAILURE) {
return;
}
ZEND_FETCH_RESOURCE(uv, php_uv_t *, &client, -1, PHP_UV_RESOURCE_NAME, uv_resource_handle);
2012-05-30 15:32:11 +02:00
r = uv_udp_set_broadcast((uv_udp_t*)&uv->uv.udp, enabled);
2012-05-29 18:47:30 +02:00
if (r) {
php_error_docref(NULL TSRMLS_CC, E_NOTICE, "uv_udp_set_muticast_loop failed");
}
}
/* }}} */
/* {{{ */
PHP_FUNCTION(uv_udp_send)
{
zval *z_cli,*z_addr, *callback;
char *data;
int data_len = 0;
php_uv_t *client;
send_req_t *w;
php_uv_sockaddr_t *addr;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
"zszz",&z_cli, &data, &data_len, &z_addr, &callback) == FAILURE) {
return;
}
ZEND_FETCH_RESOURCE(client, php_uv_t *, &z_cli, -1, PHP_UV_RESOURCE_NAME, uv_resource_handle);
ZEND_FETCH_RESOURCE(addr, php_uv_sockaddr_t *, &z_addr, -1, PHP_UV_SOCKADDR_RESOURCE_NAME, uv_sockaddr_handle);
zend_list_addref(client->resource_id);
Z_ADDREF_P(callback);
client->udp_send_cb = callback;
w = emalloc(sizeof(send_req_t));
w->req.data = client;
w->buf = uv_buf_init(estrndup(data,data_len), data_len);
uv_udp_send(&w->req, &client->uv.udp, &w->buf, 1, addr->addr.ipv4, php_uv_udp_send_cb);
}
/* }}} */
static zend_function_entry uv_functions[] = {
2012-05-29 00:31:25 +02:00
/* general */
2012-05-30 06:13:10 +02:00
PHP_FE(uv_update_time, arginfo_uv_update_time)
2012-05-28 15:24:10 +02:00
PHP_FE(uv_ref, arginfo_uv_ref)
PHP_FE(uv_unref, arginfo_uv_unref)
PHP_FE(uv_default_loop, NULL)
PHP_FE(uv_run, arginfo_uv_run)
PHP_FE(uv_run_once, arginfo_uv_run_once)
2012-05-29 00:31:25 +02:00
PHP_FE(uv_ip4_addr, arginfo_uv_ip4_addr)
PHP_FE(uv_write, arginfo_uv_write)
PHP_FE(uv_close, arginfo_uv_close)
2012-05-30 23:39:58 +02:00
PHP_FE(uv_now, arginfo_uv_now)
2012-05-30 23:36:11 +02:00
PHP_FE(uv_loop_delete, arginfo_uv_loop_delete)
2012-05-29 00:31:25 +02:00
PHP_FE(uv_read_start, arginfo_uv_read_start)
PHP_FE(uv_last_error, arginfo_uv_last_error)
2012-05-30 06:17:01 +02:00
PHP_FE(uv_err_name, arginfo_uv_err_name)
2012-05-29 01:00:17 +02:00
PHP_FE(uv_strerror, arginfo_uv_strerror)
2012-05-29 00:31:25 +02:00
/* idle */
2012-05-28 05:29:15 +02:00
PHP_FE(uv_idle_init, arginfo_uv_idle_init)
PHP_FE(uv_idle_start, arginfo_uv_idle_start)
2012-05-28 05:37:56 +02:00
PHP_FE(uv_idle_stop, arginfo_uv_idle_stop)
2012-05-29 00:31:25 +02:00
/* timer */
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-29 00:31:25 +02:00
/* tcp */
PHP_FE(uv_tcp_init, arginfo_uv_tcp_init)
2012-05-28 15:44:54 +02:00
PHP_FE(uv_tcp_nodelay, arginfo_uv_tcp_nodelay)
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)
PHP_FE(uv_tcp_connect, arginfo_uv_tcp_connect)
2012-05-29 18:47:30 +02:00
/* udp */
PHP_FE(uv_udp_init, arginfo_uv_udp_init)
PHP_FE(uv_udp_bind, arginfo_uv_udp_bind)
PHP_FE(uv_udp_set_multicast_loop, arginfo_uv_udp_set_multicast_loop)
PHP_FE(uv_udp_set_multicast_ttl, arginfo_uv_udp_set_multicast_ttl)
PHP_FE(uv_udp_send, arginfo_uv_udp_send)
PHP_FE(uv_udp_recv_start, arginfo_uv_udp_recv_start)
PHP_FE(uv_udp_recv_stop, arginfo_uv_udp_recv_stop)
2012-05-29 00:31:25 +02:00
/* for debug */
2012-05-28 15:24:10 +02:00
PHP_FE(uv_loop_refcount, arginfo_uv_loop_refcount)
2012-05-28 18:12:09 +02:00
/* c-ares */
2012-05-29 00:31:25 +02:00
PHP_FE(uv_getaddrinfo, arginfo_uv_tcp_connect)
2012-05-28 18:12:09 +02:00
/* PHP_FE(ares_gethostbyname, arginfo_ares_gethostbyname) */
{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",
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