All files / src/Ice IdleTimeoutTransceiverDecorator.js

98.19% Statements 109/111
100% Branches 24/24
94.44% Functions 17/18
98.19% Lines 109/111

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 108 109 110 111 11241x 41x 41x 41x 334x 334x 334x 334x 334x 334x 334x 334x 334x 334x 334x 334x 41x 41x 334x 334x 41x 41x 668x 668x 41x 41x 485x 485x 41x 41x 576x 576x 41x 41x 334x 334x 334x 334x 41x 41x 334x 334x 41x 41x 3543x 3543x 3543x 3541x 3541x 3541x 3543x 3543x 41x 41x 7488x 7488x 7488x 7488x 41x 41x     41x 41x 481x 481x 41x 41x 1x 1x 41x 41x 243x 243x 41x 41x 578x 578x 578x 578x 41x 41x 7816x 7482x 7482x 7482x 7816x 41x 41x 7996x 4119x 4119x 4119x 7996x 41x 41x 7488x 7482x 7482x 2x 7482x 7482x 7488x 41x 41x 4119x 4119x 24x 4119x 4119x 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);
    }
 
    setBufferSize(rcvSize, sndSize) {
        this._decoratee.setBufferSize(rcvSize, sndSize);
    }
 
    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);
    }
}