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;
}