forked from booksbyus/zguide
-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathsyncpub.cs
59 lines (50 loc) · 1.41 KB
/
syncpub.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using ZeroMQ;
namespace Examples
{
static partial class Program
{
const int SyncPub_SubscribersExpected = 3; // We wait for 3 subscribers
public static void SyncPub(string[] args)
{
//
// Synchronized publisher
//
// Author: metadings
//
// Socket to talk to clients and
// Socket to receive signals
using (var context = new ZContext())
using (var publisher = new ZSocket(context, ZSocketType.PUB))
using (var syncservice = new ZSocket(context, ZSocketType.REP))
{
publisher.SendHighWatermark = 1100000;
publisher.Bind("tcp://*:5561");
syncservice.Bind("tcp://*:5562");
// Get synchronization from subscribers
int subscribers = SyncPub_SubscribersExpected;
do
{
Console.WriteLine("Waiting for {0} subscriber" + (subscribers > 1 ? "s" : string.Empty) + "...", subscribers);
// - wait for synchronization request
syncservice.ReceiveFrame();
// - send synchronization reply
syncservice.Send(new ZFrame());
}
while (--subscribers > 0);
// Now broadcast exactly 20 updates followed by END
Console.WriteLine("Broadcasting messages:");
for (int i = 0; i < 20; ++i)
{
Console.WriteLine("Sending {0}...", i);
publisher.Send(new ZFrame(i));
}
publisher.Send(new ZFrame("END"));
}
}
}
}