forked from booksbyus/zguide
-
Notifications
You must be signed in to change notification settings - Fork 23
/
lpserver.cs
69 lines (60 loc) · 1.41 KB
/
lpserver.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using ZeroMQ;
namespace Examples
{
static partial class Program
{
//
// Lazy Pirate server
// Binds REP socket to tcp://*:5555
// Like hwserver except:
// - echoes request as-is
// - randomly runs slowly, or exits to simulate a crash.
//
// Author: metadings
//
public static void LPServer(string[] args)
{
using (var context = new ZContext())
using (var responder = new ZSocket(context, ZSocketType.REP))
{
responder.Bind("tcp://*:5555");
ZError error;
int cycles = 0;
var rnd = new Random();
while (true)
{
ZMessage incoming;
if (null == (incoming = responder.ReceiveMessage(out error)))
{
if (error == ZError.ETERM)
return; // Interrupted
throw new ZException(error);
}
using (incoming)
{
++cycles;
// Simulate various problems, after a few cycles
if (cycles > 16 && rnd.Next(16) == 0)
{
Console.WriteLine("I: simulating a crash");
break;
}
else if (cycles > 4 && rnd.Next(4) == 0)
{
Console.WriteLine("I: simulating CPU overload");
Thread.Sleep(1000);
}
Console.WriteLine("I: normal request ({0})", incoming[0].ReadInt32());
Thread.Sleep(1); // Do some heavy work
responder.Send(incoming);
}
}
}
}
}
}