-
Notifications
You must be signed in to change notification settings - Fork 0
/
zmq_router.cpp
53 lines (49 loc) · 1.35 KB
/
zmq_router.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <zmq.hpp>
#include <iostream>
#include <cstring>
int main(int argc, char *argv[])
{
if(argc >= 3)
{
char mode = argv[1][0];
char *addr = argv[2];
zmq::context_t ctx(1);
zmq::socket_t sck(ctx, ZMQ_ROUTER);
if(mode == 'b')
{
sck.setsockopt(ZMQ_IDENTITY, addr, strlen(addr));
sck.bind(addr);
zmq::message_t msg;
while(1){
while(sck.recv(&msg, ZMQ_NOBLOCK)) {
std::cout << std::string((const char*)msg.data()) << ": ";
sck.recv(&msg);
sck.recv(&msg);
std::cout << std::string((const char*)msg.data()) << std::endl;
}
}
}
else if(mode == 'c')
{
sck.setsockopt(ZMQ_IDENTITY, "cli", strlen("cli")+1);
sck.connect(addr);
zmq::message_t msg;
while(1) {
msg.rebuild(strlen(addr));
memcpy(msg.data(), addr, strlen(addr));
sck.send(msg, ZMQ_SNDMORE);
msg.rebuild();
sck.send(msg, ZMQ_SNDMORE);
std::string message = "msg! msg!";
msg.rebuild(message.size());
memcpy(msg.data(), message.c_str(), message.size());
sck.send(msg);
}
}
}
else
{
std::cout << "usage: rtr [b|c] [addr]" << std::endl;
}
return 0;
}