[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[Libevent-users] Re:problem with use libevent 2.0.21-stable on php c extension




my OS : Debian 6.0.6 without SELinux

root@Excalibur:/home/excalibur/php_new_vesion/php-5.4.10# cat /etc/debian_version 
6.0.6
root@Excalibur:/home/excalibur/php_new_vesion/php-5.4.10# gcc -v
Using built-in specs.
Target: i486-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Debian 4.4.5-8' --with-bugurl=file:///usr/share/doc/gcc-4.4/README.Bugs --enable-languages=c,c++,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-4.4 --enable-shared --enable-multiarch --enable-linker-build-id --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.4 --libdir=/usr/lib --enable-nls --enable-clocale=gnu --enable-libstdcxx-debug --enable-objc-gc --enable-targets=all --with-arch-32=i586 --with-tune=generic --enable-checking=release --build=i486-linux-gnu --host=i486-linux-gnu --target=i486-linux-gnu
Thread model: posix
gcc version 4.4.5 (Debian 4.4.5-8) 

At 2013-01-22 16:16:02,"Ming Xie" <fireco007@xxxxxxx> wrote:
Hi, all
my php version : 5.3.6
i'm writing a connection pool php extension using libevent. i build connecion like this:

conn_ptr->ev_buffer = bufferevent_socket_new(ptr_base, -1, BEV_OPT_CLOSE_ON_FREE/* | BEV_OPT_THREADSAFE*/);
if (conn_ptr->ev_buffer == NULL)
 {
     fprintf(stderr, "bufferevent socket new returns NULL\n");
    exit(1);
}

 bufferevent_setcb(conn_ptr->ev_buffer, srv_read_cb, srv_write_cb, srv_event_cb, conn_ptr);

 ///connect to server
 bufferevent_socket_connect(conn_ptr->ev_buffer, (struct sockaddr*)&srvAddr, sizeof(srvAddr));

void srv_event_cb(struct bufferevent *bev, short events, void *ctx)
{
    struct dr_connection *ptr_conn = (struct dr_connection*)ctx;
    if (events & BEV_EVENT_CONNECTED)
    {

        ///add this to server's connection list and enable read & write
        ///init the connection
        ptr_conn->status = CONN_FREE;
        ptr_conn->socket_fd = bufferevent_getfd(bev);
        
        add_connection(ptr_conn->server, ptr_conn);
        if (0 != bufferevent_enable(bev, EV_READ | EV_WRITE)) {
            syslog(LOG_ERR, "enable bufferevent failed, %s", strerror(errno));
            remove_connection_by_bev(ptr_conn->server, bev);
            bufferevent_free(bev);    
        }
        
        syslog(LOG_INFO, "socket %u connect success", ptr_conn->socket_fd);

    }
    else if (events & BEV_EVENT_ERROR)
    {
        syslog(LOG_INFO, "error occured, close the socket %u", ptr_conn->socket_fd);
        remove_connection_by_bev(ptr_conn->server, bev);
        bufferevent_free(bev);
    }
}

and i send message to server like this:
    struct bufferevent *bev = ptr_conn->ev_buffer;
    if (0 != bufferevent_write(bev, (void*)send_buffer, msg_len + 4))
    {
        syslog(LOG_ERR, "send message failed\n");
        free(send_buffer);
        return 0;
    }
here is the problem:
when i built the code as a php extension,the bufferevent_write return is 0. but the server didn't recevie any data.
but if i built it into a a.out and the server can receive the data.

here is my main loop(EVLOOP_NO_EXIT_ON_EMPTY doesn't work...)
void* dr_work_fun(void* ptr)
{
    struct event_base *ptr_base = ((struct dr_thread *)ptr)->ptr_eb;
    if (NULL == ptr_base) {
        syslog(LOG_ERR, "XXXXXXXthe event base is NULL!");
        return ;
    }
    
    /*i've tried this
    if (0 != event_reinit(ptr_base)) {
    
        syslog(LOG_INFO, "re init event base failed");
        return ;
    }*/
    
    syslog(LOG_INFO, "thread : %u start!", (unsigned int)pthread_self());

    struct timeval delay;
    delay.tv_sec = 0;
    delay.tv_usec = 100 * 1000;///ms

    ///enter event loop
    while (1)
    {
        if (0 != event_base_dispatch(ptr_base)) {
            syslog(LOG_ERR, "event base dispatch failed, %s", strerror(errno));
        }
        
        syslog(LOG_INFO, "event loop exit, try restart");
        select(0, NULL, NULL, NULL, &delay);
    }

    syslog(LOG_INFO, "thread : %u exited!", (unsigned int)pthread_self());
    return NULL;
}