-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathQvxCommandServer.cs
171 lines (154 loc) · 5.65 KB
/
QvxCommandServer.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
/*
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 NLog;
using System;
using System.Collections.Generic;
using System.IO.Pipes;
using System.Linq;
using System.Text;
using System.Threading;
#endregion
public class QvxCommandServer
{
#region Variables & Properties
private static Logger logger = LogManager.GetCurrentClassLogger();
Thread thread;
private string pipeName;
public string PipeName
{
get
{
return @"\\.\pipe\"+pipeName;
}
}
bool running = false;
public bool Running
{
get
{
return running;
}
}
#endregion
#region Construtor & Destructor
public QvxCommandServer()
{
thread = new Thread(new ThreadStart(QvxCommandServerWorker));
thread.IsBackground = true;
thread.Name = "QvxCommandServerWorker";
this.pipeName = Guid.NewGuid().ToString().Replace("-", "")+".pip";
running = true;
thread.Start();
}
~QvxCommandServer()
{
Close();
}
#endregion
#region Close
public void Close()
{
running = false;
if (thread.IsAlive)
thread.Join(100);
}
#endregion
object lockSendQvxRequest = new object();
QvxRequest request = null;
QvxReply reply = null;
public QvxReply HandleQvxRequest(QvxRequest request)
{
lock(lockSendQvxRequest)
{
// TODO better request and response handling + async
this.reply = null;
this.request = request;
while (reply == null)
Thread.Sleep(10);
return reply;
}
}
#region QvxCommandServerWorker
private void QvxCommandServerWorker()
{
using (NamedPipeServerStream pipeServer = new NamedPipeServerStream(pipeName, PipeDirection.InOut, 1))
{
pipeServer.WaitForConnection();
var buf = new byte[4];
var buf2 = new byte[4];
object state = new object();
while (running)
{
if (request != null)
{
#region Send Request
byte[] bRequest = null;
try
{
bRequest = ASCIIEncoding.ASCII.GetBytes(request.Serialize() + "\0");
request = null;
}
catch (Exception ex)
{
logger.Error(ex);
throw ex;
}
buf = BitConverter.GetBytes((Int32)bRequest.Length);
buf2[0] = buf[3];
buf2[1] = buf[2];
buf2[2] = buf[1];
buf2[3] = buf[0];
pipeServer.Write(buf2, 0, 4);
pipeServer.Write(bRequest, 0, bRequest.Length);
pipeServer.WaitForPipeDrain();
#endregion
#region Receive Response
var iar = pipeServer.BeginRead(buf, 0, 4, null, state);
while (!iar.IsCompleted) Thread.Sleep(1); // TODO: add Timeout possibility
var count = pipeServer.EndRead(iar);
if (count != 4) throw new Exception("Invalid Count Length");
buf2[0] = buf[3];
buf2[1] = buf[2];
buf2[2] = buf[1];
buf2[3] = buf[0];
var datalength = BitConverter.ToInt32(buf2, 0);
var data = new byte[datalength];
count = pipeServer.Read(data, 0, datalength);
if (count != datalength) throw new Exception("Invalid Data Length");
var sdata = ASCIIEncoding.ASCII.GetString(data);
sdata = sdata.Replace("\0", "");
try
{
reply = QvxReply.Deserialize(sdata);
}
catch (Exception ex)
{
logger.Error(ex);
reply = new QvxReply() { Result = QvxResult.QVX_PIPE_ERROR, ErrorMessage = ex.Message };
throw ex;
}
#endregion
}
}
}
}
#endregion
}
}