Skip Menu |

This queue is for tickets about the IO-Async CPAN distribution.

Report information
The Basics
Id: 97208
Status: resolved
Priority: 0/
Queue: IO-Async

People
Owner: Nobody in particular
Requestors: frioux [...] gmail.com
Cc:
AdminCc:

Bug Information
Severity: (no value)
Broken in: (no value)
Fixed in: 0.64



Subject: handle_constructor should be allowed as a method
Currently in IO::Async::Listener you can only set handle_constructor with new or configure, so in subclasses it's a little awkward.
Patched -- Paul Evans
Subject: rt97208.patch
=== modified file 'lib/IO/Async/Listener.pm' --- lib/IO/Async/Listener.pm 2014-10-17 15:24:31 +0000 +++ lib/IO/Async/Listener.pm 2014-10-17 15:54:54 +0000 @@ -161,6 +161,10 @@ $handle = $handle_constructor->( $listener ) +This can also be given as a subclass method + + $handle = $listener->handle_constructor() + =item handle_class => STRING Optional. If defined and C<handle_constructor> isn't, then new wrapper handles @@ -169,6 +173,10 @@ $handle = $handle_class->new() +This can also be given as a subclass method + + $handle = $listener->handle_class->new + =item acceptor => STRING|CODE Optional. If defined, gives the name of a method or a CODE reference to use to @@ -264,12 +272,20 @@ # on_accept needs to be last in case of multiple layers of subclassing elsif( $on_done = $self->can_event( "on_accept" ) ) { my $handle; + + # Test both params before moving on to either method if( my $constructor = $self->{handle_constructor} ) { $handle = $self->{handle_constructor}->( $self ); } elsif( my $class = $self->{handle_class} ) { $handle = $class->new; } + elsif( $self->can( "handle_constructor" ) ) { + $handle = $self->handle_constructor; + } + elsif( $self->can( "handle_class" ) ) { + $handle = $self->handle_class->new; + } $acceptor_params{handle} = $handle if $handle; } === modified file 't/24listener.t' --- t/24listener.t 2013-09-30 19:14:22 +0000 +++ t/24listener.t 2014-10-17 15:54:54 +0000 @@ -178,8 +178,15 @@ } # Subclass -my $sub_newclient; { + my $sub_newclient; + { + package TestListener; + use base qw( IO::Async::Listener ); + + sub on_accept { ( undef, $sub_newclient ) = @_ } + } + my $listener = TestListener->new( handle => $listensock, ); @@ -212,6 +219,36 @@ is_oneref( $listener, 'subclass $listener has refcount 1 after removing from Loop' ); } +# Subclass with handle_constructor +{ + { + package TestListener::WithConstructor; + use base qw( IO::Async::Listener ); + + sub handle_constructor { return IO::Async::Stream->new } + } + + my $accepted; + + my $listener = TestListener::WithConstructor->new( + handle => $listensock, + on_accept => sub { ( undef, $accepted ) = @_; }, + ); + + $loop->add( $listener ); + + my $clientsock = IO::Socket::INET->new( Type => SOCK_STREAM ) + or die "Cannot socket() - $!"; + + $clientsock->connect( $listensock->sockname ) or die "Cannot connect() - $!"; + + wait_for { defined $accepted }; + + isa_ok( $accepted, "IO::Async::Stream", '$accepted with handle_constructor method' ); + + $loop->remove( $listener ); +} + { my $newclient; my $listener = IO::Async::Listener->new( @@ -262,8 +299,3 @@ } done_testing; - -package TestListener; -use base qw( IO::Async::Listener ); - -sub on_accept { ( undef, $sub_newclient ) = @_ }
Released -- Paul Evans