-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNetService.cs
137 lines (121 loc) · 4.8 KB
/
NetService.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/*
Gamma Analyzer - Controlling application for Burn
Copyright (C) 2016 Norwegian Radiation Protection Authority
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
// Authors: Dag robole,
using System;
using System.Net;
using System.Collections.Generic;
using System.Collections.Concurrent;
using System.Linq;
using System.Text;
using System.Threading;
using System.Net.Sockets;
using Newtonsoft.Json;
using log4net;
namespace burn
{
/**
* NetService - Threaded class for network communication
*/
public partial class NetService
{
private ILog log = null;
//! Running state for this service
private volatile bool running;
//! UDP configuration
private const int servicePort = 9999;
private const int recvTimeout = 100;
private const int recvBufferSize = 1048576;
//! Network utilities
private Socket socket = null;
private EndPoint endPoint = null;
//! Queue with messages from GUI client
private ConcurrentQueue<ProtocolMessage> sendq = new ConcurrentQueue<ProtocolMessage>();
//! Queue with messages from server
private ConcurrentQueue<ProtocolMessage> recvq = new ConcurrentQueue<ProtocolMessage>();
/**
* Constructor for the NetService
* \param sendQueue - Queue with messages from GUI client
* \param recvQueue - Queue with messages from server
*/
public NetService(ILog l, ref ConcurrentQueue<ProtocolMessage> sendQueue, ref ConcurrentQueue<ProtocolMessage> recvQueue)
{
log = l;
log.Info("Creating Net Service");
socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
endPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), servicePort);
running = true;
sendQueue = sendq;
recvQueue = recvq;
}
/**
* Thread entry point
*/
public void DoWork()
{
try
{
log.Info("Initializing Net Service");
var buffer = new byte[recvBufferSize]; // FIXME: configurable size
socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, recvTimeout);
while (running)
{
// Send messages from analyzer
while (sendq.Count > 0)
{
ProtocolMessage sendMsg;
if (sendq.TryDequeue(out sendMsg))
{
IPEndPoint ep = new IPEndPoint(IPAddress.Parse(sendMsg.IPAddress), servicePort);
Byte[] sendBytes = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(sendMsg.Params));
socket.SendTo(sendBytes, (EndPoint)ep);
}
}
// Receive messages from collector
while (socket.Available > 0)
{
int nbytes = socket.ReceiveFrom(buffer, ref endPoint);
if (nbytes > 0)
{
ProtocolMessage recvMsg = new ProtocolMessage(((IPEndPoint)endPoint).Address.ToString());
string jdata = Encoding.UTF8.GetString(buffer, 0, nbytes);
recvMsg.Params = JsonConvert.DeserializeObject<Dictionary<string, object>>(jdata);
recvq.Enqueue(recvMsg);
}
}
Thread.Sleep(50);
}
}
catch (Exception ex)
{
log.Error(ex.Message, ex);
}
}
/**
* Function used to stop this service
*/
public void RequestStop()
{
running = false;
log.Info("Stopping Net Service");
}
/**
* Function used to check the running state of this service
*/
public bool IsRunning()
{
return running;
}
}
}