Acme-Sort-Sleep
view release on metacpan or search on metacpan
local/lib/perl5/IO/Async/Loop.pm view on Meta::CPAN
=cut
sub set_resolver
{
my $self = shift;
my ( $resolver ) = @_;
$resolver->can( $_ ) or croak "Resolver is unsuitable as it does not implement $_"
for qw( resolve getaddrinfo getnameinfo );
$self->{resolver} = $resolver;
$self->add( $resolver );
}
=head2 resolve
@result = $loop->resolve( %params )->get
This method performs a single name resolution operation. It uses an
internally-stored L<IO::Async::Resolver> object. For more detail, see the
C<resolve> method on the L<IO::Async::Resolver> class.
=cut
sub resolve
{
my $self = shift;
my ( %params ) = @_;
$self->resolver->resolve( %params );
}
=head2 connect
$handle|$socket = $loop->connect( %params )->get
This method performs a non-blocking connection to a given address or set of
addresses, returning a L<IO::Async::Future> which represents the operation. On
completion, the future will yield the connected socket handle, or the given
L<IO::Async::Handle> object.
There are two modes of operation. Firstly, a list of addresses can be provided
which will be tried in turn. Alternatively as a convenience, if a host and
service name are provided instead of a list of addresses, these will be
resolved using the underlying loop's C<resolve> method into the list of
addresses.
When attempting to connect to any among a list of addresses, there may be
failures among the first attempts, before a valid connection is made. For
example, the resolver may have returned some IPv6 addresses, but only IPv4
routes are valid on the system. In this case, the first C<connect(2)> syscall
will fail. This isn't yet a fatal error, if there are more addresses to try,
perhaps some IPv4 ones.
For this reason, it is possible that the operation eventually succeeds even
though some system calls initially fail. To be aware of individual failures,
the optional C<on_fail> callback can be used. This will be invoked on each
individual C<socket(2)> or C<connect(2)> failure, which may be useful for
debugging or logging.
Because this module simply uses the C<getaddrinfo> resolver, it will be fully
IPv6-aware if the underlying platform's resolver is. This allows programs to
be fully IPv6-capable.
In plain address mode, the C<%params> hash takes the following keys:
=over 8
=item addrs => ARRAY
Reference to an array of (possibly-multiple) address structures to attempt to
connect to. Each should be in the layout described for C<addr>. Such a layout
is returned by the C<getaddrinfo> named resolver.
=item addr => HASH or ARRAY
Shortcut for passing a single address to connect to; it may be passed directly
with this key, instead of in another array on its own. This should be in a
format recognised by L<IO::Async::OS>'s C<extract_addrinfo> method.
This example shows how to use the C<Socket> functions to construct one for TCP
port 8001 on address 10.0.0.1:
$loop->connect(
addr => {
family => "inet",
socktype => "stream",
port => 8001,
ip => "10.0.0.1",
},
...
);
This example shows another way to connect to a UNIX socket at F<echo.sock>.
$loop->connect(
addr => {
family => "unix",
socktype => "stream",
path => "echo.sock",
},
...
);
=item local_addrs => ARRAY
=item local_addr => HASH or ARRAY
Optional. Similar to the C<addrs> or C<addr> parameters, these specify a local
address or set of addresses to C<bind(2)> the socket to before
C<connect(2)>ing it.
=back
When performing the resolution step too, the C<addrs> or C<addr> keys are
ignored, and instead the following keys are taken:
=over 8
( run in 0.543 second using v1.01-cache-2.11-cpan-39bf76dae61 )