ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/AnyEvent/lib/AnyEvent/Util.pm
(Generate patch)

Comparing AnyEvent/lib/AnyEvent/Util.pm (file contents):
Revision 1.5 by root, Sun Apr 27 20:17:46 2008 UTC vs.
Revision 1.25 by root, Sat May 24 01:15:19 2008 UTC

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
13This module implements various utility functions, mostly replacing 11This module implements various utility functions, mostly replacing
14well-known functions by event-ised counterparts. 12well-known functions by event-ised counterparts.
13
14All functions documented without C<AnyEvent::Util::> prefix are exported
15by default.
15 16
16=over 4 17=over 4
17 18
18=cut 19=cut
19 20
20package AnyEvent::Util; 21package AnyEvent::Util;
21 22
23no warnings;
22use strict; 24use strict;
23 25
24no warnings "uninitialized"; 26use Carp ();
25 27use Errno ();
26use Socket (); 28use Socket ();
27 29
28use AnyEvent; 30use AnyEvent ();
29 31
30use base 'Exporter'; 32use base 'Exporter';
31 33
32#our @EXPORT = qw(gethostbyname gethostbyaddr); 34BEGIN {
33our @EXPORT_OK = qw(inet_aton); 35 *socket_inet_aton = \&Socket::inet_aton; # take a copy, in case Coro::LWP overrides it
36}
37
38our @EXPORT = qw(fh_nonblocking guard);
34 39
35our $VERSION = '1.0'; 40our $VERSION = '1.0';
36 41
37our $MAXPARALLEL = 16; # max. number of parallel jobs 42our $MAXPARALLEL = 16; # max. number of parallel jobs
38 43
74sub _do_asy { 79sub _do_asy {
75 push @queue, [@_]; 80 push @queue, [@_];
76 _schedule; 81 _schedule;
77} 82}
78 83
84# to be removed
79sub dotted_quad($) { 85sub 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
86my $has_ev_adns; 92# just a forwarder
87 93sub inet_aton {
88sub 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
99Works almost exactly like its Socket counterpart, except that it uses a 101Sets the blocking state of the given filehandle (true == nonblocking,
100callback. 102false == blocking). Uses fcntl on anything sensible and ioctl FIONBIO on
103broken (i.e. windows) platforms.
101 104
102=cut 105=cut
103 106
104sub inet_aton { 107sub 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
122This function creates a special object that, when called, will execute the
123code block.
124
125This is often handy in continuation-passing style code to clean up some
126resource regardless of where you break out of a process.
127
128You can call one method on the returned object:
129
130=item $guard->cancel
131
132This simply causes the code block not to be invoked: it "cancels" the
133guard.
134
135=cut
136
137sub AnyEvent::Util::Guard::DESTROY {
138 ${$_[0]}->();
139}
140
141sub AnyEvent::Util::Guard::cancel($) {
142 ${$_[0]} = sub { };
143}
144
145sub guard(&) {
146 bless \(my $cb = shift), AnyEvent::Util::Guard::
117} 147}
118 148
1191; 1491;
120 150
121=back 151=back

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines