NetMQ轮询循环超时的Rep套接字

时间:2019-04-29 17:43:16

标签: c# zeromq netmq

我正在尝试将代码从称为CastleMQ的过时库移植到NetMQ,但是遇到了一些问题。

出于可靠性考虑,我宁愿使用带有超时的轮询-与只是无限期地阻塞端口相比,我发现从反复试验来看,它最适合我。

这是我的CastleMQ代码


    public int ZeroPort;

    private void ThreadProc()
    { 
        var ctx = new Context();
        try {
            using (var repSocket = ctx.CreateSocket(SocketType.Rep))
            {
                string bindAddress = "tcp://*:"+ZeroPort;
                repSocket.Bind(bindAddress);
                print2("*** BINDING on {0} ***", bindAddress);

                bool quit = false;
                while (!quit) {
                    try {
                        var polling = new Polling(PollingEvents.RecvReady, repSocket);
                        polling.RecvReady += (socket) =>
                        { // using socket.Recv() here is guaranted to return stuff
                            var msg = socket.Recv();
                            var msgStr = Encoding.UTF8.GetString(msg);
                            print2("[REP:{0}] {1}", bindAddress, msgStr);

                            switch (msgStr) {
                                case "positions": {
                                    StringBuilder csv = new StringBuilder();
                                    print2("csv: {0}", csv.ToString());
                                    socket.Send(csv.ToString());
                                    break;
                                }

                                default: {
                                    socket.Send("Unrecognized Command: " + msgStr);
                                    break;
                                }
                            }
                        };

                        polling.Poll(POLL_TIMEOUT_MS); // this returns once some socket event happens

                    } catch (Exception e) {
                        if (e is ThreadAbortException) {
                            quit = true;
                            print2("\n*** EXITED ***");
                        } else print2(e.ToString());
                    }
                }
            }

        } catch (Exception e) {
            print2(e.ToString());
        } finally {
            ctx.Dispose();
        }
    }    

这是我尝试做的事,然后迷上了NetMQ

    private void ThreadProc()
    {   
        try {
            string bindAddress = "@tcp://*:" + ZeroPort;
            print2("*** BINDING on {0} ***", bindAddress);
            using (var repSocket = new ResponseSocket(bindAddress))
            using (var poller = new NetMQPoller { repSocket })
            {
                //                    bool quit = false;
                //                  while (!quit)

                // these event will be raised by the Poller
                repSocket.ReceiveReady += (s, a) =>
                {
                    // receive won't block as a message is ready
                    string msg = a.Socket.ReceiveString(); // defeinition for ReceiveString() can't be found
                    // send a response
                    a.Socket.Send("Response"); // it doesn't like "Response", do I need to wrap it in some object?

我对如何添加超时感到特别困惑,因此我可以像我的CastleMQ代码那样在循环中轮询超时。

感谢任何指针,谢谢

0 个答案:

没有答案