forked from HeliumProject/Foundation
-
Notifications
You must be signed in to change notification settings - Fork 1
/
RPC.cpp
594 lines (469 loc) · 11.7 KB
/
RPC.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
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
#include "FoundationPch.h"
#include "RPC.h"
#include "Platform/Assert.h"
#include "Foundation/IPC.h"
#include <string.h>
using namespace Helium;
using namespace Helium::RPC;
// Debug printing
//#define RPC_DEBUG
//#define RPC_DEBUG_MSG
Interface::Interface(const char* name)
: m_Name (name)
, m_Host (NULL)
, m_InvokerCount (0)
{
}
void Interface::AddInvoker(InvokerPtr invoker)
{
if (m_InvokerCount >= MAX_INVOKERS)
{
HELIUM_BREAK();
return;
}
m_Invokers[m_InvokerCount++] = invoker;
}
Invoker* Interface::GetInvoker(const char* name)
{
for ( uint32_t i=0; i<m_InvokerCount; i++ )
{
if ( !strcmp( m_Invokers[i]->GetName(), name ) )
{
return m_Invokers[i];
}
}
return NULL;
}
Host::Stack::Stack()
{
Reset();
}
void Host::Stack::Reset()
{
memset(m_Frames, 0, sizeof(Host::Frame) * MAX_STACK);
m_Size = 0;
}
int32_t Host::Stack::Size()
{
return m_Size;
}
Host::Frame* Host::Stack::Push()
{
HELIUM_ASSERT(m_Size < MAX_STACK);
m_Size++;
return Top();
}
Host::Frame* Host::Stack::Top()
{
HELIUM_ASSERT(m_Size > 0);
return &m_Frames[m_Size-1];
}
void Host::Stack::Pop()
{
memset(Top(), 0, sizeof(Host::Frame));
m_Size--;
}
Host::Host()
{
Reset();
}
Host::~Host()
{
}
void Host::Reset()
{
m_Connection = NULL;
m_ConnectionCount = 0;
memset(m_Interfaces, 0, sizeof(Interface*) * MAX_INTERFACES);
}
void Host::AddInterface(Interface* iface)
{
if (m_InterfaceCount >= MAX_INTERFACES)
{
HELIUM_BREAK();
return;
}
m_Interfaces[m_InterfaceCount++] = iface;
iface->SetHost( this );
}
Interface* Host::GetInterface(const char* name)
{
for ( uint32_t i=0; i<m_InterfaceCount; i++ )
{
if ( !strcmp( m_Interfaces[i]->GetName(), name ) )
{
return m_Interfaces[i];
}
}
return NULL;
}
void Host::SetConnection(IPC::Connection* con)
{
m_Connection = con;
m_ConnectionCount = m_Connection->GetConnectCount();
}
IPC::Message* Host::Create(Invoker* invoker, uint32_t size, int32_t transaction)
{
// TODO: Pack the invoker and interface name into the message data
invoker->GetName();
invoker->GetInterface()->GetName();
if (transaction != 0)
{
return m_Connection->CreateMessage(0, size, transaction);
}
else
{
return m_Connection->CreateMessage(0, size);
}
}
void Host::Emit(Invoker* invoker, Args* args, uint32_t size, SwizzleFunc swizzler)
{
if (Connected())
{
if (m_ConnectionCount != m_Connection->GetConnectCount())
{
#ifdef RPC_DEBUG
printf("RPC::Connection cycled, resetting stack\n");
#endif
m_ConnectionCount = m_Connection->GetConnectCount();
m_Stack.Reset();
}
uint32_t size = 0;
if (args != NULL)
{
HELIUM_ASSERT(size > 0);
size += size;
}
if (args->m_Payload != NULL)
{
HELIUM_ASSERT(args->m_PayloadSize > 0);
size += args->m_PayloadSize;
}
IPC::Message* message = Create(invoker, size);
uint8_t* ptr = message->GetData();
if (args != NULL)
{
swizzler(args);
memcpy(ptr, args, size);
ptr += size;
swizzler(args);
}
if (args->m_Payload != NULL)
{
memcpy(ptr, args->m_Payload, args->m_PayloadSize);
ptr += args->m_PayloadSize;
}
HELIUM_ASSERT((uint32_t)(ptr - message->GetData()) == size);
#ifdef RPC_DEBUG_MSG
uint32_t msg_id = message->GetID();
uint32_t msg_size = message->GetSize();
#endif
int32_t msg_transaction = message->GetTransaction();
if (m_Connection->Send(message)!=IPC::ConnectionStates::Active)
{
delete message;
return;
}
#ifdef RPC_DEBUG_MSG
printf("RPC::Put message id 0x%" HELIUM_PRINT_POINTER ", size %d, transaction %d\n", msg_id, msg_size, msg_transaction);
#endif
message = NULL; // assume its GONE
if (args->m_Flags & RPC::Flags::NonBlocking)
{
#ifdef RPC_DEBUG
printf("RPC::Emitting async transaction %d\n", msg_transaction);
#endif
}
else
{
// create frame for call
Frame* frame = m_Stack.Push();
// set the transaction we are blocking on
frame->m_ReplyTransaction = msg_transaction;
#ifdef RPC_DEBUG
printf("RPC::Emitting transaction %d, stack size %d\n", msg_transaction, m_Stack.Size());
#endif
// process until we retrieve it
frame->m_Replied = false;
// process messages until we receive our reply
while (Process(true) && !frame->m_Replied);
// if we did not get our reply
if (!frame->m_Replied)
{
#ifdef RPC_DEBUG
printf("RPC::Emit failed for transaction %d, stack size %d\n", msg_transaction, m_Stack.Size());
#endif
// if we didn't reset and the call timed out, pop
if (m_Stack.Size())
{
m_Stack.Pop();
}
}
else
{
#ifdef RPC_DEBUG
printf("RPC::Emit success for transaction %d, stack size %d\n", msg_transaction, m_Stack.Size());
#endif
size = 0;
ptr = frame->m_ReplyData;
if (args != NULL)
{
HELIUM_ASSERT(size > 0);
size += size;
}
if (args->m_Payload != NULL)
{
HELIUM_ASSERT(args->m_PayloadSize > 0);
size += args->m_PayloadSize;
}
// do ref args processing here
if (args->m_Flags & RPC::Flags::ReplyWithArgs && args != NULL)
{
swizzler(ptr);
// copy our data BACK
memcpy(args, ptr, size);
ptr += size;
}
// do ref payload processing here
if (args->m_Flags & RPC::Flags::ReplyWithPayload && args->m_Payload != NULL)
{
// copy our data BACK
memcpy(args->m_Payload, ptr, args->m_PayloadSize);
ptr += args->m_PayloadSize;
}
// clean up our reply message's memory
delete[] frame->m_ReplyData;
// call complete, pop
m_Stack.Pop();
}
}
}
}
bool Host::Invoke(IPC::Message* msg)
{
// TODO: Unpack the invoker and interface name from the message data
const char* invokerName = NULL;
const char* interfaceName = NULL;
// find the interface
Interface* iface = GetInterface(interfaceName);
if (iface == NULL)
{
printf("RPC::Unable to find interface '%s'\n", interfaceName);
delete msg;
return true;
}
// find the invoker
Invoker* invoker = iface->GetInvoker(invokerName);
if (invoker == NULL)
{
printf("RPC::Unable to find invoker '%s' in interface '%s'\n", invokerName, interfaceName);
delete msg;
return true;
}
// get our frame from the top of the stack
Frame* frame = m_Stack.Top();
HELIUM_ASSERT(frame->m_Message == NULL);
frame->m_Message = msg;
// call the function
frame->m_MessageTaken = false;
invoker->Invoke(msg->GetData(), msg->GetSize());
HELIUM_ASSERT(frame->m_Message != NULL);
frame->m_Message = NULL;
Args* args = (Args*)msg->GetData();
if (args->m_Flags & RPC::Flags::NonBlocking)
{
if (!frame->m_MessageTaken)
{
delete msg;
}
return true; // async call, we are done
}
// our reply
IPC::Message* reply = NULL;
// if we have data, and a reference args or payload
if (msg->GetSize() > 0 && args->m_Flags & (RPC::Flags::ReplyWithArgs | RPC::Flags::ReplyWithPayload))
{
// total size of reply
uint32_t size = 0;
// size of args section
uint32_t argSize = invoker->GetArgsSize();
// size of payload section
uint32_t payload_size = msg->GetSize() - argSize;
// if we have a ref args
if (args->m_Flags & RPC::Flags::ReplyWithArgs)
{
// alloc for args block
size += argSize;
}
// if we have a ref payload
if (args->m_Flags & RPC::Flags::ReplyWithPayload)
{
// alloc for payload block
size += payload_size;
}
// create message
reply = Create(invoker, size, msg->GetTransaction());
// where to write
uint8_t* ptr = reply->GetData();
// if we have a ref args
if (args->m_Flags & RPC::Flags::ReplyWithArgs)
{
invoker->Swizzle( msg->GetData() );
// write to ptr
memcpy(ptr, msg->GetData(), argSize);
// incr ptr by amount written
ptr += argSize;
}
// if we have a ref payload
if (args->m_Flags & RPC::Flags::ReplyWithPayload)
{
// write to ptr
memcpy(ptr, msg->GetData() + argSize, payload_size);
// incr ptr by amount written
ptr += payload_size;
}
// assert we did not overrun message size
HELIUM_ASSERT((uint32_t)(ptr - reply->GetData()) == size);
}
else // no data, or no ref args or payload
{
// just create an empty reply, the other side is blocking
reply = Create(invoker, 0, msg->GetTransaction());
}
if (m_Connection->Send(reply)!= IPC::ConnectionStates::Active)
{
delete reply;
}
#ifdef RPC_DEBUG_MSG
printf("RPC::Put message id 0x%" HELIUM_PRINT_POINTER ", size %d, transaction %d\n", reply->GetID(), reply->GetSize(), reply->GetTransaction());
#endif
if (!frame->m_MessageTaken)
{
delete msg;
}
return true;
}
uint8_t* Host::TakeData()
{
Frame* frame = m_Stack.Top();
frame->m_MessageTaken = true;
return frame->m_Message->GetData();
}
bool Host::Connected()
{
if (m_Connection == NULL)
{
return false; // no connection to use
}
if (m_Connection->GetState()!=IPC::ConnectionStates::Active)
{
return false; // no connection to use
}
return true;
}
bool Host::Dispatch()
{
return Process(false);
}
bool Host::Process(bool wait)
{
bool result = true;
if (Connected())
{
if (m_ConnectionCount != m_Connection->GetConnectCount())
{
#ifdef RPC_DEBUG
printf("RPC::Connection cycled, resetting stack\n");
#endif
m_ConnectionCount = m_Connection->GetConnectCount();
m_Stack.Reset();
}
if (m_Connection->GetState() != IPC::ConnectionStates::Active)
{
result = false;
}
while (result)
{
IPC::Message* msg = NULL;
IPC::ConnectionState state = m_Connection->Receive(&msg, wait);
if (state != IPC::ConnectionStates::Active || msg == NULL)
{
result = false;
break;
}
#ifdef RPC_DEBUG_MSG
printf("RPC::Got message id 0x%" HELIUM_PRINT_POINTER ", size %d, transaction %d\n", msg->GetID(), msg->GetSize(), msg->GetTransaction());
#endif
bool is_reply = m_Connection->CreatedMessage(msg->GetTransaction());
if (is_reply && m_Stack.Size() > 0)
{
Frame* top = m_Stack.Top();
bool is_current = msg->GetTransaction() == top->m_ReplyTransaction;
if (is_current)
{
#ifdef RPC_DEBUG
printf("RPC::Got reply to transaction %d\n", msg->GetTransaction());
#endif
// subsume the message into the frame
top->m_Replied = true;
top->m_ReplyID = msg->GetID();
top->m_ReplyData = msg->TakeData(); // taking this will disconnect it from the message, making delete below *safe*
top->m_ReplySize = msg->GetSize();
// free msg
delete msg;
// we have our reply, break out of processing messages
break;
}
else
{
printf("RPC::Got reply to transaction %d, however its not a reply for the top of the stack (stack size: %d)\n", msg->GetTransaction(), m_Stack.Size());
}
}
else // else this is not a reply, meaning this is a new invocation
{
int32_t size HELIUM_ASSERT_ONLY = m_Stack.Size();
// allocate a frame for this local call
Frame* frame = m_Stack.Push();
frame->m_ReplyTransaction = msg->GetTransaction();
#ifdef RPC_DEBUG
printf("RPC::Pushing invocation transaction %d, stack size %d\n", frame->m_ReplyTransaction, m_Stack.Size());
#endif
// the one and only call to invoke, this expects our frame to be allocated
if (Invoke(msg))
{
#ifdef RPC_DEBUG
printf("RPC::Popping invocation transaction %d, stack size %d\n", frame->m_ReplyTransaction, m_Stack.Size());
#endif
// success, pop the call
m_Stack.Pop();
HELIUM_ASSERT(size == m_Stack.Size());
}
else
{
printf("RPC::Invocation failed, resetting stack\n");
m_Stack.Reset();
}
}
}
}
else
{
result = false;
}
return result;
}
using namespace Helium::RPC::Test;
Test::TestInterface g_TestInterface;
TestInterface::TestInterface()
: Interface ("Test")
{
Helium::Signature< TestArgs&>::Delegate delegate ( this, &TestInterface::Test );
AddInvoker( new InvokerTemplate<TestArgs> ( this, delegate ) );
}
void TestInterface::Test( TestArgs& args )
{
}
void RPC::Test::AddInterface( RPC::Host& host )
{
host.AddInterface( &g_TestInterface );
}