比较文件句柄和IO :: Select :: can_read()返回的glob [英] Comparing filehandle with glob returned by IO::Select::can_read()

查看:87
本文介绍了比较文件句柄和IO :: Select :: can_read()返回的glob的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过IO :: Select在perl中管理三个文件句柄.我有1个输入手柄,1个输入/输出手柄和1个输出手柄.我在处理can_read()和can_write();的Select的返回数组时很难确定哪个文件句柄是

I am trying to manage three filehandles via IO::Select in perl. I have 1 input handle, 1 input/output handle, and 1 output handle. Im having a little trouble determining which filehandle is which when processing Select's return arrays of can_read() and can_write();

下面的例子

关于如何实际比较这两个文件句柄的任何建议?我尝试过标量,尝试过引用,没有引用等.我想不出为什么这不起作用.

Any advice on how to actually compare these two filehandles? I've tried scalars, i've tried references, no references, etc. I can't think of why this isn't working.

#!/usr/bin/perl
use strict;
use warnings;
use IO::Select;

open(INPUT, "/dev/fd/3") or die "Unable to open input! $!";
my $stdin_buf;

# Main loop
while (1)
{
    foreach my $read_fh ($select->can_read(10)) # This DOES return INPUT as being readable
    {
        if ($read_fh == \*INPUT) # THIS fails.
        {
            read($read_fh, $stdin_buf, 512);
        }
    }
}

推荐答案

我知道了.这是使用引用和eq的组合(我在修复引用之前曾尝试过);

I got it working. It was a combination of using references and eq (which i had tried prior to fixing references);

工作代码:

#!/usr/bin/perl
use strict;
use warnings;
use IO::Select;

##############################################
# Asterisk to stream pipe thingie-ma-jig-bob #
# Written by Sean Powell - 10-5-10           #
##############################################

my $extension = $ARGV[0];

if (!$extension || $extension !~ /^\d+$/)
{
    print "USAGE: Please provide a decimal extension as the first parameter";
    exit(1);
}



my $ffmpeg = "/usr/bin/ffmpeg -f s16le -ar 8000 -ac 1 -i - -ab 64k -f mp3 -";
my $ezstream = "/usr/local/bin/ezstream -c /etc/asterisk/ICES/" . $extension . ".xml";

my $stdin_buf;
my $ffmpeg_buf;
my $last_activity = 0;

open(INPUT, "/dev/fd/3") or die "Unable to open input! $!";
open(FFMPEG, "|$ffmpeg") or die "Unable to fork off ffmpeg! $!";
open(EZSTREAM, "|$ezstream") or die "Unable to fork off ezstream! $!";
open(DEBUG, ">>/root/debug.log") or die "Unable to open debug log! $!";

my ($input_fh, $ffmpeg_fh, $ezstream_fh) = (*INPUT, *FFMPEG, *EZSTREAM);

my $select = new IO::Select(*INPUT);
$select->add(*FFMPEG);
$select->add(*EZSTREAM);

# Main loop
while (1)
{
    foreach my $read_fh ($select->can_read(10))
    {
        print DEBUG "Filehandle can read: $read_fh - $input_fh - $ffmpeg_fh - $ezstream_fh\n";
        if ($read_fh eq $input_fh)
        {
            my $read = read($read_fh, $stdin_buf, 512);
            print DEBUG "Read off $read bytes from INPUT\n";
            $last_activity = time();
        }
        if ($read_fh eq $ffmpeg_fh)
        {
            my $read = read($read_fh, $ffmpeg_buf, 512);
            print DEBUG "Read off $read bytes from FFMPEG\n";
            $last_activity = time();
        }
    }

    foreach my $write_fh ($select->can_write(10))
    {
        if ($write_fh eq $ffmpeg_fh && length($stdin_buf) > 0)
        {
            my $size = length($stdin_buf);
            my $wrote = syswrite($write_fh, $stdin_buf, $size);

            while ($wrote < $size)
            {
                $wrote += syswrite($write_fh, $stdin_buf, $size - $wrote, $wrote);
            }
            print DEBUG "Wrote $wrote bytes to FFMPEG\n";
            $last_activity = time();
            $stdin_buf = undef;
        }

        if ($write_fh eq $ezstream_fh && length($ffmpeg_buf) > 0)
        {
            my $size = length($ffmpeg_buf);
            my $wrote = syswrite($write_fh, $ffmpeg_buf, $size);

            while ($wrote < $size)
            {
                $wrote += syswrite($write_fh, $ffmpeg_buf, $size - $wrote, $wrote);
            }
            $ffmpeg_buf = undef;
            print DEBUG "Wrote $wrote bytes to EZSTREAM\n";
            $last_activity = time();
        }
    }
    last if (time() - $last_activity > 30);

}
close(INPUT);
close(EZSTREAM);
close(FFMPEG);

这篇关于比较文件句柄和IO :: Select :: can_read()返回的glob的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