Skip to content

Instantly share code, notes, and snippets.

@Kobzol
Created August 24, 2019 22:49
Show Gist options
  • Select an option

  • Save Kobzol/393a2e0606f80d60960880ff00c20fce to your computer and use it in GitHub Desktop.

Select an option

Save Kobzol/393a2e0606f80d60960880ff00c20fce to your computer and use it in GitHub Desktop.
#include <zmq.hpp>
#include <sys/epoll.h>
#include <iostream>
int main()
{
void* ctx = zmq_ctx_new();
void* socket = zmq_socket(ctx, ZMQ_PUSH);
std::cerr << zmq_connect(socket, "tcp://127.0.0.1:5555") << std::endl;
uint32_t hwm = 1;
zmq_setsockopt(socket, ZMQ_SNDHWM, &hwm, sizeof(hwm));
int fd;
size_t size = sizeof(fd);
zmq_getsockopt(socket, ZMQ_FD, &fd, &size);
std::cerr << "FD: " << fd << std::endl;
struct epoll_event event;
event.events = EPOLLIN | EPOLLOUT | EPOLLET | EPOLLHUP;
event.data.fd = fd;
int epoll = epoll_create(1);
epoll_ctl(epoll, EPOLL_CTL_ADD, fd, &event);
struct epoll_event events[10];
while (true)
{
int event_count = epoll_wait(epoll, events, 10, -1);
std::cerr << "Event count: " << event_count << ", ";
for (int i = 0; i < event_count; i++)
{
if (events[i].events & EPOLLOUT) std::cerr << "EPOLLOUT ";
if (events[i].events & EPOLLIN) std::cerr << "EPOLLIN ";
if (events[i].events & EPOLLHUP) std::cerr << "EPOLLHUP ";
}
// if you comment the zmq_getsockopt call, it should not work
int zmq_events;
zmq_getsockopt(socket, ZMQ_EVENTS, &zmq_events, &size);
if (zmq_events & ZMQ_POLLIN) std::cerr << "ZMQ_POLLIN ";
if (zmq_events & ZMQ_POLLOUT) std::cerr << "ZMQ_POLLOUT ";
std::cerr << std::endl;
auto send = 1;
while (send > 0)
{
zmq_msg_t msg;
zmq_msg_init_size(&msg, 5);
std::memcpy(zmq_msg_data(&msg), "World", 5);
send = zmq_msg_send(&msg, socket, ZMQ_DONTWAIT);
std::cerr << "Sending: " << send << std::endl;
}
}
printf("AFTER SEND\n");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment