Skip to content

Commit 4804ea9

Browse files
committed
SOCKET_FORCED_MESSAGING option
1 parent 1951a1c commit 4804ea9

3 files changed

Lines changed: 41 additions & 9 deletions

File tree

src/library.js

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6835,14 +6835,31 @@ LibraryManager.library = {
68356835
#if SOCKET_DEBUG
68366836
Module.print(['onmessage', data.length, '|', Array.prototype.slice.call(data)]);
68376837
#endif
6838+
#if SOCKET_FORCED_MESSAGING
6839+
var i32View = new Uint32Array(data.buffer);
6840+
var start = 0;
6841+
while (start < data.length) {
6842+
var currLen = i32View[start>>2];
6843+
assert(currLen > 0);
6844+
start += 4;
6845+
assert(start + currLen <= data.length, [data.length, start, currLen]); // must not receive fractured messages!
6846+
info.inQueue.push(data.subarray(start, start+currLen));
6847+
#if SOCKET_DEBUG
6848+
Module.print(['onmessage message', currLen, '|', Array.prototype.slice.call(data.subarray(start, start+currLen))]);
6849+
#endif
6850+
start += currLen;
6851+
}
6852+
#else
68386853
info.inQueue.push(data);
6854+
#endif
68396855
}
68406856
function send(data) {
68416857
// TODO: if browser accepts views, can optimize this
68426858
#if SOCKET_DEBUG
68436859
Module.print('sender actually sending ' + Array.prototype.slice.call(data));
68446860
#endif
6845-
info.socket.send(new Uint8Array(data).buffer);
6861+
// ok to use the underlying buffer, we created data and know that the buffer starts at the beginning
6862+
info.socket.send(data.buffer);
68466863
}
68476864
var outQueue = [];
68486865
var intervalling = false, interval;
@@ -6865,7 +6882,15 @@ LibraryManager.library = {
68656882
}
68666883
}
68676884
info.sender = function(data) {
6868-
outQueue.push(data);
6885+
#if SOCKET_FORCED_MESSAGING
6886+
var buffer = new Uint8Array(data.length+4);
6887+
var i32View = new Uint32Array(buffer.buffer);
6888+
i32View[0] = data.length;
6889+
buffer.set(data, 4);
6890+
outQueue.push(buffer);
6891+
#else
6892+
outQueue.push(new Uint8Array(data));
6893+
#endif
68696894
trySend();
68706895
};
68716896
return 0;

src/settings.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,12 @@ var LIBRARY_DEBUG = 0; // Print out when we enter a library call (library*.js).
147147
var GL_DEBUG = 0; // Print out all calls into WebGL. As with LIBRARY_DEBUG, you can set a runtime
148148
// option, in this case GL.debug.
149149
var SOCKET_DEBUG = 0; // Log out socket/network data transfer.
150+
var SOCKET_FORCED_MESSAGING = 0; // If 1, we make sure that each socket send ends up a single socket
151+
// receive, that is, we force proper messaging (otherwise, sending
152+
// [A] and [B] can show up on the other side as [A, B]). This will
153+
// only work if both sides have it enabled, obviously, so it only
154+
// makes sense for p2p or when connecting to a special server - we
155+
// add some metadata (message size) to messages in this mode
150156

151157
var PROFILE_MAIN_LOOP = 0; // Profile the function called in set_main_loop
152158

tests/runner.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10283,13 +10283,14 @@ def relay_server(q):
1028310283
return relay_server
1028410284

1028510285
def test_zz_websockets_bi(self):
10286-
try:
10287-
with self.WebsockHarness(8992, self.make_relay_server(8992, 8994)):
10288-
with self.WebsockHarness(8994, no_server=True):
10289-
Popen([PYTHON, EMCC, path_from_root('tests', 'websockets_bi_side.c'), '-o', 'side.html', '-DSOCKK=8995']).communicate()
10290-
self.btest('websockets_bi.c', expected='2499')
10291-
finally:
10292-
self.clean_pids()
10286+
for fm in [0,1]:
10287+
try:
10288+
with self.WebsockHarness(8992, self.make_relay_server(8992, 8994)):
10289+
with self.WebsockHarness(8994, no_server=True):
10290+
Popen([PYTHON, EMCC, path_from_root('tests', 'websockets_bi_side.c'), '-o', 'side.html', '-DSOCKK=8995', '-s', 'SOCKET_FORCED_MESSAGING=%d' % fm]).communicate()
10291+
self.btest('websockets_bi.c', expected='2499', args=['-s', 'SOCKET_FORCED_MESSAGING=%d' % fm])
10292+
finally:
10293+
self.clean_pids()
1029310294

1029410295
def test_zz_websockets_bi_listen(self):
1029510296
try:

0 commit comments

Comments
 (0)