Detecting EOF in a on_read for AnyEvent::Handle

Marios Titas redneb8888 at gmail.com
Sun Dec 11 21:26:57 CET 2011


oops, I didn't do reply to all, resending:

OK, so here's a more complete description of what I am trying to do: I
have a server that implements a protocol that works like this: a
client connects and send a request which consists of one line of text.
Then the server responds by sending back one line of text. Then the
client can send another request and so on. When the client has
received a response from the server it might close the connection
instead of sending a new request. This should not be treated as an
error.
The AnyEvent::Handle man page provides two examples of serving
requests in a loop. The first one doesn't work for the above case
because it cannot handle the EOF correctly (it will throw an error).
The second doesn't have this problem, but it has a different problem:
as soon as some data is received, is will handle the request
immediately. But this is not desired in many cases. It might be
required that the server finishes processing the previous request
before it moves on to the new one. This is for example how HTTP works.
So here's an implementation of such a server that doesn't have the
above issues. The problem is that it relies on the
undocumented$handle->{_eof}:http://pastebin.com/utMuKY8e
If you want to try this, you can use the following for a client:socat
readline /tmp/sss



More information about the anyevent mailing list