How to make the correct IO :: Choose error handling?

I look through the documents and instructions that I find when IO :: Select is used correctly in terms of network sockets. I think my head wrapped around me for the most part.

However, I am still uneven in the correct handling of errors. Say I have something similar to the following code running inside an object. Yes, I understand that this is messy, I have to integrate IO :: Select into the object, not the fh socket itself, I should not recreate IO :: Select every time through the loop, I repeat that it can only ever be one returned file descriptor etc. However, this simplifies the example.

This is just a client connecting to the server, but one that I want to be able to correctly handle network-level errors, such as packet loss.

Edit: $self->sock()returns only the open socket IO :: Socket :: INET.

sub read {
    my $self = shift;
    my($length) = @_;   ### Number of bytes to read from the socket

    my $ret;

    while (length($ret) < $length) {
        my $str;

        use IO::Select;
        my $sel = IO::Select->new($self->sock());

        if (my @ready = $sel->can_read(5)) {  ### 5 sec timeout
          for my $fh (@ready) {
            my $recv_ret = $fh->recv($str, $length - length($ret));
            if (!defined $recv_ret) {
              MyApp::Exception->throw(
                message => "connection closed by remote host: $!",
              );
            }
          }
        }
        else {
          ### Nothing ready... we timed out!
          MyApp::Exception->throw(
                  message => "no response from remote host",
             );
        }
        $ret .= $str;
      }

      return $ret;
}
  • Do I need to check for returns from recv or errors that will affect their display in the IO :: Select object?
  • Am I handling timeouts correctly, or is my logic set incorrectly?
  • IO :: Socket mentions an exception that exists in the socket file descriptor for out-of-band errors and other problems. Should I check this in case of timeout? How? Or is it unimportant and normal to ignore?
  • Are there other exception cases that I have to handle for proper behavior?
+5
1

1) . (2), .

2) , 2048 , 5 . 10K = 3 . , ?

alarm 5 $SIG{ALRM} = sub {$stop = 1;}.

3 4). , read() while select() , .

+1

All Articles