summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJesse Luehrs <doy@tozt.net>2009-11-05 18:51:27 -0600
committerJesse Luehrs <doy@tozt.net>2009-11-05 18:51:27 -0600
commita47c9a50872f540d96e8fda36bd57525ccce5d18 (patch)
tree45f2d3ebe4686bc3222eff0ea379d39b04f7fb5e
parentc5e903fa1fd17b3cd788712f5524620498eedcba (diff)
downloadapp-termcast-a47c9a50872f540d96e8fda36bd57525ccce5d18.tar.gz
app-termcast-a47c9a50872f540d96e8fda36bd57525ccce5d18.zip
automatically reconnect if the connection to the server drops
-rw-r--r--lib/App/Termcast.pm29
1 files changed, 26 insertions, 3 deletions
diff --git a/lib/App/Termcast.pm b/lib/App/Termcast.pm
index 05dac86..da24a7c 100644
--- a/lib/App/Termcast.pm
+++ b/lib/App/Termcast.pm
@@ -59,6 +59,13 @@ has bell_on_watcher => (
. " or disconnects",
);
+has timeout => (
+ is => 'rw',
+ isa => 'Int',
+ default => 5,
+ documentation => "Timeout length for the connection to the termcast server",
+);
+
has _got_winch => (
traits => ['NoGetopt'],
is => 'rw',
@@ -67,14 +74,20 @@ has _got_winch => (
init_arg => undef,
);
+sub connect {
+ my $self = shift;
+ my $socket = IO::Socket::INET->new(PeerAddr => $self->host,
+ PeerPort => $self->port);
+ $socket->write('hello '.$self->user.' '.$self->password."\n");
+ return $socket;
+}
+
sub run {
my $self = shift;
my @argv = @{ $self->extra_argv };
push @argv, ($ENV{SHELL} || '/bin/sh') if !@argv;
- my $socket = IO::Socket::INET->new(PeerAddr => $self->host,
- PeerPort => $self->port);
- $socket->write('hello '.$self->user.' '.$self->password."\n");
+ my $socket = $self->connect;
my $sockfd = fileno($socket);
my $pty = IO::Pty::Easy->new(raw => 0);
@@ -85,6 +98,8 @@ sub run {
vec($rin, fileno(STDIN) ,1) = 1;
vec($rin, $ptyfd, 1) = 1;
vec($rin, $sockfd, 1) = 1;
+ my ($win, $wout) = '';
+ vec($win, $sockfd, 1) = 1;
ReadMode 5;
my $guard = Scope::Guard->new(sub { ReadMode 0 });
local $SIG{WINCH} = sub { $self->_got_winch(1) };
@@ -116,6 +131,14 @@ sub run {
last;
}
syswrite STDOUT, $buf;
+ my $ready = select(undef, $wout = $win, undef, $self->timeout);
+ if (!$ready) {
+ Carp::carp("Lost connection to server ($!), reconnecting...");
+ $socket = $self->connect;
+ vec($rin, $sockfd, 1) = 0;
+ $sockfd = fileno($socket);
+ vec($rin, $sockfd, 1) = 1;
+ }
$socket->write($buf);
}
if (vec($rout, $sockfd, 1)) {