AnyEvent-Connection

 view release on metacpan or  search on metacpan

README  view on Meta::CPAN

    connect
        Begin connection

    disconnect ($reason)
        Close current connection. reason is optional

    reconnect
        Close current connection and establish a new one

    after($interval, $cb->())
        Helper method. AE::timer(after), associated with current connection

        Will be destroyed if connection is destroyed, so no timer invocation
        after connection destruction.

    periodic($interval, $cb->())
        Helper method. AE::timer(periodic), associated with current
        connection

        Will be destroyed if connection is destroyed, so no timer invocation
        after connection destruction.

    periodic_stop()
        If called within periodic callback, periodic will be stopped.

        my $count;
        $client->periodic(1,sub {
            $client->periodic_stop if ++$count > 10;
        });

        # callback will be called only 10 times;

    destroy
        Close connection, destroy all associated objects and timers, clean
        self

CONNECT METHODS
    When connected, there are some methods, that proxied to raw connection
    or to AE::Handle

    push_write
        See AE::Handle::push_write

    push_read

lib/AnyEvent/Connection.pm  view on Meta::CPAN


Close current connection. reason is optional

=item reconnect


Close current connection and establish a new one

=item after($interval, $cb->())

Helper method. AE::timer(after), associated with current connection

Will be destroyed if connection is destroyed, so no timer invocation after connection destruction.

=item periodic($interval, $cb->())

Helper method. AE::timer(periodic), associated with current connection

Will be destroyed if connection is destroyed, so no timer invocation after connection destruction.

=item periodic_stop()

If called within periodic callback, periodic will be stopped.

    my $count;
    $client->periodic(1,sub {
        $client->periodic_stop if ++$count > 10;
    });
    
    # callback will be called only 10 times;

=item destroy

Close connection, destroy all associated objects and timers, clean self

=back

=head1 CONNECT METHODS

When connected, there are some methods, that proxied to raw connection or to AE::Handle


=over 4

lib/AnyEvent/Connection.pm  view on Meta::CPAN

	return $self;
}

sub init {
	my $self = shift;
	$self->{debug}   ||= 0;
	$self->{connected} = 0;
	$self->{connecting} = 0;
	$self->{reconnect} = 1 unless defined $self->{reconnect};
	$self->{timeout} ||= 3;
	$self->{timers}    = {};
	$self->{rawcon}  ||= 'AnyEvent::Connection::Raw';
	#warn "Init $self";
}

#sub connected {
#	warn "Connected";
#	shift->event(connected => ());
#}

sub connect {

lib/AnyEvent/Connection.pm  view on Meta::CPAN

}

sub accept {
	croak "Not implemented yet";
}


sub _reconnect_after {
	weaken( my $self = shift );
	$self->{reconnect} or return $self->{connecting} = 0;
	$self->{timers}{reconnect} = AnyEvent->timer(
		after => $self->{reconnect},
		cb => sub {
			$self or return;
			delete $self->{timers}{reconnect};
			$self->{connecting} = 0;
			$self->connect;
		}
	);
}

sub periodic_stop;
sub periodic {
	weaken( my $self = shift );
	my $interval = shift;
	my $cb = shift;
	#warn "Create periodic $interval";
	$self->{timers}{int $cb} = AnyEvent->timer(
		after => $interval,
		interval => $interval,
		cb => sub {
			local *periodic_stop = sub {
				warn "Stopping periodic ".int $cb;
				delete $self->{timers}{int $cb}; undef $cb
			};
			$self or return;
			$cb->();
		},
	);
	defined wantarray and return AnyEvent::Util::guard(sub {
		delete $self->{timers}{int $cb};
		undef $cb;
	});
	return;
}

sub after {
	weaken( my $self = shift );
	my $interval = shift;
	my $cb = shift;
	#warn "Create after $interval";
	$self->{timers}{int $cb} = AnyEvent->timer(
		after => $interval,
		cb => sub {
			$self or return;
			delete $self->{timers}{int $cb};
			$cb->();
			undef $cb;
		},
	);
	defined wantarray and return AnyEvent::Util::guard(sub {
		delete $self->{timers}{int $cb};
		undef $cb;
	});
	return;
}

sub reconnect {
	my $self = shift;
	$self->disconnect;
	$self->connect;
}

lib/AnyEvent/Connection.pm  view on Meta::CPAN

	#$self->{con} or return;
	#warn "Disconnecting $self->{connected} || $self->{connecting} || $self->{reconnect} by @{[ (caller)[1,2] ]}";
	ref $self->{con} eq 'HASH' and warn dumper($self->{con});
	$self->{con} and eval{ $self->{con}->close; };
	warn if $@;
	delete $self->{con};
	my $wascon = $self->{connected} || $self->{connecting};
	$self->{connected}  = 0;
	$self->{connecting} = 0;
	#$self->{reconnect}  = 0;
	delete $self->{timers}{reconnect};
	$self->event('disconnect',@_) if $wascon;
	return;
}

sub AnyEvent::Connection::destroyed::AUTOLOAD {}

sub destroy {
	my ($self) = @_;
	$self->DESTROY;
	bless $self, "AnyEvent::Connection::destroyed";



( run in 1.042 second using v1.01-cache-2.11-cpan-49f99fa48dc )