T13.764: poll added to prevent hang on write
-/* dnet: connections; T11.253-T13.730; $DVS:time$ */
+/* dnet: connections; T11.253-T13.764; $DVS:time$ */
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#if defined(_WIN32) || defined(_WIN64)
#include <sys/socket.h>
+#define poll WSAPoll
+#else
+#include <poll.h>
#endif
#include "dnet_connection.h"
#include "dnet_packet.h"
@@ -53,6 +56,11 @@ static ssize_t dnet_conn_write(void *private_data, void *buf, size_t size) {
struct dnet_connection *conn = (struct dnet_connection *)private_data;
ssize_t res = 0, done;
while (size) {
+ struct pollfd fd;
+ fd.fd = conn->socket;
+ fd.events = POLLOUT;
+ fd.revents = 0;
+ if (poll(&fd, 1, 0) != 1 || !(fd.revents & POLLOUT)) break;
done = write(conn->socket, buf, size);
if (done < 0) break;
res += done;
