Skip to content

Commit 4485acf

Browse files
committed
select()
1 parent faf6b1d commit 4485acf

2 files changed

Lines changed: 39 additions & 2 deletions

File tree

src/library.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7058,6 +7058,26 @@ LibraryManager.library = {
70587058
return fd;
70597059
},
70607060

7061+
select: function(nfds, readfds, writefds, exceptfds, timeout) {
7062+
// only readfds are supported, not writefds or exceptfds
7063+
// timeout is always 0 - fully async
7064+
assert(!writefds && !exceptfds);
7065+
var ret = 0;
7066+
var l = {{{ makeGetValue('readfds', 0, 'i32') }}};
7067+
var h = {{{ makeGetValue('readfds', 4, 'i32') }}};
7068+
nfds = Math.min(64, nfds); // fd sets have 64 bits
7069+
for (var fd = 0; fd < nfds; fd++) {
7070+
var bit = fd % 32, int = fd < 32 ? l : h;
7071+
if (int & (1 << bit)) {
7072+
// index is in the set, check if it is ready for read
7073+
var info = Sockets.fds[fd];
7074+
if (!info) continue;
7075+
if (info.bufferWrite != info.bufferRead) ret++;
7076+
}
7077+
}
7078+
return ret;
7079+
},
7080+
70617081
// ==========================================================================
70627082
// emscripten.h
70637083
// ==========================================================================

tests/websockets.c

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <string.h>
99
#include <unistd.h>
1010
#include <sys/ioctl.h>
11+
#include <assert.h>
1112
#if EMSCRIPTEN
1213
#include <emscripten.h>
1314
#endif
@@ -16,11 +17,25 @@
1617

1718
int SocketFD;
1819

20+
int not_always_data = 0;
21+
1922
unsigned int get_all_buf(int sock, char* output, unsigned int maxsize)
2023
{
24+
// select check for IO
25+
fd_set sett;
26+
FD_ZERO(&sett);
27+
assert(select(64, &sett, NULL, NULL, NULL) == 0); // empty set
28+
FD_SET(sock, &sett);
29+
assert(select(0, &sett, NULL, NULL, NULL) == 0); // max FD to check is 0
30+
int select_says_yes = select(64, &sett, NULL, NULL, NULL);
31+
32+
// ioctl check for IO
2133
int bytes;
22-
if (ioctl(sock, FIONREAD, &bytes)) return 0;
23-
if (bytes == 0) return 0;
34+
if (ioctl(sock, FIONREAD, &bytes) || bytes == 0) {
35+
not_always_data = 1;
36+
return 0;
37+
}
38+
assert(select_says_yes); // ioctl must agree with select
2439

2540
char buffer[1024];
2641
int n;
@@ -67,6 +82,8 @@ void iter(void *arg) {
6782
printf("sum: %d\n", sum);
6883

6984
#if EMSCRIPTEN
85+
assert(not_always_data == 1);
86+
7087
int result = sum;
7188
REPORT_RESULT();
7289
#endif

0 commit comments

Comments
 (0)