Festival-Client-Async
view release on metacpan or search on metacpan
my ($host, $port) = @_;
my $s = IO::Socket::INET->new(Proto => 'tcp',
PeerAddr => $host || 'localhost',
PeerPort => $port || 1314)
or return undef;
binmode $s;
my $self = bless {
blocked => 0,
sock => $s,
outbuf => "",
outq => {
LP => [],
},
intag => "",
inbuf => "",
inq => {
LP => [],
WV => [],
return $self->{sock};
}
sub block {
my $self = shift;
my $flags = 0;
fcntl $self->{sock}, F_GETFL, $flags
or die "fcntl(F_GETFL) failed: $!";
fcntl $self->{sock}, F_SETFL, $flags & ~O_NONBLOCK
or die "fcntl(F_SETFL) failed: $!";
$self->{blocked} = 1;
}
sub unblock {
my $self = shift;
my $flags = 0;
fcntl $self->{sock}, F_GETFL, $flags
or die "fcntl(F_GETFL) failed: $!";
fcntl $self->{sock}, F_SETFL, $flags | O_NONBLOCK
or die "fcntl(F_SETFL) failed: $!";
$self->{blocked} = 0;
}
# Protocol encoding
use constant KEY => "ft_StUfF_key";
use constant KEYLEN => length KEY;
sub write_more {
my $self = shift;
while (defined(my $expr = shift @{$self->{outq}{LP}})) {
$self->{outbuf} .= $expr;
}
my $count;
while (defined(my $b = syswrite($self->{sock}, $self->{outbuf}, 4096))) {
print "wrote $b bytes\n" if DEBUG;
last if $b == 0;
$count += $b;
substr($self->{outbuf}, 0, $b) = "";
last if $self->{blocked} and $b < 4096;
}
return $count;
}
sub read_more {
my $self = shift;
my $fh = $self->{sock};
my $count = 0;
( run in 1.605 second using v1.01-cache-2.11-cpan-49f99fa48dc )