aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAlon Zakai <alonzakai@gmail.com>2013-01-04 17:14:50 -0800
committerAlon Zakai <alonzakai@gmail.com>2013-01-04 17:14:50 -0800
commit4804ea9ca448401588a061ea5356fa1aeeec126c (patch)
tree1274f2c2e23597bceb06dbf16b24a24956b00b3d /src
parent1951a1c74302c6ef11991cca801ec00d75ec1cc8 (diff)
SOCKET_FORCED_MESSAGING option
Diffstat (limited to 'src')
-rw-r--r--src/library.js29
-rw-r--r--src/settings.js6
2 files changed, 33 insertions, 2 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