aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/library.js20
-rw-r--r--tests/websockets.c21
2 files changed, 39 insertions, 2 deletions
diff --git a/src/library.js b/src/library.js
index 42eb951f..75a8a90a 100644
--- a/src/library.js
+++ b/src/library.js
@@ -7058,6 +7058,26 @@ LibraryManager.library = {
return fd;
},
+ select: function(nfds, readfds, writefds, exceptfds, timeout) {
+ // only readfds are supported, not writefds or exceptfds
+ // timeout is always 0 - fully async
+ assert(!writefds && !exceptfds);
+ var ret = 0;
+ var l = {{{ makeGetValue('readfds', 0, 'i32') }}};
+ var h = {{{ makeGetValue('readfds', 4, 'i32') }}};
+ nfds = Math.min(64, nfds); // fd sets have 64 bits
+ for (var fd = 0; fd < nfds; fd++) {
+ var bit = fd % 32, int = fd < 32 ? l : h;
+ if (int & (1 << bit)) {
+ // index is in the set, check if it is ready for read
+ var info = Sockets.fds[fd];
+ if (!info) continue;
+ if (info.bufferWrite != info.bufferRead) ret++;
+ }
+ }
+ return ret;
+ },
+
// ==========================================================================
// emscripten.h
// ==========================================================================
diff --git a/tests/websockets.c b/tests/websockets.c
index 8a8a0919..57549e94 100644
--- a/tests/websockets.c
+++ b/tests/websockets.c
@@ -8,6 +8,7 @@
#include <string.h>
#include <unistd.h>
#include <sys/ioctl.h>
+#include <assert.h>
#if EMSCRIPTEN
#include <emscripten.h>
#endif
@@ -16,11 +17,25 @@
int SocketFD;
+int not_always_data = 0;
+
unsigned int get_all_buf(int sock, char* output, unsigned int maxsize)
{
+ // select check for IO
+ fd_set sett;
+ FD_ZERO(&sett);
+ assert(select(64, &sett, NULL, NULL, NULL) == 0); // empty set
+ FD_SET(sock, &sett);
+ assert(select(0, &sett, NULL, NULL, NULL) == 0); // max FD to check is 0
+ int select_says_yes = select(64, &sett, NULL, NULL, NULL);
+
+ // ioctl check for IO
int bytes;
- if (ioctl(sock, FIONREAD, &bytes)) return 0;
- if (bytes == 0) return 0;
+ if (ioctl(sock, FIONREAD, &bytes) || bytes == 0) {
+ not_always_data = 1;
+ return 0;
+ }
+ assert(select_says_yes); // ioctl must agree with select
char buffer[1024];
int n;
@@ -67,6 +82,8 @@ void iter(void *arg) {
printf("sum: %d\n", sum);
#if EMSCRIPTEN
+ assert(not_always_data == 1);
+
int result = sum;
REPORT_RESULT();
#endif