Page Menu
Home
Phabricator
Search
Configure Global Search
Log In
Files
F56559
No One
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Authored By
stwalkerster
Mar 17 2018, 6:43 PM
2018-03-17 18:43:20 (UTC+0)
Size
2 KB
Referenced Files
None
Subscribers
None
View Options
namespace foo
{
using System.Threading;
using ZeroMQ;
internal class zmq_sender
{
public static void Main(string[] args)
{
// fancy stuff to queue up three instances of DoStuff on different threads, and wait until they're all done
// most of this is waiting-for-completion garbage, the key bits are ThreadPool.QueueUserWorkItem() which
// takes a method or lambda, and DoStuff().
int instances = 3;
var mre = new WaitHandle[instances];
for (int i = 0; i < instances; i++)
{
mre[i] = new ManualResetEvent(false);
var current = (ManualResetEvent)mre[i];
int c = i;
ThreadPool.QueueUserWorkItem(
x =>
{
try
{
DoStuff(c);
}
finally
{
current.Set();
}
});
}
WaitHandle.WaitAll(mre);
}
// emulates a client. Connects, sends three things, disconnects.
private static void DoStuff(int i)
{
using (var context = new ZContext())
{
using (var sock = new ZSocket(context, ZSocketType.PUB){})
{
sock.Connect("tcp://127.0.0.1:12345");
sock.Send(new ZFrame(i + ": test 1"));
Thread.Sleep(1000);
sock.Send(new ZFrame(i + ": test 2"));
Thread.Sleep(1000);
sock.Send(new ZFrame(i + ": test 3"));
}
}
}
}
internal class zmq_receiver
{
// connects, binds to socket, and prints whatever it receives.
public static void Main(string[] args)
{
var context = new ZContext();
var sock = new ZSocket(context, ZSocketType.SUB);
sock.Bind("tcp://*:12345");
sock.SubscribeAll();
while (true)
{
using (ZFrame request = sock.ReceiveFrame())
{
Console.WriteLine(request.ReadString());
}
}
}
}
}
/*
OUTPUT:
0: test 1
2: test 1
1: test 1
0: test 2
2: test 2
1: test 2
2: test 3
1: test 3
0: test 3
File Metadata
Details
Attached
Mime Type
text/plain; charset=utf-8
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
52104
Default Alt Text
(2 KB)
Attached To
Mode
P194 (An Untitled Masterwork)
Attached
Detach File
Event Timeline
Log In to Comment