NAME

Reflex::Role::Connecting - add non-blocking client connecting to a class

VERSION

version 0.056

SYNOPSIS

package Reflex::Connector;

use Moose;
extends 'Reflex::Base';

has socket => (
	is        => 'rw',
	isa       => 'FileHandle',
);

has port => (
	is => 'ro',
	isa => 'Int',
);

has address => (
	is      => 'ro',
	isa     => 'Str',
	default => '127.0.0.1',
);

with 'Reflex::Role::Connecting' => {
	connector   => 'socket',      # Default!
	address     => 'address',     # Default!
	port        => 'port',        # Default!
	cb_success  => 'on_connection',
	cb_error    => 'on_error',
};

1;

DESCRIPTION

Reflex::Role::Connecting is a Moose parameterized role that adds non-blocking connect() behaviors to classes.

See Reflex::Connector if you prefer runtime composition with objects, or if Moose syntax just gives you the willies.

Required Role Parameters

None.

Optional Parameters

address

address defines the attribute that will contain the address to which the class will connect. The address() attribute will be used if the class doesn't override the name. The default address will be the IPv4 localhost, "127.0.0.1".

port

port defines the attribute that will contain the port to which this role will connect. By default, the role will use the port() attribute. There is no default port() value.

socket

The socket parameter must contain the name of an attribute that contains the connecting socket handle. "socket" will be used if a name isn't provided. A socket must be provided if two or more client connections will be created from the same class, otherwise they will both attempt to use the same "socket".

Reflex::Role::Connecting will create a plain TCP socket if socket's attribute is empty at connecting time. A class may build its own socket, if it needs to set special options.

The role will generate additional methods and callbacks that are named after socket. For example, if socket contains XYZ, then the default error callback will be on_XYZ_error().

cb_success

cb_success overrides the default name for the class's successful connection handler method. This handler will be called whenever a client connection is successfully connected.

The default method name is "on_${socket}_success", where $socket is the name of the socket attribute. This role defines a default callback that emits an "success" event.

All callback methods receive two parameters: $self and an anonymous hash containing information specific to the callback. In cb_success's case, the anonymous hash contains one value: the socket that has just established a connection.

cb_error

cb_error names the $self method that will be called whenever connect() encounters an error. By default, this method will be the catenation of "on_", the socket name, and "_error". As in on_XYZ_error(), if the socket attribute is named "XYZ". The role defines a default callback that will emit an "error" event with cb_error()'s parameters.

cb_error callbacks receive two parameters, $self and an anonymous hashref of named values specific to the callback. Reflex error callbacks include three standard values. errfun contains a single word description of the function that failed. errnum contains the numeric value of $! at the time of failure. errstr holds the stringified version of $!.

Values of $! are passed as parameters since the global variable may change before the callback can be invoked.

When overriding this callback, please be sure to call stopped(), which is provided by Reflex::Role::Collectible. Calling stopped() is vital for collectible objects to be released from memory when managed by Reflex::Collection.

EXAMPLES

TODO - I'm sure there are some.

SEE ALSO

Reflex Reflex::Role::Accepting Reflex::Acceptor Reflex::Connector

"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