aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlon Zakai <alonzakai@gmail.com>2012-09-28 11:16:01 -0700
committerAlon Zakai <alonzakai@gmail.com>2012-09-28 11:16:01 -0700
commitfc15d3d8092bce3319a2b5e210dc97cb7859634a (patch)
tree74b07e18a3b6ad18ee3cfbfafe338c8e1c76258b
parent4298da27622e634de5c7e6ee910a0cc67c649e9a (diff)
fix websockify usage issues and add basic data reading
-rw-r--r--src/library.js35
-rwxr-xr-xtests/runner.py4
2 files changed, 33 insertions, 6 deletions
diff --git a/src/library.js b/src/library.js
index 06b03764..e03af95a 100644
--- a/src/library.js
+++ b/src/library.js
@@ -6345,6 +6345,7 @@ LibraryManager.library = {
$Sockets__deps: ['__setErrNo', '$ERRNO_CODES'],
$Sockets: {
+ BUFFER_SIZE: 10*1024,
nextFd: 1,
fds: {},
sockaddr_in_layout: Runtime.generateStructInfo([
@@ -6371,17 +6372,43 @@ LibraryManager.library = {
info.addr = getValue(addr + Sockets.sockaddr_in_layout.sin_addr, 'i32');
info.port = _ntohs(getValue(addr + Sockets.sockaddr_in_layout.sin_port, 'i16'));
info.host = __inet_ntop_raw(info.addr);
- info.socket = new WebSocket('ws://' + info.host + ':' + info.port, ['binary']);
+ info.socket = new WebSocket('ws://' + info.host + ':' + info.port, ['arraybuffer']);
+ info.socket.binaryType = 'arraybuffer';
+ info.buffer = new Uint8Array(Sockets.BUFFER_SIZE);
+ info.bufferWrite = info.bufferRead = 0;
info.socket.onmessage = function (event) {
- console.log(event.data + ',' + typeof event.data);
+ var data = event.data;
+ if (typeof data == 'string') {
+ var binaryString = window.atob(data);
+ var len = binaryString.length;
+ for (var i = 0; i < len; i++) {
+ info.buffer[info.bufferWrite++] = binaryString.charCodeAt(i);
+ if (info.bufferWrite == Sockets.BUFFER_SIZE) info.bufferWrite = 0;
+ if (info.bufferWrite == info.bufferRead) throw 'socket buffer overflow';
+ }
+ } else {
+ console.log('binary!');
+ }
}
return 0;
},
recv__deps: ['$Sockets'],
recv: function(fd, buf, len, flags) {
- //___setErrNo(ERRNO_CODES.EINTR);
- return 0; // TODO
+ var info = Sockets.fds[fd];
+ 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?
+ }
+ var ret = 0;
+ while (info.bufferWrite != info.bufferRead && len > 0) {
+ // write out a byte
+ {{{ makeSetValue('buf++', '0', 'info.buffer[info.bufferRead++]', 'i8') }}};
+ if (info.bufferRead == Sockets.BUFFER_SIZE) info.bufferRead = 0;
+ len--;
+ ret++;
+ }
+ return ret;
},
// ==========================================================================
diff --git a/tests/runner.py b/tests/runner.py
index 209064fc..776ebcb2 100755
--- a/tests/runner.py
+++ b/tests/runner.py
@@ -8450,14 +8450,14 @@ elif 'browser' in str(sys.argv):
def zzztest_websockets(self):
try:
def server_func():
- os.system('while true; do (/bin/echo -en "test\x02" ; sleep 1000) | nc -vvvl 8990; done;') # sleep to work around websockify issue 63
+ os.system('while true; do (/bin/echo -en "te\x01\xff\x79st\x02" ; sleep 1000) | nc -vvvl 127.0.0.1 8990; done;') # sleep to work around websockify issue 63
server = multiprocessing.Process(target=server_func)
server.start()
print '[Socket server on process %d]' % server.pid
def websockify_func():
- os.system('python ' + path_from_root('third_party', 'websockify', 'run') + ' -vvv 8991 127.0.0.1:8990')
+ os.system(path_from_root('third_party', 'websockify', 'other', 'websockify') + ' -vvv 8991 127.0.0.1:8990')
websockify = multiprocessing.Process(target=websockify_func)
websockify.start()