perl - Why isn't on_eof called in this AnyEvent::Handle example? -



perl - Why isn't on_eof called in this AnyEvent::Handle example? -

this simple server. when run , telnet (port 5222), , have telnet quit connection, why isn't on_eof function called? i.e. why isn't string "catastrophe!!!" printed?

#!/usr/bin/perl utilize v5.18; utilize warnings; utilize ev; utilize anyevent; utilize anyevent::socket; utilize anyevent::handle; our $hdl; $server = tcp_server undef, 5222, sub { ($fh) = @_; $hdl = anyevent::handle->new(fh => $fh); $hdl->on_eof(sub { ($handle) = @_; "catastrophe!!!"; }); }; ev::run;

tl;dr: without trying read socket, eof cannot detected. utilize ->on_eof or ->push_read.

long version:

the illustration doesn't effort read handle, , why anyevent::handle doesn't effort read data. without attempting read data, cannot observe eof (a consequence of posix api).

this behaviour described indirectly in description start_read/stop_read methods:

note anyevent::handle automatically "start_read" when alter "on_read" callback or push/unshift read callback, , automatically "stop_read" when neither "on_read" set nor there read requests in queue.

the reason why behaves that, in event-based program, there might amount of time between reading info (done internally) , enqueuing read callback. since error receive info isn't expected (and since not making error overflow read buffer, causing error), there needs way avoid these errors.

not reading info when none requested automatic way of avoiding spurious errors caused programme not beingness "fast enough" process data: anyevent::handle doesn't read socket until programme made decision on data.

in rather untypical example, don't socket. on_eof detected, can phone call ->start_read, unlikely useful in real world programs.

perl anyevent

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

django - Access session in user model .save() -

php - .htaccess Multiple Rewrite Rules / Prioritizing -