diff options
author | Alon Zakai <alonzakai@gmail.com> | 2013-01-04 17:14:50 -0800 |
---|---|---|
committer | Alon Zakai <alonzakai@gmail.com> | 2013-01-04 17:14:50 -0800 |
commit | 4804ea9ca448401588a061ea5356fa1aeeec126c (patch) | |
tree | 1274f2c2e23597bceb06dbf16b24a24956b00b3d | |
parent | 1951a1c74302c6ef11991cca801ec00d75ec1cc8 (diff) |
SOCKET_FORCED_MESSAGING option
-rw-r--r-- | src/library.js | 29 | ||||
-rw-r--r-- | src/settings.js | 6 | ||||
-rwxr-xr-x | tests/runner.py | 15 |
3 files changed, 41 insertions, 9 deletions
diff --git a/src/library.js b/src/library.js index 9c011730..fad7b58c 100644 --- a/src/library.js +++ b/src/library.js @@ -6835,14 +6835,31 @@ LibraryManager.library = { #if SOCKET_DEBUG Module.print(['onmessage', data.length, '|', Array.prototype.slice.call(data)]); #endif +#if SOCKET_FORCED_MESSAGING + var i32View = new Uint32Array(data.buffer); + var start = 0; + while (start < data.length) { + var currLen = i32View[start>>2]; + assert(currLen > 0); + start += 4; + assert(start + currLen <= data.length, [data.length, start, currLen]); // must not receive fractured messages! + info.inQueue.push(data.subarray(start, start+currLen)); +#if SOCKET_DEBUG + Module.print(['onmessage message', currLen, '|', Array.prototype.slice.call(data.subarray(start, start+currLen))]); +#endif + start += currLen; + } +#else info.inQueue.push(data); +#endif } function send(data) { // TODO: if browser accepts views, can optimize this #if SOCKET_DEBUG Module.print('sender actually sending ' + Array.prototype.slice.call(data)); #endif - info.socket.send(new Uint8Array(data).buffer); + // ok to use the underlying buffer, we created data and know that the buffer starts at the beginning + info.socket.send(data.buffer); } var outQueue = []; var intervalling = false, interval; @@ -6865,7 +6882,15 @@ LibraryManager.library = { } } info.sender = function(data) { - outQueue.push(data); +#if SOCKET_FORCED_MESSAGING + var buffer = new Uint8Array(data.length+4); + var i32View = new Uint32Array(buffer.buffer); + i32View[0] = data.length; + buffer.set(data, 4); + outQueue.push(buffer); +#else + outQueue.push(new Uint8Array(data)); +#endif trySend(); }; return 0; diff --git a/src/settings.js b/src/settings.js index c530b083..ba37c715 100644 --- a/src/settings.js +++ b/src/settings.js @@ -147,6 +147,12 @@ var LIBRARY_DEBUG = 0; // Print out when we enter a library call (library*.js). var GL_DEBUG = 0; // Print out all calls into WebGL. As with LIBRARY_DEBUG, you can set a runtime // option, in this case GL.debug. var SOCKET_DEBUG = 0; // Log out socket/network data transfer. +var SOCKET_FORCED_MESSAGING = 0; // If 1, we make sure that each socket send ends up a single socket + // receive, that is, we force proper messaging (otherwise, sending + // [A] and [B] can show up on the other side as [A, B]). This will + // only work if both sides have it enabled, obviously, so it only + // makes sense for p2p or when connecting to a special server - we + // add some metadata (message size) to messages in this mode var PROFILE_MAIN_LOOP = 0; // Profile the function called in set_main_loop diff --git a/tests/runner.py b/tests/runner.py index 1ab5c7af..b26162c0 100755 --- a/tests/runner.py +++ b/tests/runner.py @@ -10283,13 +10283,14 @@ elif 'browser' in str(sys.argv): return relay_server def test_zz_websockets_bi(self): - try: - with self.WebsockHarness(8992, self.make_relay_server(8992, 8994)): - with self.WebsockHarness(8994, no_server=True): - Popen([PYTHON, EMCC, path_from_root('tests', 'websockets_bi_side.c'), '-o', 'side.html', '-DSOCKK=8995']).communicate() - self.btest('websockets_bi.c', expected='2499') - finally: - self.clean_pids() + for fm in [0,1]: + try: + with self.WebsockHarness(8992, self.make_relay_server(8992, 8994)): + with self.WebsockHarness(8994, no_server=True): + Popen([PYTHON, EMCC, path_from_root('tests', 'websockets_bi_side.c'), '-o', 'side.html', '-DSOCKK=8995', '-s', 'SOCKET_FORCED_MESSAGING=%d' % fm]).communicate() + self.btest('websockets_bi.c', expected='2499', args=['-s', 'SOCKET_FORCED_MESSAGING=%d' % fm]) + finally: + self.clean_pids() def test_zz_websockets_bi_listen(self): try: |