forked from booksbyus/zguide
-
Notifications
You must be signed in to change notification settings - Fork 23
/
msreader.cs
79 lines (70 loc) · 1.64 KB
/
msreader.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using ZeroMQ;
namespace Examples
{
static partial class Program
{
public static void MSReader(string[] args)
{
//
// Reading from multiple sockets
// This version uses a simple recv loop
//
// Author: metadings
//
using (var context = new ZContext())
using (var receiver = new ZSocket(context, ZSocketType.PULL))
using (var subscriber = new ZSocket(context, ZSocketType.SUB))
{
// Connect to task ventilator
receiver.Connect("tcp://127.0.0.1:5557");
// Connect to weather server
subscriber.Connect("tcp://127.0.0.1:5556");
subscriber.SetOption(ZSocketOption.SUBSCRIBE, "10001 ");
// Process messages from both sockets
// We prioritize traffic from the task ventilator
ZError error;
ZFrame frame;
while (true)
{
while (true)
{
if (null != (frame = receiver.ReceiveFrame(ZSocketFlags.DontWait, out error)))
{
// Process task
}
else
{
if (error == ZError.ETERM)
return; // Interrupted
if (error != ZError.EAGAIN)
throw new ZException(error);
break;
}
}
while (true)
{
if (null != (frame = subscriber.ReceiveFrame(ZSocketFlags.DontWait, out error)))
{
// Process weather update
}
else
{
if (error == ZError.ETERM)
return; // Interrupted
if (error != ZError.EAGAIN)
throw new ZException(error);
break;
}
}
// No activity, so sleep for 1 msec
Thread.Sleep(1);
}
}
}
}
}