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

Really put socket into non-blocking mode

Really fixes #1051
parent 2a31ac75
...@@ -276,82 +276,77 @@ int *__net_ListenTCP (vlc_object_t *p_this, const char *psz_host, int i_port) ...@@ -276,82 +276,77 @@ int *__net_ListenTCP (vlc_object_t *p_this, const char *psz_host, int i_port)
***************************************************************************** *****************************************************************************
* Accept a connection on a set of listening sockets and return it * Accept a connection on a set of listening sockets and return it
*****************************************************************************/ *****************************************************************************/
int __net_Accept( vlc_object_t *p_this, int *pi_fd, mtime_t i_wait ) int __net_Accept( vlc_object_t *p_this, int pi_fd[], mtime_t i_wait )
{ {
vlc_bool_t b_die = p_this->b_die, b_block = (i_wait < 0); vlc_bool_t b_block = (i_wait < 0);
msg_Err (p_this, "waiting for connection...");
while( p_this->b_die == b_die ) while( !p_this->b_die )
{ {
int i_val = -1, *pi, *pi_end; int maxfd = -1;
struct timeval timeout; fd_set readset;
fd_set fds_r, fds_e;
pi = pi_fd;
/* Initialize file descriptor set */ /* Initialize file descriptor set */
FD_ZERO( &fds_r ); FD_ZERO (&readset);
FD_ZERO( &fds_e );
for( pi = pi_fd; *pi != -1; pi++ ) int *pi_end;
for (const int *pi = pi_fd; *pi != -1; pi++)
{ {
int i_fd = *pi; int fd = *pi;
if( i_fd > i_val ) if (fd > maxfd)
i_val = i_fd; maxfd = fd;
FD_SET( i_fd, &fds_r ); FD_SET (fd, &readset);
FD_SET( i_fd, &fds_e ); pi_end++;
} }
pi_end = pi;
timeout.tv_sec = 0; struct timeval tv = { 0, b_block ? 500000 : i_wait };
timeout.tv_usec = b_block ? 500000 : i_wait;
i_val = select( i_val + 1, &fds_r, NULL, &fds_e, &timeout ); int val = select (maxfd, &readset, NULL, NULL, &tv);
if( ( ( i_val < 0 ) && ( net_errno == EINTR ) ) || i_val == 0 ) if (val == 0)
{ {
if( b_block ) if (b_block)
continue; continue;
else return -1;
return -1;
} }
else if( i_val < 0 ) if (val < 0)
{ {
msg_Err( p_this, "network select error (%s)", if (net_errno != EINTR)
net_strerror( net_errno ) ); msg_Err( p_this, "network select error (%s)",
net_strerror( net_errno ) );
return -1; return -1;
} }
for( pi = pi_fd; *pi != -1; pi++ ) for (const int *pi = pi_fd; *pi != -1; pi++)
{ {
int i_fd = *pi; int fd = *pi;
if( !FD_ISSET( i_fd, &fds_r ) && !FD_ISSET( i_fd, &fds_e ) ) if (!FD_ISSET (fd, &readset))
continue; continue;
i_val = accept( i_fd, NULL, 0 ); fd = accept (fd, NULL, 0);
if( i_val < 0 ) if (fd < 0)
msg_Err( p_this, "accept failed (%s)",
net_strerror( net_errno ) );
else
{ {
const int yes = 1; msg_Err (p_this, "accept failed (%s)",
setsockopt( i_fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof( yes )); net_strerror (net_errno));
continue;
}
setsockopt (fd, SOL_SOCKET, SO_REUSEADDR, &(int){ 1 }, sizeof (int));
#if defined (WIN32) || defined (UNDER_CE) #if defined (WIN32) || defined (UNDER_CE)
ioctlsocket (i_fd, FIONBIO, &(unsigned long){ 1 }); ioctlsocket (fd, FIONBIO, &(unsigned long){ 1 });
#else #else
fcntl (i_fd, F_SETFD, FD_CLOEXEC); fcntl (fd, F_SETFD, FD_CLOEXEC);
fcntl (i_fd, F_SETFL, fcntl (i_fd, F_GETFL, 0) | O_NONBLOCK); fcntl (fd, F_SETFL, fcntl (fd, F_GETFL, 0) | O_NONBLOCK);
#endif #endif
/*
* This round-robin trick ensures that the first sockets in /*
* pi_fd won't prevent the last ones from getting accept'ed. * This round-robin trick ensures that the first sockets in
*/ * pi_fd won't prevent the last ones from getting accept'ed.
--pi_end; */
memmove( pi, pi + 1, pi_end - pi ); --pi_end;
*pi_end = i_fd; memmove (pi_fd, pi_fd + 1, pi_end - pi_fd);
return i_val; *pi_end = *pi;
}
} }
} }
......
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