0

I'm trying to port my code from an obsolete library called CastleMQ to NetMQ but I'm running into some problems.

I prefer to using polling with a timeout, for reliability - I just found that it worked best for me from trial and error compared to just sitting blocking the port indefinitely.

here is my CastleMQ code


    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();
        }
    }    

here is what I tried to do and then got lost with 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?

I'm especially confused as how to add a timeout so I can poll with a timeout in a loop the way my CastleMQ code does.

Any pointers would be much appreciated, thanks

ycomp
  • 8,316
  • 19
  • 57
  • 95
  • What exactly is the timeout for? The point of the poller is to tell you when the socket has data. I haven't used the arguments from RecieveReady before, did you try using `repSocket` directly? – BradleyDotNET Apr 29 '19 at 17:53
  • I had problems on other platforms that only got resolved by using a timeout and by polling..perhaps the polling was done because that was the only way I could implement a timeout with that library in that language, I can't remember. However I found this to be the most reliable way for me. My network is not entirely stable also. I not entirely sure but it seemed to me that I wouldn't have so many headaches with implementing ZeroMQ reliably if I had no VPN connection cutting in and out. Sometimes that just seemed to mess things up. – ycomp Apr 29 '19 at 18:08
  • But perhaps with NetMQ I could just loop a rep socket with a timeout I guess? – ycomp Apr 29 '19 at 18:09
  • I'm saying just use the poller, you might be able to give the poller a timeout but you don't need anything else – BradleyDotNET Apr 29 '19 at 18:33
  • Use the trySend and tryReceive, you can specify the timeout. NetMQ has a lot of send and receive overloads, check them out. – somdoron Apr 30 '19 at 04:44
  • Also poller is for multiple sockets, you are only using one, you can use the receive or tryReceive methods. – somdoron Apr 30 '19 at 04:45
  • @somdoron thanks for the advice, this NetMQ seems simple although the polling syntax confused me as it is different than what I'm used to. but I'm happy to get rid of polling as I don't need multiple sockets, just a timeout. Possibly I don't even need that here since this library seems very well thought out, maybe it is not even necessary for reliability like I discovered it was in my several generations previous code. – ycomp Apr 30 '19 at 05:03

0 Answers0