Perl Tcp transfer not consistent

88 Views Asked by At

This is my below code to transfer file using TCP in Perl. We are transferring 'n' number of files using thread to other servers and also each file we chunk as 5mb before sending. For most of the files in the below function i am getting issue as 'Connection dropped' or 'packet header' recieving 0 bytes. Not sure what is the reason and i m learning stage to threads tcp transfer . So please suggest how i need to debug, whether the issue in code or in server we are transferring, what solution i need to apply.

    #GET THE PACKET HEADER
    my $data;
    my $tempdata;
    my $datalen = 0;
    my $templen = 0;
    my $retrycount = $self->{retrycount};

    my $mark = time();
    my $iloops = 0;
    my $waitlist = IO::Select->new($self->{sock});
    while($datalen < 9)
    {
            my $iloops++;
            usleep( $usleep_wait );

            if($waitlist->can_read($timeout || 0)) { $templen = $self->{sock}->sysread($tempdata, 9 - $datalen); } else { $templen = 0; $! = EWOULDBLOCK; }
            if(($templen || 0) > 0)
            {       
                    #EVERYTHING'S FINE
                    print_message(LOGLEVEL_TRACE, "Got $templen bytes", LOG_TAG) unless ( $templen == 9 ); # 
                    $mark = time();                 #RESET THE TIMEOUT
                    $data .= $tempdata;
                    $datalen += $templen;
                    
                    last if $datalen == 9;
                    next;
            }
            #AN IRRECOVERABLE ERROR HAPPENED (i.e. WE CAN'T JUST WAIT IT OUT)
            elsif(!defined $templen)
            {       
                    if($! != EWOULDBLOCK && $! != EAGAIN && $! != EINTR && $! != EINPROGRESS)
                    {       
                            my $err = $!;
                            
                            #DISCONNECTION
                            print_message(LOGLEVEL_ERROR, "__grab_data: Connection was dropped ($@, $!).", LOG_TAG); # if !defined $self->__reconnect();
                            $self->{sock}->close();
                            #print_message(LOGLEVEL_INFO, "Connection was dropped, but was re-established ($err)", LOG_TAG);
                            return undef;
                    }
                    else
                    {       
                            print_message(LOGLEVEL_ERROR, "__grab_data: An unknown error happened ($@, $!).", LOG_TAG);
                            $self->{sock}->close();
            elsif($templen == 0)
            {
                    print_message(LOGLEVEL_TRACE, "Finished the stream, nothing to collect", LOG_TAG);
                    last;
            }
            ## OPTIONAL TIMEOUT FOR NONBLOCKING REQUESTS
            elsif(defined $timeout)
            {
                    return undef if time() - $mark > $timeout;
            }

            usleep( $usleep_wait );
            print_message(LOGLEVEL_FAILURE, "Connection dropped whilst trying to receive data", LOG_TAG) if !$self->is_connected();
    }
    my $interim = time();


    #WE DIDN'T RECEIVE THE WHOLE HEADER
    if($datalen != 9)
    {
            usleep( $usleep_whole_header_wait );
            print_message(LOGLEVEL_ERROR, "Packet header is truncated (we only got $datalen bytes, '$data')", LOG_TAG);
            return undef;
    }
0

There are 0 best solutions below