diff --git a/config.m4 b/config.m4 index f1192d5..5a76d28 100644 --- a/config.m4 +++ b/config.m4 @@ -7,7 +7,7 @@ if test $PHP_UV != "no"; then PHP_CHECK_LIBRARY(uv, uv_run, [ AC_MSG_RESULT(found) PHP_ADD_LIBRARY_WITH_PATH(uv, libuv, MRUBY_SHARED_LIBADD) - PHP_ADD_INCLUDE([$ext_srcdir/uv/include]) + PHP_ADD_INCLUDE([$ext_srcdir/libuv/include]) ],[ AC_MSG_RESULT([not found]) AC_MSG_ERROR([Please make libuv first]) @@ -18,6 +18,6 @@ if test $PHP_UV != "no"; then PHP_SUBST(UV_SHARED_LIBADD) - CFLAGS=" -Wunused-variable -Wpointer-sign -Wimplicit-function-declaration" + CFLAGS=" -Wunused-variable -Wpointer-sign -Wimplicit-function-declaration -Wl,libuv/uv.a" PHP_SUBST([CFLAGS]) fi diff --git a/php_uv.c b/php_uv.c index 706104c..6e86d11 100644 --- a/php_uv.c +++ b/php_uv.c @@ -22,15 +22,105 @@ extern void php_uv_init(TSRMLS_D); extern zend_class_entry *uv_class_entry; +static int uv_resource_handle; + void php_uv_init(TSRMLS_D); +static + +void static destruct_uv(zend_rsrc_list_entry *rsrc TSRMLS_DC) +{ + php_uv_t *obj = (php_uv_t *)rsrc->ptr; + + // todo + //if (Z_TYPE_P(cons->car) == IS_RESOURCE) { + // zend_list_delete(Z_RESVAL_P(cons->car)); +//} + + efree(obj); +} + 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); return SUCCESS; } +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() + + +ZEND_BEGIN_ARG_INFO_EX(arginfo_uv_tcp_init, 0, 0, 1) + ZEND_ARG_INFO(0, addrress) + ZEND_ARG_INFO(0, port) + ZEND_ARG_INFO(0, loop) +ZEND_END_ARG_INFO() + + +PHP_FUNCTION(uv_run) +{ + uv_run(uv_default_loop()); +} + +static void php_uv_tcp_connect_cb(uv_connect_t *conn_req, int status) +{ + fprintf(stderr,"hello"); +} + +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); + uv_tcp_connect(&uv->connect, &uv->socket, uv->addr, php_uv_tcp_connect_cb); +} + +PHP_FUNCTION(uv_tcp_init) +{ + char *address; + int address_len; + long port; + zval *loop; + + php_uv_t *uv; + + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, + "s|l|z",&address, &address_len, &port, &loop) == FAILURE) { + return; + } + + uv = ecalloc(1,sizeof(php_uv_t)); + uv->addr = uv_ip4_addr(address,port); + uv_tcp_init(uv_default_loop(),&uv->socket); + + ZEND_REGISTER_RESOURCE(return_value, uv, uv_resource_handle); +} + +static zend_function_entry uv_functions[] = { + PHP_FE(uv_run, arginfo_uv_run) + PHP_FE(uv_tcp_init, arginfo_uv_tcp_init) + PHP_FE(uv_tcp_connect, arginfo_uv_tcp_connect) + {NULL, NULL, NULL} +}; + PHP_MINFO_FUNCTION(uv) { @@ -42,7 +132,7 @@ zend_module_entry uv_module_entry = { STANDARD_MODULE_HEADER, #endif "uv", - NULL, /* Functions */ + uv_functions, /* Functions */ PHP_MINIT(uv), /* MINIT */ NULL, /* MSHUTDOWN */ NULL, /* RINIT */ diff --git a/php_uv.h b/php_uv.h index 9c4ac7d..1f990a3 100644 --- a/php_uv.h +++ b/php_uv.h @@ -10,6 +10,7 @@ #endif #include "php.h" +#include "uv.h" #include "ext/spl/spl_exceptions.h" #include "zend_interfaces.h" @@ -22,4 +23,13 @@ extern zend_module_entry uv_module_entry; extern zend_class_entry *uv_class_entry; + +typedef struct { + struct sockaddr_in addr; + uv_connect_t connect; + uv_tcp_t socket; +} php_uv_t; + +#define PHP_UV_RESOURCE_NAME "uv" + #endif /* PHP_UV_H */ diff --git a/uv.c b/uv.c index bcf4d33..4dbd990 100644 --- a/uv.c +++ b/uv.c @@ -26,12 +26,14 @@ static zend_function_entry php_uv_methods[] = { {NULL, NULL, NULL} }; -static php_uv_class_init(TSRMLS_D) +static int php_uv_class_init(TSRMLS_D) { zend_class_entry ce; INIT_CLASS_ENTRY(ce, "UV", php_uv_methods); uv_class_entry = zend_register_internal_class(&ce TSRMLS_CC); //uv_class_entry->create_object = php_uv_new; + + return 0; } void php_uv_init(TSRMLS_D)