-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathQvxDataServer.cs
207 lines (184 loc) · 6.7 KB
/
QvxDataServer.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/*
This Library is to have an easy access to Qvx Files and the Qlikview
Connector Interface.
Copyright (C) 2011 Konrad Mattheis ([email protected])
This Software is available under the GPL and a comercial licence.
For further information to the comercial licence please contact
Konrad Mattheis.
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.
*/
namespace QvxLib
{
#region Usings
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NLog;
using System.Threading;
using System.IO.Pipes;
#endregion
#region QvxDataServer
public class QvxDataServer
{
#region Variables & Properties
private static Logger logger = LogManager.GetCurrentClassLogger();
public Action<byte[],Boolean> HandleQvxReceivedData;
public Action FinishedQvxReceivedData;
private object locksendQueue = new object();
private List<byte[]> sendQueue = new List<byte[]>();
Thread thread;
Thread sendthread;
private string pipeName;
public string PipeName
{
get
{
return @"\\.\pipe\"+pipeName;
}
}
bool running = false;
public bool Running
{
get
{
return running;
}
}
#endregion
#region Construtor & Destructor
public QvxDataServer()
{
running = true;
thread = new Thread(new ThreadStart(QvxDataServerWorker))
{
IsBackground = true,
Name = "QvxDataServerWorker",
};
this.pipeName = Guid.NewGuid().ToString().Replace("-", "")+".pip";
sendthread = new Thread(new ThreadStart(QvxDataServerHandleReceivedDataWorker))
{
IsBackground = true,
Name = "QvxDataServerHandleReceivedDataWorker",
};
sendthread.Start();
thread.Start();
}
~QvxDataServer()
{
Close();
}
#endregion
#region Close
public void Close()
{
running = false;
if (thread.IsAlive)
thread.Join(100);
if (sendthread.IsAlive)
sendthread.Join(100);
}
#endregion
#region QvxDataServerHandleReceivedDataWorker
private void QvxDataServerHandleReceivedDataWorker()
{
var sendList = new List<byte[]>();
var counter = 3;
while (counter > 0)
{
if (running || (sendList.Count > 0 && HandleQvxReceivedData != null))
counter = 3;
else
counter--;
try
{
if (HandleQvxReceivedData != null)
{
lock (locksendQueue)
{
if (sendQueue.Count > 0)
{
sendList.AddRange(sendQueue);
sendQueue.Clear();
}
}
if (sendList.Count > 0)
{
int cnt = 0;
foreach (var item in sendList)
cnt += item.Length;
var buf = new Byte[cnt];
cnt = 0;
bool finished = false;
foreach (var item in sendList)
{
if (item.Length > 0)
{
Array.Copy(item, 0, buf, cnt, item.Length);
cnt += item.Length;
}
else finished = true;
}
sendList.Clear();
if (buf.Length == 0) finished = true;
Console.WriteLine("Date ToSend: " + finished.ToString()+" " + buf.Length.ToString());
HandleQvxReceivedData(buf, finished);
}
}
Thread.Sleep(5);
}
catch(Exception ex)
{
Console.WriteLine("Close Exception");
logger.Error(ex);
sendList.Clear();
}
}
Console.WriteLine("Close QvxDataServerHandleReceivedDataWorker");
}
#endregion
#region QvxDataServerWorker
private void QvxDataServerWorker()
{
using (NamedPipeServerStream pipeServer = new NamedPipeServerStream(pipeName, PipeDirection.InOut, 1))
{
pipeServer.WaitForConnection();
var buf = new byte[65536*20];
object state = new object();
while (running && pipeServer.IsConnected)
{
var iar = pipeServer.BeginRead(buf, 0, buf.Length, null, state);
while (!iar.IsCompleted) Thread.Sleep(1); // TODO: add Timeout possibility
var count = pipeServer.EndRead(iar);
Console.WriteLine("Date Read: " + count.ToString());//+ ASCIIEncoding.ASCII.GetString(buf, 0, count));
if (count > 0)
{
var sendBuf = new byte[count];
Array.Copy(buf, sendBuf, count);
lock (locksendQueue)
{
sendQueue.Add(sendBuf);
}
}
}
if (pipeServer.IsConnected) pipeServer.Close();
}
Console.WriteLine("PIPE finished");
lock (locksendQueue)
{
sendQueue.Add(new byte[0]);
}
running = false;
Console.WriteLine("Close QvxDataServerWorker");
}
#endregion
}
#endregion
}