将文件句柄与 IO::Select::can_read() 返回的 glob 进行比较

发布于 2024-09-26 02:22:25 字数 627 浏览 0 评论 0原文

我正在尝试通过 perl 中的 IO::Select 管理三个文件句柄。我有 1 个输入句柄、1 个输入/输出句柄和 1 个输出句柄。在处理 Select 的 can_read() 和 can_write() 返回数组时,我在确定哪个文件句柄是哪个文件句柄时遇到了一些麻烦;

下面的示例

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

#!/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);
        }
    }
}

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();

Example below

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);
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

め七分饶幸 2024-10-03 02:22:25

我成功了。这是使用引用和 eq 的组合(我在修复引用之前已经尝试过);

工作代码:

#!/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);

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

working code:

#!/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);
半﹌身腐败 2024-10-03 02:22:25

使用 eq 而不是 == 进行这种比较。

if ($read_fh eq *INPUT) { ... }

Use eq instead of == for this kind of comparison.

if ($read_fh eq *INPUT) { ... }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文