in reply to Re^4: Joining a Thread in w While loop
in thread Joining a Thread in w While loop

I think you should rethink your design a bit, to avoid relying on the IO::Watch to watch the filehandle. You could setup a timer instead to read the filehandle at a fast interval, and check for $die at every timer callback run.

Another super simple "hack", would be to have the server send out newlines to every client, like a strobe, every second. Then in your code, when the socket reads just a newline, close up and go to the end of the thread code block.


I'm not really a human, but I play one on earth. Cogito ergo sum a bum

Replies are listed 'Best First'.
Re^6: Joining a Thread in w While loop
by deadpickle (Pilgrim) on Mar 05, 2008 at 21:14 UTC
    A good idea. In shorthand:
    #-------------------Connect to IRC Server------------------- sub connecting { # Connect to the IRC server. $sock = new IO::Socket::INET( PeerAddr => $irc, PeerPort => 6667, Proto => 'tcp', ) or die "Can't connect\n"; print $sock "NICK $nick\r\n"; print $sock "USER $login 8 * :CoCoNUE Member $nick\r\n"; $sel->add($sock); ################################################# #Timeout my $timer_chat = Glib::Timeout->add(1, \&incoming_data); #################################################\ return 1; } #-------------------Watch for IRC Inputs------------------- sub incoming_data { if ($sel->can_read(1)){ my $input = <$sock>; $input =~ s/\r\n//g; my $hashref = $parser->parse( $input ); SWITCH: { my $type = lc $hashref->{command}; my @args; push @args, $hashref->{prefix} if $hashref->{prefix}; push @args, @{ $hashref->{params} }; if ( defined $dispatch{$type} ) { $dispatch{$type}->(@args); last SWITCH; } print STDOUT join( ' ', "irc_$type:", @args ), "\n"; } } return 1; }
    Now the only problem is that the GTK2 window stops responding. I think its caused by the my $input = <$sock> having nothing to read and it just makes the GTK2 program angry. Is there a way to avoid this other than adding a watch or any other crazy GTK2 dispatcher? Thanks for the help everyone.
    Update: should IO::Select be used? I have been barking up that tree with not much success either. I added the IO::Select to the loop and the program is now responding but it is very sluggish which is bad. any ideas?
      I don't know, you are asking people to make your script work, and you just kind of slapped together some code, without an understanding of the basics of sockets, and it's big and complicated. I would want to be paid to work out all your bugs....but

      sub incoming_data { if ($sel->can_read(1)){ my $input = <$sock>; $input =~ s/\r\n//g;
      Well 2 things, your $sel->can_read will always be true if the socket is connected. $input =<$sock> will block Gtk2, since it will read until $sock is closed.

      Try

      my $input; sysread ($sock, $input, 1024); # that will not block gtk2 print $input;
      So just try a sysread in your sub until you get a non-blocking behavior, then work from there.

      I'm not really a human, but I play one on earth. Cogito ergo sum a bum