forked from booksbyus/zguide
-
Notifications
You must be signed in to change notification settings - Fork 23
/
interrupt.cs
96 lines (84 loc) · 1.95 KB
/
interrupt.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using ZeroMQ;
namespace ZeroMQ.Test
{
static partial class Program
{
public static void Interrupt(IDictionary<string, string> dict, string[] args)
{
//
// Interrupt
//
// Author: metadings
//
if (args == null || args.Length == 0)
{
args = new string[] { "World" };
}
string name = args[0];
using (var context = new ZContext())
using (var responder = new ZSocket(context, ZSocketType.REP))
{
Console.CancelKeyPress += (s, ea) =>
{
ea.Cancel = true;
context.Shutdown();
};
responder.Bind("tcp://*:5555");
var error = ZError.None;
ZFrame request;
while (true)
{
if (Console.KeyAvailable)
{
ConsoleKeyInfo info = Console.ReadKey(true);
/* if (info.Modifiers == ConsoleModifiers.Control && info.Key == ConsoleKey.C)
{
context.Shutdown();
} /**/
if (info.Key == ConsoleKey.Escape)
{
context.Shutdown();
}
}
if (null == (request = responder.ReceiveFrame(ZSocketFlags.DontWait, out error)))
{
if (error == ZError.EAGAIN)
{
Thread.Sleep(1);
continue;
}
if (error == ZError.ETERM)
break; // Interrupted
throw new ZException(error);
}
using (request)
{
Console.Write("Received: {0}!", request.ReadString());
Thread.Sleep(512); // See also the much slower reaction
Console.WriteLine(" Sending {0}... ", name);
using (var response = new ZFrame(name))
{
if (!responder.Send(response, out error))
{
if (error == ZError.ETERM)
break; // Interrupted
throw new ZException(error);
}
}
}
}
if (error == ZError.ETERM)
{
Console.WriteLine("Terminated! You have pressed CTRL+C or ESC.");
return;
}
throw new ZException(error);
}
}
}
}