I am trying to integrate a ZeroMQ ZMQ_SERVER socket into an an epoll event loop in C++ on Ubuntu. Unfortunately I cannot get the zeromq api to give me a file descriptor. For me he following test method produces an invalid argument error:
void test_create_zmq_socket()
{
std::string address = "tcp://127.0.0.1:5558";
void* socket = zmq_socket(zmq_ctx_new(), ZMQ_SERVER);
//bind to address
int error_code = zmq_connect(socket, address.c_str());
std::cout << "INIT CLIENT SOCKET @ " << address << " STATUS: " << error_code << std::endl;
if (error_code < 0)
{
int error = zmq_errno();
std::cout << "ZMQ ERROR " << zmq_strerror(error) << std::endl;
assert(error_code >= 0);
}
size_t temp = 4;
int file_descriptor;
int error = zmq_getsockopt(socket, ZMQ_FD, &file_descriptor, &temp);
if (error < 0)
{
int errorno_val = zmq_errno();
std::cout << "ZMQ ERROR " << zmq_strerror(errorno_val) << std::endl;
assert(error >= 0);
}
std::cout << "FD VAL " << file_descriptor << std::endl;
}
The code snippet above prints the following output:
ExamplesREPL:
core/message_handlers/ZMQTest.cpp:36: void core::message_handlers::test_create_zmq_socket(): Assertion `error >= 0' failed.
INIT CLIENT SOCKET @ tcp://127.0.0.1:5558 STATUS: 0
ZMQ ERROR Invalid argument
Process finished with exit code 134 (interrupted by signal 6: SIGABRT)
I can get the socket to send and receive messages so the socket isn't completely non-functioning.
I am using ZMQ Version 4.2
After some reading I found out that you cant retrieve a file descriptor for any of the thread safe sockets, you must instead use the new zeromq poller. This poller also accepts file descriptors from other sources thus allowing them to be integrated into the event loop. There is an example in the tests folder of the zeromq source code which demonstrates this functionality.