本文整理汇总了C#中NetMQ.Sockets.DealerSocket.SendMultipartMessage方法的典型用法代码示例。如果您正苦于以下问题:C# DealerSocket.SendMultipartMessage方法的具体用法?C# DealerSocket.SendMultipartMessage怎么用?C# DealerSocket.SendMultipartMessage使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类NetMQ.Sockets.DealerSocket
的用法示例。
在下文中一共展示了DealerSocket.SendMultipartMessage方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: InprocRouterDealerTest
public void InprocRouterDealerTest()
{
// The main thread simply starts several clients and a server, and then
// waits for the server to finish.
var readyMsg = Encoding.UTF8.GetBytes("RDY");
var freeWorkers = new Queue<byte[]>();
using (var backendsRouter = new RouterSocket())
{
backendsRouter.Options.Identity = Guid.NewGuid().ToByteArray();
backendsRouter.Bind("inproc://backend");
backendsRouter.ReceiveReady += (o, e) =>
{
// Handle worker activity on backend
while (e.Socket.HasIn)
{
var msg = e.Socket.ReceiveMultipartMessage();
var idRouter = msg.Pop();
// forget the empty frame
if (msg.First.IsEmpty)
msg.Pop();
var id = msg.Pop();
if (msg.First.IsEmpty)
msg.Pop();
if (msg.FrameCount == 1)
{
// worker send RDY message queue his Identity to the free workers queue
if (readyMsg[0] == msg[0].Buffer[0] &&
readyMsg[1] == msg[0].Buffer[1] &&
readyMsg[2] == msg[0].Buffer[2])
{
lock (freeWorkers)
{
freeWorkers.Enqueue(id.Buffer);
}
}
}
}
};
var poller = new NetMQPoller { backendsRouter };
for (int i = 0; i < 2; i++)
{
var workerThread = new Thread(state =>
{
byte[] routerId = (byte[])state;
byte[] workerId = Guid.NewGuid().ToByteArray();
using (var workerSocket = new DealerSocket())
{
workerSocket.Options.Identity = workerId;
workerSocket.Connect("inproc://backend");
var workerReadyMsg = new NetMQMessage();
workerReadyMsg.Append(workerId);
workerReadyMsg.AppendEmptyFrame();
workerReadyMsg.Append(readyMsg);
workerSocket.SendMultipartMessage(workerReadyMsg);
Thread.Sleep(1000);
}
});
workerThread.IsBackground = true;
workerThread.Name = "worker" + i;
workerThread.Start(backendsRouter.Options.Identity);
}
poller.RunAsync();
Thread.Sleep(1000);
poller.Stop();
Assert.AreEqual(2, freeWorkers.Count);
}
}
示例2: RouterDealerMessaging
public void RouterDealerMessaging()
{
using (var server = new RouterSocket())
using (var client = new DealerSocket())
{
int port = server.BindRandomPort("tcp://127.0.0.1");
client.Connect("tcp://127.0.0.1:" + port);
var clientOutgoingMessage = new NetMQMessage();
clientOutgoingMessage.Append("Hello");
client.SendMultipartMessage(clientOutgoingMessage);
NetMQMessage serverIncomingMessage = server.ReceiveMultipartMessage();
// number of frames should be one because first message should be identity of client
Assert.AreEqual(2, serverIncomingMessage.FrameCount);
Assert.AreEqual("Hello", serverIncomingMessage[1].ConvertToString());
var serverOutgoingMessage = new NetMQMessage();
// first adding the identity
serverOutgoingMessage.Append(serverIncomingMessage[0]);
serverOutgoingMessage.Append("World");
server.SendMultipartMessage(serverOutgoingMessage);
var incomingClientMessage = client.ReceiveMultipartMessage();
Assert.AreEqual(1, incomingClientMessage.FrameCount);
Assert.AreEqual("World", incomingClientMessage[0].ConvertToString());
}
}