本文整理汇总了C#中NetMQPoller类的典型用法代码示例。如果您正苦于以下问题:C# NetMQPoller类的具体用法?C# NetMQPoller怎么用?C# NetMQPoller使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
NetMQPoller类属于命名空间,在下文中一共展示了NetMQPoller类的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: NetMQPoller
void IShimHandler.Run(PairSocket shim)
{
Poller = new NetMQPoller();
Shim = shim;
Shim.ReceiveReady += OnShimReady;
Poller.Add(Shim);
Initialize();
Shim.SignalOK();
Poller.Run();
Cleanup();
}
示例4: TwoMessagesFromRouterToDealer
public void TwoMessagesFromRouterToDealer()
{
using (var server = new RouterSocket())
using (var client = new DealerSocket())
using (var poller = new NetMQPoller { client })
{
var port = server.BindRandomPort("tcp://*");
client.Connect("tcp://127.0.0.1:" + port);
var cnt = 0;
client.ReceiveReady += (sender, e) =>
{
var strs = e.Socket.ReceiveMultipartStrings();
foreach (var str in strs)
{
Console.WriteLine(str);
}
cnt++;
if (cnt == 2)
{
poller.Stop();
}
};
byte[] clientId = Encoding.Unicode.GetBytes("ClientId");
client.Options.Identity = clientId;
const string request = "GET /\r\n";
const string response = "HTTP/1.0 200 OK\r\n" +
"Content-Type: text/plain\r\n" +
"\r\n" +
"Hello, World!";
client.SendFrame(request);
byte[] serverId = server.ReceiveFrameBytes();
Assert.AreEqual(request, server.ReceiveFrameString());
// two messages in a row, not frames
server.SendMoreFrame(serverId).SendFrame(response);
server.SendMoreFrame(serverId).SendFrame(response);
poller.Run();
}
}
示例5: 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));
}
}
示例6: 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();
}
}
示例7: 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);
}
示例8: 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);
}
}
示例9: ContinueWith
public void ContinueWith()
{
int threadId1 = 0;
int threadId2 = 1;
int runCount1 = 0;
int runCount2 = 0;
using (var poller = new NetMQPoller())
{
poller.RunAsync();
var task = new Task(() =>
{
threadId1 = Thread.CurrentThread.ManagedThreadId;
runCount1++;
});
var task2 = task.ContinueWith(t =>
{
threadId2 = Thread.CurrentThread.ManagedThreadId;
runCount2++;
}, poller);
task.Start(poller);
task.Wait();
task2.Wait();
Assert.AreEqual(threadId1, threadId2);
Assert.AreEqual(1, runCount1);
Assert.AreEqual(1, runCount2);
}
}
示例10: 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();
}
}
示例11: SetsCurrentTaskScheduler
public void SetsCurrentTaskScheduler()
{
using (var poller = new NetMQPoller())
{
poller.RunAsync();
var task = new Task(() => Assert.AreSame(TaskScheduler.Current, poller));
task.Start(poller);
task.Wait();
}
}
示例12: OneTask
public void OneTask()
{
bool triggered = false;
using (var poller = new NetMQPoller())
{
poller.RunAsync();
var task = new Task(() =>
{
triggered = true;
Assert.IsTrue(poller.CanExecuteTaskInline, "Should be on NetMQPoller thread");
});
task.Start(poller);
task.Wait();
Assert.IsTrue(triggered);
}
}
示例13: 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();
}
}
}
示例14: Polling
public void Polling()
{
using (var speaker = new NetMQBeacon())
using (var listener = new NetMQBeacon())
{
speaker.Configure(9999);
speaker.Publish("Hello", s_publishInterval);
var manualResetEvent = new ManualResetEvent(false);
listener.Configure(9999);
listener.Subscribe("H");
string message = "";
listener.ReceiveReady += (sender, args) =>
{
message = listener.Receive().String;
manualResetEvent.Set();
};
using (var poller = new NetMQPoller { listener })
{
poller.RunAsync();
manualResetEvent.WaitOne();
Assert.AreEqual("Hello", message);
}
}
}
示例15: ISynchronizeInvokeWorks
public void ISynchronizeInvokeWorks()
{
using (var poller = new NetMQPoller())
using (var signal = new ManualResetEvent(false))
using (var timer = new System.Timers.Timer { AutoReset = false, Interval = 100, SynchronizingObject = poller, Enabled = true })
{
var isCorrectThread = false;
poller.RunAsync();
timer.Elapsed += (sender, args) =>
{
isCorrectThread = poller.CanExecuteTaskInline;
Assert.True(signal.Set());
};
Assert.True(signal.WaitOne(TimeSpan.FromSeconds(2)));
Assert.True(isCorrectThread);
}
}