tcp_server + AnyEvent::Handle

Sergei perllance at yandex.ru
Tue Apr 29 22:12:48 CEST 2014


Hello,

I have a tcp_server and I want to use AnyEvent::Handle to manage the read and write queue.
My code looks something like this (see below, very simple). The problem is that my read handler is missing some initial data from the $fh.
Obviously, by the time the $hdl object is instantiated, $fh already contains some data. I can circumvent this by trying to sysread from the $fh first, but the data is not necessarily there, so I'll have to write conditions and callbacks which makes my code clumsy and duplicates AnyEvent::Handle.
Ideally, AnyEvent::Handle would have some parameter in its constructor which would drain the $fh so that I can rely on AnyEvent::Handle methods only and avoid sysread in my code. Or there is got to be a way to trigger a read event on the $fh which would make AnyEvent::Handle to process whatever was written to the $fh so far.

tcp_server '127.0.0.1', '123', sub {
    my $fh = shift;
    my $hdl = AnyEvent::Handle->new(
        fh => $fh,
        on_read => sub {
            # my handler goes here
        }
    );
};

Thanks.

Best regards, Sergei



More information about the anyevent mailing list