php擴展開發---kqueue 實例

/*
  +----------------------------------------------------------------------+
  | PHP Version 5                                                        |
  +----------------------------------------------------------------------+
  | Copyright (c) 1997-2016 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          |
  | [email protected] so we can mail you a copy immediately.               |
  +----------------------------------------------------------------------+
  | Author: [email protected]                                                             |
  +----------------------------------------------------------------------+
*/

/* $Id$ */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

//#include <Zend/zend_interfaces.h>
#include "php_xing2233.h"

/* If you declare any globals in php_xing2233.h uncomment this:
ZEND_DECLARE_MODULE_GLOBALS(xing2233)
*/

/* True global resources - no need for thread safety here */
static int le_xing2233;

/* {{{ PHP_INI
 */
/* Remove comments and fill if you need to have entries in php.ini
PHP_INI_BEGIN()
    STD_PHP_INI_ENTRY("xing2233.global_value",      "42", PHP_INI_ALL, OnUpdateLong, global_value, zend_xing2233_globals, xing2233_globals)
    STD_PHP_INI_ENTRY("xing2233.global_string", "foobar", PHP_INI_ALL, OnUpdateString, global_string, zend_xing2233_globals, xing2233_globals)
PHP_INI_END()
*/
/* }}} */

/* Remove the following function when you have successfully modified config.m4
   so that your module can be compiled into PHP, it exists only for testing
   purposes. */

/* Every user-visible function in PHP should document itself in the source */
/* {{{ proto string confirm_xing2233_compiled(string arg)
   Return a string to confirm that the module is compiled in */
PHP_FUNCTION(confirm_xing2233_compiled)
{
//    zval *response;
//    MAKE_STD_ZVAL(response);
//    array_init(response);
//    add_assoc_string(response, "end", "111", 1);
//    zend_hash_add(EG(active_symbol_table),"response", sizeof("response"), &response, sizeof(zval*), NULL);
//    php_printf("111");
}
/* }}} */
/* The previous line is meant for vim and emacs, so it can correctly fold and
   unfold functions in source code. See the corresponding marks just before
   function definition, where the functions purpose is also documented. Please
   follow this convention for the convenience of others editing your code.
*/

int xSocket_bind(int sock, int type, char *host, int *port, TSRMLS_D)
{
    int ret;

    struct sockaddr_in addr_in4;
    struct sockaddr_in6 addr_in6;
    struct sockaddr_un addr_un;
    socklen_t len;

    //SO_REUSEADDR option
    int option = 1;
    if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &option, sizeof(int)) < 0)
    {
        php_error_docref(NULL TSRMLS_CC, E_ERROR, "setsockopt failed");
    }
    //reuse port
    //unix socket
    if (type == SW_SOCK_UNIX_DGRAM || type == SW_SOCK_UNIX_STREAM)
    {
        bzero(&addr_un, sizeof(addr_un));
        unlink(host);
        addr_un.sun_family = AF_UNIX;
        strncpy(addr_un.sun_path, host, sizeof(addr_un.sun_path) - 1);
        ret = bind(sock, (struct sockaddr*) &addr_un, sizeof(addr_un));
    }
        //IPv6
    else if (type > SW_SOCK_UDP)
    {
        bzero(&addr_in6, sizeof(addr_in6));
        inet_pton(AF_INET6, host, &(addr_in6.sin6_addr));
        addr_in6.sin6_port = htons(*port);
        addr_in6.sin6_family = AF_INET6;
        ret = bind(sock, (struct sockaddr *) &addr_in6, sizeof(addr_in6));
        if (ret == 0 && *port == 0)
        {
            len = sizeof(addr_in6);
            if (getsockname(sock, (struct sockaddr *) &addr_in6, &len) != -1)
            {
                *port = ntohs(addr_in6.sin6_port);

            }
        }
    }
        //IPv4
    else
    {
        bzero(&addr_in4, sizeof(addr_in4));
        inet_pton(AF_INET, host, &(addr_in4.sin_addr));
        addr_in4.sin_port = htons(*port);
        addr_in4.sin_family = AF_INET;
        ret = bind(sock, (struct sockaddr *) &addr_in4, sizeof(addr_in4));
        if (ret == 0 && *port == 0)
        {
            len = sizeof(addr_in4);
            if (getsockname(sock, (struct sockaddr *) &addr_in4, &len) != -1)
            {
                *port = ntohs(addr_in4.sin_port);
            }
        }
    }
    //bind failed
    if (ret < 0)
    {
//        swoole_error_log(SW_LOG_WARNING, SW_ERROR_SYSTEM_CALL_FAIL, "bind(%s:%d) failed. Error: %s [%d]", host, *port, strerror(errno), errno);
//        return SW_ERR;
        return FAILURE;
    }

    return ret;
}

