#include <sys/uio.h>
#include <string.h>
#include <stdarg.h>
+#include <poll.h>
#include <systemd/sd-event.h>
aws_disconnect(ws, 1);
}
+/*
+ * Is the websocket 'ws' still connected ?
+ */
+int afb_ws_is_connected(struct afb_ws *ws)
+{
+ return ws->ws != NULL;
+}
+
/*
* Sends a 'close' command to the endpoint of 'ws' with the 'code' and the
* 'reason' (that can be NULL and that else should not be greater than 123
static ssize_t aws_writev(struct afb_ws *ws, const struct iovec *iov, int iovcnt)
{
ssize_t rc;
- do {
+ for (;;) {
rc = writev(ws->fd, iov, iovcnt);
- } while(rc == -1 && errno == EINTR);
- return rc;
+ if (rc == -1) {
+ if (errno == EINTR)
+ continue;
+ else if (errno == EAGAIN) {
+ struct pollfd pfd;
+ pfd.fd = ws->fd;
+ pfd.events = POLLOUT;
+ poll(&pfd, 1, 10);
+ continue;
+ }
+ }
+ return rc;
+ }
}
/*
*/
static int aws_read(struct afb_ws *ws, size_t size)
{
+ struct pollfd pfd;
ssize_t sz;
char *buffer;
if (buffer == NULL)
return 0;
ws->buffer.buffer = buffer;
- sz = websock_read(ws->ws, &buffer[ws->buffer.size], size);
- if ((size_t)sz != size)
- return 0;
- ws->buffer.size += size;
+ do {
+ sz = websock_read(ws->ws, &buffer[ws->buffer.size], size);
+ if (sz < 0) {
+ if (errno != EAGAIN)
+ return 0;
+ pfd.fd = ws->fd;
+ pfd.events = POLLIN;
+ poll(&pfd, 1, 10);
+ } else {
+ ws->buffer.size += (size_t)sz;
+ size -= (size_t)sz;
+ }
+ } while (size != 0);
}
return 1;
}