[go: nahoru, domu]

Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Websock API cleanup #1782

Merged
merged 18 commits into from
Jun 30, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Move WebSocket queue index reset to receive
It's more robust to do this just before we need the space, rather than
assume when the queue will be read and adjust things right after.
  • Loading branch information
CendioOssman committed Jun 4, 2023
commit 7356d4e60b689fc098b55f02ae3d7688e166194f
12 changes: 6 additions & 6 deletions core/websock.js
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,12 @@ export default class Websock {

// push arraybuffer values onto the end of the receive que
_recvMessage(e) {
if (this._rQlen == this._rQi) {
// All data has now been processed, this means we
// can reset the receive queue.
this._rQlen = 0;
this._rQi = 0;
}
const u8 = new Uint8Array(e.data);
if (u8.length > this._rQbufferSize - this._rQlen) {
this._expandCompactRQ(u8.length);
Expand All @@ -317,12 +323,6 @@ export default class Websock {

if (this._rQlen - this._rQi > 0) {
this._eventHandlers.message();
if (this._rQlen == this._rQi) {
// All data has now been processed, this means we
// can reset the receive queue.
this._rQlen = 0;
this._rQi = 0;
}
} else {
Log.Debug("Ignoring empty message");
}
Expand Down
11 changes: 5 additions & 6 deletions tests/test.websock.js
Original file line number Diff line number Diff line change
Expand Up @@ -418,14 +418,13 @@ describe('Websock', function () {
expect(sock._eventHandlers.message).not.to.have.been.called;
});

it('should compact the receive queue when a message handler empties it', function () {
sock._eventHandlers.message = () => { sock._rQi = sock._rQlen; };
it('should compact the receive queue when fully read', function () {
sock._rQ = new Uint8Array([0, 1, 2, 3, 4, 5, 0, 0, 0, 0]);
sock._rQlen = 6;
sock._rQi = 6;
const msg = { data: new Uint8Array([1, 2, 3]).buffer };
sock._recvMessage(msg);
expect(sock._rQlen).to.equal(0);
expect(sock._rQlen).to.equal(3);
expect(sock._rQi).to.equal(0);
});

Expand Down Expand Up @@ -455,13 +454,13 @@ describe('Websock', function () {
it('should automatically resize the receive queue if the incoming message is larger than 1/8th of the buffer and we reach the end of the buffer', function () {
sock._rQ = new Uint8Array(20);
sock._rQlen = 16;
sock._rQi = 16;
sock._rQi = 15;
sock._rQbufferSize = 20;
const msg = { data: new Uint8Array(6).buffer };
sock._recvMessage(msg);
expect(sock._rQlen).to.equal(6);
expect(sock._rQlen).to.equal(7);
expect(sock._rQi).to.equal(0);
expect(sock._rQ.length).to.equal(48);
expect(sock._rQ.length).to.equal(56);
});
});
});