Skip to content

Commit

Permalink
Applied Casey Zacek's patch adding a ConnectTimeout to Client::TCP.
Browse files Browse the repository at this point in the history
  • Loading branch information
rcaputo committed Oct 19, 2002
1 parent cf75184 commit 11a94e8
Showing 1 changed file with 55 additions and 19 deletions.
74 changes: 55 additions & 19 deletions lib/POE/Component/Client/TCP.pm
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use vars qw($VERSION);
$VERSION = (qw($Revision$ ))[1];

use Carp qw(carp croak);
use POSIX qw(ETIMEDOUT);

# Explicit use to import the parameter constants;
use POE::Session;
Expand Down Expand Up @@ -45,6 +46,7 @@ sub new {
my $domain = delete $param{Domain};
my $bind_address = delete $param{BindAddress};
my $bind_port = delete $param{BindPort};
my $ctimeout = delete $param{ConnectTimeout};

foreach ( qw( Connected ConnectError Disconnected ServerInput
ServerError ServerFlushed
Expand Down Expand Up @@ -137,6 +139,11 @@ sub new {
SuccessEvent => 'got_connect_success',
FailureEvent => 'got_connect_error',
);
$_[KERNEL]->alarm_remove( delete $heap->{ctimeout_id} )
if exists $heap->{ctimeout_id};
$heap->{ctimeout_id} = $_[KERNEL]->alarm_set
( got_connect_timeout => time + $ctimeout
) if defined $ctimeout;
},

connect => sub {
Expand All @@ -149,6 +156,9 @@ sub new {
got_connect_success => sub {
my ($kernel, $heap, $socket) = @_[KERNEL, HEAP, ARG0];

$kernel->alarm_remove( delete $heap->{ctimeout_id} )
if exists $heap->{ctimeout_id};

# Ok to overwrite like this as of 0.13.
$_[HEAP]->{server} = POE::Wheel::ReadWrite->new
( Handle => $socket,
Expand All @@ -165,11 +175,24 @@ sub new {

got_connect_error => sub {
my $heap = $_[HEAP];
$_[KERNEL]->alarm_remove( delete $heap->{ctimeout_id} )
if exists $heap->{ctimeout_id};
$heap->{connected} = 0;
$conn_error_callback->(@_);
delete $heap->{server};
},

got_connect_timeout => sub {
my $heap = $_[HEAP];
$heap->{connected} = 0;
$_[KERNEL]->alarm_remove( delete $heap->{ctimeout_id} )
if exists $heap->{ctimeout_id};
$! = ETIMEDOUT;
@_[ARG0,ARG1,ARG2] = ('connect', $!+0, $!);
$conn_error_callback->(@_);
delete $heap->{server};
},

got_server_error => sub {
$error_callback->(@_);
$_[KERNEL]->yield("shutdown") if $_[HEAP]->{shutdown_on_error};
Expand All @@ -191,6 +214,9 @@ sub new {
my $heap = $_[HEAP];
$heap->{shutdown} = 1;

$_[KERNEL]->alarm_remove( delete $heap->{ctimeout_id} )
if exists $heap->{ctimeout_id};

if ($heap->{connected}) {
if (defined $heap->{server}) {
delete $heap->{server}
Expand Down Expand Up @@ -256,25 +282,26 @@ POE::Component::Client::TCP - a simplified TCP client
# Complete usage.
POE::Component::Client::TCP->new
( RemoteAddress => "127.0.0.1",
RemotePort => "chargen",
BindAddress => "127.0.0.1",
BindPort => 8192,
Domain => AF_INET, # Optional.
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 => [ ... ],
( RemoteAddress => "127.0.0.1",
RemotePort => "chargen",
BindAddress => "127.0.0.1",
BindPort => 8192,
Domain => AF_INET, # Optional.
ConnectTimeout => 5, # Seconds; optional.
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.
Expand Down Expand Up @@ -381,6 +408,15 @@ 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().
=item ConnectTimeout
ConnectTimeout is the maximum time in seconds to wait for a connection
to be established. If it is omitted, Client::TCP relies on the
operating system to abort stalled connect() calls.
Upon a connection timeout, Client::TCP will send a ConnectError event.
Its ARG0 will be 'connect' and ARG1 will be the POSIX ETIMEDOUT value.
=item Disconnected
Disconnected is an optional callback to notify a program that an
Expand Down

0 comments on commit 11a94e8

Please sign in to comment.