Skip to content

Commit

Permalink
Merge branch 'user_getters_and_setters_in_websock' of https://github.…
Browse files Browse the repository at this point in the history
  • Loading branch information
CendioOssman committed Jan 8, 2019
2 parents e355702 + 879e33a commit 7a1f2e4
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 93 deletions.
5 changes: 2 additions & 3 deletions core/decoders/hextile.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ export default class HextileDecoder {
return false;
}

let rQ = sock.get_rQ();
let rQi = sock.get_rQi();
let rQ = sock.rQ;
let rQi = sock.rQi;

let subencoding = rQ[rQi]; // Peek
if (subencoding > 30) { // Raw
Expand Down Expand Up @@ -129,7 +129,6 @@ export default class HextileDecoder {
}
display.finishTile();
}
sock.set_rQi(rQi);
this._lastsubencoding = subencoding;
this._tiles--;
}
Expand Down
6 changes: 3 additions & 3 deletions core/decoders/raw.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ export default class RawDecoder {

const cur_y = y + (height - this._lines);
const curr_height = Math.min(this._lines,
Math.floor(sock.rQlen() / bytesPerLine));
let data = sock.get_rQ();
let index = sock.get_rQi();
Math.floor(sock.rQlen / bytesPerLine));
let data = sock.rQ;
let index = sock.rQi;

