Skip to content

Instantly share code, notes, and snippets.

@SysOverdrive
Created April 9, 2018 15:08
Show Gist options
  • Save SysOverdrive/33c4d4745df98c465251a0ef7badb8ca to your computer and use it in GitHub Desktop.
Save SysOverdrive/33c4d4745df98c465251a0ef7badb8ca to your computer and use it in GitHub Desktop.
int main(void)
{
//Variables
zmq::context_t *ctx;
zmq::socket_t *subSocket;
//Initialise context and connect
ctx = new zmq::context_t(1);
std::string addr = "tcp://127.0.0.1:";
std::string req_port = "5555";
std::string con_addr = addr + req_port;
zmq::socket_t s(*ctx, ZMQ_REQ);
s.connect(con_addr);
/// SUBSCRIBER SOCKET
std::string subPortAddr = addr + req_port;
subSocket = new zmq::socket_t(*ctx, ZMQ_SUB);
subSocket->connect(subPortAddr);
//Subscribe to topic
std::string filter = u8"";
subSocket->setsockopt(ZMQ_SUBSCRIBE, filter.c_str(), filter.length());
int i = 0;
while (i < 100)
{
zmq::message_t topic;
subSocket->recv(&topic);
std::string rpl = std::string(static_cast<char*>(topic.data()), topic.size());
printf("ZMQ >>>>Topic %s \n", rpl.c_str());
zmq::message_t info;
subSocket->recv(&info);
printf("RECEIVED DATA >>>>RECEIVED DATA %s \n", info.data());
msgpack::unpacked unpacked;
unpack(&unpacked, reinterpret_cast<char*>(info.data()), info.size());
msgpack::object obj = unpacked.get();
printf("OBJECT >>>>OBJECT %s \n", obj);
//TEST
MY_STRUCT mystructVar;
mystructVar.id = 1;
mystructVar.integer = 32;
mystructVar.myFloat = 12.34;
struct MY_STRUCT* strucPtr = &mystructVar;
unsigned char* charPtr = (unsigned char*)strucPtr;
int i;
printf("STRUCT MYSTRUCT : \n");
printf("structure size : %zu bytes\n", sizeof(struct MY_STRUCT));
for (i = 0; i<sizeof(struct MY_STRUCT); i++)
printf("%02x ", charPtr[i]);
printf(" \nINFO.Data : \n");
zmq::message_t* infoPtr = &info;
charPtr = (unsigned char*)infoPtr;
for (i = 0; i<info.size(); i++)
printf("%02x ", charPtr[i]);
printf("\n");
char* payload = static_cast<char*>(info.data());
std::string rplInfo = std::string(payload, info.size());
printf("ZMQ >>>>Info %s \n", rplInfo.c_str());
if (i == 76)
{
printf("ZMQ >>>>Info %s \n", rplInfo.c_str());
}
i++;
}
zmq_ctx_destroy(ctx);
getchar();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment