| 1 | |||
| 2 | package IO::Event::Callback; | ||
| 3 | |||
| 4 | # ABSTRACT: A closure based API for IO::Event | ||
| 5 | our $VERSION = '0.814'; # VERSION | ||
| 6 | |||
| 7 | use strict; | ||
| 8 | use warnings; | ||
| 9 | |||
| 10 | use IO::Event; | ||
| 11 | |||
| 12 | our @handlers; | ||
| 13 | BEGIN { | ||
| 14 | @handlers = qw(input connection read_ready werror eof output | ||
| 15 | outputdone connected connect_failed died timer exception | ||
| 16 | outputoverflow); | ||
| 17 | } | ||
| 18 | |||
| 19 | sub new | ||
| 20 | { | ||
| 21 | my ($pkg, $filehandle, %h) = @_; | ||
| 22 | |||
| 23 | my $ro = $h{read_only}; | ||
| 24 | my $wo = $h{write_only}; | ||
| 25 | delete $h{read_only}; | ||
| 26 | delete $h{write_only}; | ||
| 27 | |||
| 28 | my $self = handler($pkg, %h); | ||
| 29 | |||
| 30 | return IO::Event->new($filehandle, $self, read_only => $ro, write_only => $wo); | ||
| 31 | } | ||
| 32 | |||
| 33 | sub ie_input { $_[0]->{'ie_input'}->(@_) }; | ||
| 34 | sub ie_connection { $_[0]->{'ie_connection'}->(@_) }; | ||
| 35 | sub ie_read_ready { $_[0]->{'ie_read_ready'}->(@_) }; | ||
| 36 | sub ie_werror { $_[0]->{'ie_werror'}->(@_) }; | ||
| 37 | sub ie_eof { $_[0]->{'ie_eof'}->(@_) }; | ||
| 38 | sub ie_output { $_[0]->{'ie_output'}->(@_) }; | ||
| 39 | sub ie_outputdone { $_[0]->{'ie_outputdone'}->(@_) }; | ||
| 40 | sub ie_connected { $_[0]->{'ie_connected'}->(@_) }; | ||
| 41 | sub ie_connect_failed { $_[0]->{'ie_connect_failed'}->(@_) }; | ||
| 42 | sub ie_died { $_[0]->{'ie_died'}->(@_) }; | ||
| 43 | sub ie_timer { $_[0]->{'ie_timer'}->(@_) }; | ||
| 44 | sub ie_exception { $_[0]->{'ie_exception'}->(@_) }; | ||
| 45 | sub ie_outputoverflow { $_[0]->{'ie_outputoverflow'}->(@_) }; | ||
| 46 | |||
| 47 | sub handler | ||
| 48 | { | ||
| 49 | my ($pkg, %h) = @_; | ||
| 50 | |||
| 51 | my $self = bless {}, $pkg; | ||
| 52 | |||
| 53 | for my $h (@handlers) { | ||
| 54 | my $key = | ||
| 55 | exists($h{$h}) ? $h : | ||
| 56 | exists($h{"ie_$h"}) ? "ie_$h" : undef; | ||
| 57 | if ($key) { | ||
| 58 | $self->{"ie_$h"} = $h{$key}; | ||
| 59 | delete $h{$key}; | ||
| 60 | } else { | ||
| 61 | $self->{"ie_$h"} = sub {}; | ||
| 62 | } | ||
| 63 | } | ||
| 64 | my @k = keys %h; | ||
| 65 | die "unexpected parameters: @k" if @k; | ||
| 66 | return $self; | ||
| 67 | } | ||
| 68 | |||
| 69 | sub sock2handler | ||
| 70 | { | ||
| 71 | my ($pkg, $sref) = @_; | ||
| 72 | my %h; | ||
| 73 | for my $h (@handlers) { | ||
| 74 | next unless exists $sref->{$h}; | ||
| 75 | my $key = | ||
| 76 | exists($sref->{$h}) ? $h : | ||
| 77 | exists($sref->{"ie_$h"}) ? "ie_$h" : next; | ||
| 78 | $h{$h} = $sref->{$key}; | ||
| 79 | delete $sref->{$key}; | ||
| 80 | } | ||
| 81 | my $handler = handler($pkg,%h); | ||
| 82 | } | ||
| 83 | |||
| 84 | package IO::Event::INET::Callback; | ||
| 85 | |||
| 86 | use strict; | ||
| 87 | use warnings; | ||
| 88 | |||
| 89 | sub new | ||
| 90 | { | ||
| 91 | my ($pkg, %sock) = @_; | ||
| 92 | my $handler = IO::Event::Callback->sock2handler(\%sock); | ||
| 93 | return IO::Event::INET->new(%sock, Handler => $handler); | ||
| 94 | } | ||
| 95 | |||
| 96 | package IO::Event::UNIX::Callback; | ||
| 97 | |||
| 98 | use strict; | ||
| 99 | use warnings; | ||
| 100 | |||
| 101 | sub new | ||
| 102 | { | ||
| 103 | my ($pkg, %sock) = @_; | ||
| 104 | my $handler = IO::Event::Callback->sock2handler(\%sock); | ||
| 105 | return IO::Event::UNIX->new(%sock, Handler => $handler); | ||
| 106 | } | ||
| 107 | |||
| 108 | 1; | ||
| 109 | |||
| 110 | __END__ |