From d053a25f535fbdfec5fb54a469e29decc6a27047 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Wed, 17 Aug 2016 03:20:34 -0700 Subject: [PATCH] peer: add lastSend and lastRecv. --- lib/bcoin/peer.js | 6 ++++++ lib/bcoin/utils.js | 4 +++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/lib/bcoin/peer.js b/lib/bcoin/peer.js index d83c0cb1..f1a5c029 100644 --- a/lib/bcoin/peer.js +++ b/lib/bcoin/peer.js @@ -108,6 +108,8 @@ function Peer(pool, options) { this.compactBlocks = {}; this.sentAddr = false; this.bip151 = null; + this.lastSend = 0; + this.lastRecv = 0; if (this.pool.options.bip151) this.bip151 = new bcoin.bip151(); @@ -673,6 +675,8 @@ Peer.prototype.write = function write(chunk) { if (this.destroyed) return false; + this.lastSend = utils.ms(); + return this.socket.write(chunk); }; @@ -824,6 +828,8 @@ Peer.prototype._onPacket = function onPacket(packet) { if (this.lastBlock && cmd !== 'tx') this._flushMerkle(); + this.lastRecv = utils.ms(); + switch (cmd) { case 'version': return this._handleVersion(payload); diff --git a/lib/bcoin/utils.js b/lib/bcoin/utils.js index 90f23938..5b7f82be 100644 --- a/lib/bcoin/utils.js +++ b/lib/bcoin/utils.js @@ -1007,7 +1007,7 @@ utils.testTarget = function testTarget(hash, target) { */ utils.now = function now() { - return Math.floor(+new Date() / 1000); + return Math.floor(utils.ms() / 1000); }; /** @@ -1016,6 +1016,8 @@ utils.now = function now() { */ utils.ms = function ms() { + if (Date.now) + return Date.now(); return +new Date(); };