1

According to doc SendReady is triggered when at least one frame can be sent without blocking. Ok, when I send a frame within the callback of this event it works as documented, but when I add some delay using timer I have such effect that looks like SendReady is cached, and sent blindly not checking if at the moment of notification it is still true.

Consider such code:

private const string address = "tcp://localhost:5000";
private readonly DealerSocket socket;
private readonly NetMQPoller poller;
private readonly NetMQTimer timer;
private string lastLine;
private int lineCount;

private Program()
{
    socket = new DealerSocket();
    socket.Options.SendHighWatermark = 1;
    socket.Options.Identity = Encoding.ASCII.GetBytes(Guid.NewGuid().ToString("N"));
    socket.Connect(address);

    this.poller = new NetMQPoller();
    poller.Add(socket);

    this.timer = new NetMQTimer(500);
    this.timer.Enable = false;
    this.timer.Elapsed += (sender, args) => SendData();
    poller.Add(timer);

    poller.RunAsync();
    socket.SendReady += OnSendReady;

    WriteLine("Sleeping");
    Thread.Sleep(5000);
    poller.Dispose();
    WriteLine("DONE.");
    Console.ReadLine();
}

// proxy for console so we not get flooded
private void WriteLine(string s)
{
    if (s != lastLine)
        lineCount = 0;
    else if (++lineCount >= 2)
        return;

    lastLine = s;
    Console.WriteLine(s);
}

private void OnSendReady(object sender, NetMQSocketEventArgs e)
{
    this.timer.Enable = true;
    WriteLine("we are ready to send: " + e.IsReadyToSend);
}

private void SendData()
{
    this.timer.Enable = false;
    bool result = socket.TrySendFrame("hello");
    WriteLine("Sending "+result);
}

The output says:

we are ready to send: True
we are ready to send: True
Sending True
we are ready to send: True
Sending False

But such output should be (?) impossible -- the last line says the sending failed, however line before we have acknowledgment that sending at least one frame is possible.

So if this event is cached and should not be trusted 100%, or am I missing something?

astrowalker
  • 3,123
  • 3
  • 21
  • 40

0 Answers0