Reflex::Connector - Connect to a server without blocking.
version 0.005
This is an incomplete excerpt from Reflex::Client. See that module's source for a more complete example.
package SomeKindaClient; use Moose; extends 'Reflex::Connector'; sub on_connector_success { my ($self, $args) = @_; # Do something with $arg->{socket} here. } sub on_connector_failure { my ($self, $args) = @_; warn "$args->{errfun} error $args->{errnum}: $args->{errstr}\n"; $self->stop(); }
Reflex objects may also be used in condvar-like ways. This excerpts from eg/eg-38-promise-client.pl in the distribution.
my $connector = Reflex::Connector->new(remote_port => 12345); my $event = $connector->wait(); if ($event->{name} eq "failure") { eg_say("connection error $event->{arg}{errnum}: $event->{arg}{errstr}"); exit; } eg_say("Connected."); # Do something with $event->{arg}{socket}.
Reflex::Connector performs a non-blocking connect() object on a plain socket. It extends Reflex::Handle to wait for the connection without blocking the rest of a program.
By default, it will create its own TCP socket. A program can provide a specially prepared socket via the inherited "handle" attribute.
Two other attributes, "remote_addr" and "remote_port" specify where to connect the socket.
This connector was written with TCP in mind, but it's intended to also be useful for other connected sockets.
Reflex::Connector supplies its own attributes in addition to those provided by Reflex::Handle.
The "remote_addr" attribute specifies the address of a remote server. It defaults to "127.0.0.1".
The "remote_port" attribute sets the port of the server to which it will attempt a connection. The remote port may be an integer or the symbolic port name from /etc/services.
Reflex::Connector inherits its methods from Reflex::Handle. It doesn't add new methods at this time.
Reflex::Connector emits some events, which may be mapped to a subclass' methods, or to handlers in a container object. Please see Reflex and Reflex::Callbacks for more information.
Revlex::Connector emits a "failure" event if it can't establish a connection. Failure events include a few, fairly standard parameters:
socket - Undefined, since a connection could not be made.
errnum - The numeric value of $! at the time of error.
errstr - The string value of $! at the time of error.
errfun - A brief description of the function call that failed.
The "success" event is emitted if a connection has been established. It will return a "socket", the value of which is the connected socket.
Reflex::Client extends Reflex::Connector to include a Reflex::Stream when the socket is connected.
eg/eg-38-promise-client.pl shows how to use Reflex::Connector in a condvar-like fashion.
Reflex Reflex::Client
"ACKNOWLEDGEMENTS" in Reflex "ASSISTANCE" in Reflex "AUTHORS" in Reflex "BUGS" in Reflex "BUGS" in Reflex "CONTRIBUTORS" in Reflex "COPYRIGHT" in Reflex "LICENSE" in Reflex "TODO" in Reflex
To install Reflex, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Reflex
CPAN shell
perl -MCPAN -e shell install Reflex
For more information on module installation, please visit the detailed CPAN module installation guide.