Implicitly flush Websock if needed

Callers shouldn't have to deal with the internal buffering limits of
Websock, so implicitly flush the buffer if more room is needed.
This commit is contained in:
Pierre Ossman
2023-05-30 20:32:31 +02:00
parent f8b65f9fe1
commit ccef89f556
3 changed files with 139 additions and 19 deletions

View File

@@ -3047,23 +3047,8 @@ RFB.messages = {
}
sock.sQpush32(length);
// We have to keep track of from where in the data we begin creating the
// buffer for the flush in the next iteration.
let dataOffset = 0;
let remaining = data.length;
while (remaining > 0) {
let flushSize = Math.min(remaining, (sock._sQbufferSize - sock._sQlen));
sock.sQpushBytes(data.subarray(dataOffset, dataOffset + flushSize));
sock.flush();
remaining -= flushSize;
dataOffset += flushSize;
}
sock.sQpushBytes(data);
sock.flush();
},
setDesktopSize(sock, width, height, id, flags) {

View File

@@ -176,15 +176,18 @@ export default class Websock {
// Send Queue
sQpush8(num) {
this._sQensureSpace(1);
this._sQ[this._sQlen++] = num;
}
sQpush16(num) {
this._sQensureSpace(2);
this._sQ[this._sQlen++] = (num >> 8) & 0xff;
this._sQ[this._sQlen++] = (num >> 0) & 0xff;
}
sQpush32(num) {
this._sQensureSpace(4);
this._sQ[this._sQlen++] = (num >> 24) & 0xff;
this._sQ[this._sQlen++] = (num >> 16) & 0xff;
this._sQ[this._sQlen++] = (num >> 8) & 0xff;
@@ -197,8 +200,18 @@ export default class Websock {
}
sQpushBytes(bytes) {
this._sQ.set(bytes, this._sQlen);
this._sQlen += bytes.length;
for (let offset = 0;offset < bytes.length;) {
this._sQensureSpace(1);
let chunkSize = this._sQbufferSize - this._sQlen;
if (chunkSize > bytes.length - offset) {
chunkSize = bytes.length - offset;
}
this._sQ.set(bytes.subarray(offset, chunkSize), this._sQlen);
this._sQlen += chunkSize;
offset += chunkSize;
}
}
flush() {
@@ -208,6 +221,12 @@ export default class Websock {
}
}
_sQensureSpace(bytes) {
if (this._sQbufferSize - this._sQlen < bytes) {
this.flush();
}
}
// Event Handlers
off(evt) {
this._eventHandlers[evt] = () => {};