-
Notifications
You must be signed in to change notification settings - Fork 2
/
RpcEngine.cpp
453 lines (360 loc) · 11.7 KB
/
RpcEngine.cpp
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
/**
* Rpc engine build upon google protocol buffers
*
* Copyright 2013 Thinstuff Technologies GmbH
* Copyright 2013 DI (FH) Martin Haimberger <martin.haimberger@thinstuff.at>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "RpcEngine.h"
#include <winpr/pipe.h>
#include <winpr/thread.h>
#include <call/CallFactory.h>
#include <arpa/inet.h>
#include <winpr/wlog.h>
#include <call/CallIn.h>
#include <appcontext/ApplicationContext.h>
namespace freerds {
namespace pbrpc {
#define CLIENT_DISCONNECTED 2
#define CLIENT_ERROR -1
#define CLIENT_SUCCESS 0
static wLog * logger_RPCEngine = WLog_Get("freerds.pbrpc.RpcEngine");
RpcEngine::RpcEngine() :
mPacktLength(0), mHeaderRead(0), mPayloadRead(0) {
mhStopEvent = CreateEvent(NULL,TRUE,FALSE,NULL);
WLog_SetLogLevel(logger_RPCEngine, WLOG_ERROR);
}
RpcEngine::~RpcEngine() {
google::protobuf::ShutdownProtobufLibrary();
}
HANDLE RpcEngine::createServerPipe(const char* endpoint) {
HANDLE hNamedPipe;
hNamedPipe = CreateNamedPipe(endpoint, PIPE_ACCESS_DUPLEX,
PIPE_TYPE_BYTE | PIPE_READMODE_BYTE | PIPE_WAIT,
PIPE_UNLIMITED_INSTANCES, PIPE_BUFFER_SIZE, PIPE_BUFFER_SIZE, 0, NULL);
if ((!hNamedPipe) || (hNamedPipe == INVALID_HANDLE_VALUE)) {
WLog_Print(logger_RPCEngine, WLOG_ERROR, "creating namedpipe failed");
return NULL;
}
return hNamedPipe;
}
int RpcEngine::startEngine() {
mhServerThread = CreateThread(NULL, 0,
(LPTHREAD_START_ROUTINE) RpcEngine::listenerThread, (void*) this,
CREATE_SUSPENDED, NULL);
ResumeThread(mhServerThread);
return CLIENT_SUCCESS;
}
int RpcEngine::stopEngine() {
if (mhServerThread) {
SetEvent(mhStopEvent);
WaitForSingleObject(mhServerThread,INFINITE);
CloseHandle(mhServerThread);
mhServerThread = NULL;
}
return CLIENT_SUCCESS;
}
int RpcEngine::createServerPipe() {
mhServerPipe = createServerPipe("\\\\.\\pipe\\FreeRDS_SessionManager");
if (!mhServerPipe) {
WLog_Print(logger_RPCEngine, WLOG_ERROR, "Could not create named pipe \\\\.\\pipe\\FreeRDS_SessionManager");
return CLIENT_ERROR;
}
}
void* RpcEngine::listenerThread(void* arg) {
RpcEngine* engine;
engine = (RpcEngine*) arg;
WLog_Print(logger_RPCEngine, WLOG_TRACE, "started RPC listener Thread");
while (1) {
if (!engine->createServerPipe()) {
break;
}
HANDLE clientPipe = engine->acceptClient();
if (!clientPipe)
break;
if (engine->serveClient() == CLIENT_ERROR ) {
break;
}
engine->resetStatus();
}
return NULL;
}
HANDLE RpcEngine::acceptClient() {
if (!mhServerPipe) {
return NULL;
}
DWORD status;
DWORD nCount;
HANDLE events[2];
nCount = 0;
events[nCount++] = mhStopEvent;
events[nCount++] = mhServerPipe;
status = WaitForMultipleObjects(nCount, events, FALSE, INFINITE);
/* if (status == WAIT_OBJECT_0) {
return NULL;
} else if (status == WAIT_OBJECT_0 +1) {*/
if (WaitForSingleObject(mhStopEvent, 0) == WAIT_OBJECT_0) {
WLog_Print(logger_RPCEngine, WLOG_TRACE, "got shutdown signal");
return NULL;
}
if (WaitForSingleObject(mhServerPipe, 0) == WAIT_OBJECT_0) {
BOOL fConnected;
DWORD dwPipeMode;
fConnected = ConnectNamedPipe(mhServerPipe, NULL);
if (!fConnected)
fConnected = (GetLastError() == ERROR_PIPE_CONNECTED);
if (!fConnected) {
WLog_Print(logger_RPCEngine, WLOG_ERROR, "could not connect client");
return NULL;
}
mhClientPipe = mhServerPipe;
dwPipeMode = PIPE_WAIT;
SetNamedPipeHandleState(mhClientPipe, &dwPipeMode, NULL, NULL);
WLog_Print(logger_RPCEngine, WLOG_TRACE, "connect client with handle %x",mhClientPipe);
return mhClientPipe;
}
return NULL;
}
int RpcEngine::read() {
if (mPacktLength > 0) {
return readPayload();
} else {
return readHeader();
}
}
int RpcEngine::readHeader() {
DWORD lpNumberOfBytesRead = 0;
BOOL fSuccess;
fSuccess = ReadFile(mhClientPipe, mHeaderBuffer + mHeaderRead,
4 - mHeaderRead, &lpNumberOfBytesRead, NULL);
if (!fSuccess || (lpNumberOfBytesRead == 0)) {
WLog_Print(logger_RPCEngine, WLOG_ERROR, "error reading");
return CLIENT_DISCONNECTED;
}
mHeaderRead += lpNumberOfBytesRead;
if (mHeaderRead == 4) {
mPacktLength = ntohl(*(DWORD *)mHeaderBuffer);
WLog_Print(logger_RPCEngine, WLOG_TRACE, "header read, packet size %d",mPacktLength);
}
return CLIENT_SUCCESS;
}
int RpcEngine::readPayload() {
DWORD lpNumberOfBytesRead = 0;
BOOL fSuccess;
fSuccess = ReadFile(mhClientPipe, mPayloadBuffer + mPayloadRead,
mPacktLength - mPayloadRead, &lpNumberOfBytesRead,
NULL);
if (!fSuccess || (lpNumberOfBytesRead == 0)) {
WLog_Print(logger_RPCEngine, WLOG_ERROR, "error reading payload");
return CLIENT_DISCONNECTED;
}
mPayloadRead += lpNumberOfBytesRead;
return CLIENT_SUCCESS;
}
int RpcEngine::processData() {
mpbRPC.Clear();
mpbRPC.ParseFromArray(mPayloadBuffer, mPayloadRead);
uint32_t callID = mpbRPC.tag();
uint32_t callType = mpbRPC.msgtype();
if (mpbRPC.isresponse()) {
// search the stored call to fill back answer
callNS::CallOut* foundCallOut = 0;
std::list<callNS::CallOut*>::iterator it;
for ( it = mAnswerWaitingQueue.begin(); it != mAnswerWaitingQueue.end(); it++) {
callNS::CallOut* currentCallOut = (callNS::CallOut*)(*it);
if (currentCallOut->getTag() == callID) {
foundCallOut = currentCallOut;
mAnswerWaitingQueue.remove(foundCallOut);
break;
}
}
if (foundCallOut == NULL) {
WLog_Print(logger_RPCEngine, WLOG_ERROR, "Received answer for callID %d, but no responding call was found",callID);
return CLIENT_SUCCESS;
} else {
// fill the answer and signal
if (mpbRPC.status() == RPCBase_RPCSTATUS_SUCCESS ) {
foundCallOut->setEncodedeResponse(mpbRPC.payload());
foundCallOut->setResult(0);
} else if (mpbRPC.status() == RPCBase_RPCSTATUS_FAILED ) {
foundCallOut->setErrorDescription(mpbRPC.errordescription());
foundCallOut->setResult(1);
} else if (mpbRPC.status() == RPCBase_RPCSTATUS_NOTFOUND) {
foundCallOut->setResult(2);
}
}
} else {
callNS::Call* createdCall = CALL_FACTORY.createClass(callType);
if (createdCall == NULL) {
// call not found ... send error
WLog_Print(logger_RPCEngine, WLOG_ERROR, "no registered class for calltype=%d",callType);
sendError(callID, callType);
delete createdCall;
return CLIENT_ERROR;
}
if ( createdCall->getDerivedType() == 1) {
// we got an CallIn object ... so handle it
callNS::CallIn* createdCallIn = (callNS::CallIn*)createdCall;
createdCallIn->setEncodedRequest(mpbRPC.payload());
createdCallIn->setTag(callID);
WLog_Print(logger_RPCEngine, WLOG_TRACE, "call upacked for callType=%d and callID=%d",callType,callID);
// call the implementation ...
createdCallIn->decodeRequest();
if (!createdCallIn->doStuff()) {
createdCallIn->encodeResponse();
}
// send the result
send(createdCall);
delete createdCall;
} else {
WLog_Print(logger_RPCEngine, WLOG_ERROR, "callobject had wrong baseclass, callType=%d",callType);
sendError(callID, callType);
delete createdCall;
return CLIENT_ERROR;
}
return CLIENT_SUCCESS;
}
}
int RpcEngine::send(freerds::sessionmanager::call::Call * call) {
DWORD lpNumberOfBytesWritten;
std::string serialized;
if (call->getDerivedType() == 1) {
// this is a CallIn
callNS::CallIn * callIn = (callNS::CallIn *)call;
// create answer
mpbRPC.Clear();
mpbRPC.set_isresponse(true);
mpbRPC.set_tag(callIn->getTag());
mpbRPC.set_msgtype(callIn->getCallType());
if (call->getResult()) {
WLog_Print(logger_RPCEngine, WLOG_TRACE, "call for callType=%d and callID=%d failed, sending error response",callIn->getCallType(),callIn->getTag());
mpbRPC.set_status(RPCBase_RPCSTATUS_FAILED);
std::string errordescription = callIn->getErrorDescription();
if (errordescription.size() > 0) {
mpbRPC.set_errordescription(errordescription);
}
} else {
WLog_Print(logger_RPCEngine, WLOG_TRACE, "call for callType=%d and callID=%d success, sending response",callIn->getCallType(),callIn->getTag());
mpbRPC.set_status(RPCBase_RPCSTATUS_SUCCESS);
mpbRPC.set_payload(callIn->getEncodedResponse());
}
} else if (call->getDerivedType() == 2) {
// this is a CallOut
callNS::CallOut * callOut = (callNS::CallOut *)call;
// create answer
mpbRPC.Clear();
mpbRPC.set_isresponse(false);
mpbRPC.set_tag(callOut->getTag());
mpbRPC.set_msgtype(callOut->getCallType());
mpbRPC.set_status(RPCBase_RPCSTATUS_SUCCESS);
mpbRPC.set_payload(callOut->getEncodedRequest());
}
mpbRPC.SerializeToString(&serialized);
return sendInternal(serialized);
}
int RpcEngine::sendError(uint32_t callID, uint32_t callType) {
std::string serialized;
mpbRPC.Clear();
mpbRPC.set_isresponse(true);
mpbRPC.set_tag(callID);
mpbRPC.set_msgtype(callType);
mpbRPC.set_status(RPCBase_RPCSTATUS_NOTFOUND);
mpbRPC.SerializeToString(&serialized);
return sendInternal(serialized);
}
int RpcEngine::sendInternal(std::string data) {
DWORD lpNumberOfBytesWritten;
DWORD messageSize = htonl(data.size());
bool fSuccess = WriteFile(mhClientPipe, &messageSize,
4, &lpNumberOfBytesWritten, NULL);
if (!fSuccess || (lpNumberOfBytesWritten == 0)) {
WLog_Print(logger_RPCEngine, WLOG_ERROR, "error sending");
return CLIENT_ERROR;
}
fSuccess = WriteFile(mhClientPipe, data.c_str(),
data.size(), &lpNumberOfBytesWritten, NULL);
if (!fSuccess || (lpNumberOfBytesWritten == 0)) {
WLog_Print(logger_RPCEngine, WLOG_ERROR, "error sending");
return CLIENT_ERROR;
}
return CLIENT_SUCCESS;
}
void RpcEngine::resetStatus() {
mPacktLength = 0;
mHeaderRead = 0;
mPayloadRead = 0;
}
int RpcEngine::serveClient() {
DWORD status;
DWORD nCount;
SignalingQueue<callNS::Call>* outgoingQueue = APP_CONTEXT.getRpcOutgoingQueue();
HANDLE queueHandle = outgoingQueue->getSignalHandle();
HANDLE events[3];
nCount = 0;
events[nCount++] = mhStopEvent;
events[nCount++] = mhClientPipe;
events[nCount++] = queueHandle;
int retValue = 0;
while (1) {
status = WaitForMultipleObjects(nCount, events, FALSE, INFINITE);
if (WaitForSingleObject(mhStopEvent, 0) == WAIT_OBJECT_0) {
retValue = CLIENT_ERROR;
break;
}
if (WaitForSingleObject(mhClientPipe, 0) == WAIT_OBJECT_0) {
retValue = read();
if (retValue) {
break;
}
// process the data
if (mPayloadRead == mPacktLength) {
processData();
resetStatus();
}
}
if (WaitForSingleObject(queueHandle, 0) == WAIT_OBJECT_0) {
outgoingQueue->resetEventAndLockQueue();
callNS::Call * currentCall = outgoingQueue->getElementLockFree();
while (currentCall != NULL) {
processOutgoingCall(currentCall);
currentCall = outgoingQueue->getElementLockFree();
}
outgoingQueue->unlockQueue();
}
}
return retValue;
}
int RpcEngine::processOutgoingCall(freerds::sessionmanager::call::Call* call) {
if (call->getDerivedType() == 2) {
// this is a CallOut
callNS::CallOut * callOut = (callNS::CallOut *)call;
if (send(call) == CLIENT_SUCCESS ) {
mAnswerWaitingQueue.push_back(callOut);
return CLIENT_SUCCESS;
} else {
WLog_Print(logger_RPCEngine, WLOG_ERROR, "error sending call, informing call");
callOut->setResult(1); // for failed
return CLIENT_ERROR;
}
} else {
WLog_Print(logger_RPCEngine, WLOG_ERROR, "call was no outgoing call, wrong type in queue, dropping packet!");
delete call;
return CLIENT_SUCCESS;
}
}
}
}