POE::Component::Client::TCP - a simplified TCP client |
POE::Component::Client::TCP - a simplified TCP client
use POE qw(Component::Client::TCP);
# Basic usage.
POE::Component::Client::TCP->new ( RemoteAddress => "127.0.0.1", RemotePort => "chargen", Domain => AF_INET, # Optional. Alias => $session_alias # Optional. ServerInput => sub { my $input = $_[ARG0]; print "from server: $input\n"; } );
# Complete usage.
my $session_id = POE::Component::Client::TCP->new ( RemoteAddress => "127.0.0.1", RemotePort => "chargen", BindAddress => "127.0.0.1", BindPort => 8192, Domain => AF_INET, # Optional. Alias => $session_alias # Optional. ConnectTimeout => 5, # Seconds; optional.
SessionType => "POE::Session::Abc", # Optional. SessionParams => [ options => { debug => 1 } ], # Optional.
Started => \&handle_starting, # Optional. Args => [ "arg0", "arg1" ], # Optional. Start args.
Connected => \&handle_connect, ConnectError => \&handle_connect_error, Disconnected => \&handle_disconnect,
ServerInput => \&handle_server_input, ServerError => \&handle_server_error, ServerFlushed => \&handle_server_flush,
Filter => "POE::Filter::Something",
InlineStates => { ... }, PackageStates => [ ... ], ObjectStates => [ ... ], );
# Sample callbacks.
sub handle_start { my @args = @_[ARG0..$#_]; }
sub handle_connect { my ($socket, $peer_address, $peer_port) = @_[ARG0, ARG1, ARG2]; }
sub handle_connect_error { my ($syscall_name, $error_number, $error_string) = @_[ARG0, ARG1, ARG2]; }
sub handle_disconnect { # no special parameters }
sub handle_server_input { my $input_record = $_[ARG0]; }
sub handle_server_error { my ($syscall_name, $error_number, $error_string) = @_[ARG0, ARG1, ARG2]; }
sub handle_server_flush { # no special parameters }
# Reserved HEAP variables:
$heap->{server} = ReadWrite wheel representing the server. $heap->{shutdown} = Shutdown flag (check to see if shutting down). $heap->{connected} = Connected flag (check to see if session is connected). $heap->{shutdown_on_error} = Automatically disconnect on error.
# Accepted public events.
$kernel->yield( "connect", $host, $port ) # connect to a new host/port $kernel->yield( "reconnect" ) # reconnect to the previous host/port $kernel->yield( "shutdown" ) # shut down a connection gracefully
# Responding to a server.
$heap->{server}->put(@things_to_send);
The TCP client component hides the steps needed to create a client using Wheel::SocketFactory and Wheel::ReadWrite. The steps aren't many, but they're still tiresome after a while.
POE::Component::Client::TCP supplies common defaults for most callbacks and handlers. The authors hope that clients can be created with as little work as possible.
new()
method can accept quite a lot of parameters. It will return
the session ID of the accecptor session. One must use callbacks to
check for errors rather than the return value of new().
SessionType => "POE::Session::MultiDispatch"
SessionType is optional. The component will supply a ``POE::Session'' type if none is specified.
SessionParams => [ options => { debug => 1, trace => 1 } ],
It is important to realize that some of the arguments to SessionHandler may get clobbered when defining them for your SessionHandler. It is advised that you stick to defining arguments in the ``options'' hash such as trace and debug. See the POE::Session manpage for an example list of options.
ConnectError must contain a subroutine reference. The subroutine will be called as a SocketFactory error handler. In addition to the usual POE event parameters, ARG0 will contain the name of the syscall that failed. ARG1 will contain the numeric version of $! after the failure, and ARG2 will contain $!'s string version.
Depending on the nature of the error and the type of client, it may be useful to post a reconnect event from ConnectError's callback.
sub handle_connect_error { $_[KERNEL]->delay( reconnect => 60 ); }
The component will shut down after ConnectError if a reconnect isn't requested.
ARG0 contains a socket handle. It's not necessary to save this under most circumstances. ARG1 and ARG2 contain the peer address and port as returned from getpeername().
connect()
calls.
Upon a connection timeout, Client::TCP will send a ConnectError event. Its ARG0 will be 'connect' and ARG1 will be the POSIX/Errno ETIMEDOUT value.
For persistent connections, such as MUD bots or long-running services, a useful thing to do from a Disconnected handler is reconnect. For example, this reconnects after waiting a minute:
sub handle_disconnect { $_[KERNEL]->delay( reconnect => 60 ); }
The component will shut down after disconnecting if a reconnect isn't requested.
Note: AF_INET6 and PF_INET6 are supplied by the Socket6 module, which is available on the CPAN. You must have Socket6 loaded before POE::Component::Server::TCP will create IPv6 sockets.
Filter => "POE::Filter::Line",
If it is a list reference, the first item in the list will be a POE::Filter class name, and the remaining items will be constructor parameters for the filter. For example, this changes the line separator to a vertical pipe:
Filter => [ "POE::Filter::Line", Literal => "|" ],
If it is an object, it will be clone()'d.
Filter => POE::Filter::Line->new()
Filter is optional. The component will supply a ``POE::Filter::Line''
instance none is specified. If you supply a different value for
Filter, then you must also use
that filter class.
create()
method.
create()
method.
create()
method.
ARG0 contains the name of the syscall that failed. ARG1 contains the numeric failure code from $!. ARG2 contains the string version of $!.
The component will shut down after a server error if a reconnect isn't requested.
The component will shut down after a server flush if $heap->{shutdown} is set.
The ServerInput function will stop being called when $heap->{shutdown} is true.
The Args parameter can be used to pass initialization values to the Started callback, eliminating the need for closures to get values into the component. These values are included in the @_[ARG0..$#_] parameters.
POE::Component::Server::TCP, POE::Wheel::SocketFactory, POE::Wheel::ReadWrite, POE::Filter
This may not be suitable for complex client tasks. After a point, it becomes easier to roll a custom client using POE::Wheel::SocketFactory and POE::Wheel::ReadWrite.
This looks nothing like what Ann envisioned.
POE::Component::Client::TCP is Copyright 2001-2006 by Rocco Caputo. All rights are reserved. POE::Component::Client::TCP is free software, and it may be redistributed and/or modified under the same terms as Perl itself.
POE::Component::Client::TCP is based on code, used with permission, from Ann Barcomb <kudra@domaintje.com>.
POE::Component::Client::TCP is based on code, used with permission, from Jos Boumans <kane@cpan.org>.
POE::Component::Client::TCP - a simplified TCP client |