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 @@
#endif
#include <vlc_network.h>
#include <vlc_interrupt.h>
#ifndef INADDR_ANY
# define INADDR_ANY 0x00000000
......@@ -337,7 +338,6 @@ error:
return -1;
}
#undef net_Write
/**
* Writes data to a socket.
* This blocks until all data is written or an error occurs.
......@@ -347,73 +347,40 @@ error:
* @return the total number of bytes written, or -1 if an error occurs
* before any data is written.
*/
ssize_t net_Write( vlc_object_t *p_this, int fd,
const void *restrict p_data, size_t i_data )
ssize_t (net_Write)(vlc_object_t *obj, int fd, const void *buf, size_t len)
{
size_t i_total = 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;
}
size_t written = 0;
while( i_data > 0 )
do
{
ufd[0].revents = ufd[1].revents = 0;
if (poll (ufd, sizeof (ufd) / sizeof (ufd[0]), -1) == -1)
if (!vlc_object_alive(obj))
{
if (errno == EINTR)
continue;
msg_Err (p_this, "Polling error: %s", vlc_strerror_c(errno));
vlc_testcancel();
errno = EINTR;
return -1;
}
if (i_total > 0)
{ /* 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);
ssize_t val = vlc_send_i11e (fd, buf, len, MSG_NOSIGNAL);
if (val == -1)
{
if (errno == EINTR)
if (errno == EINTR || errno == EAGAIN)
continue;
msg_Err (p_this, "Write error: %s", vlc_strerror_c(errno));
break;
}
p_data = (const char *)p_data + val;
i_data -= val;
i_total += val;
}
msg_Err(obj, "write error: %s", vlc_strerror_c(errno));
return written ? (ssize_t)written : -1;
}
if (unlikely(i_data == 0))
vlc_testcancel (); /* corner case */
if (val == 0)
break;
if ((i_total > 0) || (i_data == 0))
return i_total;
written += val;
assert(len >= (size_t)val);
len -= val;
buf = ((const char *)buf) + val;
}
while (len > 0);
error:
return -1;
return written;
}
#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