X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=scpubgit%2FObject-Remote.git;a=blobdiff_plain;f=lib%2FObject%2FRemote%2FMiniLoop.pm;h=e7be944633f268484c6ae712c4a015cec75ba30b;hp=de2515fe2f68db432d29928240971d036c9e4f77;hb=fbd3b8ecbd2c9004f0e56ff1c0bc30f677a19c62;hpb=befabdee3a3a75da8dd2fd21a2a6c80d8ed0bcff diff --git a/lib/Object/Remote/MiniLoop.pm b/lib/Object/Remote/MiniLoop.pm index de2515f..e7be944 100644 --- a/lib/Object/Remote/MiniLoop.pm +++ b/lib/Object/Remote/MiniLoop.pm @@ -11,6 +11,9 @@ has is_running => (is => 'ro', clearer => 'stop'); has _read_watches => (is => 'ro', default => sub { {} }); has _read_select => (is => 'ro', default => sub { IO::Select->new }); +has _write_watches => (is => 'ro', default => sub { {} }); +has _write_select => (is => 'ro', default => sub { IO::Select->new }); + has _timers => (is => 'ro', default => sub { [] }); sub pass_watches_to { @@ -21,6 +24,12 @@ sub pass_watches_to { on_read_ready => $self->_read_watches->{$fh} ); } + foreach my $fh ($self->_write_select->handles) { + $new_loop->watch_io( + handle => $fh, + on_write_ready => $self->_write_watches->{$fh} + ); + } } sub watch_io { @@ -30,6 +39,10 @@ sub watch_io { $self->_read_select->add($fh); $self->_read_watches->{$fh} = $cb; } + if (my $cb = $watch{on_write_ready}) { + $self->_write_select->add($fh); + $self->_write_watches->{$fh} = $cb; + } } sub unwatch_io { @@ -39,6 +52,10 @@ sub unwatch_io { $self->_read_select->remove($fh); delete $self->_read_watches->{$fh}; } + if ($watch{on_write_ready}) { + $self->_write_select->remove($fh); + delete $self->_write_watches->{$fh}; + } return; } @@ -64,12 +81,18 @@ sub unwatch_time { sub loop_once { my ($self) = @_; my $read = $self->_read_watches; - my ($readable) = IO::Select->select($self->_read_select, undef, undef, 0.5); + my $write = $self->_write_watches; + my ($readable, $writeable) = IO::Select->select( + $self->_read_select, $self->_write_select, undef, 0.5 + ); # I would love to trap errors in the select call but IO::Select doesn't # differentiate between an error and a timeout. # -- no, love, mst. foreach my $fh (@$readable) { - $read->{$fh}(); + $read->{$fh}() if $read->{$fh}; + } + foreach my $fh (@$writeable) { + $write->{$fh}() if $write->{$fh}; } my $timers = $self->_timers; my $now = time();