forked from booksbyus/zguide
-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathrrbroker.cs
68 lines (62 loc) · 1.5 KB
/
rrbroker.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
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 RRBroker(string[] args)
{
//
// Simple request-reply broker
//
// Author: metadings
//
// Prepare our context and sockets
using (var ctx = new ZContext())
using (var frontend = new ZSocket(ctx, ZSocketType.ROUTER))
using (var backend = new ZSocket(ctx, ZSocketType.DEALER))
{
frontend.Bind("tcp://*:5559");
backend.Bind("tcp://*:5560");
// Initialize poll set
var poll = ZPollItem.CreateReceiver();
// Switch messages between sockets
ZError error;
ZMessage message;
while (true)
{
if (frontend.PollIn(poll, out message, out error, TimeSpan.FromMilliseconds(64)))
{
// Process all parts of the message
Console_WriteZMessage("frontend", 2, message);
backend.Send(message);
}
else
{
if (error == ZError.ETERM)
return; // Interrupted
if (error != ZError.EAGAIN)
throw new ZException(error);
}
if (backend.PollIn(poll, out message, out error, TimeSpan.FromMilliseconds(64)))
{
// Process all parts of the message
Console_WriteZMessage(" backend", 2, message);
frontend.Send(message);
}
else
{
if (error == ZError.ETERM)
return; // Interrupted
if (error != ZError.EAGAIN)
throw new ZException(error);
}
}
}
}
}
}