31
|
1 using System;
|
|
2 using System.Text;
|
|
3 using Common.Messages;
|
|
4 using GalaSoft.MvvmLight.Messaging;
|
|
5
|
|
6 namespace Server.Listeners
|
|
7 {
|
|
8 public class ZeroMqListener : IListener
|
|
9 {
|
|
10 public bool IsListening { get; set; }
|
|
11 private readonly string _zeroMqAddress;
|
|
12 public ZeroMqListener(string zeroMqAddress)
|
|
13 {
|
|
14 _zeroMqAddress = zeroMqAddress;
|
|
15 }
|
|
16
|
|
17 public void Start()
|
|
18 {
|
|
19 if (IsListening) return;
|
|
20
|
|
21 Messenger.Default.Send(new ZeroMqLogMessage() { Body = "Opening listener" });
|
|
22 IsListening = true;
|
|
23
|
|
24 try
|
|
25 {
|
|
26 using (var ctx = new ZMQ.Context(1))
|
|
27 {
|
|
28
|
|
29 var socket = ctx.Socket(ZMQ.REP);
|
|
30 socket.Bind(_zeroMqAddress);
|
|
31
|
|
32 while (IsListening)
|
|
33 {
|
|
34 byte[] message;
|
|
35 socket.Recv(out message);
|
|
36 Messenger.Default.Send(new ZeroMqClientMessage());
|
|
37
|
|
38 socket.Send(Encoding.ASCII.GetBytes(""));
|
|
39 }
|
|
40 }
|
|
41 }
|
|
42 catch (Exception e)
|
|
43 {
|
|
44 Messenger.Default.Send(new ZeroMqLogMessage() { Body = e.Message });
|
|
45 }
|
|
46 }
|
|
47
|
|
48 public void Stop()
|
|
49 {
|
|
50 if (!IsListening) return;
|
|
51 IsListening = false;
|
|
52 Messenger.Default.Send(new ZeroMqLogMessage() { Body = "Listener closed." });
|
|
53 }
|
|
54 }
|
|
55 }
|