… | |
… | |
4 | |
4 | |
5 | =head1 SYNOPSIS |
5 | =head1 SYNOPSIS |
6 | |
6 | |
7 | use AnyEvent::Util; |
7 | use AnyEvent::Util; |
8 | |
8 | |
9 | inet_aton $name, $cb->($ipn || undef); |
|
|
10 | |
|
|
11 | =head1 DESCRIPTION |
9 | =head1 DESCRIPTION |
12 | |
10 | |
13 | This module implements various utility functions, mostly replacing |
11 | This module implements various utility functions, mostly replacing |
14 | well-known functions by event-ised counterparts. |
12 | well-known functions by event-ised counterparts. |
|
|
13 | |
|
|
14 | All functions documented without C<AnyEvent::Util::> prefix are exported |
|
|
15 | by default. |
15 | |
16 | |
16 | =over 4 |
17 | =over 4 |
17 | |
18 | |
18 | =cut |
19 | =cut |
19 | |
20 | |
20 | package AnyEvent::Util; |
21 | package AnyEvent::Util; |
21 | |
22 | |
|
|
23 | no warnings; |
22 | use strict; |
24 | use strict; |
23 | |
25 | |
24 | no warnings "uninitialized"; |
26 | use Carp (); |
25 | |
27 | use Errno (); |
26 | use Socket (); |
28 | use Socket (); |
27 | |
29 | |
28 | use AnyEvent; |
30 | use AnyEvent (); |
29 | |
31 | |
30 | use base 'Exporter'; |
32 | use base 'Exporter'; |
31 | |
33 | |
32 | #our @EXPORT = qw(gethostbyname gethostbyaddr); |
34 | BEGIN { |
33 | our @EXPORT_OK = qw(inet_aton); |
35 | *socket_inet_aton = \&Socket::inet_aton; # take a copy, in case Coro::LWP overrides it |
|
|
36 | } |
|
|
37 | |
|
|
38 | our @EXPORT = qw(fh_nonblocking guard); |
34 | |
39 | |
35 | our $VERSION = '1.0'; |
40 | our $VERSION = '1.0'; |
36 | |
41 | |
37 | our $MAXPARALLEL = 16; # max. number of parallel jobs |
42 | our $MAXPARALLEL = 16; # max. number of parallel jobs |
38 | |
43 | |
… | |
… | |
74 | sub _do_asy { |
79 | sub _do_asy { |
75 | push @queue, [@_]; |
80 | push @queue, [@_]; |
76 | _schedule; |
81 | _schedule; |
77 | } |
82 | } |
78 | |
83 | |
|
|
84 | # to be removed |
79 | sub dotted_quad($) { |
85 | sub dotted_quad($) { |
80 | $_[0] =~ /^(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[0-9][0-9]?) |
86 | $_[0] =~ /^(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[0-9][0-9]?) |
81 | \.(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[0-9][0-9]?) |
87 | \.(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[0-9][0-9]?) |
82 | \.(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[0-9][0-9]?) |
88 | \.(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[0-9][0-9]?) |
83 | \.(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[0-9][0-9]?)$/x |
89 | \.(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[0-9][0-9]?)$/x |
84 | } |
90 | } |
85 | |
91 | |
86 | my $has_ev_adns; |
92 | # just a forwarder |
87 | |
93 | sub inet_aton { |
88 | sub has_ev_adns { |
94 | require AnyEvent::Socket; |
89 | ($has_ev_adns ||= do { |
95 | *inet_aton = \&AnyEvent::Socket::inet_aton; |
90 | my $model = AnyEvent::detect; |
96 | goto &inet_aton |
91 | (($model eq "AnyEvent::Impl::CoroEV" or $model eq "AnyEvent::Impl::EV") |
|
|
92 | && eval { local $SIG{__DIE__}; require EV::ADNS }) |
|
|
93 | ? 2 : 1 # so that || always detects as true |
|
|
94 | }) - 1 # 2 => true, 1 => false |
|
|
95 | } |
97 | } |
96 | |
98 | |
97 | =item AnyEvent::Util::inet_aton $name_or_address, $cb->($binary_address_or_undef) |
99 | =item fh_nonblocking $fh, $nonblocking |
98 | |
100 | |
99 | Works almost exactly like its Socket counterpart, except that it uses a |
101 | Sets the blocking state of the given filehandle (true == nonblocking, |
100 | callback. |
102 | false == blocking). Uses fcntl on anything sensible and ioctl FIONBIO on |
|
|
103 | broken (i.e. windows) platforms. |
101 | |
104 | |
102 | =cut |
105 | =cut |
103 | |
106 | |
104 | sub inet_aton { |
107 | sub fh_nonblocking($$) { |
105 | my ($name, $cb) = @_; |
108 | my ($fh, $nb) = @_; |
106 | |
109 | |
107 | if (&dotted_quad) { |
110 | require Fcntl; |
108 | $cb->(Socket::inet_aton $name); |
111 | |
109 | } elsif (&has_ev_adns) { |
112 | if ($^O eq "MSWin32") { |
110 | EV::ADNS::submit ($name, &EV::ADNS::r_addr, 0, sub { |
113 | $nb = (! ! $nb) + 0; |
111 | my (undef, undef, @a) = @_; |
114 | ioctl $fh, 0x8004667e, \$nb; # FIONBIO |
112 | $cb->(@a ? Socket::inet_aton $a[0] : undef); |
|
|
113 | }); |
|
|
114 | } else { |
115 | } else { |
115 | _do_asy $cb, sub { Socket::inet_aton $_[0] }, @_; |
116 | fcntl $fh, &Fcntl::F_SETFL, $nb ? &Fcntl::O_NONBLOCK : 0; |
116 | } |
117 | } |
|
|
118 | } |
|
|
119 | |
|
|
120 | =item $guard = guard { CODE } |
|
|
121 | |
|
|
122 | This function creates a special object that, when called, will execute the |
|
|
123 | code block. |
|
|
124 | |
|
|
125 | This is often handy in continuation-passing style code to clean up some |
|
|
126 | resource regardless of where you break out of a process. |
|
|
127 | |
|
|
128 | You can call one method on the returned object: |
|
|
129 | |
|
|
130 | =item $guard->cancel |
|
|
131 | |
|
|
132 | This simply causes the code block not to be invoked: it "cancels" the |
|
|
133 | guard. |
|
|
134 | |
|
|
135 | =cut |
|
|
136 | |
|
|
137 | sub AnyEvent::Util::Guard::DESTROY { |
|
|
138 | ${$_[0]}->(); |
|
|
139 | } |
|
|
140 | |
|
|
141 | sub AnyEvent::Util::Guard::cancel($) { |
|
|
142 | ${$_[0]} = sub { }; |
|
|
143 | } |
|
|
144 | |
|
|
145 | sub guard(&) { |
|
|
146 | bless \(my $cb = shift), AnyEvent::Util::Guard:: |
117 | } |
147 | } |
118 | |
148 | |
119 | 1; |
149 | 1; |
120 | |
150 | |
121 | =back |
151 | =back |