本文整理汇总了C#中NetMQPoller.RunAsync方法的典型用法代码示例。如果您正苦于以下问题:C# NetMQPoller.RunAsync方法的具体用法?C# NetMQPoller.RunAsync怎么用?C# NetMQPoller.RunAsync使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类NetMQPoller
的用法示例。
在下文中一共展示了NetMQPoller.RunAsync方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Zyre
/// <summary>
/// Create a Zyre API that communicates with a node on the ZRE bus.
/// </summary>
/// <param name="name">The name of the node</param>
/// <param name="useEvents">Set this to true to disable Receive() and instead subscribe to events for getting messages from peers. Default is true.</param>
/// <param name="loggerDelegate">An action to take for logging when _verbose is true. Default is null.</param>
public Zyre (string name, bool useEvents = true, Action<string> loggerDelegate = null)
{
_useEvents = useEvents;
// Create front-to-back pipe pair for data traffic
// outbox is passed to ZyreNode for sending Zyre message traffic back to _inbox
PairSocket outbox;
PairSocket.CreateSocketPair(out outbox, out _inbox);
// Start node engine and wait for it to be ready
// All node control is done through _actor
_actor = ZyreNode.Create(outbox, loggerDelegate);
if (useEvents)
{
_inboxPoller = new NetMQPoller();
_inbox.ReceiveReady += InboxReceiveReady;
_inboxPoller.RunAsync();
}
// Send name, if any, to node ending
if (!string.IsNullOrEmpty(name))
{
_actor.SendMoreFrame("SET NAME").SendFrame(name);
}
}
示例2: ResponsePoll
public void ResponsePoll()
{
using (var rep = new ResponseSocket())
using (var req = new RequestSocket())
using (var poller = new NetMQPoller { rep })
{
int port = rep.BindRandomPort("tcp://127.0.0.1");
req.Connect("tcp://127.0.0.1:" + port);
rep.ReceiveReady += (s, e) =>
{
bool more;
Assert.AreEqual("Hello", e.Socket.ReceiveFrameString(out more));
Assert.False(more);
e.Socket.SendFrame("World");
};
poller.RunAsync();
req.SendFrame("Hello");
bool more2;
Assert.AreEqual("World", req.ReceiveFrameString(out more2));
Assert.IsFalse(more2);
poller.Stop();
}
}
示例3: WithPoller
public void WithPoller()
{
using (var queue = new NetMQQueue<int>())
using (var poller = new NetMQPoller { queue })
{
var manualResetEvent = new ManualResetEvent(false);
queue.ReceiveReady += (sender, args) =>
{
Assert.AreEqual(1, queue.Dequeue());
manualResetEvent.Set();
};
poller.RunAsync();
Assert.IsFalse(manualResetEvent.WaitOne(100));
queue.Enqueue(1);
Assert.IsTrue(manualResetEvent.WaitOne(100));
}
}
示例4: Monitoring
public void Monitoring()
{
var listeningEvent = new ManualResetEvent(false);
var acceptedEvent = new ManualResetEvent(false);
var connectedEvent = new ManualResetEvent(false);
using (var rep = new ResponseSocket())
using (var req = new RequestSocket())
using (var poller = new NetMQPoller())
using (var repMonitor = new NetMQMonitor(rep, "inproc://rep.inproc", SocketEvents.Accepted | SocketEvents.Listening))
using (var reqMonitor = new NetMQMonitor(req, "inproc://req.inproc", SocketEvents.Connected))
{
repMonitor.Accepted += (s, e) => acceptedEvent.Set();
repMonitor.Listening += (s, e) => listeningEvent.Set();
repMonitor.AttachToPoller(poller);
int port = rep.BindRandomPort("tcp://127.0.0.1");
reqMonitor.Connected += (s, e) => connectedEvent.Set();
reqMonitor.AttachToPoller(poller);
poller.RunAsync();
req.Connect("tcp://127.0.0.1:" + port);
req.SendFrame("a");
rep.SkipFrame();
rep.SendFrame("b");
req.SkipFrame();
Assert.IsTrue(listeningEvent.WaitOne(300));
Assert.IsTrue(connectedEvent.WaitOne(300));
Assert.IsTrue(acceptedEvent.WaitOne(300));
poller.Stop();
}
}
示例5: CycleCreateTerminate
public void CycleCreateTerminate()
{
NetMQConfig.ContextCreate(true);
var isTerminated = VerifyTermination();
Assert.AreEqual(false, isTerminated);
// We use the Poller Test code.
using (var rep = new ResponseSocket())
using (var req = new RequestSocket())
using (var poller = new NetMQPoller { rep })
{
var port = rep.BindRandomPort("tcp://127.0.0.1");
req.Connect("tcp://127.0.0.1:" + port);
rep.ReceiveReady += (s, e) =>
{
bool more;
Assert.AreEqual("Hello", e.Socket.ReceiveFrameString(out more));
Assert.False(more);
e.Socket.SendFrame("World");
};
poller.RunAsync();
req.SendFrame("Hello");
bool more2;
Assert.AreEqual("World", req.ReceiveFrameString(out more2));
Assert.IsFalse(more2);
poller.Stop();
}
NetMQConfig.ContextTerminate();
isTerminated = VerifyTermination();
Assert.AreEqual(true, isTerminated);
}
示例6: TestPollerDispose
public void TestPollerDispose()
{
const int timerIntervalMillis = 10;
var timer = new NetMQTimer(TimeSpan.FromMilliseconds(timerIntervalMillis));
var signal = new ManualResetEvent(false);
var count = 0;
timer.Elapsed += (s, a) =>
{
if (count++ == 5)
signal.Set();
};
NetMQPoller poller;
using (poller = new NetMQPoller { timer })
{
poller.RunAsync();
Assert.IsTrue(signal.WaitOne(500));
Assert.IsTrue(poller.IsRunning);
Assert.Throws<InvalidOperationException>(() => poller.Run());
}
Assert.IsFalse(poller.IsRunning);
Assert.Throws<ObjectDisposedException>(() => poller.Run());
Assert.Throws<ObjectDisposedException>(() => poller.Stop());
Assert.Throws<ObjectDisposedException>(() => poller.Add(timer));
Assert.Throws<ObjectDisposedException>(() => poller.Remove(timer));
}
示例7: NativeSocket
public void NativeSocket()
{
using (var streamServer = new StreamSocket())
using (var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
{
int port = streamServer.BindRandomPort("tcp://*");
socket.Connect("127.0.0.1", port);
var buffer = new byte[] { 1 };
socket.Send(buffer);
byte[] identity = streamServer.ReceiveFrameBytes();
byte[] message = streamServer.ReceiveFrameBytes();
Assert.AreEqual(buffer[0], message[0]);
var socketSignal = new ManualResetEvent(false);
using (var poller = new NetMQPoller())
{
poller.Add(socket, s =>
{
socket.Receive(buffer);
socketSignal.Set();
// removing the socket
poller.Remove(socket);
});
poller.RunAsync();
// no message is waiting for the socket so it should fail
Assert.IsFalse(socketSignal.WaitOne(100));
// sending a message back to the socket
streamServer.SendMoreFrame(identity).SendFrame("a");
Assert.IsTrue(socketSignal.WaitOne(100));
socketSignal.Reset();
// sending a message back to the socket
streamServer.SendMoreFrame(identity).SendFrame("a");
// we remove the native socket so it should fail
Assert.IsFalse(socketSignal.WaitOne(100));
poller.Stop();
}
}
}
示例8: 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)));
}
}
}
}
示例9: ChangeTimerInterval
public void ChangeTimerInterval()
{
int count = 0;
const int timerIntervalMillis = 10;
var timer = new NetMQTimer(TimeSpan.FromMilliseconds(timerIntervalMillis));
var stopwatch = new Stopwatch();
long length1 = 0;
long length2 = 0;
timer.Elapsed += (s, a) =>
{
count++;
if (count == 1)
{
stopwatch.Start();
}
else if (count == 2)
{
length1 = stopwatch.ElapsedMilliseconds;
timer.Interval = 20;
stopwatch.Restart();
}
else if (count == 3)
{
length2 = stopwatch.ElapsedMilliseconds;
stopwatch.Stop();
timer.Enable = false;
}
};
using (var poller = new NetMQPoller { timer })
{
poller.RunAsync();
Thread.Sleep(timerIntervalMillis * 6);
poller.Stop();
}
Assert.AreEqual(3, count);
Assert.AreEqual(10.0, length1, 2.0);
Assert.AreEqual(20.0, length2, 2.0);
}
示例10: TwoThreads
public void TwoThreads()
{
int count1 = 0;
int count2 = 0;
var allTasks = new ConcurrentBag<Task>();
using (var poller = new NetMQPoller())
{
poller.RunAsync();
Task t1 = Task.Factory.StartNew(() =>
{
for (int i = 0; i < 100; i++)
{
var task = new Task(() => { count1++; });
allTasks.Add(task);
task.Start(poller);
}
});
Task t2 = Task.Factory.StartNew(() =>
{
for (int i = 0; i < 100; i++)
{
var task = new Task(() => { count2++; });
allTasks.Add(task);
task.Start(poller);
}
});
t1.Wait(1000);
t2.Wait(1000);
Task.WaitAll(allTasks.ToArray(), 1000);
Assert.AreEqual(100, count1);
Assert.AreEqual(100, count2);
}
}
示例11: AddSocketDuringWork
public void AddSocketDuringWork()
{
using (var router1 = new RouterSocket())
using (var router2 = new RouterSocket())
using (var dealer1 = new DealerSocket())
using (var dealer2 = new DealerSocket())
using (var poller = new NetMQPoller { router1 })
{
int port1 = router1.BindRandomPort("tcp://127.0.0.1");
int port2 = router2.BindRandomPort("tcp://127.0.0.1");
dealer1.Connect("tcp://127.0.0.1:" + port1);
dealer2.Connect("tcp://127.0.0.1:" + port2);
bool router1Arrived = false;
bool router2Arrived = false;
var signal1 = new ManualResetEvent(false);
var signal2 = new ManualResetEvent(false);
router1.ReceiveReady += (s, e) =>
{
router1.SkipFrame();
router1.SkipFrame();
router1Arrived = true;
poller.Add(router2);
signal1.Set();
};
router2.ReceiveReady += (s, e) =>
{
router2.SkipFrame();
router2.SkipFrame();
router2Arrived = true;
signal2.Set();
};
poller.RunAsync();
dealer1.SendFrame("1");
Assert.IsTrue(signal1.WaitOne(300));
dealer2.SendFrame("2");
Assert.IsTrue(signal2.WaitOne(300));
poller.Stop();
Assert.IsTrue(router1Arrived);
Assert.IsTrue(router2Arrived);
}
}
示例12: RemoveTimer
public void RemoveTimer()
{
using (var router = new RouterSocket())
using (var dealer = new DealerSocket())
using (var poller = new NetMQPoller { router })
{
int port = router.BindRandomPort("tcp://127.0.0.1");
dealer.Connect("tcp://127.0.0.1:" + port);
bool timerTriggered = false;
var timer = new NetMQTimer(TimeSpan.FromMilliseconds(100));
timer.Elapsed += (s, a) => { timerTriggered = true; };
// The timer will fire after 100ms
poller.Add(timer);
bool messageArrived = false;
router.ReceiveReady += (s, e) =>
{
router.SkipFrame();
router.SkipFrame();
messageArrived = true;
// Remove timer
poller.Remove(timer);
};
poller.RunAsync();
Thread.Sleep(20);
dealer.SendFrame("hello");
Thread.Sleep(300);
poller.Stop();
Assert.IsTrue(messageArrived);
Assert.IsFalse(timerTriggered);
}
}
示例13: CanExecuteTaskInline
public void CanExecuteTaskInline()
{
using (var poller = new NetMQPoller())
{
Assert.IsFalse(poller.CanExecuteTaskInline);
poller.RunAsync();
Assert.IsFalse(poller.CanExecuteTaskInline);
var task = new Task(() => Assert.IsTrue(poller.CanExecuteTaskInline));
task.Start(poller);
task.Wait();
}
}
示例14: 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.");
}
}
}
示例15: SimpleTimer
public void SimpleTimer()
{
// TODO it is not really clear what this test is actually testing -- maybe split it into a few smaller tests
using (var router = new RouterSocket())
using (var dealer = new DealerSocket())
using (var poller = new NetMQPoller { router })
{
int port = router.BindRandomPort("tcp://127.0.0.1");
dealer.Connect("tcp://127.0.0.1:" + port);
bool messageArrived = false;
router.ReceiveReady += (s, e) =>
{
Assert.IsFalse(messageArrived);
router.SkipFrame();
router.SkipFrame();
messageArrived = true;
};
bool timerTriggered = false;
int count = 0;
const int timerIntervalMillis = 100;
var timer = new NetMQTimer(TimeSpan.FromMilliseconds(timerIntervalMillis));
timer.Elapsed += (s, a) =>
{
// the timer should jump before the message
Assert.IsFalse(messageArrived);
timerTriggered = true;
timer.Enable = false;
count++;
};
poller.Add(timer);
poller.RunAsync();
Thread.Sleep(150);
dealer.SendFrame("hello");
Thread.Sleep(300);
poller.Stop();
Assert.IsTrue(messageArrived);
Assert.IsTrue(timerTriggered);
Assert.AreEqual(1, count);
}
}