aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlon Zakai <alonzakai@gmail.com>2012-09-28 15:37:02 -0700
committerAlon Zakai <alonzakai@gmail.com>2012-09-28 15:37:02 -0700
commit32e88a57eb101df7a25f885699a05bcac1f791f7 (patch)
treeaa3ff673ecb9cb9ade75e46385f029ff77d8a234
parent46b3356e4812b8eb5e8986abee4626d9bac140b9 (diff)
support ioctl-FIONREAD
-rw-r--r--src/library.js17
-rw-r--r--src/settings.js3
-rw-r--r--system/include/sys/ioctl.h2
-rw-r--r--tests/websockets.c6
4 files changed, 25 insertions, 3 deletions
diff --git a/src/library.js b/src/library.js
index d69068fc..938a3c92 100644
--- a/src/library.js
+++ b/src/library.js
@@ -6368,6 +6368,7 @@ LibraryManager.library = {
connect__deps: ['$Sockets', '_inet_ntop_raw', 'ntohs'],
connect: function(fd, addr, addrlen) {
var info = Sockets.fds[fd];
+ if (!info) return -1;
info.connected = true;
info.addr = getValue(addr + Sockets.sockaddr_in_layout.sin_addr, 'i32');
info.port = _ntohs(getValue(addr + Sockets.sockaddr_in_layout.sin_port, 'i16'));
@@ -6396,6 +6397,7 @@ LibraryManager.library = {
recv__deps: ['$Sockets'],
recv: function(fd, buf, len, flags) {
var info = Sockets.fds[fd];
+ if (!info) return -1;
if (info.bufferWrite == info.bufferRead) {
___setErrNo(ERRNO_CODES.EAGAIN); // no data, and all sockets are nonblocking, so this is the right behavior
return 0; // should this be -1 like the spec says?
@@ -6412,10 +6414,23 @@ LibraryManager.library = {
},
shutdown: function(fd, how) {
- Sockets.fds[fd].socket.close();
+ var info = Sockets.fds[fd];
+ if (!info) return -1;
+ info.socket.close();
Sockets.fds[fd] = null;
},
+ ioctl: function(fd, request, varargs) {
+ var info = Sockets.fds[fd];
+ if (!info) return -1;
+ var start = info.bufferRead;
+ var end = info.bufferWrite;
+ if (end < start) end += Sockets.BUFFER_SIZE;
+ var dest = {{{ makeGetValue('varargs', '0', 'i32') }}};
+ {{{ makeSetValue('dest', '0', 'end - start', 'i32') }}};
+ return 0;
+ },
+
// ==========================================================================
// emscripten.h
// ==========================================================================
diff --git a/src/settings.js b/src/settings.js
index 0b9cf14b..9d0ccc05 100644
--- a/src/settings.js
+++ b/src/settings.js
@@ -1126,6 +1126,7 @@ var C_DEFINES = {'SI_MESGQ': '5',
'_CS_POSIX_V7_LP64_OFF64_LDFLAGS': '10',
'_SC_TTY_NAME_MAX': '41',
'AF_INET': '1',
- 'AF_INET6': '6'
+ 'AF_INET6': '6',
+ 'FIONREAD': '1'
};
diff --git a/system/include/sys/ioctl.h b/system/include/sys/ioctl.h
index edc9ea36..12857ae3 100644
--- a/system/include/sys/ioctl.h
+++ b/system/include/sys/ioctl.h
@@ -11,6 +11,8 @@ extern "C" {
#define TIOCGSIZE 80 // bogus
#define TIOCGWINSZ 80 // bogus
+#define FIONREAD 1
+
int ioctl(int d, int request, ...);
#define SO_RCVTIMEO 1000
diff --git a/tests/websockets.c b/tests/websockets.c
index c74b145d..6e81eb47 100644
--- a/tests/websockets.c
+++ b/tests/websockets.c
@@ -7,6 +7,7 @@
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
+#include <sys/ioctl.h>
#if EMSCRIPTEN
#include <emscripten.h>
#endif
@@ -17,8 +18,11 @@ int SocketFD;
unsigned int get_all_buf(int sock, char* output, unsigned int maxsize)
{
- char buffer[1024];
+ int bytes;
+ if (ioctl(sock, FIONREAD, &bytes)) return 0;
+ if (bytes == 0) return 0;
+ char buffer[1024];
int n;
unsigned int offset = 0;
while((errno = 0, (n = recv(sock, buffer, sizeof(buffer), 0))>0) ||