int xSocket_create(int type, TSRMLS_D) {
    int _domain;
    int _type;

    switch (type) {
        case SW_SOCK_TCP:
            _domain = PF_INET;
            _type = SOCK_STREAM;
            break;
        case SW_SOCK_TCP6:
            _domain = PF_INET6;
            _type = SOCK_STREAM;
            break;
        case SW_SOCK_UDP:
            _domain = PF_INET;
            _type = SOCK_DGRAM;
            break;
        case SW_SOCK_UDP6:
            _domain = PF_INET6;
            _type = SOCK_DGRAM;
            break;
        case SW_SOCK_UNIX_DGRAM:
            _domain = PF_UNIX;
            _type = SOCK_DGRAM;
            break;
        case SW_SOCK_UNIX_STREAM:
            _domain = PF_UNIX;
            _type = SOCK_STREAM;
            break;
        default:
            php_error_docref(NULL TSRMLS_CC, E_ERROR, "unknown socket type [%d]", type);
            return FAILURE;
    }
    return socket(_domain, _type, 0);
}

zend_class_entry xing2233_http_server_ce;
zend_class_entry *xing2233_http_server_class_entry_ptr;
//response
zend_class_entry xing2233_http_server_ce_response;
zend_class_entry *xing2233_http_server_class_entry_ptr_response;

PHP_METHOD(xing_http_server, __construct)
{
    char *serv_host;
    int host_len;
    long serv_port, serv_mode, sock_type; //port 類型必須是long ,int不起作用
    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|lll", &serv_host, &host_len, &serv_port, &serv_mode, &sock_type) == FAILURE)
    {
        php_printf("failure");
        return ;
    }
    zend_update_property_stringl(
            xing2233_http_server_class_entry_ptr,
            getThis(),
            "host",
            sizeof("host") - 1,
            serv_host,
            host_len TSRMLS_CC
    );
    zend_update_property_long(
            xing2233_http_server_class_entry_ptr,
            getThis(),
            "port",
            sizeof("port") - 1,
            serv_port TSRMLS_CC
    );
    zend_update_property_long(
            xing2233_http_server_class_entry_ptr,
            getThis(),
            "mode",
            sizeof("mode") - 1,
            serv_mode TSRMLS_CC
    );
    zend_update_property_long(
            xing2233_http_server_class_entry_ptr,
            getThis(),
            "type",
            sizeof("type") - 1,
            sock_type TSRMLS_CC
    );

}




int make_sock_non_blocking(int sock, TSRMLS_D)
{
    int flags;
    flags = fcntl(sock, F_GETFL, 0);
    if (flags < 0)
    {
        php_error_docref(NULL TSRMLS_CC, E_ERROR, "fcntl sock failed.");
    }
    flags |= O_NONBLOCK;
    return fcntl(sock, F_SETFL, flags);
}


