sub send_document_chunk
{
my $chunk = shift;
while (defined (my $line = $in->getline())) {
push(@linebuffer,$line);
}
# if it would block, wait up to 5 seconds until either we can read # or write and then try again.
my $written = 0;
# Only exit out of this when the write filehandle is ready while (1) {
my @ready = IO::Select::select($s_read,$s_write,$s_error,10);
foreach my $error (@{$ready[2]}) {
die("Error waiting for MxTerminator to be ready: $!");
}
foreach my $write_ready (@{$ready[1]}) {
$written = $write_ready->syswrite($chunk);
die("Error writing: $!") if not defined $written;
# print STEDRR "MXTERMINATOR WRITE: '", substr($chunk,0,$written), "'\n";
# return if we managed to write the whole thing return if($written == length($chunk));
# otherwise shrink the chunk and go back into the select $chunk = substr($chunk,$written);
}
# if we can read (there should only be one filehandle in the array) # read stuff, then redo the select
foreach my $read_ready (@{$ready[0]}) {
while (defined (my $line = $in->getline())) {
# print STDERR 'MXTERMINATOR READ: ', "'$line'\n"; push(@linebuffer,$line);
}
}
}
die("MxTerminator blocked for more than 10 seconds: $!");} |