From 677df7f787950add2904d93ab57caa8c2c56205d Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Tue, 21 Oct 2014 18:51:05 -0700 Subject: [PATCH] more packet polling. --- example/index.js | 6 ++++++ lib/bitcoind.js | 11 +++++++++-- src/bitcoindjs.cc | 18 ++++++++++++------ 3 files changed, 27 insertions(+), 8 deletions(-) diff --git a/example/index.js b/example/index.js index 7998467a..cbde2790 100755 --- a/example/index.js +++ b/example/index.js @@ -113,6 +113,12 @@ bitcoind.on('open', function(status) { }); } + bitcoind.on('packets', function(packets) { + bitcoind.log(packets); + }); + + return; + argv['on-block'] = true; setTimeout(function() { bitcoind.on('block', function callee(block) { diff --git a/lib/bitcoind.js b/lib/bitcoind.js index 2a694045..d69f63d7 100644 --- a/lib/bitcoind.js +++ b/lib/bitcoind.js @@ -140,8 +140,15 @@ Bitcoin.prototype.start = function(options, callback) { setInterval(function() { var packets = bitcoindjs.hookPackets(); - console.log(packets); - }, 20); + if (!packets.length) return; + self.emit('packets', packets); + packets.forEach(function(packet) { + setImmediate(function() { + self.emit('packet:' + packet.name, packet); + self.emit('packet', packet); + }); + }); + }, 50); [sigint, sighup, sigquit].forEach(function(signal) { process.on(signal.name, signal.listener = function() { diff --git a/src/bitcoindjs.cc b/src/bitcoindjs.cc index f859c049..c57e56b1 100644 --- a/src/bitcoindjs.cc +++ b/src/bitcoindjs.cc @@ -3378,7 +3378,6 @@ boost::mutex poll_packets_mutex; /** * HookPackets() * bitcoind.hookPackets(callback) - * NOTE: THIS NEEDS A MUTEX XXXXXXXXXX */ NAN_METHOD(HookPackets) { @@ -3392,15 +3391,22 @@ NAN_METHOD(HookPackets) { poll_packets_mutex.lock(); for (cur = packets_queue_head; cur; cur = next) { - //obj->Set(i, NanNew(cur->strCommand.c_str())); - obj->Set(i, NanNew(cur->strCommand)); + Local o = NanNew(); + + o->Set(NanNew("name"), NanNew(cur->strCommand)); + o->Set(NanNew("received"), NanNew((int64_t)cur->nTimeReceived)); + + obj->Set(i, o); i++; + if (cur == packets_queue_head) { packets_queue_head = NULL; } + if (cur == packets_queue_tail) { packets_queue_tail = NULL; } + next = cur->next; // delete cur->pfrom; // cleaned up elsewhere? C++ I DON'T UNDERSTAND YOU free(cur->strCommand); @@ -3427,9 +3433,9 @@ static bool process_packets(CNode* pfrom) { bool fOk = true; - if (!pfrom->vRecvGetData.empty()) { - return process_getdata(pfrom); - } + // if (!pfrom->vRecvGetData.empty()) { + // return process_getdata(pfrom); + // } std::deque::iterator it = pfrom->vRecvMsg.begin(); while (!pfrom->fDisconnect && it != pfrom->vRecvMsg.end()) {