perl and epoll

Discussion in 'Perl Misc' started by sonet, Nov 3, 2006.

  1. sonet

    sonet Guest

    When i use one program to send data. The speed of upload file is slow.
    But when i use two(>=2) programs to testing the transmission speed at
    the same time. It is very fast. The transmission speed is almost equal
    to (* how many programs is running)

    I can not find any solution to slove the problem.I try to modify the the
    value of epoll_wait()'s parameter.

    my $events_count = epoll_wait( $epoll,15,3600, $events );
    my $events_count = epoll_wait( $epoll,2,0, $events );
    my $events_count = epoll_wait( $epoll,2,-1, $events );
    my $events_count = epoll_wait( $epoll,2,100, $events );

    But the problem still exist.
    And i find that the $events_count always is 1 when i upload file via
    another programs.
    =========================================================
    #!/usr/bin/perl
    use strict;
    use IO::Socket;
    use Sys::Syscall ':epoll';
    use Errno 'EWOULDBLOCK';
    my ( $haveEpoll, $epoll );
    if ( Sys::Syscall::epoll_defined() )
    {
    $epoll = eval { epoll_create(1); };
    $haveEpoll = defined $epoll && $epoll >= 0;
    }
    if ( !$haveEpoll )
    {
    require IO::poll;
    }

    my $listen = IO::Socket::INET->new(Proto => 'tcp',
    LocalPort => 21,
    Listen => SOMAXCONN ,
    Reuse => 1) or die $!;

    PollIn_Event_Add( $epoll, fileno($listen) );

    my %accepthash;
    my $events;
    while (1)
    {
    $events = [];
    my $events_count = epoll_wait( $epoll,15,3600, $events );

    for ( my $i = 0 ; $i < $events_count ; $i++ )
    {
    my $ev = $events->[$i];
    $ev ||= [];

    if (! defined $accepthash{$ev->[0] } && $ev->[0] eq
    fileno($listen) ){
    my $connect=$listen->accept();
    PollIn_Event_Add( $epoll, fileno($connect) );
    $accepthash{fileno($connect)}=$connect;
    open(handle,">/usr/local/Portal/cache/" . fileno($connect) );
    close(handle);
    }else{

    if ( $ev->[1] == EPOLLIN )
    {
    my $buffer='';
    my $rc = sysread($accepthash{$ev->[0]}, $buffer, 16384,0 );
    if ( defined $rc )
    {
    if ( $rc > 0 )
    {
    #print $rc . "\n";
    open(handle,">>/usr/local/Portal/cache/" . $ev->[0]);
    print handle $buffer;
    close(handle);
    substr( $buffer, 0, $rc ) = '';
    } else
    {

    eval { shutdown( $accepthash{$ev->[0]}, 2 ); };
    delete $accepthash{$ev->[0]};
    }


    } elsif ( $! == EWOULDBLOCK )
    {

    } else
    {

    eval { shutdown( $accepthash{$ev->[0]}, 2 ); };
    delete $accepthash{$ev->[0]};
    PollIn_Event_Del( $epoll, $ev->[0] );
    }

    }elsif($ev->[1] == EPOLLERR){

    eval { shutdown( $accepthash{$ev->[0]}, 2 ); };
    delete $accepthash{$ev->[0]};
    PollIn_Event_Del( $epoll, $ev->[0] );
    }
    }
    }
    }

    sub PollIn_Event_Add
    {
    my ( $epoll, $fd ) = @_;
    my $ctl_rtn;
    $ctl_rtn = epoll_ctl( $epoll, EPOLL_CTL_ADD, $fd, EPOLLIN | EPOLLERR );
    print 'PollIn_Event_Add:' . $ctl_rtn . "\n";
    }

    sub PollIn_Event_Del
    {
    my ( $epoll, $fd ) = @_;
    my $ctl_rtn;
    $ctl_rtn = epoll_ctl( $epoll, EPOLL_CTL_DEL, $fd,0 );
    print 'PollIn_Event_Del:' . $ctl_rtn . "\n";
    }
     
    sonet, Nov 3, 2006
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. James Mills

    epoll socket server

    James Mills, Oct 10, 2008, in forum: Python
    Replies:
    0
    Views:
    742
    James Mills
    Oct 10, 2008
  2. _wolf
    Replies:
    0
    Views:
    918
    _wolf
    Mar 24, 2010
  3. lovecreatesbeauty

    epoll networking code review

    lovecreatesbeauty, Aug 30, 2012, in forum: C Programming
    Replies:
    4
    Views:
    707
    Ben Bacarisse
    Aug 30, 2012
  4. Miki Tebeka
    Replies:
    2
    Views:
    289
    Miki Tebeka
    Sep 12, 2012
  5. Paul Rubin

    select.epoll question

    Paul Rubin, Feb 7, 2013, in forum: Python
    Replies:
    5
    Views:
    120
    Kushal Kumaran
    Feb 8, 2013
Loading...

Share This Page