All files / src/Ice IdleTimeoutTransceiverDecorator.js

98.13% Statements 105/107
100% Branches 23/23
94.11% Functions 16/17
98.13% Lines 105/107

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 10841x 41x 41x 41x 320x 320x 320x 320x 320x 320x 320x 320x 320x 320x 320x 320x 41x 41x 320x 320x 41x 41x 640x 640x 41x 41x 473x 473x 41x 41x 556x 556x 41x 41x 320x 320x 320x 320x 41x 41x 320x 320x 41x 41x 3457x 3457x 3457x 3455x 3455x 3455x 3457x 3457x 41x 41x 7302x 7302x 7302x 7302x 41x 41x     41x 41x 475x 475x 41x 41x 237x 237x 41x 41x 558x 558x 558x 558x 41x 41x 7616x 7296x 7296x 7296x 7616x 41x 41x 7790x 4013x 4013x 4013x 7790x 41x 41x 7302x 7296x 7296x 2x 7296x 7296x 7302x 41x 41x 4013x 4013x 34x 4013x 4013x 41x  
// Copyright (c) ZeroC, Inc.
 
export class IdleTimeoutTransceiverDecorator {
    constructor(decoratee, connection, timer, idleTimeout, enableIdleCheck) {
        console.assert(idleTimeout > 0);
 
        this._decoratee = decoratee;
        this._idleTimeout = idleTimeout * 1000; // Convert seconds to milliseconds
        this._timer = timer;
        this._connection = connection;
 
        // _idleCheckEnabled is initially enableIdleCheck (by default, true) unlike C++/C#/Java.
        // Since JS supports only client connections, we know the connection will read at a minimum the initial
        // ValidateConnection message, and this reading will start or reset the read timer.
        this._idleCheckEnabled = enableIdleCheck;
    }
 
    setCallbacks(connectedCallback, bytesAvailableCallback, bytesWrittenCallback) {
        this._decoratee.setCallbacks(connectedCallback, bytesAvailableCallback, bytesWrittenCallback);
    }
 
    initialize(readBuffer, writeBuffer) {
        return this._decoratee.initialize(readBuffer, writeBuffer);
    }
 
    register() {
        this._decoratee.register();
    }
 
    unregister() {
        this._decoratee.unregister();
    }
 
    close() {
        this.cancelReadTimer();
        this.cancelWriteTimer();
        this._decoratee.close();
    }
 
    destroy() {
        this._decoratee.destroy();
    }
 
    write(buffer) {
        this.cancelWriteTimer();
        const completed = this._decoratee.write(buffer);
        if (completed) {
            // write completed
            this.rescheduleWriteTimer();
        }
        return completed;
    }
 
    read(buf, moreData) {
        // We don't want the idle check to run while we're reading, so we reschedule it before reading.
        this.rescheduleReadTimer();
        return this._decoratee.read(buf, moreData);
    }
 
    type() {
        return this._decoratee.type();
    }
 
    getInfo(adapterName, connectionId) {
        return this._decoratee.getInfo(adapterName, connectionId);
    }
 
    toString() {
        return this._decoratee.toString();
    }
 
    scheduleHeartbeat() {
        // Reschedule because the connection establishment may have already written to the connection and scheduled a
        // heartbeat.
        this.rescheduleWriteTimer();
    }
 
    cancelReadTimer() {
        if (this._readTimerToken !== undefined) {
            this._timer.cancel(this._readTimerToken);
            this._readTimerToken = undefined;
        }
    }
 
    cancelWriteTimer() {
        if (this._writeTimerToken !== undefined) {
            this._timer.cancel(this._writeTimerToken);
            this._writeTimerToken = undefined;
        }
    }
 
    rescheduleReadTimer() {
        if (this._idleCheckEnabled) {
            this.cancelReadTimer();
            this._readTimerToken = this._timer.schedule(() => {
                this._connection.idleCheck(this._idleTimeout);
            }, this._idleTimeout);
        }
    }
 
    rescheduleWriteTimer() {
        this.cancelWriteTimer();
        this._writeTimerToken = this._timer.schedule(() => {
            this._connection.sendHeartbeat();
        }, this._idleTimeout / 2);
    }
}