Skip to content

Instantly share code, notes, and snippets.

@bitkevin
Last active August 29, 2015 14:10
Show Gist options
  • Save bitkevin/98787424dc26e33cadb0 to your computer and use it in GitHub Desktop.
Save bitkevin/98787424dc26e33cadb0 to your computer and use it in GitHub Desktop.
diff --git a/configure.ac b/configure.ac
index 30b9ff5..6ff9680 100644
--- a/configure.ac
+++ b/configure.ac
@@ -400,6 +400,9 @@ if test x$use_upnp != xno; then
)
fi
+ZMQ_LIBS="-lzmq"
+LIBS="$LIBS $ZMQ_LIBS"
+
dnl Check for boost libs
AX_BOOST_BASE
AX_BOOST_SYSTEM
diff --git a/src/main.cpp b/src/main.cpp
index 0b54558..7a69165 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -2128,8 +2128,16 @@ bool ActivateBestChain(CValidationState &state) {
std::string strCmd = GetArg("-blocknotify", "");
if (!IsInitialBlockDownload() && !strCmd.empty())
{
- boost::replace_all(strCmd, "%s", chainActive.Tip()->GetBlockHash().GetHex());
- boost::thread t(runCommand, strCmd); // thread runs free
+ CBlockIndex* pblockindex = chainActive.Tip();
+ CBlock block;
+ ReadBlockFromDisk(block, pblockindex);
+
+ CDataStream ssBlock(SER_NETWORK, PROTOCOL_VERSION);
+ ssBlock << block;
+
+ std::vector<uint8_t> buf(ssBlock.begin(), ssBlock.end());
+ std::string blockHash = pblockindex->GetBlockHash().GetHex();
+ boost::thread t(zmqBlockNotify, blockHash, buf, strCmd);
}
}
diff --git a/src/util.cpp b/src/util.cpp
index b8036a3..6863cc0 100644
--- a/src/util.cpp
+++ b/src/util.cpp
@@ -11,6 +11,7 @@
#include "ui_interface.h"
#include "uint256.h"
#include "version.h"
+#include "zmq.hpp"
#include <stdarg.h>
@@ -1385,6 +1386,27 @@ void runCommand(std::string strCommand)
LogPrintf("runCommand error: system(%s) returned %d\n", strCommand, nErr);
}
+void zmqBlockNotify(const std::string &blockHash, const std::vector<uint8_t> blockBin, const std::string &zmqLocalUri)
+{
+ zmq::context_t zContext(1/* io.threads */);
+ zmq::socket_t zSocket(zContext, ZMQ_PUSH);
+ zSocket.connect(zmqLocalUri.c_str());
+
+ // ZMQ_LINGER: 设置延时,socket调用close后,依然留存的时间,单位:毫秒
+ // 默认是-1:一直留存,会导致 zContext 无法关闭,并阻塞
+ int zmqLinger = 3000;
+ zSocket.setsockopt(ZMQ_LINGER, &zmqLinger/*ms*/, sizeof(zmqLinger));
+ int timeoutMS = 10000;
+ zSocket.setsockopt(ZMQ_SNDTIMEO, &timeoutMS, sizeof(timeoutMS));
+
+ zmq::message_t zmsg;
+ zmsg.rebuild(blockBin.size());
+ memcpy(zmsg.data(), blockBin.data(), blockBin.size());
+
+ if (!zSocket.send(zmsg))
+ LogPrintf("zmqBlockNotify error: block hex %s\n", blockHash.c_str());
+}
+
void RenameThread(const char* name)
{
#if defined(PR_SET_NAME)
diff --git a/src/util.h b/src/util.h
index 89c6016..265353b 100644
--- a/src/util.h
+++ b/src/util.h
@@ -202,7 +202,7 @@ std::string FormatFullVersion();
std::string FormatSubVersion(const std::string& name, int nClientVersion, const std::vector<std::string>& comments);
void AddTimeData(const CNetAddr& ip, int64_t nTime);
void runCommand(std::string strCommand);
-
+void zmqBlockNotify(const std::string &blockHash, const std::vector<uint8_t> blockBin, const std::string &zmqLocalUri);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment