根据文档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%信任,或者我错过了什么?