On Mon, Sep 3, 2012 at 1:05 PM, Parvez Shaikh <pshaikh.wo...@gmail.com>wrote:
> Thanks Oleg and Nir. > > I am also pursuing another approach. Here I have defined a custom event > (associated with FD but not having either EV_READ or EV_WRITE) and there is > no time out either. > > A thread queues data to "outgoing message list" and activates an event. In > response to this, event reactor thread (one with dispatch) calls a function > that sends message from this list one by one until "send" encounters EAGAIN. > > Requesting your feedback on this approach. > If you must create your messages in another thread (maybe it is cpu bound, or you must use blocking operations), this should work. But if you don't have too, I would not use threads since I don't like deadlocks :-) Anyway, when the send fails with EAGAIN, you want to enable EV_WRITE, so you can continue to drain your messages queue when socket is ready. The way you work with EV_WRITE is not related to working with threads. > On Sun, Sep 2, 2012 at 3:45 PM, Nir Soffer <nir...@gmail.com> wrote: > >> >> >> On Sun, Sep 2, 2012 at 6:54 AM, Parvez Shaikh <pshaikh.wo...@gmail.com>wrote: >> >>> Thanks Oleg, >>> >>> Second approach is what I am doing. >>> >>> Why disabled EV_WRITE in write callback? >>> >>> I'd wish to have this callback called again whenever send buffer has >>> space, so disabling EV_WRITE will prevent this. >> >> >> You want to enable EV_WRITE only when you have something to write. >> >> When you enable EV_WRITE, libevent will add the descriptor to the the >> event backend (e.g select). When the descriptor is ready for writing, the >> write callback will be called. If you always enable EV_WRITE, the callback >> will be called on every loop iteration, and the event loop will never wait >> for events, since the descriptor is almost always ready for writing. >> >> >>> >>> >>> On Fri, Aug 31, 2012 at 3:54 PM, Oleg <mybrokenb...@gmail.com> wrote: >>> >>>> EV_WRITE calls whenever you can write to send buffer until it's not >>>> full. >>>> So if you have never called send(), but EV_WRITE is enabled you will >>>> receive this event each new loop (your CPU will be ~100% used). >>>> If you have called send() and it didn't return EAGAIN you will also >>>> receive this event next loop. >>>> If you have called send() and it returned EAGAIN you will receive this >>>> event when your send buffer will have some free space. >>>> >>>> >>>> So for your sending queue it should look like this in pseudo code: >>>> >>>> 1) Application->Send(data) >>>> { >>>> if (send() != EAGAIN ) >>>> return; >>>> else >>>> { >>>> queue.push(data); >>>> enable(EV_WRITE,onWriteCallback); >>>> } >>>> } >>>> >>>> 2) onWriteCallback() >>>> { >>>> while(!queue.empty) >>>> { >>>> if (send(queue.front())== EAGAIN); >>>> return; >>>> queue.pop(); >>>> } >>>> disable(EV_WRITE); >>>> } >>>> >>>> 31.08.2012, в 8:50, Parvez Shaikh написал(а): >>>> >>>> > Hi all, >>>> > >>>> > I have a question about EV_WRITE event as to when does it's callback >>>> function invoked? >>>> > >>>> > Is it that when someone first executes write on an fd associated with >>>> EV_WRITE event? >>>> > >>>> > Or when libevent detects that application can now write to fd without >>>> getting errors? >>>> > >>>> > For EV_READ it is easy to understand that it's callback is invoked >>>> when data is available for read on fd but not clear about EV_WRITE. >>>> > >>>> > Here is what I am trying to do - >>>> > >>>> > I am trying to write asynchronous send/recv application; in which I >>>> will read data on connected sockets asynchronously using "EV_READ's >>>> callback. >>>> > >>>> > For send however, I will enqueue the data to be sent in my own >>>> queue(application will write data to this buffer) and I will flush the >>>> buffer in EV_WRITE callback. >>>> > >>>> > Now if I get the error EAGAIN in send operation in callback of >>>> EV_WRITE, I will simply return and on next invocation of EV_WRITE's >>>> callback I will start flushing my buffer again. >>>> > >>>> > Thanks, >>>> > Parvez >>>> >>>> *********************************************************************** >>>> To unsubscribe, send an e-mail to majord...@freehaven.net with >>>> unsubscribe libevent-users in the body. >>>> >>> >>> >> >