Andrew said:
Hello.
Lets suppose I have a pool of about 300-400 non-blocking sockets and I want to
monitor their state and do some actions depending on some events.
From what I can see, your aim is to find out which element of @socks
each socket you can read/write from is in. Am I right?
If so, I'd use Tie::RefHash (included in my distrib of 5.8, possibly not
in older versions), which allows you to have hash references as keys.
Then somewhere (probably where you set up @socks) create a RefHash with
the sockets as keys and their array index as values. I think the
following will work:
my %sock_index;
@sock_index{@socks}=(0..@socks);
but you'd better check that as I'm not certain I remember the syntax.
Basically what I'm trying to do is something along the lines of
for(my $i=0;$i<@socks;$i++){
$sock_index{$socks[$i]}=$i;
}
Then a modified version of your code would be:
while(1)
{
my ($can_read, $can_write) = IO::Select->select($rd_set,
$wr_set, undef, $TIMEOUT);
foreach my $reader(@$can_read){
my $i = $sock_index{$reader};
#read from socket
}
foreach my $writer(@$can_write){
my $i = $sock_index{$writer};
#write to socket
}
}
which avoids the massive loop through all of @socks. I'm assuming
there's no reason to write/read in the order they are in @socks. If
there is, then you can hopefully tweak mine to do what you want.