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

network: use vlc_write_i11e() in net_Write() and simplify

parent a3a6a95a
...@@ -48,6 +48,7 @@ ...@@ -48,6 +48,7 @@
#endif #endif
#include <vlc_network.h> #include <vlc_network.h>
#include <vlc_interrupt.h>
#ifndef INADDR_ANY #ifndef INADDR_ANY
# define INADDR_ANY 0x00000000 # define INADDR_ANY 0x00000000
...@@ -337,7 +338,6 @@ error: ...@@ -337,7 +338,6 @@ error:
return -1; return -1;
} }
#undef net_Write
/** /**
* Writes data to a socket. * Writes data to a socket.
* This blocks until all data is written or an error occurs. * This blocks until all data is written or an error occurs.
...@@ -347,73 +347,40 @@ error: ...@@ -347,73 +347,40 @@ error:
* @return the total number of bytes written, or -1 if an error occurs * @return the total number of bytes written, or -1 if an error occurs
* before any data is written. * before any data is written.
*/ */
ssize_t net_Write( vlc_object_t *p_this, int fd, ssize_t (net_Write)(vlc_object_t *obj, int fd, const void *buf, size_t len)
const void *restrict p_data, size_t i_data )
{ {
size_t i_total = 0; size_t written = 0;
struct pollfd ufd[2] = {
{ .fd = fd, .events = POLLOUT },
{ .fd = vlc_object_waitpipe (p_this), .events = POLLIN },
};
if (unlikely(ufd[1].fd == -1))
{
vlc_testcancel ();
return -1;
}
while( i_data > 0 ) do
{ {
ufd[0].revents = ufd[1].revents = 0; if (!vlc_object_alive(obj))
if (poll (ufd, sizeof (ufd) / sizeof (ufd[0]), -1) == -1)
{ {
if (errno == EINTR) vlc_testcancel();
continue; errno = EINTR;
msg_Err (p_this, "Polling error: %s", vlc_strerror_c(errno));
return -1; return -1;
} }
if (i_total > 0) ssize_t val = vlc_send_i11e (fd, buf, len, MSG_NOSIGNAL);
{ /* If POLLHUP resp. POLLERR|POLLNVAL occurs while we have already
* read some data, it is important that we first return the number
* of bytes read, and then return 0 resp. -1 on the NEXT call. */
if (ufd[0].revents & (POLLHUP|POLLERR|POLLNVAL))
break;
if (ufd[1].revents) /* VLC object signaled */
break;
}
else
{
if (ufd[1].revents)
{
errno = EINTR;
goto error;
}
}
ssize_t val = send (fd, p_data, i_data, MSG_NOSIGNAL);
if (val == -1) if (val == -1)
{ {
if (errno == EINTR) if (errno == EINTR || errno == EAGAIN)
continue; continue;
msg_Err (p_this, "Write error: %s", vlc_strerror_c(errno));
break;
}
p_data = (const char *)p_data + val; msg_Err(obj, "write error: %s", vlc_strerror_c(errno));
i_data -= val; return written ? (ssize_t)written : -1;
i_total += val; }
}
if (unlikely(i_data == 0)) if (val == 0)
vlc_testcancel (); /* corner case */ break;
if ((i_total > 0) || (i_data == 0)) written += val;
return i_total; assert(len >= (size_t)val);
len -= val;
buf = ((const char *)buf) + val;
}
while (len > 0);
error: return written;
return -1;
} }
#undef net_Gets #undef net_Gets
......
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