/*
- * Copyright (C) 2016 "IoT.bzh"
+ * Copyright (C) 2016, 2017 "IoT.bzh"
* Author: José Bollo <jose.bollo@iot.bzh>
*
* Licensed under the Apache License, Version 2.0 (the "License");
#include <sys/uio.h>
#include <string.h>
#include <stdarg.h>
+#include <poll.h>
#include <systemd/sd-event.h>
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;
+ }
}
/*
int rc;
assert(ws->ws != NULL);
- rc = websock_dispatch(ws->ws);
+ rc = websock_dispatch(ws->ws, 0);
if (rc < 0 && errno == EPIPE)
afb_ws_hangup(ws);
}
*/
static int aws_read(struct afb_ws *ws, size_t size)
{
+ struct pollfd pfd;
ssize_t sz;
char *buffer;
- if (size != 0) {
+ if (size != 0 || ws->buffer.buffer == NULL) {
buffer = realloc(ws->buffer.buffer, ws->buffer.size + size + 1);
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;
+ while (size != 0) {
+ 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); /* TODO: make fully asynchronous websockets */
+ } else {
+ ws->buffer.size += (size_t)sz;
+ size -= (size_t)sz;
+ }
+ }
}
return 1;
}
}
/*
- * Callback when 'close' command received from 'ws' with 'code' and 'size'.
+ * Callback when 'continue' command received from 'ws' with 'code' and 'size'.
*/
static void aws_on_continue(struct afb_ws *ws, int last, size_t size)
{