Last active
January 16, 2017 18:17
-
-
Save andr1972/dc2b43bcdf0878fc44930e3e280bdb54 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
using: http://zeromq.org/ | |
see https://github.com/bitcoin/bitcoin/blob/master/contrib/zmq/zmq_sub.py | |
first call: bitcoind -zmqpubhashtx=tcp://127.0.0.1:28332 -zmqpubrawtx=tcp://127.0.0.1:28332 & | |
*/ | |
#include <stdexcept> | |
#include <iostream> | |
#include <cstdlib> | |
#include <cstring> | |
#include <vector> | |
#include <assert.h> | |
#include <zmq.h> | |
char digitToHex(int n) | |
{ | |
static char hexStr[] = "0123456789abcdef"; | |
return hexStr[n]; | |
} | |
std::string hashToStringLE(uint8_t *bytes, size_t len) | |
{ //sprintf is too slow | |
if (len < 500) | |
{ | |
char buf[1000]; | |
for (int i = 0; i < len; i++) | |
{ | |
buf[2 * i] = digitToHex(bytes[i] / 16); | |
buf[2 * i + 1] = digitToHex(bytes[i] % 16); | |
} | |
buf[2 * len] = 0; | |
return buf; | |
} | |
else | |
{ | |
char *buf = new char[2 * len + 1]; | |
for (int i = 0; i < len; i++) | |
{ | |
buf[2 * i] = digitToHex(bytes[i] / 16); | |
buf[2 * i + 1] = digitToHex(bytes[i] % 16); | |
} | |
buf[2 * len] = 0; | |
std::string result = buf; | |
delete buf; | |
return result; | |
} | |
} | |
int main(int argc, char **argv) { | |
void *context = zmq_ctx_new(); | |
void *socket = zmq_socket(context, ZMQ_SUB); | |
zmq_setsockopt(socket, ZMQ_SUBSCRIBE, "hashblock", 9); | |
zmq_setsockopt(socket, ZMQ_SUBSCRIBE, "hashtx", 6); | |
zmq_setsockopt(socket, ZMQ_SUBSCRIBE, "rawblock", 8); | |
zmq_setsockopt(socket, ZMQ_SUBSCRIBE, "rawtx", 5); | |
zmq_connect(socket, "tcp://127.0.0.1:28332"); | |
//http://api.zeromq.org/3-3:zmq-msg-recv | |
int cnt = 0; | |
while(cnt<20) | |
{ | |
int64_t more; | |
size_t more_size = sizeof more; | |
do { | |
/* Create an empty ØMQ message to hold the message part */ | |
zmq_msg_t part; | |
int rc = zmq_msg_init (&part); | |
assert (rc == 0); | |
/* Block until a message is available to be received from socket */ | |
rc = zmq_msg_recv (&part, socket, 0); | |
assert (rc != -1); | |
char *data = (char*)zmq_msg_data (&part); | |
size_t msg_size = zmq_msg_size (&part); | |
printf("part size: %d\n", msg_size); | |
printf("part: %s\n", data); | |
std::string hexPart = hashToStringLE((uint8_t*)(data), msg_size); | |
printf("hexpart: %s\n", hexPart.c_str()); | |
/* Determine if more message parts are to follow */ | |
rc = zmq_getsockopt (socket, ZMQ_RCVMORE, &more, &more_size); | |
assert (rc == 0); | |
zmq_msg_close (&part); | |
printf("\n"); | |
} while (more); | |
printf("end\n"); | |
cnt++; | |
} | |
printf("end\n"); | |
zmq_ctx_shutdown(context); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment