I did end up using threads actually.
In the case that I can't open a non-blocking listener, I make a thread and share the messages in an array. It worked great and I can get the IP from the recv().
Really, the best part is, this is the first time I've every needed to use a thread and now that I know how, it's like there's a whole new world out there.
And for posterity, here is my original non-blocking loop along with my new magical threaded version.
sub listen { my $this = shift; if( $this->{blocking} ) { if( not $this->{blocking_thread} ) { $this->{blocking_thread} = new threads( \&_blocking_listen +, $this ); } my @msgs = map([ split /^G^G/, $_ ], @global_shared_messages); @global_shared_messages = (); return @msgs; } my @msgs = (); my $msg = ""; while( my $portaddr = recv($this->{in}, $msg, 1024*1024, 0) ) { my ($portno, $ipaddr) = sockaddr_in($portaddr); my $ip = inet_ntoa($ipaddr); push @msgs, [$ip, $this->_transform($msg)]; } return @msgs; } sub _blocking_listen { my $this = shift; { my $msg = ""; while( my $portaddr = recv($this->{in}, $msg, 1024*1024, 0) ) +{ my ($portno, $ipaddr) = sockaddr_in($portaddr); my $ip = inet_ntoa($ipaddr); push @global_shared_messages, "$ip^G^G" . $this->_transfor +m($msg); } redo; # we stay in here forever, btw. } }
In reply to Re^3: IO::Socket::INET -- Jettero verses non-blocking in windows
by jettero
in thread IO::Socket::INET -- jettero verses non-blocking in windows
by jettero
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |