Skip to content

Commit

Permalink
added experimental close method to Mojo::IOLoop::Stream
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Dec 19, 2011
1 parent e7cfa06 commit 6a39daf
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 24 deletions.
1 change: 1 addition & 0 deletions Changes
@@ -1,6 +1,7 @@
This file documents the revision history for Perl extension Mojolicious.

2.39 2011-12-19 00:00:00
- Added EXPERIMENTAL close method to Mojo::IOLoop::Stream.
- Improved documentation. (marcus, vervain, sri)
- Fixed small argument bug in client method of Mojo::IOLoop.

Expand Down
48 changes: 27 additions & 21 deletions lib/Mojo/IOLoop/Stream.pm
Expand Up @@ -17,10 +17,24 @@ has timeout => 15;
# Iran, Iraq, China, Mordor, the hoochies that laid low Tiger Woods,
# undesirable immigrants - by which I mean everyone that came after me,
# including my children..."
sub DESTROY { shift->_close }
sub DESTROY { shift->close }

sub new { shift->SUPER::new(handle => shift, buffer => '', active => time) }

sub close {
my $self = shift;

# Cleanup
return unless my $handle = delete $self->{handle};
return unless my $watcher = $self->{iowatcher};
$watcher->drop_handle($handle);
$watcher->drop_timer(delete $self->{timer}) if $self->{timer};

# Close
close $handle;
$self->emit_safe('close');
}

sub handle { shift->{handle} }

sub is_readable {
Expand Down Expand Up @@ -48,7 +62,7 @@ sub resume {
weaken $self;
$self->{timer} ||= $watcher->recurring(
'0.025' => sub {
$self->emit_safe('timeout')->_close
$self->emit_safe('timeout')->close
if $self && (time - ($self->{active})) >= $self->timeout;
}
);
Expand Down Expand Up @@ -88,20 +102,6 @@ sub write {
if $self->{handle};
}

sub _close {
my $self = shift;

# Cleanup
return unless my $handle = delete $self->{handle};
return unless my $watcher = $self->{iowatcher};
$watcher->drop_handle($handle);
$watcher->drop_timer(delete $self->{timer}) if $self->{timer};

# Close
close $handle;
$self->emit_safe('close');
}

sub _read {
my $self = shift;

Expand All @@ -115,14 +115,14 @@ sub _read {
return if $! ~~ [EAGAIN, EINTR, EWOULDBLOCK];

# Closed
return $self->_close if $! == ECONNRESET;
return $self->close if $! == ECONNRESET;

# Read error
return $self->emit_safe(error => $!)->_close;
return $self->emit_safe(error => $!)->close;
}

# EOF
return $self->_close if $read == 0;
return $self->close if $read == 0;

# Handle read
$self->emit_safe(read => $buffer);
Expand All @@ -146,10 +146,10 @@ sub _write {
return if $! ~~ [EAGAIN, EINTR, EWOULDBLOCK];

# Closed
return $self->_close if $! ~~ [ECONNRESET, EPIPE];
return $self->close if $! ~~ [ECONNRESET, EPIPE];

# Write error
return $self->emit_safe(error => $!)->_close;
return $self->emit_safe(error => $!)->close;
}

# Remove written chunk from buffer
Expand Down Expand Up @@ -293,6 +293,12 @@ implements the following new ones.
Construct a new L<Mojo::IOLoop::Stream> object.
=head2 C<close>
$stream->close;
Close stream immediately.
=head2 C<handle>
my $handle = $stream->handle;
Expand Down
3 changes: 1 addition & 2 deletions t/mojo/ioloop.t
Expand Up @@ -130,8 +130,7 @@ Mojo::IOLoop->server(
my ($stream, $chunk) = @_;
$buffer .= $chunk;
return unless $buffer eq 'acceptedhello';
$stream->write('world');
$stream->emit('close');
$stream->write('world', sub { shift->close });
}
);
}
Expand Down
2 changes: 1 addition & 1 deletion t/mojolicious/longpolling_lite_app.t
Expand Up @@ -307,7 +307,7 @@ Mojo::IOLoop->client(
$stream->on(
read => sub {
my ($stream, $chunk) = @_;
$stream->emit('close');
$stream->close;
Mojo::IOLoop->timer('0.5', sub { Mojo::IOLoop->stop });
}
);
Expand Down

0 comments on commit 6a39daf

Please sign in to comment.