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

Re: [Libevent-users] EV_WRITE - Wait for a socket or FD to become writeable.



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.

On Fri, Aug 31, 2012 at 3:54 PM, Oleg <mybrokenbeat@xxxxxxxxx> 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 majordomo@xxxxxxxxxxxxx with
unsubscribe libevent-users  Âin the body.