Skip to content

Commit de82576

Browse files
committed
refactor socket code, ensure that we send all a sendmsg's iovs in a single socket send
1 parent a11bed2 commit de82576

1 file changed

Lines changed: 31 additions & 29 deletions

File tree

src/library.js

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -6853,39 +6853,36 @@ LibraryManager.library = {
68536853
}
68546854
}
68556855
}
6856-
info.sendQueue = new Uint8Array(1024);
6857-
info.sendQueueUsed = 0;
6858-
info.senderWaiting = false;
6859-
info.sender = function(data, justQueue) {
6860-
if (data) {
6856+
function send(data) {
6857+
// TODO: if browser accepts views, can optimize this
68616858
#if SOCKET_DEBUG
6862-
Module.print(['sender', data, data.length, '|', Array.prototype.slice.call(data)]);
6859+
Module.print('sender actually sending ' + Array.prototype.slice.call(data));
68636860
#endif
6864-
if (info.sendQueueUsed + data.length >= info.sendQueue.length) {
6865-
var newQueue = new Uint8Array(2*Math.max(info.sendQueue.length, data.length));
6866-
newQueue.set(info.sendQueue);
6867-
info.sendQueue = newQueue;
6868-
}
6869-
info.sendQueue.set(data, info.sendQueueUsed); // must copy, because while this waits memory can change!
6870-
info.sendQueueUsed += data.length;
6871-
} else {
6872-
info.senderWaiting = false; // we are a setTimeout callback
6873-
if (info.sendQueueUsed == 0) return;
6874-
}
6861+
info.socket.send(new Uint8Array(data).buffer);
6862+
}
6863+
var queue = [];
6864+
var intervalling = false, interval;
6865+
function trySend() {
68756866
if (info.socket.readyState != info.socket.OPEN) {
6876-
if (!info.senderWaiting) {
6867+
if (!intervalling) {
6868+
intervalling = true;
68776869
console.log('waiting for socket in order to send');
6878-
setTimeout(info.sender, 100);
6879-
info.senderWaiting = true;
6870+
interval = setInterval(trySend, 100);
68806871
}
68816872
return;
68826873
}
6883-
if (justQueue) return;
6884-
#if SOCKET_DEBUG
6885-
Module.print('sender actually sending ' + info.sendQueueUsed);
6886-
#endif
6887-
info.socket.send(new Uint8Array(info.sendQueue.subarray(0, info.sendQueueUsed)).buffer); // TODO: if browser accepts views, can optimize this
6888-
info.sendQueueUsed = 0;
6874+
for (var i = 0; i < queue.length; i++) {
6875+
send(queue[i]);
6876+
}
6877+
queue.length = 0;
6878+
if (intervalling) {
6879+
intervalling = false;
6880+
clearInterval(interval);
6881+
}
6882+
}
6883+
info.sender = function(data) {
6884+
queue.push(data);
6885+
trySend();
68896886
};
68906887
return 0;
68916888
},
@@ -6935,21 +6932,26 @@ LibraryManager.library = {
69356932
}
69366933
var iov = {{{ makeGetValue('msg', 'Sockets.msghdr_layout.msg_iov', 'i8*') }}};
69376934
var num = {{{ makeGetValue('msg', 'Sockets.msghdr_layout.msg_iovlen', 'i32') }}};
6938-
var ret = 0;
69396935
#if SOCKET_DEBUG
69406936
Module.print('sendmsg vecs: ' + num);
69416937
#endif
6938+
var totalSize = 0;
6939+
for (var i = 0; i < num; i++) {
6940+
totalSize += {{{ makeGetValue('iov', '8*i + 4', 'i32') }}};
6941+
}
6942+
var buffer = new Uint8Array(totalSize);
6943+
var ret = 0;
69426944
for (var i = 0; i < num; i++) {
69436945
var currNum = {{{ makeGetValue('iov', '8*i + 4', 'i32') }}};
69446946
#if SOCKET_DEBUG
69456947
Module.print('sendmsg curr size: ' + currNum);
69466948
#endif
69476949
if (!currNum) continue;
69486950
var currBuf = {{{ makeGetValue('iov', '8*i', 'i8*') }}};
6949-
info.sender(HEAPU8.subarray(currBuf, currBuf+currNum), true);
6951+
buffer.set(HEAPU8.subarray(currBuf, currBuf+currNum), ret);
69506952
ret += currNum;
69516953
}
6952-
info.sender(null); // flush all of these together. Important they get sent as a single socket message
6954+
info.sender(buffer); // send all the iovs as a single message
69536955
return ret;
69546956
},
69556957

0 commit comments

Comments
 (0)