zeromq/clrzmq

how about the method CreatePollItem in rc1

Closed this issue · 3 comments

I had this code working in the 2.2 version but seems like the classes CreatePollItem is replaced with something else in the new rc1, do you know how to convert this in the rc1 version:

static void Main(string[] args)
        {
            using (ZmqContext context = ZmqContext.Create())   //similar to C: void *context = zmq_ctx_new ();
            {
                using (ZmqSocket frontend = context.CreateSocket(SocketType.ROUTER), backend = context.CreateSocket(SocketType.DEALER)) //similar to C: void *frontend = zmq_socket (context, ZMQ_ROUTER); void *backend  = zmq_socket (context, ZMQ_DEALER);
                {
                    frontend.Bind("tcp://*:5559");  //similar to C: zmq_bind (frontend, "tcp://*:5559");
                    backend.Bind("tcp://*:5560");  //similar to C: zmq_bind (backend,  "tcp://*:5560");

                    var pollItems = new PollItem[2]; /* this line and the next 4 lines are similar to: zmq_pollitem_t items [] = {
                                                        { frontend, 0, ZMQ_POLLIN, 0 },
                                                        { backend,  0, ZMQ_POLLIN, 0 }
                                                      */
                    pollItems[0] = frontend.CreatePollItem(IOMultiPlex.POLLIN);
                    pollItems[0].PollInHandler += (socket, revents) => FrontendPollInHandler(socket, backend);
                    pollItems[1] = backend.CreatePollItem(IOMultiPlex.POLLIN);
                    pollItems[1].PollInHandler += (socket, revents) => BackendPollInHandler(socket, frontend);

                    while (true)
                    {
                        context.Poll(pollItems, -1);
                    }
                }   //similar to C:zmq_close(frontend); & C:zmq_close (backend);
            }   //similar to C:zmq_ctx_destroy (context);
        }

jgoz commented

Try this:

static void Main(string[] args)
{
    using (ZmqContext context = ZmqContext.Create())
    {
        using (ZmqSocket frontend = context.CreateSocket(SocketType.ROUTER),
                         backend = context.CreateSocket(SocketType.DEALER))
        using (var poller = new Poller(new[] { frontend, backend }))
        {
            frontend.Bind("tcp://*:5559");
            backend.Bind("tcp://*:5560");

            frontend.ReceiveReady += (sender, args) => FrontendPollInHandler(args.Socket, backend);
            backend.ReceiveReady += (sender, args) => BackendPollInHandler(args.Socket, frontend);

            while (true)
            {
                poller.Poll();
            }
        }
    }
}

It looks like you're creating a Queue device, in which case the ZeroMQ.Devices.QueueDevice may simplify things for you.

jgoz commented

I'm closing this issue as it isn't a defect.

thanks jgoz, I agree, the QueueDevice replaces my "long" code with just couple of lines, it was hard to find an example!!

QueueDevice myQueueDevice = new QueueDevice(context, "tcp://:5559", "tcp://:5560", DeviceMode.Blocking);
myQueueDevice.Start();