summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorJesse Luehrs <doy@tozt.net>2010-04-18 16:24:46 -0500
committerJesse Luehrs <doy@tozt.net>2010-04-18 16:52:31 -0500
commit4a3506e0e8e1a7bd5173e0dc4136b13b75cb3b45 (patch)
treeafa4d57d43e7840b796f0e2d228263e5c631d85c /lib
parentd90610a3e8f01b90b3d296d778977a2de82977b2 (diff)
downloadapp-termcast-4a3506e0e8e1a7bd5173e0dc4136b13b75cb3b45.tar.gz
app-termcast-4a3506e0e8e1a7bd5173e0dc4136b13b75cb3b45.zip
factor out sending data to the server, to allow using that separately
Diffstat (limited to 'lib')
-rw-r--r--lib/App/Termcast.pm61
1 files changed, 36 insertions, 25 deletions
diff --git a/lib/App/Termcast.pm b/lib/App/Termcast.pm
index 36f5def..bc0ff94 100644
--- a/lib/App/Termcast.pm
+++ b/lib/App/Termcast.pm
@@ -111,20 +111,24 @@ sub _build_pty {
sub _build_select_args {
my $self = shift;
- my $sockfd = fileno($self->socket);
- my $ptyfd = fileno($self->pty);
- my $infd = fileno(STDIN);
-
- my $rin = '';
- vec($rin, $infd ,1) = 1;
- vec($rin, $ptyfd, 1) = 1;
- vec($rin, $sockfd, 1) = 1;
-
- my $win = '';
- vec($win, $sockfd, 1) = 1;
-
- my $ein = '';
- vec($ein, $sockfd, 1) = 1;
+ my @for = @_ ? @_ : (qw(socket pty input));
+ my %for = map { $_ => 1 } @for;
+
+ my ($rin, $win, $ein) = ('', '', '');
+ if ($for{socket}) {
+ my $sockfd = fileno($self->socket);
+ vec($rin, $sockfd, 1) = 1;
+ vec($win, $sockfd, 1) = 1;
+ vec($ein, $sockfd, 1) = 1;
+ }
+ if ($for{pty}) {
+ my $ptyfd = fileno($self->pty);
+ vec($rin, $ptyfd, 1) = 1;
+ }
+ if ($for{input}) {
+ my $infd = fileno(STDIN);
+ vec($rin, $infd ,1) = 1;
+ }
return ($rin, $win, $ein);
}
@@ -147,23 +151,36 @@ sub _in_ready {
vec($vec, fileno(STDIN), 1);
}
+sub write_to_termcast {
+ my $self = shift;
+ my ($buf) = @_;
+
+ my ($rin, $win, $ein) = $self->_build_select_args('socket');
+ my ($rout, $wout, $eout);
+ my $ready = select(undef, $wout = $win, $eout = $ein, $self->timeout);
+ if (!$ready || $self->_socket_ready($eout)) {
+ Carp::carp("Lost connection to server ($!), reconnecting...");
+ $self->clear_socket;
+ return $self->socket_write(@_);
+ }
+ $self->socket->write($buf);
+}
+
sub run {
my $self = shift;
ReadMode 5;
my $guard = Scope::Guard->new(sub { ReadMode 0 });
- my ($rin, $win, $ein) = $self->_build_select_args;
- my ($rout, $wout, $eout);
-
local $SIG{WINCH} = sub { $self->_got_winch(1) };
while (1) {
+ my ($rin, $win, $ein) = $self->_build_select_args;
+ my ($rout, $wout, $eout);
select($rout = $rin, undef, $eout = $ein, undef);
if ($self->_socket_ready($eout)) {
Carp::carp("Lost connection to server ($!), reconnecting...");
$self->clear_socket;
- ($rin, $win, $ein) = $self->_build_select_args;
}
if ($self->_in_ready($rout)) {
@@ -196,13 +213,7 @@ sub run {
syswrite STDOUT, $buf;
- my $ready = select(undef, $wout = $win, $eout = $ein, $self->timeout);
- if (!$ready || $self->_socket_ready($eout)) {
- Carp::carp("Lost connection to server ($!), reconnecting...");
- $self->clear_socket;
- ($rin, $win, $ein) = $self->_build_select_args;
- }
- $self->socket->write($buf);
+ $self->write_to_termcast($buf);
}
if ($self->_socket_ready($rout)) {