1

根据 doc SendReady,当至少一帧可以无阻塞地发送时触发。好的,当我在此事件的回调中发送一个帧时,它按记录工作,但是当我使用计时器添加一些延迟时,我有这样的效果,看起来像是SendReady被缓存了,并且盲目发送而不检查在通知的那一刻它是否仍然真的。

考虑这样的代码:

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

输出说:

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

但是这样的输出应该是(?)不可能的——最后一行说发送失败,但是在我们确认发送至少一帧是可能之前的那一行。

因此,如果此事件被缓存并且不应该 100% 被信任,或者我错过了什么?

4

0 回答 0