Question

I've been working with ZMQ in Python with Pyzmq for a while and I have to port some of our code over to C# with the CLRZMQ bindings. I've been going around Google trying to find some kind of documentation, but I found next to nothing except for a few outdated code examples that don't even work and it's very frustrating.

What I am trying to achieve now is something very simple. We have a simple function that polls a socket with a timeout and when the timeout period is elapsed, it raises a custom exception. Here's what it looks like in Python:

def raise_on_timeout(sock, timeout):
    """
    sock is a ZMQ socket
    timeout is a timedelta object from datetime.
    """
    if timeout is not None:
        timeout = int(timeout.total_seconds() * 1000)
        if not sock.poll(timeout):
            raise TimeoutException("Communication timed out")

Looks easy, right? This function is called between a send_multipart and a recv_multipart so that if we get a timeout when listening to data, we can manage that.

How do you implement something similar in C# with CLRZMQ?

Also, if there is any kind of api documentation out ther efor CLRZMQ, I would appreciate it if you could point out where I can find it.

Thanks

Was it helpful?

Solution

Ok, here's the actual final answer. In this one, I don't even need an instance of the Context. It's much better that way.

Here's the code to make a similar raise_on_timeout method in C#.

public static void RaiseOnTimeout(Socket sock, TimeSpan timeout)
{
    List<PollItem> pollItemsList = new List<PollItem>();
    PollItem pollItem = sock.CreatePollItem(IOMultiPlex.POLLIN);
    pollItemsList.Add(pollItem);

    int numReplies = Context.Poller(pollItemsList.ToArray(), timeout.Value.Ticks * 10);

    if (numReplies == 0)
    {
        throw new TimeoutException();
    }
}

OTHER TIPS

Ok. I think that I may have found the solution. Though I haven't tested it yet.

To achieve the same thing as what I want to do in Python, this should do it in the same way.

private static void RaiseOnTimeout(Socket sock, long timeoutMicroSeconds)
{
    List<socket> sockList = new List<Socket>() {sock};
    int numEvents = Context.Poller(sockList), timeoutMicroSeconds);

    if (numEvents == 0)
    {
        throw new TimeoutException();
    }
}

Thoughts?

Ok, I managed to make it work.

Here's the code to make a similar raise_on_timeout method in C#.

public static void RaiseOnTimeout(Context ctx, Socket sock, TimeSpan timeout)
{
    List<PollItem> pollItemsList = new List<PollItem>();
    PollItem pollItem = sock.CreatePollItem(IOMultiPlex.POLLIN);
    pollItemsList.Add(pollItem);

    int numReplies = ctx.Poll(pollItemsList.ToArray(), timeout.Value.Ticks * 10);

    if (numReplies == 0)
    {
        throw new TimeoutException();
    }
}

That did it. Wasn't obvious though as there is very little doc for C#. I just inspired myself form the Java examples in the guide, and even there they don't really explain what does what. Just had to figure it out.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top