PHP_METHOD(xing_http_server, start)
{
    char *serv_host;
    int host_len;
    long serv_port, serv_mode, sock_type;
    zval *host = zend_read_property(
            xing2233_http_server_class_entry_ptr,
            getThis(),
            "host",
            sizeof("host") - 1 , 0 TSRMLS_CC
    );
    serv_host = Z_STRVAL_P(host);
    zval *port = zend_read_property(
            xing2233_http_server_class_entry_ptr,
            getThis(),
            "port",
            sizeof("port") - 1 , 0 TSRMLS_CC
    );
    int ser_port = Z_LVAL_P(port);

    zval *mode = zend_read_property(
            xing2233_http_server_class_entry_ptr,
            getThis(),
            "mode",
            sizeof("mode") - 1 , 0 TSRMLS_CC
    );
    serv_mode = Z_LVAL_P(mode);

    zval *type = zend_read_property(
            xing2233_http_server_class_entry_ptr,
            getThis(),
            "type",
            sizeof("type") - 1 , 0 TSRMLS_CC
    );
    int soc_type = Z_LVAL_P(type);

    int sock = xSocket_create(soc_type ,TSRMLS_C);

    if (sock < 0)
    {
        php_error_docref(NULL TSRMLS_CC, E_ERROR, "create socket failed.");
        return ;
    }

    if (xSocket_bind(sock, soc_type, serv_host, &ser_port, TSRMLS_C) < 0)
    {
        close(sock);
        php_error_docref(NULL TSRMLS_CC, E_ERROR, "bind sock failed.");
        return ;
    }

    make_sock_non_blocking(sock, TSRMLS_C);

    if (listen(sock, SW_BACKLOG) < 0)
    {
        php_error_docref(NULL TSRMLS_CC, E_ERROR, "listen failed");
        return ;
    }


    zval *onResponse = zend_read_property(
            xing2233_http_server_class_entry_ptr,
            getThis(),
            "onResponse",
            sizeof("onResponse") - 1 , 0 TSRMLS_CC
    );
    //回調方法參數賦值
    zval *args[2];
    MAKE_STD_ZVAL(args[0]);
    MAKE_STD_ZVAL(args[1]);
    ZVAL_STRING(args[0], "request", 1);
    object_init_ex(args[1], xing2233_http_server_class_entry_ptr_response);
//    zval *resonseValue;
    //
    if (call_user_function(
            EG(function_table),
            NULL,
            onResponse,
            return_value, 2, args TSRMLS_CC) != SUCCESS) {
        php_printf("callback faild");
        RETURN_FALSE;
    }



    zval *response = zend_read_property(
            xing2233_http_server_class_entry_ptr_response,
            args[1],
            "response",
            sizeof("response") - 1 , 0 TSRMLS_CC
    );
    zval **end;
    if (zend_hash_find(Z_ARRVAL_P(response), "end", strlen("end") + 1 , (void**)&end) != SUCCESS)
    {

    }
    //直接賦值 char *endVal = Z_STRVAL_PP(end) 不起作用
    char endVal[Z_STRLEN_PP(end)];
    snprintf(endVal, Z_STRLEN_PP(end) + 1, Z_STRVAL_PP(end));
    zval_ptr_dtor(&args[0]);
    zval_ptr_dtor(&args[1]);
//    in_port_t

    struct sockaddr_in remote_addr;
    socklen_t remote_addr_size = sizeof(struct sockaddr);

    int kq = kqueue();
    struct kevent changes[1];
    EV_SET(&changes[0], sock, EVFILT_READ, EV_ADD,0, 0, NULL);
    if (kevent(kq, changes, 1, NULL, 0, NULL) < 0)
    {
        php_error_docref(NULL TSRMLS_CC, E_ERROR, "registe sock failed");
    }

    struct kevent events[2];
    while (1)
    {
        int ret = kevent(kq, NULL, 0, events, 2, NULL);
        for (int i = 0; i < ret; ++i) {
            if (events[i].ident == sock)
            {
                    int client = accept(sock, (struct sockaddr *) &remote_addr, &remote_addr_size);
                    if (client < 0)
                    {
                        php_printf("%s", "accept failed");
                        break;
                    }

                    php_printf("Accepted connection on descriptor=%d host=%s port=%d\n", client, inet_ntoa(remote_addr.sin_addr), remote_addr.sin_port);
                    make_sock_non_blocking(client, TSRMLS_C);
                    EV_SET(&changes[0], client, EVFILT_READ, EV_ADD, 0, 0, NULL);
                    if (kevent(kq, changes, 1, NULL, 0, NULL) < 0) {
                        php_error_docref(NULL TSRMLS_CC, E_ERROR, "registe client failed");
                    }
//                    char word[BUFSIZ];
//                    sprintf(word,
//                            "HTTP/1.1 200 OK\r\nContent-Type:text/html;charset=utf8\r\nContent-Length: %lu\r\n\r\n%s",
//                            strlen(endVal), endVal);
//                    if (send(client, word, strlen(word), 0) < 0) {
//                        php_error_docref(NULL TSRMLS_CC, E_ERROR, "send failed");
//                        close(client);
//                        return;
//                    }
//                    close(client);

                //}
            }
            else
            {
                int done = 0;
                while (1)
                {
                    ssize_t count;
                    char buf[512];

                    count = read(events[i].ident, buf, sizeof(buf));
                    if (count < 0)
                    {
                        if (errno != EAGAIN)
                        {
                            done = 1;
                        }
                        break;
                    }
                    else if (count == 0)
                    {
                        done = 1;
                        break;
                    }
                    write(1, buf, count);
                }
                if (done)
                {
                    php_printf("Closed connection on descriptor=%lu\n", events[i].ident);
                    close(events[i].ident);
                }
            }
        }
    }
    free(events);
    close(sock);
}

