forked from booksbyus/zguide
-
Notifications
You must be signed in to change notification settings - Fork 23
/
interrupt.cs
84 lines (76 loc) · 1.67 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
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
//
// Authors: Pieter Hintjens, Uli Riehm
//
using (var context = ZContext.Create())
using (var responder = ZSocket.Create(context, ZSocketType.REP))
{
var thread = new Thread(() =>
{
Console.CancelKeyPress += (sender, e) =>
{
// e.Cancel = false;
context.Terminate();
};
while (true)
{
if (Console.KeyAvailable)
{
ConsoleKeyInfo info = Console.ReadKey(true);
if (info.Modifiers == ConsoleModifiers.Control && info.Key == ConsoleKey.C)
{
break;
}
if (info.Key == ConsoleKey.Escape)
{
context.Terminate();
break;
}
}
Thread.Sleep(1);
}
});
thread.Start();
thread.Join(64);
responder.Bind("tcp://*:5555");
ZError error;
ZFrame request;
while (true)
{
if (null == (request = responder.ReceiveFrame(out error)))
{
if (error == ZError.ETERM)
{
Console.WriteLine("Terminating, you have pressed ESC.");
break;
}
throw new ZException(error);
}
using (request)
{
string respondText = "Hello";
Console.WriteLine("Received: {0}!", respondText, request.ReadString());
Console.Write("Sending {0}... ", respondText);
using (var response = new ZFrame(respondText))
{
responder.Send(response);
}
}
}
}
}
}
}