// Convert data if needed
if (depth == 8) {
Expand Down
4 changes: 2 additions & 2 deletions core/decoders/tight.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ export default class TightDecoder {
return false;
}

const rQi = sock.get_rQi();
const rQ = sock.rQwhole();
const rQi = sock.rQi;
const rQ = sock.rQ;

display.fillRect(x, y, width, height,
[rQ[rQi + 2], rQ[rQi + 1], rQ[rQi]], false);
Expand Down
12 changes: 6 additions & 6 deletions core/rfb.js
Original file line number Diff line number Diff line change
Expand Up @@ -681,7 +681,7 @@ export default class RFB extends EventTargetMixin {
}

_handle_message() {
if (this._sock.rQlen() === 0) {
if (this._sock.rQlen === 0) {
Log.Warn("handle_message called on an empty receive queue");
return;
}
Expand All @@ -698,7 +698,7 @@ export default class RFB extends EventTargetMixin {
if (!this._normal_msg()) {
break;
}
if (this._sock.rQlen() === 0) {
if (this._sock.rQlen === 0) {
break;
}
}
Expand Down Expand Up @@ -779,7 +779,7 @@ export default class RFB extends EventTargetMixin {
// Message Handlers

_negotiate_protocol_version() {
if (this._sock.rQlen() < 12) {
if (this._sock.rQlen < 12) {
return this._fail("Received incomplete protocol version.");
}

Expand Down Expand Up @@ -1360,7 +1360,7 @@ export default class RFB extends EventTargetMixin {

_handle_xvp_msg() {
if (this._sock.rQwait("XVP version and message", 3, 1)) { return false; }
this._sock.rQskip8(); // Padding
this._sock.rQskipBytes(1); // Padding
const xvp_ver = this._sock.rQshift8();
const xvp_msg = this._sock.rQshift8();

Expand Down Expand Up @@ -1442,15 +1442,15 @@ export default class RFB extends EventTargetMixin {
_onFlush() {
this._flushing = false;
// Resume processing
if (this._sock.rQlen() > 0) {
if (this._sock.rQlen > 0) {
this._handle_message();
}
}

_framebufferUpdate() {
if (this._FBU.rects === 0) {
if (this._sock.rQwait("FBU header", 3, 1)) { return false; }
this._sock.rQskip8(); // Padding
this._sock.rQskipBytes(1); // Padding
this._FBU.rects = this._sock.rQshift16();

// Make sure the previous frame is fully rendered first
Expand Down
69 changes: 30 additions & 39 deletions core/websock.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,58 +45,58 @@ export default class Websock {
}

// Getters and Setters
get_sQ() {
get sQ() {
return this._sQ;
}

get_rQ() {
get rQ() {
return this._rQ;
}

get_rQi() {
get rQi() {
return this._rQi;
}

set_rQi(val) {
set rQi(val) {
this._rQi = val;
}

// Receive Queue
rQlen() {
get rQlen() {
return this._rQlen - this._rQi;
}

rQpeek8() {
return this._rQ[this._rQi];
}

rQshift8() {
return this._rQ[this._rQi++];
}

rQskip8() {
this._rQi++;
rQskipBytes(bytes) {
this._rQi += bytes;
}

rQskipBytes(num) {
this._rQi += num;
rQshift8() {
return this._rQshift(1);
}

// TODO(directxman12): test performance with these vs a DataView
rQshift16() {
return (this._rQ[this._rQi++] << 8) +
this._rQ[this._rQi++];
return this._rQshift(2);
}

rQshift32() {
return (this._rQ[this._rQi++] << 24) +
(this._rQ[this._rQi++] << 16) +
(this._rQ[this._rQi++] << 8) +
this._rQ[this._rQi++];
return this._rQshift(4);
}

// TODO(directxman12): test performance with these vs a DataView
_rQshift(bytes) {
let res = 0;
for (let byte = bytes - 1; byte >= 0; byte--) {
res += this._rQ[this._rQi++] << (byte * 8);
}
return res;
}

rQshiftStr(len) {
if (typeof(len) === 'undefined') { len = this.rQlen(); }
if (typeof(len) === 'undefined') { len = this.rQlen; }
let str = "";
// Handle large arrays in steps to avoid long strings on the stack
for (let i = 0; i < len; i += 4096) {
Expand All @@ -107,36 +107,27 @@ export default class Websock {
}

rQshiftBytes(len) {
if (typeof(len) === 'undefined') { len = this.rQlen(); }
if (typeof(len) === 'undefined') { len = this.rQlen; }
this._rQi += len;
return new Uint8Array(this._rQ.buffer, this._rQi - len, len);
}

rQshiftTo(target, len) {
if (len === undefined) { len = this.rQlen(); }
if (len === undefined) { len = this.rQlen; }
// TODO: make this just use set with views when using a ArrayBuffer to store the rQ
target.set(new Uint8Array(this._rQ.buffer, this._rQi, len));
this._rQi += len;
}

rQwhole() {
return new Uint8Array(this._rQ.buffer, 0, this._rQlen);
}

rQslice(start, end) {
if (end) {
return new Uint8Array(this._rQ.buffer, this._rQi + start, end - start);
} else {
return new Uint8Array(this._rQ.buffer, this._rQi + start, this._rQlen - this._rQi - start);
}
rQslice(start, end = this.rQlen) {
return new Uint8Array(this._rQ.buffer, this._rQi + start, end - start);
}

// Check to see if we must wait for 'num' bytes (default to FBU.bytes)
// to be available in the receive queue. Return true if we need to
// wait (and possibly print a debug message), otherwise false.
rQwait(msg, num, goback) {
const rQlen = this._rQlen - this._rQi; // Skip rQlen() function call
if (rQlen < num) {
if (this.rQlen < num) {
if (goback) {
if (this._rQi < goback) {
throw new Error("rQwait cannot backup " + goback + " bytes");
Expand Down Expand Up @@ -235,21 +226,21 @@ export default class Websock {
}

_expand_compact_rQ(min_fit) {
const resizeNeeded = min_fit || this._rQlen - this._rQi > this._rQbufferSize / 2;
const resizeNeeded = min_fit || this.rQlen > this._rQbufferSize / 2;
if (resizeNeeded) {
if (!min_fit) {
// just double the size if we need to do compaction
this._rQbufferSize *= 2;
} else {
// otherwise, make sure we satisy rQlen - rQi + min_fit < rQbufferSize / 8
this._rQbufferSize = (this._rQlen - this._rQi + min_fit) * 8;
this._rQbufferSize = (this.rQlen + min_fit) * 8;
}
}

// we don't want to grow unboundedly
if (this._rQbufferSize > MAX_RQ_GROW_SIZE) {
this._rQbufferSize = MAX_RQ_GROW_SIZE;
if (this._rQbufferSize - this._rQlen - this._rQi < min_fit) {
if (this._rQbufferSize - this.rQlen < min_fit) {
throw new Error("Receive Queue buffer exceeded " + MAX_RQ_GROW_SIZE + " bytes, and the new message could not fit");
}
}
Expand Down Expand Up @@ -283,7 +274,7 @@ export default class Websock {

_recv_message(e) {
this._decode_message(e.data);
if (this.rQlen() > 0) {
if (this.rQlen > 0) {
this._eventHandlers.message();
// Compact the receive queue
if (this._rQlen == this._rQi) {
Expand Down
2 changes: 1 addition & 1 deletion tests/test.rfb.js
Original file line number Diff line number Diff line change
Expand Up @@ -1635,7 +1635,7 @@ describe('Remote Frame Buffer Protocol Client', function () {
client._sock._websocket._receive_data(new Uint8Array([0, 0, 0, 3]));
expect(client._sock._websocket._get_sent_data()).to.have.length(0);

client._framebufferUpdate = function () { this._sock.rQskip8(); return true; }; // we magically have enough data
client._framebufferUpdate = function () { this._sock.rQskipBytes(1); return true; }; // we magically have enough data
// 247 should *not* be used as the message type here
client._sock._websocket._receive_data(new Uint8Array([247]));
expect(client._sock).to.have.sent(expected_msg._sQ);
Expand Down
Loading

0 comments on commit 7a1f2e4

Please sign in to comment.