PHP_METHOD(xing_http_server, on)
{
//    php_printf("this is on");
    zval *callback;
    zval *event_name;

    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zz", &event_name, &callback) == FAILURE)
    {
        php_printf("failure");
    }


    if (strncasecmp("request", Z_STRVAL_P(event_name), Z_STRLEN_P(event_name)) == 0)
    {
        zend_update_property(
                xing2233_http_server_class_entry_ptr,
                getThis(),
                "onResponse",
                sizeof("onResponse") - 1,
                callback TSRMLS_CC
        );


    }
    else
    {

        //判斷callback是不是回調方法
        char *func_name = NULL;
        if (!zend_is_callable(callback, 0, &func_name TSRMLS_CC)) {
            php_error_docref(NULL TSRMLS_CC, E_ERROR, "function '%s' is not callable", func_name);
            efree(func_name);
            RETURN_FALSE;
        }
        efree(func_name);
        //回調方法參數賦值
        zval *args[1];
        MAKE_STD_ZVAL(args[0]);
        ZVAL_LONG(args[0], 1);

        if (call_user_function(EG(function_table), NULL, callback, return_value, 1, args TSRMLS_CC) != SUCCESS) {
            php_printf("callback faild");
            RETURN_FALSE;
        }
        zval_ptr_dtor(&args[0]);
    }

//    efree(func_name);

}

PHP_METHOD(xing_http_server_response, header)
{
    char *header_one;
    char *header_two;
    int header_one_len, header_two_len;

    if (zend_parse_parameters(
            ZEND_NUM_ARGS() TSRMLS_CC,
            "ss",
            &header_one,
            &header_one_len,
            &header_two,
            &header_two_len) == FAILURE)
    {
        php_printf("failure");
    }




}

PHP_METHOD(xing_http_server_response, end)
{
    char *end_one;
    int end_one_len;

    if (zend_parse_parameters(
            ZEND_NUM_ARGS() TSRMLS_CC,
            "s",
            &end_one,
            &end_one_len) == FAILURE)
    {
        php_printf("failure");
    }

    zval *response;
    MAKE_STD_ZVAL(response);
    array_init(response);

    zend_update_property(
            xing2233_http_server_class_entry_ptr_response,
            getThis(),
            "response",
            sizeof("response") - 1,
            response TSRMLS_CC
    );
    add_assoc_stringl_ex(response, "end", sizeof("end"), end_one, end_one_len, 1);
    zval_ptr_dtor(&response);
}





