2011/11/6 钱晓明 <mailtoanta...@163.com>

> Hi, I am working on libev few days, and there is a question about EV_WRITE:
> When processed request from client, the server has some data to write
> back. At this time, it can:
> *A.* write back directly in a loop, until all data has been written
> *B.* install a EV_WRITE event when client connected, check buffer in the
> callback function, and write back if there is data. The EV_WRITE event only
> install once.
> *C. *install/start a EV_WRITE event when adding reply data to buffer, and
> in callback function write all data to client, stop EV_WRITE event at last
> before returning from this function.
> Which one is best? How frequent the callback function will be called in
> situation B?
>
>

C is the best general purpose solution.  Create can create the EV_WRITE
callback when you create the socket, but simply not start it immediately.
 When you have reply data that needs to be sent, you buffer it in
application code and start your EV_WRITE watcher to drain that buffer.
 When the reply buffer becomes empty, you stop the EV_WRITE watcher.  This
gets you correct, nonblocking behavior.  The only problem with it is that
it's slightly sub-optimal in the common case where the entire reply easily
fits in the socket's send buffer, so sometimes you adapt other strategies
to handle the common case efficiently (e.g. attempt immediate send first,
then fall back to C on EAGAIN).  But you almost always need a backup plan
that does the full work of (C) in the case that the socket cannot accept
all of your data immediately.  If you don't have code that does scenario C
in your application, your application likely breaks down (fails to function
correctly, or blocks) in corner cases that weren't obvious to you initially.
_______________________________________________
libev mailing list
libev@lists.schmorp.de
http://lists.schmorp.de/cgi-bin/mailman/listinfo/libev

Reply via email to