Changeset f3743d455d3ec649821b104e0c103f6a97b029f7

Show
Ignore:
Timestamp:
21/02/07 19:19:11 (2 years ago)
Author:
Rémi Denis-Courmont <rem@videolan.org>
git-committer:
Rémi Denis-Courmont <rem@videolan.org> 1172081951 +0000
git-parent:

[52b0f7ea4eea7123432ed3cf0db42be3697fef92]

git-author:
Rémi Denis-Courmont <rem@videolan.org> 1172081951 +0000
Message:

- Use poll in net_Accept
- Fix som bugs

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • src/network/tcp.c

    rf9279bb rf3743d4  
    4141#   include <unistd.h> 
    4242#endif 
     43#ifdef HAVE_POLL 
     44# include <poll.h> 
     45#endif 
    4346 
    4447#include <vlc_network.h> 
     
    273276    while( !p_this->b_die ) 
    274277    { 
    275         int maxfd = -1; 
    276         fd_set readset; 
     278        unsigned n = 0; 
     279        while (pi_fd[n] != -1) 
     280            n++; 
     281        struct pollfd ufd[n]; 
    277282 
    278283        /* Initialize file descriptor set */ 
    279         FD_ZERO (&readset); 
    280  
    281         int *pi_end = pi_fd; 
    282         for (const int *pi = pi_fd; *pi != -1; pi++) 
    283         { 
    284             int fd = *pi; 
    285  
    286             if (fd > maxfd) 
    287                 maxfd = fd; 
    288  
    289             FD_SET (fd, &readset); 
    290             pi_end++; 
    291         } 
    292  
    293         struct timeval tv = { 0, b_block ? 500000 : i_wait }; 
    294  
    295         int val = select (maxfd + 1, &readset, NULL, NULL, &tv); 
    296         if (val == 0) 
    297         { 
    298             if (b_block) 
     284        for (unsigned i = 0; i < n; i++) 
     285        { 
     286            ufd[i].fd = pi_fd[i]; 
     287            ufd[i].events = POLLIN; 
     288            ufd[i].revents = 0; 
     289        } 
     290 
     291        switch (poll (ufd, n, b_block ? 500 : i_wait)) 
     292        { 
     293            case -1: 
     294                if (net_errno != EINTR) 
     295                { 
     296                    msg_Err (p_this, "poll error: %s", 
     297                             net_strerror (net_errno)); 
     298                } 
     299                return -1; 
     300 
     301            case 0: 
     302                if (b_block) 
     303                    continue; 
     304                return -1; 
     305        } 
     306 
     307        for (unsigned i = 0; i < n; i++) 
     308        { 
     309            if (ufd[i].revents == 0) 
    299310                continue; 
    300             return -1; 
    301         } 
    302         if (val < 0) 
    303         { 
    304             if (net_errno != EINTR) 
    305                 msg_Err( p_this, "network select error (%s)", 
    306                         net_strerror( net_errno ) ); 
    307             return -1; 
    308         } 
    309  
    310         for (const int *pi = pi_fd; *pi != -1; pi++) 
    311         { 
    312             int fd = *pi; 
    313  
    314             if (!FD_ISSET (fd, &readset)) 
    315                 continue; 
    316  
    317             fd = accept (fd, NULL, 0); 
    318             if (fd < 0) 
     311 
     312            int sfd = ufd[i].fd; 
     313            int fd = accept (sfd, NULL, NULL); 
     314            if (fd == -1) 
    319315            { 
    320316                msg_Err (p_this, "accept failed (%s)", 
     
    325321 
    326322            /* 
    327              * This round-robin trick ensures that the first sockets in 
    328              * pi_fd won't prevent the last ones from getting accept'ed
     323             * Move listening socket to the end to let the others in the 
     324             * set a chance next time
    329325             */ 
    330             --pi_end; 
    331             memmove (pi_fd, pi_fd + 1, pi_end - pi_fd); 
    332             *pi_end = *pi; 
    333             msg_Dbg (p_this, "accepted socket %d (from socket %d)", fd, *pi); 
     326            memmove (pi_fd + i, pi_fd + i + 1, n - (i + 1)); 
     327            pi_fd[n - 1] = sfd; 
     328            msg_Dbg (p_this, "accepted socket %d (from socket %d)", fd, sfd); 
    334329            return fd; 
    335330        }