本文整理汇总了C#中NetMQ.Sockets.RouterSocket.Bind方法的典型用法代码示例。如果您正苦于以下问题:C# RouterSocket.Bind方法的具体用法?C# RouterSocket.Bind怎么用?C# RouterSocket.Bind使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类NetMQ.Sockets.RouterSocket
的用法示例。
在下文中一共展示了RouterSocket.Bind方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: SendAndReceive
public void SendAndReceive()
{
using (var front = new RouterSocket())
using (var back = new DealerSocket())
{
front.Bind("inproc://frontend");
back.Bind("inproc://backend");
var proxy = new Proxy(front, back);
Task.Factory.StartNew(proxy.Start);
using (var client = new RequestSocket())
using (var server = new ResponseSocket())
{
client.Connect("inproc://frontend");
server.Connect("inproc://backend");
client.SendFrame("hello");
Assert.AreEqual("hello", server.ReceiveFrameString());
server.SendFrame("reply");
Assert.AreEqual("reply", client.ReceiveFrameString());
}
proxy.Stop();
}
}
示例2: ControlSocketObservedMessages
public void ControlSocketObservedMessages()
{
using (var front = new RouterSocket())
using (var back = new DealerSocket())
using (var controlPush = new PushSocket())
using (var controlPull = new PullSocket())
{
front.Bind("inproc://frontend");
back.Bind("inproc://backend");
controlPush.Bind("inproc://control");
controlPull.Connect("inproc://control");
var proxy = new Proxy(front, back, controlPush);
Task.Factory.StartNew(proxy.Start);
using (var client = new RequestSocket())
using (var server = new ResponseSocket())
{
client.Connect("inproc://frontend");
server.Connect("inproc://backend");
client.SendFrame("hello");
Assert.AreEqual("hello", server.ReceiveFrameString());
server.SendFrame("reply");
Assert.AreEqual("reply", client.ReceiveFrameString());
}
Assert.IsNotNull(controlPull.ReceiveFrameBytes()); // receive identity
Assert.IsEmpty(controlPull.ReceiveFrameString()); // pull terminator
Assert.AreEqual("hello", controlPull.ReceiveFrameString());
Assert.IsNotNull(controlPull.ReceiveFrameBytes()); // receive identity
Assert.IsEmpty(controlPull.ReceiveFrameString()); // pull terminator
Assert.AreEqual("reply", controlPull.ReceiveFrameString());
proxy.Stop();
}
}
示例3: Mandatory
public void Mandatory()
{
using (var router = new RouterSocket())
{
router.Options.RouterMandatory = true;
router.Bind("tcp://127.0.0.1:5555");
using (var dealer = new DealerSocket())
{
dealer.Options.Identity = Encoding.ASCII.GetBytes("1");
dealer.Connect("tcp://127.0.0.1:5555");
dealer.SendFrame("Hello");
Assert.AreEqual("1", router.ReceiveFrameString());
Assert.AreEqual("Hello", router.ReceiveFrameString());
}
Thread.Sleep(100);
Assert.Throws<HostUnreachableException>(() => router.SendMoreFrame("1").SendFrame("Hello"));
}
}
示例4: Handover
public void Handover()
{
using (var router = new RouterSocket())
using (var dealer1 = new DealerSocket())
{
router.Options.RouterHandover = true;
router.Bind("inproc://127.0.0.1:5555");
dealer1.Options.Identity = Encoding.ASCII.GetBytes("ID");
dealer1.Connect("inproc://127.0.0.1:5555");
dealer1.SendMoreFrame("Hello").SendFrame("World");
var identity = router.ReceiveFrameString();
Assert.AreEqual("ID", identity);
using (var dealer2 = new DealerSocket())
{
dealer2.Options.Identity = Encoding.ASCII.GetBytes("ID");
dealer2.Connect("inproc://127.0.0.1:5555");
// We have new peer which should take over, however we are still reading a message
var message = router.ReceiveFrameString();
Assert.AreEqual("Hello", message);
message = router.ReceiveFrameString();
Assert.AreEqual("World", message);
dealer2.SendMoreFrame("Hello").SendFrame("World");
identity = router.ReceiveFrameString();
Assert.AreEqual("ID", identity);
message = router.ReceiveFrameString();
Assert.AreEqual("Hello", message);
message = router.ReceiveFrameString();
Assert.AreEqual("World", message);
}
}
}
示例5: 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);
}
}
示例6: TestProxySendAndReceiveWithExternalPoller
public void TestProxySendAndReceiveWithExternalPoller()
{
using (var front = new RouterSocket())
using (var back = new DealerSocket())
using (var poller = new NetMQPoller { front, back })
{
front.Bind("inproc://frontend");
back.Bind("inproc://backend");
var proxy = new Proxy(front, back, null, poller);
proxy.Start();
poller.RunAsync();
using (var client = new RequestSocket())
using (var server = new ResponseSocket())
{
client.Connect("inproc://frontend");
server.Connect("inproc://backend");
client.SendFrame("hello");
Assert.AreEqual("hello", server.ReceiveFrameString());
server.SendFrame("reply");
Assert.AreEqual("reply", client.ReceiveFrameString());
// Now stop the external poller
poller.Stop();
client.SendFrame("anyone there?");
// Should no longer receive any messages
Assert.IsFalse(server.TrySkipFrame(TimeSpan.FromMilliseconds(50)));
}
}
}
示例7: StoppingProxyDisengagesFunctionality
public void StoppingProxyDisengagesFunctionality()
{
using (var front = new RouterSocket())
using (var back = new DealerSocket())
{
front.Bind("inproc://frontend");
back.Bind("inproc://backend");
var proxy = new Proxy(front, back);
Task.Factory.StartNew(proxy.Start);
// Send a message through to ensure the proxy has started
using (var client = new RequestSocket())
using (var server = new ResponseSocket())
{
client.Connect("inproc://frontend");
server.Connect("inproc://backend");
client.SendFrame("hello");
Assert.AreEqual("hello", server.ReceiveFrameString());
server.SendFrame("reply");
Assert.AreEqual("reply", client.ReceiveFrameString());
proxy.Stop(); // blocks until stopped
using (var poller = new NetMQPoller { front, back })
{
poller.RunAsync();
client.SendFrame("anyone there?");
// Should no longer receive any messages
Assert.IsFalse(server.TrySkipFrame(TimeSpan.FromMilliseconds(50)));
}
}
}
}
示例8: StartAgainAfterStop
public void StartAgainAfterStop()
{
using (var front = new RouterSocket())
using (var back = new DealerSocket())
{
front.Bind("inproc://frontend");
back.Bind("inproc://backend");
var proxy = new Proxy(front, back);
Task.Factory.StartNew(proxy.Start);
// Send a message through to ensure the proxy has started
using (var client = new RequestSocket())
using (var server = new ResponseSocket())
{
client.Connect("inproc://frontend");
server.Connect("inproc://backend");
client.SendFrame("hello");
Assert.AreEqual("hello", server.ReceiveFrameString());
server.SendFrame("reply");
Assert.AreEqual("reply", client.ReceiveFrameString());
}
proxy.Stop(); // blocks until stopped
// Start it again
Task.Factory.StartNew(proxy.Start);
// Send a message through to ensure the proxy has started
using (var client = new RequestSocket())
using (var server = new ResponseSocket())
{
client.Connect("inproc://frontend");
server.Connect("inproc://backend");
client.SendFrame("hello");
Assert.AreEqual("hello", server.ReceiveFrameString());
server.SendFrame("reply");
Assert.AreEqual("reply", client.ReceiveFrameString());
}
proxy.Stop(); // blocks until stopped
}
}
示例9: StartAndStopStateValidation
public void StartAndStopStateValidation()
{
using (var front = new RouterSocket())
using (var back = new DealerSocket())
{
front.Bind("inproc://frontend");
back.Bind("inproc://backend");
var proxy = new Proxy(front, back);
Task.Factory.StartNew(proxy.Start);
// Send a message through to ensure the proxy has started
using (var client = new RequestSocket())
using (var server = new ResponseSocket())
{
client.Connect("inproc://frontend");
server.Connect("inproc://backend");
client.SendFrame("hello");
Assert.AreEqual("hello", server.ReceiveFrameString());
server.SendFrame("reply");
Assert.AreEqual("reply", client.ReceiveFrameString());
}
Assert.Throws<InvalidOperationException>(proxy.Start);
Assert.Throws<InvalidOperationException>(proxy.Start);
Assert.Throws<InvalidOperationException>(proxy.Start);
proxy.Stop(); // blocks until stopped
Assert.Throws<InvalidOperationException>(proxy.Stop);
}
}
示例10: WhisperTest
public void WhisperTest()
{
Action<ZreMsg> setMessage = m =>
{
m.Id = ZreMsg.MessageId.Whisper;
m.Whisper.Sequence = 123;
m.Whisper.Content = new NetMQMessage();
m.Whisper.Content.Append("Hello");
m.Whisper.Content.Append("World");
};
Action<ZreMsg> checkMessage = m =>
{
Assert.That(m.Id, Is.EqualTo(ZreMsg.MessageId.Whisper));
Assert.That(m.Whisper.Sequence, Is.EqualTo(123));
Assert.That(m.Whisper.Content.FrameCount, Is.EqualTo(2));
var str1 = m.Whisper.Content[0].ConvertToString();
Assert.That(str1, Is.EqualTo("Hello"));
var str2 = m.Whisper.Content[1].ConvertToString();
Assert.That(str2, Is.EqualTo("World"));
};
using (var client = new DealerSocket())
using (var server = new RouterSocket())
{
server.Bind("inproc://zprototestWhisper");
client.Connect("inproc://zprototestWhisper");
ZreMsg clientMessage = new ZreMsg();
ZreMsg serverMessage = new ZreMsg();
for (int i = 0; i < 2; i++)
{
// client send message to server
setMessage(clientMessage);
clientMessage.Send(client);
// server receive the message
serverMessage.Receive(server);
// check that message received ok
Assert.That(serverMessage.RoutingId, Is.Not.Null);
checkMessage(serverMessage);
// reply to client, no need to set the message, using client data
serverMessage.Send(server);
// client receive the message
clientMessage.Receive(client);
// check that message received ok
Assert.That(clientMessage.RoutingId, Is.Null);
checkMessage(clientMessage);
}
}
}
示例11: Messages_From_Router_To_Dealer_With_Subscription
public void Messages_From_Router_To_Dealer_With_Subscription()
{
Console.Write("Test sending message from publisher(router) to subscribers (dealer).\n");
int maxMessage = 5;
CountdownEvent cd = new CountdownEvent(maxMessage);
string endpoint;
using (var publisher = new RouterSocket())
using (var subscriber = new DealerSocket())
using (var poller = new NetMQPoller { subscriber })
{
publisher.Bind("tcp://127.0.0.1:0");
endpoint = publisher.Options.LastEndpoint;
subscriber.Connect(endpoint);
subscriber.ReceiveReady += (sender, e) =>
{
var strs = e.Socket.ReceiveMultipartStrings();
foreach (var str in strs)
{
Console.WriteLine("Subscribe: " + str);
}
cd.Signal();
};
byte[] clientId = Encoding.Unicode.GetBytes("ClientIdTheIsLongerThen32BytesForSureAbsolutelySure");
subscriber.Options.Identity = clientId;
const string request = "Ping";
// Work around "feature" of router/dealer: the publisher does not know the subscriber exists, until it
// sends at least one message which makes it necessary to open the connection. I believe this is a
// low-level feature of the TCP/IP transport.
subscriber.SendFrame(request); // Ping.
byte[] serverId = publisher.ReceiveFrameBytes();
//Assert.AreEqual(request, publisher.ReceiveFrameString());
for (int i = 0; i < maxMessage; i++)
{
string msg = string.Format("[message: {0}]", i);
Console.Write("Publish: {0}\n", msg);
publisher.SendMoreFrame(serverId).SendFrame(msg);
//publisher.SendMoreFrame("").SendFrame(msg);
}
poller.RunAsync();
if (cd.Wait(TimeSpan.FromSeconds(10)) == false) // Blocks until _countdown.Signal has been called.
{
Assert.Fail("Timed out, this test should complete in less than 10 seconds.");
}
}
}
示例12: PingOkTest
public void PingOkTest()
{
Action<ZreMsg> setMessage = m =>
{
m.Id = ZreMsg.MessageId.PingOk;
m.PingOk.Sequence = 123;
};
Action<ZreMsg> checkMessage = m =>
{
Assert.That(m.Id, Is.EqualTo(ZreMsg.MessageId.PingOk));
Assert.That(m.PingOk.Sequence, Is.EqualTo(123));
};
using (var client = new DealerSocket())
using (var server = new RouterSocket())
{
server.Bind("inproc://zprototestPingOk");
client.Connect("inproc://zprototestPingOk");
ZreMsg clientMessage = new ZreMsg();
ZreMsg serverMessage = new ZreMsg();
for (int i = 0; i < 2; i++)
{
// client send message to server
setMessage(clientMessage);
clientMessage.Send(client);
// server receive the message
serverMessage.Receive(server);
// check that message received ok
Assert.That(serverMessage.RoutingId, Is.Not.Null);
checkMessage(serverMessage);
// reply to client, no need to set the message, using client data
serverMessage.Send(server);
// client receive the message
clientMessage.Receive(client);
// check that message received ok
Assert.That(clientMessage.RoutingId, Is.Null);
checkMessage(clientMessage);
}
}
}
示例13: LeaveTest
public void LeaveTest()
{
Action<ZreMsg> setMessage = m =>
{
m.Id = ZreMsg.MessageId.Leave;
m.Leave.Sequence = 123;
m.Leave.Group = "Life is short but Now lasts for ever";
m.Leave.Status = 123;
};
Action<ZreMsg> checkMessage = m =>
{
Assert.That(m.Id, Is.EqualTo(ZreMsg.MessageId.Leave));
Assert.That(m.Leave.Sequence, Is.EqualTo(123));
Assert.That(m.Leave.Group, Is.EqualTo("Life is short but Now lasts for ever"));
Assert.That(m.Leave.Status, Is.EqualTo(123));
};
using (var client = new DealerSocket())
using (var server = new RouterSocket())
{
// Cannot bind address ( inproc://zprototest ) - already in use.
// But only when I run all these tests at the same time.
server.Bind("inproc://zprototestLeave");
client.Connect("inproc://zprototestLeave");
ZreMsg clientMessage = new ZreMsg();
ZreMsg serverMessage = new ZreMsg();
for (int i = 0; i < 2; i++)
{
// client send message to server
setMessage(clientMessage);
clientMessage.Send(client);
// server receive the message
serverMessage.Receive(server);
// check that message received ok
Assert.That(serverMessage.RoutingId, Is.Not.Null);
checkMessage(serverMessage);
// reply to client, no need to set the message, using client data
serverMessage.Send(server);
// client receive the message
clientMessage.Receive(client);
// check that message received ok
Assert.That(clientMessage.RoutingId, Is.Null);
checkMessage(clientMessage);
}
}
}
示例14: HelloTest
public void HelloTest()
{
Action<ZreMsg> setMessage = m =>
{
m.Id = ZreMsg.MessageId.Hello;
m.Hello.Sequence = 123;
m.Hello.Endpoint = "Life is short but Now lasts for ever";
m.Hello.Groups = new List<string>();
m.Hello.Groups.Add("Name: Brutus");
m.Hello.Groups.Add("Age: 43");
m.Hello.Status = 123;
m.Hello.Name = "Life is short but Now lasts for ever";
m.Hello.Headers = new Dictionary<string, string>();
m.Hello.Headers.Add("Name", "Brutus");
m.Hello.Headers.Add("Age", "43");
};
Action<ZreMsg> checkMessage = m =>
{
Assert.That(m.Id, Is.EqualTo(ZreMsg.MessageId.Hello));
Assert.That(m.Hello.Sequence, Is.EqualTo(123));
Assert.That(m.Hello.Endpoint, Is.EqualTo("Life is short but Now lasts for ever"));
Assert.That(m.Hello.Groups.Count, Is.EqualTo(2));
Assert.That(m.Hello.Groups[0], Is.EqualTo("Name: Brutus"));
Assert.That(m.Hello.Groups[1], Is.EqualTo("Age: 43"));
Assert.That(m.Hello.Status, Is.EqualTo(123));
Assert.That(m.Hello.Name, Is.EqualTo("Life is short but Now lasts for ever"));
Assert.That(m.Hello.Headers.Count, Is.EqualTo(2));
Assert.That(m.Hello.Headers["Name"], Is.EqualTo("Brutus"));
Assert.That(m.Hello.Headers["Age"], Is.EqualTo("43"));
};
using (var client = new DealerSocket())
using (var server = new RouterSocket())
{
server.Bind("inproc://zprototestHello");
client.Connect("inproc://zprototestHello");
ZreMsg clientMessage = new ZreMsg();
ZreMsg serverMessage = new ZreMsg();
for (int i = 0; i < 2; i++)
{
// client send message to server
setMessage(clientMessage);
clientMessage.Send(client);
// server receive the message
serverMessage.Receive(server);
// check that message received ok
Assert.That(serverMessage.RoutingId, Is.Not.Null);
checkMessage(serverMessage);
// reply to client, no need to set the message, using client data
serverMessage.Send(server);
// client receive the message
clientMessage.Receive(client);
// check that message received ok
Assert.That(clientMessage.RoutingId, Is.Null);
checkMessage(clientMessage);
}
}
}
示例15: JoinTest
public void JoinTest()
{
Action<ZreMsg> setMessage = m =>
{
m.Id = ZreMsg.MessageId.Join;
m.Join.Sequence = 123;
m.Join.Group = "Life is short but Now lasts for ever";
m.Join.Status = 123;
};
Action<ZreMsg> checkMessage = m =>
{
Assert.That(m.Id, Is.EqualTo(ZreMsg.MessageId.Join));
Assert.That(m.Join.Sequence, Is.EqualTo(123));
Assert.That(m.Join.Group, Is.EqualTo("Life is short but Now lasts for ever"));
Assert.That(m.Join.Status, Is.EqualTo(123));
};
using (var client = new DealerSocket())
using (var server = new RouterSocket())
{
server.Bind("inproc://zprototestJoin");
client.Connect("inproc://zprototestJoin");
ZreMsg clientMessage = new ZreMsg();
ZreMsg serverMessage = new ZreMsg();
for (int i = 0; i < 2; i++)
{
// client send message to server
setMessage(clientMessage);
clientMessage.Send(client);
// server receive the message
serverMessage.Receive(server);
// check that message received ok
Assert.That(serverMessage.RoutingId, Is.Not.Null);
checkMessage(serverMessage);
// reply to client, no need to set the message, using client data
serverMessage.Send(server);
// client receive the message
clientMessage.Receive(client);
// check that message received ok
Assert.That(clientMessage.RoutingId, Is.Null);
checkMessage(clientMessage);
}
}
}