static zend_function_entry xing2233_http_server_methods[] = {
        PHP_ME(xing_http_server, __construct, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
        PHP_ME(xing_http_server, on, NULL, ZEND_ACC_PUBLIC)
        PHP_ME(xing_http_server, start, NULL, ZEND_ACC_PUBLIC)
        PHP_FE_END
};

//reponse
static zend_function_entry xing2233_http_server_methods_response[] = {
        PHP_ME(xing_http_server_response, header, NULL, ZEND_ACC_PUBLIC)
        PHP_ME(xing_http_server_response, end, NULL, ZEND_ACC_PUBLIC)
        PHP_FE_END
};





PHP_MINIT_FUNCTION(xing2233)
{
    /* If you have INI entries, uncomment these lines
    REGISTER_INI_ENTRIES();
    */
    INIT_CLASS_ENTRY(xing2233_http_server_ce, "xing_http_server", xing2233_http_server_methods);
    xing2233_http_server_class_entry_ptr = zend_register_internal_class(&xing2233_http_server_ce TSRMLS_CC);
    INIT_CLASS_ENTRY(xing2233_http_server_ce_response, "xing_http_server_response", xing2233_http_server_methods_response);
    xing2233_http_server_class_entry_ptr_response = zend_register_internal_class(&xing2233_http_server_ce_response TSRMLS_CC);

    //constant
    REGISTER_LONG_CONSTANT("XING_SOCK_TCP", SW_SOCK_TCP, CONST_CS|CONST_PERSISTENT);
    //mode
    REGISTER_LONG_CONSTANT("XING_BASE", SW_MODE_SINGLE, CONST_CS | CONST_PERSISTENT);
    REGISTER_LONG_CONSTANT("XING_THREAD", SW_MODE_THREAD, CONST_CS | CONST_PERSISTENT);
    REGISTER_LONG_CONSTANT("XING_PROCESS", SW_MODE_PROCESS, CONST_CS | CONST_PERSISTENT);

    return SUCCESS;
}
/* }}} */

/* {{{ PHP_MSHUTDOWN_FUNCTION
 */
PHP_MSHUTDOWN_FUNCTION(xing2233)
{
    /* uncomment this line if you have INI entries
    UNREGISTER_INI_ENTRIES();
    */
//    FILE *fp=fopen("/Users/albert/Documents/php/php-5.6.22/ext/xing2233/m.txt","a+");
//    fprintf(fp, "%d\n", time_of_minit);
//    fclose(fp);
//  free(module_number);
    return SUCCESS;
}
/* }}} */

/* Remove if there's nothing to do at request start */
/* {{{ PHP_RINIT_FUNCTION
 */

//int time_of_rinit;
PHP_RINIT_FUNCTION(xing2233)
{
//    time_of_rinit = time(NULL);
    return SUCCESS;
}
/* }}} */

/* Remove if there's nothing to do at request end */
/* {{{ PHP_RSHUTDOWN_FUNCTION
 */
PHP_RSHUTDOWN_FUNCTION(xing2233)
{
//    FILE *fp=fopen("/Users/albert/Documents/php/php-5.6.22/ext/xing2233/r.txt","a+");
//    fprintf(fp, "%d\n", time_of_rinit);
//    fclose(fp);
    return SUCCESS;
}
/* }}} */

/* {{{ PHP_MINFO_FUNCTION
 */
PHP_MINFO_FUNCTION(xing2233)
{
    php_info_print_table_start();
    php_info_print_table_header(2, "xing2233 support", "enabled");
    php_info_print_table_end();

    /* Remove comments if you have entries in php.ini
    DISPLAY_INI_ENTRIES();
    */
}
/* }}} */

/* {{{ xing2233_functions[]
 *
 * Every user visible function must have an entry in xing2233_functions[].
 */
const zend_function_entry xing2233_functions[] = {
    PHP_FE(confirm_xing2233_compiled,   NULL)       /* For testing, remove later. */
    PHP_FE_END  /* Must be the last line in xing2233_functions[] */
};
/* }}} */

/* {{{ xing2233_module_entry
 */
zend_module_entry xing2233_module_entry = {
    STANDARD_MODULE_HEADER,
    "xing2233",
    xing2233_functions,
    PHP_MINIT(xing2233),
    PHP_MSHUTDOWN(xing2233),
    PHP_RINIT(xing2233),        /* Replace with NULL if there's nothing to do at request start */
    PHP_RSHUTDOWN(xing2233),    /* Replace with NULL if there's nothing to do at request end */
    PHP_MINFO(xing2233),
    PHP_XING2233_VERSION,
    STANDARD_MODULE_PROPERTIES
};
/* }}} */

#ifdef COMPILE_DL_XING2233
ZEND_GET_MODULE(xing2233)
#endif

/*
 * Local variables:
 * tab-width: 4
 * c-basic-offset: 4
 * End:
 * vim600: noet sw=4 ts=4 fdm=marker
 * vim<600: noet sw=4 ts=4
 */
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章