我遇到了一个问题,我设法解决了......但是我有点担心,因为我并不真正理解为什么解决方案有效;
我正在使用NetMQ,特别是一个NetMQ轮询器,它有许多套接字,其中一个是REQ-REP对。
我有一个请求队列,它们被队列化为请求,服务器根据需要处理每个请求类型并发送回适当的响应。这一直没有问题,但是当我尝试添加一个额外的请求类型时,系统停止按预期工作;会发生什么是请求将到达服务器,服务器将发送响应...并且客户端将不会收到它。在服务器关闭之前,客户端不会收到该消息(异常行为!)。
我一直在管理REQ-REP对,并在发送请求之前设置了一个标志,并在收到回复后重置。我设法通过仅触发" SendReady"中的回复来解决问题。 REQ套接字的事件 - 这自动修复了我的所有问题,但是我在文档中找不到任何东西,告诉我为什么套接字可能不在" sendready"国家,或实际上做了什么。
任何可以解释为什么现在有效的信息都会很棒:)
干杯。
客户端:
"订阅"作为UI的单独线程运行
private void Subscribe(string address)
{
using (var req = new RequestSocket(address + ":5555"))
using (var sub = new SubscriberSocket(address + ":5556"))
using (var poller = new NetMQPoller { req, sub })
{
// Send program code when a request for a code update is received
sub.ReceiveReady += (s, a) =>
{
var type = sub.ReceiveFrameString();
var reply = sub.ReceiveFrameString();
switch (type)
{
case "Type1":
manager.ChangeValue(reply);
break;
case "Type2":
string[] args = reply.Split(',');
eventAggregator.PublishOnUIThread(new MyEvent(args[0], (SimObjectActionEventType)Enum.Parse(typeof(MyEventType), args[1])));
break;
}
};
req.ReceiveReady += Req_ReceiveReady;
poller.RunAsync();
sub.Connect(address + ":5556");
sub.SubscribeToAnyTopic();
sub.Options.ReceiveHighWatermark = 10;
reqQueue = new Queue<string[]>();
reqQueue.Enqueue(new string[] { "InitialiseClient", "" });
req_sending = false;
while (programRunning)
{
if (reqQueue.Count > 0 && !req_sending)
{
req_sending = true;
string[] request = reqQueue.Dequeue();
Console.WriteLine("Sending " + request[0] + " " + request[1]);
req.SendMoreFrame(request[0]).SendFrame(request[1]);
}
Thread.Sleep(1);
}
}
}
private void Req_ReceiveReady(object sender, NetMQSocketEventArgs e)
{
var req = e.Socket;
var messageType = req.ReceiveFrameString();
Console.WriteLine("Received {0}", messageType);
switch (messageType)
{
case "Reply1":
// Receive action
break;
case "Reply2":
// Receive action
break;
case "Reply3":
// Receive action
break;
}
req_sending = false;
}
服务器:
using (var rep = new ResponseSocket("@tcp://*:5555"))
using (var pub = new PublisherSocket("@tcp://*:5556"))
using (var beacon = new NetMQBeacon())
using (var poller = new NetMQPoller { rep, pub, beacon })
{
// Send program code when a request for a code update is received
rep.ReceiveReady += (s, a) =>
{
var messageType = rep.ReceiveFrameString();
var message = rep.ReceiveFrameString();
Console.WriteLine("Received {0} - Content: {1}", messageType, message);
switch (messageType)
{
case "InitialiseClient":
// Send
rep.SendMoreFrame("Reply1").SendFrame(repData);
break;
case "Req2":
// do something
rep.SendMoreFrame("Reply2").SendFrame("RequestOK");
break;
case "Req3":
args = message.Split(',');
if (args.Length == 2)
{
// Do Something
rep.SendMoreFrame("Reply3").SendFrame("RequestOK");
}
else
{
rep.SendMoreFrame("Ack").SendFrame("RequestError - incorrect argument format");
}
break;
case "Req4":
args = message.Split(',');
if (args.Length == 2)
{
requestData = //do something
rep.SendMoreFrame("Reply4").SendFrame(requestData);
}
else
{
rep.SendMoreFrame("Ack").SendFrame("RequestError - incorrect argument format");
}
break;
default:
rep.SendMoreFrame("Ack").SendFrame("Error");
break;
}
};
// setup discovery beacon with 1 second interval
beacon.Configure(5555);
beacon.Publish("server", TimeSpan.FromSeconds(1));
// start the poller
poller.RunAsync();
// run the simulation loop
while (serverRunning)
{
// todo - make this operate for efficiently
// push updated variable values to clients
foreach (string[] message in pubQueue)
{
pub.SendMoreFrame(message[0]).SendFrame(message[1]);
}
pubQueue.Clear();
Thread.Sleep(2);
}
poller.StopAsync();
}
答案 0 :(得分:2)
您正在使用来自多个线程的请求套接字,这不受支持。您正在发送主线程并在轮询线程上接收。
不是使用常规队列尝试使用NetMQQueue,而是可以将其添加到轮询器并从UI线程入队。然后发送在轮询线程和接收上发生。
您可以在此处阅读文档: http://netmq.readthedocs.io/en/latest/queue/
答案 1 :(得分:0)
我唯一能想到的是,只有在你完全收到一条消息(所有部分)之后,才能发送REP套接字。您是否有机会在未事先收到请求的情况下发送邮件?
你能分享来源吗?