|
| 1 | +#include "cereal/messaging/bridge_zmq.h" |
| 2 | + |
| 3 | +#include <cassert> |
| 4 | +#include <cstring> |
| 5 | +#include <unistd.h> |
| 6 | + |
| 7 | +static size_t fnv1a_hash(const std::string &str) { |
| 8 | + const size_t fnv_prime = 0x100000001b3; |
| 9 | + size_t hash_value = 0xcbf29ce484222325; |
| 10 | + for (char c : str) { |
| 11 | + hash_value ^= (unsigned char)c; |
| 12 | + hash_value *= fnv_prime; |
| 13 | + } |
| 14 | + return hash_value; |
| 15 | +} |
| 16 | + |
| 17 | +// FIXME: This is a hack to get the port number from the socket name, might have collisions. |
| 18 | +static int get_port(std::string endpoint) { |
| 19 | + size_t hash_value = fnv1a_hash(endpoint); |
| 20 | + int start_port = 8023; |
| 21 | + int max_port = 65535; |
| 22 | + return start_port + (hash_value % (max_port - start_port)); |
| 23 | +} |
| 24 | + |
| 25 | +BridgeZmqContext::BridgeZmqContext() { |
| 26 | + context = zmq_ctx_new(); |
| 27 | +} |
| 28 | + |
| 29 | +BridgeZmqContext::~BridgeZmqContext() { |
| 30 | + if (context != nullptr) { |
| 31 | + zmq_ctx_term(context); |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +void BridgeZmqMessage::init(size_t sz) { |
| 36 | + size = sz; |
| 37 | + data = new char[size]; |
| 38 | +} |
| 39 | + |
| 40 | +void BridgeZmqMessage::init(char *d, size_t sz) { |
| 41 | + size = sz; |
| 42 | + data = new char[size]; |
| 43 | + memcpy(data, d, size); |
| 44 | +} |
| 45 | + |
| 46 | +void BridgeZmqMessage::close() { |
| 47 | + if (size > 0) { |
| 48 | + delete[] data; |
| 49 | + } |
| 50 | + data = nullptr; |
| 51 | + size = 0; |
| 52 | +} |
| 53 | + |
| 54 | +BridgeZmqMessage::~BridgeZmqMessage() { |
| 55 | + close(); |
| 56 | +} |
| 57 | + |
| 58 | +int BridgeZmqSubSocket::connect(BridgeZmqContext *context, std::string endpoint, std::string address, bool conflate, bool check_endpoint) { |
| 59 | + sock = zmq_socket(context->getRawContext(), ZMQ_SUB); |
| 60 | + if (sock == nullptr) { |
| 61 | + return -1; |
| 62 | + } |
| 63 | + |
| 64 | + zmq_setsockopt(sock, ZMQ_SUBSCRIBE, "", 0); |
| 65 | + |
| 66 | + if (conflate) { |
| 67 | + int arg = 1; |
| 68 | + zmq_setsockopt(sock, ZMQ_CONFLATE, &arg, sizeof(int)); |
| 69 | + } |
| 70 | + |
| 71 | + int reconnect_ivl = 500; |
| 72 | + zmq_setsockopt(sock, ZMQ_RECONNECT_IVL_MAX, &reconnect_ivl, sizeof(reconnect_ivl)); |
| 73 | + |
| 74 | + full_endpoint = "tcp://" + address + ":"; |
| 75 | + if (check_endpoint) { |
| 76 | + full_endpoint += std::to_string(get_port(endpoint)); |
| 77 | + } else { |
| 78 | + full_endpoint += endpoint; |
| 79 | + } |
| 80 | + |
| 81 | + return zmq_connect(sock, full_endpoint.c_str()); |
| 82 | +} |
| 83 | + |
| 84 | +void BridgeZmqSubSocket::setTimeout(int timeout) { |
| 85 | + zmq_setsockopt(sock, ZMQ_RCVTIMEO, &timeout, sizeof(int)); |
| 86 | +} |
| 87 | + |
| 88 | +Message *BridgeZmqSubSocket::receive(bool non_blocking) { |
| 89 | + zmq_msg_t msg; |
| 90 | + assert(zmq_msg_init(&msg) == 0); |
| 91 | + |
| 92 | + int flags = non_blocking ? ZMQ_DONTWAIT : 0; |
| 93 | + int rc = zmq_msg_recv(&msg, sock, flags); |
| 94 | + |
| 95 | + Message *ret = nullptr; |
| 96 | + if (rc >= 0) { |
| 97 | + ret = new BridgeZmqMessage; |
| 98 | + ret->init((char *)zmq_msg_data(&msg), zmq_msg_size(&msg)); |
| 99 | + } |
| 100 | + |
| 101 | + zmq_msg_close(&msg); |
| 102 | + return ret; |
| 103 | +} |
| 104 | + |
| 105 | +BridgeZmqSubSocket::~BridgeZmqSubSocket() { |
| 106 | + if (sock != nullptr) { |
| 107 | + zmq_close(sock); |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +int BridgeZmqPubSocket::connect(BridgeZmqContext *context, std::string endpoint, bool check_endpoint) { |
| 112 | + sock = zmq_socket(context->getRawContext(), ZMQ_PUB); |
| 113 | + if (sock == nullptr) { |
| 114 | + return -1; |
| 115 | + } |
| 116 | + |
| 117 | + full_endpoint = "tcp://*:"; |
| 118 | + if (check_endpoint) { |
| 119 | + full_endpoint += std::to_string(get_port(endpoint)); |
| 120 | + } else { |
| 121 | + full_endpoint += endpoint; |
| 122 | + } |
| 123 | + |
| 124 | + // ZMQ pub sockets cannot be shared between processes, so we need to ensure pid stays the same. |
| 125 | + pid = getpid(); |
| 126 | + |
| 127 | + return zmq_bind(sock, full_endpoint.c_str()); |
| 128 | +} |
| 129 | + |
| 130 | +int BridgeZmqPubSocket::sendMessage(Message *message) { |
| 131 | + assert(pid == getpid()); |
| 132 | + return zmq_send(sock, message->getData(), message->getSize(), ZMQ_DONTWAIT); |
| 133 | +} |
| 134 | + |
| 135 | +int BridgeZmqPubSocket::send(char *data, size_t size) { |
| 136 | + assert(pid == getpid()); |
| 137 | + return zmq_send(sock, data, size, ZMQ_DONTWAIT); |
| 138 | +} |
| 139 | + |
| 140 | +BridgeZmqPubSocket::~BridgeZmqPubSocket() { |
| 141 | + if (sock != nullptr) { |
| 142 | + zmq_close(sock); |
| 143 | + } |
| 144 | +} |
| 145 | + |
| 146 | +void BridgeZmqPoller::registerSocket(BridgeZmqSubSocket *socket) { |
| 147 | + assert(num_polls + 1 < (sizeof(polls) / sizeof(polls[0]))); |
| 148 | + polls[num_polls].socket = socket->getRawSocket(); |
| 149 | + polls[num_polls].events = ZMQ_POLLIN; |
| 150 | + |
| 151 | + sockets.push_back(socket); |
| 152 | + num_polls++; |
| 153 | +} |
| 154 | + |
| 155 | +std::vector<BridgeZmqSubSocket *> BridgeZmqPoller::poll(int timeout) { |
| 156 | + std::vector<BridgeZmqSubSocket *> ret; |
| 157 | + |
| 158 | + int rc = zmq_poll(polls, num_polls, timeout); |
| 159 | + if (rc < 0) { |
| 160 | + return ret; |
| 161 | + } |
| 162 | + |
| 163 | + for (size_t i = 0; i < num_polls; i++) { |
| 164 | + if (polls[i].revents) { |
| 165 | + ret.push_back(sockets[i]); |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + return ret; |
| 170 | +} |
0 commit comments