3
0
mirror of https://github.com/pragma-/pbot.git synced 2024-11-19 10:29:30 +01:00

Add remove_reader() to SelectHandler and remove unnecessary foreground check from StdinReader's initialize()

This commit is contained in:
Pragmatic Software 2014-03-16 01:47:16 +00:00
parent edd507b2e0
commit e728276aca
3 changed files with 10 additions and 9 deletions

View File

@ -35,28 +35,30 @@ sub add_reader {
$self->{readers}->{$handle} = $sub; $self->{readers}->{$handle} = $sub;
} }
sub remove_reader {
my ($self, $handle) = @_;
$self->{select}->remove($handle);
delete $self->{readers}->{$handle};
}
sub do_select { sub do_select {
my ($self) = @_; my ($self) = @_;
my @ready = $self->{select}->can_read(.5); my @ready = $self->{select}->can_read(.5);
foreach my $fh (@ready) { foreach my $fh (@ready) {
my $ret = sysread($fh, my $buf, 4096); my $ret = sysread($fh, my $buf, 8192);
if(not defined $ret) { if(not defined $ret) {
$self->{pbot}->logger->log("Error with $fh: $!\n"); $self->{pbot}->logger->log("Error with $fh: $!\n");
$self->{select}->remove($fh); $self->remove_reader($fh);
delete $self->{readers}->{$fh};
next; next;
} }
if($ret == 0) { if($ret == 0) {
$self->{pbot}->logger->log("done with $fh\n"); $self->remove_reader($fh);
$self->{select}->remove($fh);
delete $self->{readers}->{$fh};
next; next;
} }
chomp $buf; chomp $buf;
$self->{pbot}->logger->log("read from $fh: [$buf]\n");
if(not exists $self->{readers}->{$fh}) { if(not exists $self->{readers}->{$fh}) {
$self->{pbot}->logger->log("Error: no reader for $fh\n"); $self->{pbot}->logger->log("Error: no reader for $fh\n");

View File

@ -28,7 +28,6 @@ sub initialize {
# used to check whether process is in background or foreground, for stdin reading # used to check whether process is in background or foreground, for stdin reading
open TTY, "</dev/tty" or die $!; open TTY, "</dev/tty" or die $!;
$self->{tty_fd} = fileno(TTY); $self->{tty_fd} = fileno(TTY);
$self->{foreground} = (tcgetpgrp($self->{tty_fd}) == getpgrp()) ? 1 : 0;
$self->{pbot}->{select_handler}->add_reader(\*STDIN, sub { $self->stdin_reader(@_) }); $self->{pbot}->{select_handler}->add_reader(\*STDIN, sub { $self->stdin_reader(@_) });
} }

View File

@ -13,7 +13,7 @@ use warnings;
# These are set automatically by the build/commit script # These are set automatically by the build/commit script
use constant { use constant {
BUILD_NAME => "PBot", BUILD_NAME => "PBot",
BUILD_REVISION => 528, BUILD_REVISION => 529,
BUILD_DATE => "2014-03-15", BUILD_DATE => "2014-03-15",
}; };