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

2013-01-22 Thread Ming Xie
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, "XXXthe 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;
}

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

2013-01-22 Thread Ming Xie

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"  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, "XXXthe 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;
}




Re: [Libevent-users] evhttp client error handling

2013-01-22 Thread Azat Khuzhin
Hi Patrick, thanks, now understand that I don't see at the right peace of code.

Could you try to test my patch
https://github.com/azat/libevent/commit/71e709c7829275a594f767b27468b1b52e8b5bb9.patch

and write if it works for you?

Thanks.

On Tue, Jan 22, 2013 at 9:05 AM, Patrick Pelletier
 wrote:
> Azat Khuzhin wrote:
>>
>> Also on which operating system did you work?
>
>
> Ubuntu 10.04 LTS:
>
> Linux chives 2.6.32-34-generic #77-Ubuntu SMP Tue Sep 13 19:39:17 UTC 2011
> x86_64 GNU/Linux
>
> But the operating system doesn't really matter.  This is a general problem
> with evhttp, regardless of OS.
>
>
>> Maybe this will be useful for you
>>
>> http://instantbadger.blogspot.ru/2010/06/eafnosupport-socket-connection-issue-on.html
>
>
> Sure, obviously you can sweep the problem under the carpet by only using
> IPv4.  That's what I'm doing now: I have a horrible little hack that
> compares the hostname with "localhost" and replaces it with "127.0.0.1" in
> that case.
>
> But that does nothing to address the underlying problem that evhttp doesn't
> support IPv6.  Although IPv6 is still in the minority, and adoption is slow,
> it's still something we ought to support.  If evhttp is IPv4-only, it is not
> future-proof.
>
>> On Sun, Jan 20, 2013 at 7:18 PM, Azat Khuzhin 
>> wrote:
>>>
>>> But "ai" can't be NULL in "bind_socket_ai()", see
>>>
>>> https://github.com/libevent/libevent/blob/0c2bacca43ffe452d4f5cdc4b57fd29a7795777d/http.c#L4088
>
>
> Sure it can.  It's explicitly called with NULL right here:
>
> https://github.com/libevent/libevent/blob/b452a4345088dcc7e7491bcff612f9f11dfb17da/http.c#L4084
>
> address and port are going to be NULL, because they come from
> evcon->bind_address and evcon->bind_port:
>
> https://github.com/libevent/libevent/blob/b452a4345088dcc7e7491bcff612f9f11dfb17da/http.c#L2336
>
> and evcon->bind_address and evcon->bind_port default to NULL, and are going
> to stay NULL, unless evhttp_connection_set_local_address() or
> evhttp_connection_set_local_port() are called.  But these wouldn't normally
> be called in the client case.  The example that I'm basing my code off of
> doesn't call them:
>
> http://archives.seul.org/libevent/users/Mar-2012/binGP2R6ys0C_.bin
>
> and it doesn't make sense for the client to call them anyway.  In the client
> case, you want to use whichever local address will allow you to reach the
> remote address.  So we are once again back to our chicken-and-egg problem,
> that you have to resolve the remote address (and know whether it is IPv4 or
> IPv6) before you can create the socket.  It doesn't make sense any other
> way.
>
>
> --Patrick
> ***
> To unsubscribe, send an e-mail to majord...@freehaven.net with
> unsubscribe libevent-usersin the body.



-- 
Azat Khuzhin
***
To unsubscribe, send an e-mail to majord...@freehaven.net with
unsubscribe libevent-usersin the body.