Handle line-based input intelligently:
Handle line-based input intelligently:

This requires a bit of explanation.

While "on_read" will be called as soon as there's at least one
octet of data, this means two things:

1. It might be multiple lines.
2. It might not be complete lines.

The developer needs to maintain those. That's part of the
design of this callback.

A way to handle both is to use the "push_read" callback method
and ask it to handle lines:

    $handle->push_read( line => sub {...} );

The problem is that "push_read" adds a single callback to the
read queue which will be shifted and removed. This means you
need to continually add it:

    my $reader; $reader = sub {
        my $handle = $_[0];
        $handle->push_read( line => $reader );
    };

    $handle->push_read( line => $reader );

This will work, except when a client disconnects without you
controlling it.

AnyEvent considers it an error for a client to disconnect when
you still have a callback in the read queue. This makes sense.
Unfortunately, the "eof" callback is then *not* called, so you
can't just clean up the read callback queue in the "on_eof"
callback. It won't get run.

If you had a protocol that required calling "quit", you could
do it then. You could make sure not to add another read callback,
but this protocol is not like it.

A different approach, albeit slower, is to have a "on_read"
callback that creates a single "push_read". The "push_read" will
buffer for the next line and call the callback. Next "on_read"
will create another "push_read", buffering another line.

(This approach is described in the FAQ in the AnyEvent::Handle
docs.)

This is still much faster then the original implementation so
no worries.

xsawyerx/anyevent-eris,xsawyerx/anyevent-eris