Last active
January 22, 2025 13:08
-
-
Save chengscott/fa20e379db048441d3b5129ba879fde7 to your computer and use it in GitHub Desktop.
cppzmq ROUTER DEALER & pyzmq DEALER
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
#include "client.hpp" | |
void Sock::bind(size_t port) { | |
sock = zmq::socket_t(ctx, zmq::socket_type::router); | |
sock.set(zmq::sockopt::linger, 0); | |
sock.bind("tcp://*:" + std::to_string(port)); | |
} | |
void Sock::close() { | |
sock.close(); | |
ctx.close(); | |
} | |
void Sock::send_multipart(const std::string &m0, const std::string &m1) { | |
zmq::send_multipart(sock, | |
std::initializer_list<zmq::const_buffer>{zmq::buffer(m0), zmq::buffer(m1)}); | |
} | |
#include <iostream> | |
int main() { | |
int port = 5566; | |
Sock sock; | |
sock.bind(port); | |
std::array<std::string, 3> msgs; | |
sock.recv_multipart_n<3>(msgs); | |
auto &identity = msgs[0]; | |
std::cout << msgs[1] << ' ' << msgs[2] << '\n'; | |
sock.send_multipart(identity, "pong"); | |
sock.close(); | |
} |
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
#pragma once | |
#include "zmq_addon.hpp" | |
#include <array> | |
#include <string> | |
class Sock { | |
zmq::context_t ctx; | |
zmq::socket_t sock; | |
public: | |
void bind(size_t); | |
void close(); | |
void send_multipart(const std::string &, const std::string &); | |
template <size_t N> void recv_multipart_n(std::array<std::string, N> &ret) { | |
std::array<zmq::message_t, N> msgs; | |
try { | |
(void)zmq::recv_multipart_n(sock, msgs.data(), msgs.size()); | |
for (size_t i = 0; i < N; ++i) { | |
ret[i] = msgs[i].to_string(); | |
} | |
} catch (...) { | |
throw; | |
} | |
} | |
}; |
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
import zmq | |
remote = "127.0.0.1:5566" | |
ctx = zmq.Context() | |
zsock = ctx.socket(zmq.DEALER) | |
zsock.setsockopt(zmq.LINGER, 0) | |
zsock.setsockopt(zmq.ROUTING_ID, b"0") | |
zsock.connect(f"tcp://{remote}") | |
zsock.send_multipart([b"ping", b"data"]) |
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
#include "server.hpp" | |
void Sock::connect(const std::string &remote, const std::string &id) { | |
sock = zmq::socket_t(ctx, zmq::socket_type::dealer); | |
sock.set(zmq::sockopt::linger, 0); | |
sock.set(zmq::sockopt::routing_id, id); | |
sock.connect(remote); | |
} | |
void Sock::close() { | |
sock.close(); | |
ctx.close(); | |
} | |
void Sock::send_multipart(const std::string &m0, const std::string &m1) { | |
zmq::send_multipart(sock, | |
std::initializer_list<zmq::const_buffer>{zmq::buffer(m0), zmq::buffer(m1)}); | |
} | |
void Sock::recv(std::string &ret) { | |
zmq::message_t msg; | |
try { | |
(void)sock.recv(msg); | |
ret = msg.to_string(); | |
} catch (...) { | |
throw; | |
} | |
} | |
#include <iostream> | |
int main() { | |
std::string remote{"127.0.0.1:5566"}; | |
int rank = 0; | |
Sock sock; | |
sock.connect("tcp://" + remote, std::to_string(rank)); | |
sock.send_multipart("ping", "data"); | |
{ | |
std::string msg; | |
try { | |
sock.recv(msg); | |
} catch (...) { | |
return; | |
} | |
std::cout << msg << '\n'; | |
} | |
sock.close(); | |
} |
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
#pragma once | |
#include "zmq_addon.hpp" | |
#include <string> | |
class Sock { | |
zmq::context_t ctx; | |
zmq::socket_t sock; | |
public: | |
void connect(const std::string &, const std::string &); | |
void close(); | |
void send_multipart(const std::string &, const std::string &); | |
void recv(std::string &); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment