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

network: use vlc_read_i11e() in net_Read() and simplify

parent 4b865c66
...@@ -32,47 +32,22 @@ ...@@ -32,47 +32,22 @@
# include "config.h" # include "config.h"
#endif #endif
#include <vlc_common.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include <limits.h> #include <limits.h>
#include <errno.h> #include <errno.h>
#include <assert.h> #include <assert.h>
#include <fcntl.h>
#include <unistd.h> #include <unistd.h>
#ifdef HAVE_POLL
# include <poll.h>
#endif
#include <vlc_network.h>
#include <vlc_interrupt.h>
#ifndef INADDR_ANY
# define INADDR_ANY 0x00000000
#endif
#ifndef INADDR_NONE
# define INADDR_NONE 0xFFFFFFFF
#endif
#if defined(_WIN32)
# undef EAFNOSUPPORT
# define EAFNOSUPPORT WSAEAFNOSUPPORT
# undef EWOULDBLOCK
# define EWOULDBLOCK WSAEWOULDBLOCK
# undef EAGAIN
# define EAGAIN WSAEWOULDBLOCK
#endif
#ifdef HAVE_LINUX_DCCP_H #ifdef HAVE_LINUX_DCCP_H
/* TODO: use glibc instead of linux-kernel headers */ /* TODO: use glibc instead of linux-kernel headers */
# include <linux/dccp.h> # include <linux/dccp.h>
# define SOL_DCCP 269 # define SOL_DCCP 269
#endif #endif
#include "libvlc.h" /* vlc_object_waitpipe */ #include <vlc_common.h>
#include <vlc_network.h>
#include <vlc_interrupt.h>
extern int rootwrap_bind (int family, int socktype, int protocol, extern int rootwrap_bind (int family, int socktype, int protocol,
const struct sockaddr *addr, size_t alen); const struct sockaddr *addr, size_t alen);
...@@ -256,86 +231,55 @@ int *net_Listen (vlc_object_t *p_this, const char *psz_host, ...@@ -256,86 +231,55 @@ int *net_Listen (vlc_object_t *p_this, const char *psz_host,
* We repeat until we have read the right amount of data; * We repeat until we have read the right amount of data;
* a short count means EOF has been reached or an error. * a short count means EOF has been reached or an error.
*****************************************************************************/ *****************************************************************************/
ssize_t (net_Read)(vlc_object_t *restrict p_this, int fd, ssize_t (net_Read)(vlc_object_t *restrict obj, int fd,
void *restrict p_buf, size_t i_buflen) void *restrict buf, size_t len)
{ {
struct pollfd ufd[2]; size_t rd = 0;
ufd[0].fd = fd;
ufd[0].events = POLLIN;
ufd[1].fd = vlc_object_waitpipe (p_this);
ufd[1].events = POLLIN;
size_t i_total = 0;
do do
{ {
#ifdef _WIN32 if (!vlc_object_alive(obj))
ssize_t n = recv (fd, p_buf, i_buflen, 0);
#else
ssize_t n = read (fd, p_buf, i_buflen);
#endif
if (n < 0)
{
switch (net_errno)
{ {
case EAGAIN: /* no data */ vlc_testcancel();
#if (EAGAIN != EWOULDBLOCK) errno = EINTR;
case EWOULDBLOCK: return -1;
#endif }
break;
#ifndef _WIN32 #ifndef _WIN32
case EINTR: /* asynchronous signal */ ssize_t val = vlc_read_i11e(fd, buf, len);
continue;
#else #else
case WSAEMSGSIZE: /* datagram too big */ ssize_t val = vlc_recv_i11e(fd, buf, len, 0);
n = i_buflen;
break;
#endif #endif
default: if (val < 0)
goto error;
}
}
else
if (n > 0)
{ {
i_total += n; if (errno == EINTR || errno == EAGAIN)
p_buf = (char *)p_buf + n; continue;
i_buflen -= n; #ifdef _WIN32
else if (WSAGetLastError() == WSAEMSGSIZE) /* datagram too big */
if (i_buflen == 0)
break;
}
else /* n == 0 */
break;/* end of stream or empty packet */
if (ufd[1].fd == -1)
{ {
errno = EINTR; msg_Warn(obj, "read truncated to %zu bytes", len);
return -1; val = len;
} }
/* Wait for more data */ #endif
if (poll (ufd, sizeof (ufd) / sizeof (ufd[0]), -1) < 0) else
{ {
if (errno == EINTR) msg_Err(obj, "read error: %s", vlc_strerror_c(errno));
continue; return rd ? (ssize_t)rd : -1;
goto error;
} }
if (ufd[1].revents)
{
msg_Dbg (p_this, "socket %d polling interrupted", fd);
errno = EINTR;
return -1;
} }
assert (ufd[0].revents); rd += val;
if (val == 0)
break;
assert(len >= (size_t)val);
len -= val;
buf = ((char *)buf) + val;
} }
while (i_buflen > 0); while (len > 0);
return i_total; return rd;
error:
msg_Err (p_this, "read error: %s", vlc_strerror_c(errno));
return -1;
} }
/** /**
......
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