Danga-Socket-AnyEvent

 view release on metacpan or  search on metacpan

lib/Danga/Socket/AnyEvent.pm  view on Meta::CPAN

            AnyEvent->io(
                fh => $fd,
                poll => $mode,
                cb => _wrap_watcher_cb($coderef),
            )
        } qw(r w) ];
    }
}

=head2 C<< CLASS->SetLoopTimeout( $timeout ) >>

Set the loop timeout for the event loop to some value in milliseconds.

A timeout of 0 (zero) means poll forever. A timeout of -1 means poll and return
immediately.

=cut
sub SetLoopTimeout {
    return $LoopTimeout = $_[1] + 0;
}

=head2 C<< CLASS->DebugMsg( $format, @args ) >>

Print the debugging message specified by the C<sprintf>-style I<format> and
I<args>

=cut
sub DebugMsg {
    my ( $class, $fmt, @args ) = @_;
    chomp $fmt;
    printf STDERR ">>> $fmt\n", @args;
}

=head2 C<< CLASS->AddTimer( $seconds, $coderef ) >>

Add a timer to occur $seconds from now. $seconds may be fractional, but timers
are not guaranteed to fire at the exact time you ask for.

Returns a timer object which you can call C<< $timer->cancel >> on if you need to.

=cut
sub AddTimer {
    my $class = shift;
    my ($secs, $coderef) = @_;

    my $timer = [ undef ];

    my $key = "$timer"; # Just stringify the timer array to get our hash key

    my $cancel = sub {
        delete $Timers{$key};
    };

    my $cb = sub {
        $coderef->();
        $cancel->();
    };

    $timer->[0] = $cancel;

    # We save the watcher in $Timers to keep it alive until it runs,
    # or until $cancel above overwrites it with undef to cause it to
    # get collected.
    $Timers{$key} = AnyEvent->timer(
        after => $secs,
        cb => _wrap_watcher_cb($cb),
    );

    return bless $timer, 'Danga::Socket::Timer';
}

=head2 C<< CLASS->DescriptorMap() >>

Get the hash of Danga::Socket objects keyed by the file descriptor (fileno) they
are wrapping.

Returns a hash in list context or a hashref in scalar context.

=cut
sub DescriptorMap {
    return wantarray ? %DescriptorMap : \%DescriptorMap;
}
*descriptor_map = *DescriptorMap;
*get_sock_ref = *DescriptorMap;

=head2 C<< CLASS->EventLoop() >>

Start processing IO events. In most daemon programs this never exits. See
C<PostLoopCallback> below for how to exit the loop.

=cut

sub EventLoop {
    my $class = shift;

    my $timeout_watcher;
    if ($LoopTimeout && $LoopTimeout != -1) {
        # Return after the given amount of milliseconds (which we must of, of course, convert to seconds)
        my $timeout = $LoopTimeout * 0.001;
        $timeout_watcher = AnyEvent->timer(
            cb => sub { PostEventLoop() },
            after => $timeout,
            interval => $timeout,
        );
    }

    $MainLoopCondVar = AnyEvent->condvar;
    $MainLoopCondVar->recv(); # Blocks until $MainLoopCondVar->send is called

    # Always run PostLoopCallback before we return, even if we timed out before we completed an event.
    PostEventLoop();

    $MainLoopCondVar = undef;
}

## profiling-related data/functions
our ($Prof_utime0, $Prof_stime0);
sub _pre_profile {
    ($Prof_utime0, $Prof_stime0) = getrusage();
}



( run in 0.502 second using v1.01-cache-2.11-cpan-39bf76dae61 )