add uv_tcp_bind and uv_listen

This commit is contained in:
Shuhei Tanuma 2012-05-23 20:41:04 +09:00
parent 9d3bde8942
commit 568c29dd34
2 changed files with 85 additions and 13 deletions

View File

@ -58,13 +58,25 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_uv_tcp_connect, 0, 0, 2)
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_BEGIN_ARG_INFO_EX(arginfo_uv_tcp_init, 0, 0, 0)
ZEND_ARG_INFO(0, loop)
ZEND_END_ARG_INFO()
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()
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()
PHP_FUNCTION(uv_run)
{
uv_run(uv_default_loop());
@ -72,9 +84,61 @@ PHP_FUNCTION(uv_run)
static void php_uv_tcp_connect_cb(uv_connect_t *conn_req, int status)
{
fprintf(stderr,"hello");
fprintf(stderr,"status: %d\n", status);
}
PHP_FUNCTION(uv_tcp_bind)
{
zval *resource;
char *address;
int address_len;
long port = 8080;
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);
memset(&uv->addr,'\0',sizeof(struct sockaddr_in));
uv->addr = uv_ip4_addr(address, port);
r = uv_tcp_bind((uv_handle_t*)&uv->socket, uv->addr);
if (r) {
fprintf(stderr,"bind error %d\n", r);
}
}
static void php_uv_listen_cb(uv_stream_t* stream, int status)
{
fprintf(stderr,"status; %d\n",status);
}
PHP_FUNCTION(uv_listen)
{
zval *resource;
long backlog = SOMAXCONN;
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,
"zlf",&resource, &backlog, &fci, &fci_cache) == FAILURE) {
return;
}
ZEND_FETCH_RESOURCE(uv, php_uv_t *, &resource, -1, PHP_UV_RESOURCE_NAME, uv_resource_handle);
uv_listen((uv_stream_t*)&uv->socket, backlog, php_uv_listen_cb);
}
PHP_FUNCTION(uv_tcp_connect)
{
zval *resource;
@ -90,26 +154,30 @@ PHP_FUNCTION(uv_tcp_connect)
}
ZEND_FETCH_RESOURCE(uv, php_uv_t *, &resource, -1, PHP_UV_RESOURCE_NAME, uv_resource_handle);
memcpy(&uv->fci_connect, &fci, sizeof(zend_fcall_info));
memcpy(&uv->fcc_connect, &fci_cache, sizeof(zend_fcall_info_cache));
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;
int r;
/* TODO */
zval *loop;
php_uv_t *uv;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
"s|l|z",&address, &address_len, &port, &loop) == FAILURE) {
"|z",&loop) == FAILURE) {
return;
}
uv = (php_uv_t *)ecalloc(1,sizeof(php_uv_t));
r = uv_tcp_init(uv_default_loop(), &uv->socket);
if (r) {
fprintf(stderr, "Socket creation error\n");
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);
}
@ -117,6 +185,8 @@ PHP_FUNCTION(uv_tcp_init)
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_bind, arginfo_uv_tcp_bind)
PHP_FE(uv_listen, arginfo_uv_listen)
PHP_FE(uv_tcp_connect, arginfo_uv_tcp_connect)
{NULL, NULL, NULL}
};

View File

@ -28,6 +28,8 @@ typedef struct {
struct sockaddr_in addr;
uv_connect_t connect;
uv_tcp_t socket;
zend_fcall_info fci_connect;
zend_fcall_info_cache fcc_connect;
} php_uv_t;
#define PHP_UV_RESOURCE_NAME "uv"