本文整理汇总了C#中NetMQPoller.Add方法的典型用法代码示例。如果您正苦于以下问题:C# NetMQPoller.Add方法的具体用法?C# NetMQPoller.Add怎么用?C# NetMQPoller.Add使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类NetMQPoller
的用法示例。
在下文中一共展示了NetMQPoller.Add方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: NetMQPoller
void IShimHandler.Run(PairSocket shim)
{
Poller = new NetMQPoller();
Shim = shim;
Shim.ReceiveReady += OnShimReady;
Poller.Add(Shim);
Initialize();
Shim.SignalOK();
Poller.Run();
Cleanup();
}
示例2: Router_Dealer_Demonstrating_Messages_From_Subscribers_To_Publisher
public void Router_Dealer_Demonstrating_Messages_From_Subscribers_To_Publisher()
{
// NOTES
// 1. Use ThreadLocal<DealerSocket> where each thread has
// its own client DealerSocket to talk to server
// 2. Each thread can send using it own socket
// 3. Each thread socket is added to poller
const int delay = 500; // millis
var clientSocketPerThread = new ThreadLocal<DealerSocket>();
using (var server = new RouterSocket("@tcp://127.0.0.1:5556"))
{
using (var poller = new NetMQPoller())
{
// Start some threads, each with its own DealerSocket
// to talk to the server socket. Creates lots of sockets,
// but no nasty race conditions no shared state, each
// thread has its own socket, happy days.
for (int i = 0; i < 4; i++)
{
Task.Factory.StartNew(state =>
{
DealerSocket client = null;
if (!clientSocketPerThread.IsValueCreated)
{
client = new DealerSocket();
client.Options.Identity =
Encoding.Unicode.GetBytes(state.ToString());
client.Connect("tcp://127.0.0.1:5556");
client.ReceiveReady += Client_ReceiveReady;
clientSocketPerThread.Value = client;
poller.Add(client);
}
else
{
client = clientSocketPerThread.Value;
}
while (true)
{
NetMQMessage messageToServer = new NetMQMessage();
messageToServer.AppendEmptyFrame();
messageToServer.Append(state.ToString());
Console.WriteLine("======================================");
Console.WriteLine(" OUTGOING MESSAGE TO SERVER ");
Console.WriteLine("======================================");
PrintFrames("Client Sending", messageToServer);
client.SendMultipartMessage(messageToServer);
Thread.Sleep(delay);
}
},
string.Format("client {0}", i),
TaskCreationOptions.LongRunning);
}
// start the poller
poller.RunAsync();
// server loop
for(int i=0;i<6;i++)
{
NetMQMessage clientMessage = server.ReceiveMessage();
Console.WriteLine("======================================");
Console.WriteLine(" INCOMING CLIENT MESSAGE FROM CLIENT ");
Console.WriteLine("======================================");
PrintFrames("Server receiving", clientMessage);
if (clientMessage.FrameCount == 3)
{
var clientAddress = clientMessage[0];
var clientOriginalMessage = clientMessage[2].ConvertToString();
string response = string.Format("{0} back from server {1}",
clientOriginalMessage,
DateTime.Now.ToLongTimeString());
var messageToClient = new NetMQMessage();
messageToClient.Append(clientAddress);
messageToClient.AppendEmptyFrame();
messageToClient.Append(response);
server.SendMultipartMessage(messageToClient);
}
}
}
}
}
示例3: 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);
}
}
示例4: 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();
}
}
}
示例5: 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));
}
示例6: 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);
}
}
示例7: 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);
}
}
示例8: AddTwoSocketAfterRemoving
public void AddTwoSocketAfterRemoving()
{
using (var router1 = new RouterSocket())
using (var router2 = new RouterSocket())
using (var router3 = new RouterSocket())
using (var router4 = new RouterSocket())
using (var dealer1 = new DealerSocket())
using (var dealer2 = new DealerSocket())
using (var dealer3 = new DealerSocket())
using (var dealer4 = new DealerSocket())
using (var poller = new NetMQPoller { router1, router2 })
{
int port1 = router1.BindRandomPort("tcp://127.0.0.1");
int port2 = router2.BindRandomPort("tcp://127.0.0.1");
int port3 = router3.BindRandomPort("tcp://127.0.0.1");
int port4 = router4.BindRandomPort("tcp://127.0.0.1");
dealer1.Connect("tcp://127.0.0.1:" + port1);
dealer2.Connect("tcp://127.0.0.1:" + port2);
dealer3.Connect("tcp://127.0.0.1:" + port3);
dealer4.Connect("tcp://127.0.0.1:" + port4);
int router1Arrived = 0;
int router2Arrived = 0;
bool router3Arrived = false;
bool router4Arrived = false;
var signal1 = new ManualResetEvent(false);
var signal2 = new ManualResetEvent(false);
var signal3 = new ManualResetEvent(false);
var signal4 = new ManualResetEvent(false);
router1.ReceiveReady += (s, e) =>
{
router1Arrived++;
router1.SkipFrame(); // identity
router1.SkipFrame(); // message
poller.Remove(router1);
signal1.Set();
};
router2.ReceiveReady += (s, e) =>
{
router2Arrived++;
router2.SkipFrame(); // identity
router2.SkipFrame(); // message
if (router2Arrived == 1)
{
poller.Add(router3);
poller.Add(router4);
signal2.Set();
}
};
router3.ReceiveReady += (s, e) =>
{
router3.SkipFrame(); // identity
router3.SkipFrame(); // message
router3Arrived = true;
signal3.Set();
};
router4.ReceiveReady += (s, e) =>
{
router4.SkipFrame(); // identity
router4.SkipFrame(); // message
router4Arrived = true;
signal4.Set();
};
poller.RunAsync();
dealer1.SendFrame("1");
Assert.IsTrue(signal1.WaitOne(300));
dealer2.SendFrame("2");
Assert.IsTrue(signal2.WaitOne(300));
dealer3.SendFrame("3");
dealer4.SendFrame("4");
dealer2.SendFrame("2");
dealer1.SendFrame("1");
Assert.IsTrue(signal3.WaitOne(300));
Assert.IsTrue(signal4.WaitOne(300));
poller.Stop();
router1.SkipFrame();
bool more;
Assert.AreEqual("1", router1.ReceiveFrameString(out more));
Assert.IsFalse(more);
Assert.AreEqual(1, router1Arrived);
Assert.AreEqual(2, router2Arrived);
Assert.IsTrue(router3Arrived);
Assert.IsTrue(router4Arrived);
}
}
示例9: Router_Dealer_Demonstrating_Messages_From_Publisher_To_Subscribers
public void Router_Dealer_Demonstrating_Messages_From_Publisher_To_Subscribers()
{
// NOTES
// 1. Use ThreadLocal<DealerSocket> where each thread has
// its own client DealerSocket to talk to server
// 2. Each thread can send using it own socket
// 3. Each thread socket is added to poller
const int delay = 500; // millis
var clientSocketPerThread = new ThreadLocal<DealerSocket>();
string endpoint;
using (var server = new RouterSocket("@tcp://127.0.0.1:0")) // If we specify 0, it will choose a random port for us.
{
endpoint = server.Options.LastEndpoint; // Lets us know which port was chosen.
Console.Write("Last endpoint, including port: {0}\n", server.Options.LastEndpoint);
using (var poller = new NetMQPoller())
{
// Start some threads, each with its own DealerSocket
// to talk to the server socket. Creates lots of sockets,
// but no nasty race conditions no shared state, each
// thread has its own socket, happy days.
for (int i = 0; i < 4; i++)
{
Task.Factory.StartNew(state =>
{
DealerSocket client = null;
if (!clientSocketPerThread.IsValueCreated)
{
client = new DealerSocket();
client.Options.Identity =
Encoding.Unicode.GetBytes(state.ToString());
client.Connect(endpoint);
//client.ReceiveReady += Client_ReceiveReady;
clientSocketPerThread.Value = client;
poller.Add(client);
}
else
{
client = clientSocketPerThread.Value;
}
Thread.Sleep(3000); // Wait until server is up.
client.SendFrame("Ping");
while (true)
{
Console.Write("Client {0}: Waiting for ping...\n", i);
// 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.
var clientMessage = client.ReceiveMultipartMessage();
Console.WriteLine("======================================");
Console.WriteLine(" INCOMING CLIENT MESSAGE FROM SERVER");
Console.WriteLine("======================================");
PrintFrames("Server receiving", clientMessage);
}
},
string.Format("client {0}", i),
TaskCreationOptions.LongRunning);
}
// start the poller
poller.RunAsync();
// server loop
int sequenceNo = 0;
for (int i=0;i<10;i++)
{
NetMQMessage messageToServer = new NetMQMessage();
messageToServer.AppendEmptyFrame();
messageToServer.Append(sequenceNo.ToString());
sequenceNo++;
Console.WriteLine("======================================");
Console.WriteLine(" OUTGOING MESSAGE {0} TO CLIENTS ", sequenceNo);
Console.WriteLine("======================================");
PrintFrames("Client Sending", messageToServer);
server.SendMultipartMessage(messageToServer);
Thread.Sleep(delay);
}
Console.WriteLine("Finished.");
}
}
}