Commit f3743d45 authored by Rémi Denis-Courmont's avatar Rémi Denis-Courmont

- Use poll in net_Accept

- Fix som bugs
parent 52b0f7ea
...@@ -40,6 +40,9 @@ ...@@ -40,6 +40,9 @@
#ifdef HAVE_UNISTD_H #ifdef HAVE_UNISTD_H
# include <unistd.h> # include <unistd.h>
#endif #endif
#ifdef HAVE_POLL
# include <poll.h>
#endif
#include <vlc_network.h> #include <vlc_network.h>
#if defined (WIN32) || defined (UNDER_CE) #if defined (WIN32) || defined (UNDER_CE)
...@@ -272,50 +275,43 @@ int __net_Accept( vlc_object_t *p_this, int pi_fd[], mtime_t i_wait ) ...@@ -272,50 +275,43 @@ int __net_Accept( vlc_object_t *p_this, int pi_fd[], mtime_t i_wait )
while( !p_this->b_die ) while( !p_this->b_die )
{ {
int maxfd = -1; unsigned n = 0;
fd_set readset; while (pi_fd[n] != -1)
n++;
struct pollfd ufd[n];
/* Initialize file descriptor set */ /* Initialize file descriptor set */
FD_ZERO (&readset); for (unsigned i = 0; i < n; i++)
int *pi_end = pi_fd;
for (const int *pi = pi_fd; *pi != -1; pi++)
{ {
int fd = *pi; ufd[i].fd = pi_fd[i];
ufd[i].events = POLLIN;
if (fd > maxfd) ufd[i].revents = 0;
maxfd = fd;
FD_SET (fd, &readset);
pi_end++;
} }
struct timeval tv = { 0, b_block ? 500000 : i_wait }; switch (poll (ufd, n, b_block ? 500 : i_wait))
{
int val = select (maxfd + 1, &readset, NULL, NULL, &tv); case -1:
if (val == 0) if (net_errno != EINTR)
{ {
msg_Err (p_this, "poll error: %s",
net_strerror (net_errno));
}
return -1;
case 0:
if (b_block) if (b_block)
continue; continue;
return -1; return -1;
} }
if (val < 0)
{
if (net_errno != EINTR)
msg_Err( p_this, "network select error (%s)",
net_strerror( net_errno ) );
return -1;
}
for (const int *pi = pi_fd; *pi != -1; pi++) for (unsigned i = 0; i < n; i++)
{ {
int fd = *pi; if (ufd[i].revents == 0)
if (!FD_ISSET (fd, &readset))
continue; continue;
fd = accept (fd, NULL, 0); int sfd = ufd[i].fd;
if (fd < 0) int fd = accept (sfd, NULL, NULL);
if (fd == -1)
{ {
msg_Err (p_this, "accept failed (%s)", msg_Err (p_this, "accept failed (%s)",
net_strerror (net_errno)); net_strerror (net_errno));
...@@ -324,13 +320,12 @@ int __net_Accept( vlc_object_t *p_this, int pi_fd[], mtime_t i_wait ) ...@@ -324,13 +320,12 @@ int __net_Accept( vlc_object_t *p_this, int pi_fd[], mtime_t i_wait )
net_SetupSocket (fd); net_SetupSocket (fd);
/* /*
* This round-robin trick ensures that the first sockets in * Move listening socket to the end to let the others in the
* pi_fd won't prevent the last ones from getting accept'ed. * set a chance next time.
*/ */
--pi_end; memmove (pi_fd + i, pi_fd + i + 1, n - (i + 1));
memmove (pi_fd, pi_fd + 1, pi_end - pi_fd); pi_fd[n - 1] = sfd;
*pi_end = *pi; msg_Dbg (p_this, "accepted socket %d (from socket %d)", fd, sfd);
msg_Dbg (p_this, "accepted socket %d (from socket %d)", fd, *pi);
return fd; return fd;
} }
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment