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

Re: [Libevent-users] Closing bufferevent



On Tue, Mar 2, 2010 at 6:54 AM, Zac Sims <zac.sims@xxxxxxxxx> wrote:
> G'day,
>
> Perhaps I'm missing something, but what's the proper way to force a
> bufferevent closed? That is, get a BEV_EVENT_EOF or similar event with the
> registered event call-back once it's closed.
>
>>    bufferevent_setcb(bev, readcb, NULL, eventcb, NULL);
>>    bufferevent_enable(bev, EV_READ | EV_WRITE);
>>
>>    void con_readcb(struct bufferevent * bev, void * arg)
>>    {
>>       ...
>>
>>       fd = bufferevent_getfd();
>>       ...
>>
>>       EVUTIL_CLOSESOCKET(fd);
>>    }
>>
>>    void eventcb(struct bufferevent * bev, short events, void * arg)
>>    {
>>       ... event never comes ...
>>
>>       bufferevent_free(bev);
>>    }
>
> I've tried closing the socket myself once I have the fd (using
> bufferevent_getfd()). But that
> doesn't seem to generate an event on the registered eventcb.
>
> Could I set a timeout for 0 seconds in order to get the eventcb to fire?
> That way I could use
> BEV_OPT_CLOSE_ON_FREE. Of course, that runs with the problem of more data
> arriving.
>
> Thanks
> Zac

If you just want to close it immediately, you can do
bufferevent_free() from either callback, assuming that
BEV_OPT_CLOSE_ON_FREE is set.  (If this turns out not to work, that's
a bug.)

The BEV_EVENT_EOF event is only produced when it's closed by the other
side of the connection, though; you won't get it when you close the
socket yourself.

Or were you talking about half-open connections?  I don't believe we
support those through our  now, but you should just be able to say
shutdown(fd, SHUT_WR) on the fd, and then disable writing immediately.

-- 
Nick
***********************************************************************
To unsubscribe, send an e-mail to majordomo@xxxxxxxxxxxxx with
unsubscribe libevent-users    in the body.