-
Notifications
You must be signed in to change notification settings - Fork 127
/
Copy pathIUnknown.h
554 lines (471 loc) · 21.1 KB
/
IUnknown.h
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
/*
* If not stated otherwise in this file or this component's LICENSE file the
* following copyright and licenses apply:
*
* Copyright 2020 Metrological
*
* 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.
*/
#ifndef __COM_IUNKNOWN_H
#define __COM_IUNKNOWN_H
#include "Module.h"
#include "Ids.h"
#include "Administrator.h"
#include "Messages.h"
namespace Thunder {
namespace RPC {
class Communicator;
}
namespace ProxyStub {
// -------------------------------------------------------------------------------------------
// STUB
// -------------------------------------------------------------------------------------------
typedef void (*MethodHandler)(Core::ProxyType<Core::IPCChannel>& channel, Core::ProxyType<RPC::InvokeMessage>& message);
class EXTERNAL UnknownStub {
public:
UnknownStub(UnknownStub&&) = delete;
UnknownStub(const UnknownStub&) = delete;
UnknownStub& operator=(UnknownStub&&) = delete;
UnknownStub& operator=(const UnknownStub&) = delete;
UnknownStub() = default;
virtual ~UnknownStub() = default;
public:
inline uint16_t Length() const {
return (3);
}
virtual Core::IUnknown* Convert(void* incomingData) const {
return (reinterpret_cast<Core::IUnknown*>(incomingData));
}
virtual uint32_t InterfaceId() const {
return (Core::IUnknown::ID);
}
virtual void Handle(const uint16_t index, Core::ProxyType<Core::IPCChannel>& channel, Core::ProxyType<RPC::InvokeMessage>& message);
};
template <typename INTERFACE, MethodHandler METHODS[]>
class UnknownStubType : public UnknownStub {
public:
typedef INTERFACE* CLASS_INTERFACE;
public:
UnknownStubType(UnknownStubType<INTERFACE, METHODS>&&) = delete;
UnknownStubType(const UnknownStubType<INTERFACE, METHODS>&) = delete;
UnknownStubType<INTERFACE, METHODS>& operator=(UnknownStubType<INTERFACE, METHODS>&&) = delete;
UnknownStubType<INTERFACE, METHODS>& operator=(const UnknownStubType<INTERFACE, METHODS>&) = delete;
UnknownStubType()
{
_myHandlerCount = 0;
while (METHODS[_myHandlerCount] != nullptr) {
_myHandlerCount++;
}
}
~UnknownStubType() override = default;
public:
inline uint16_t Length() const
{
return (_myHandlerCount + UnknownStub::Length());
}
Core::IUnknown* Convert(void* incomingData) const override
{
return (reinterpret_cast<INTERFACE*>(incomingData));
}
uint32_t InterfaceId() const override
{
return (INTERFACE::ID);
}
void Handle(const uint16_t index, Core::ProxyType<Core::IPCChannel>& channel, Core::ProxyType<RPC::InvokeMessage>& message) override
{
uint16_t baseNumber(UnknownStub::Length());
if (index < baseNumber) {
UnknownStub::Handle(index, channel, message);
} else if ((index - baseNumber) < _myHandlerCount) {
MethodHandler handler(METHODS[index - baseNumber]);
ASSERT(handler != nullptr);
handler(channel, message);
}
}
private:
uint16_t _myHandlerCount;
};
// -------------------------------------------------------------------------------------------
// Proxy/Stub generation requirements:
// 1) Interfaces, used as retrieved in the stub code are called inbound. These proxies hold no reference on account of
// the callee. The initial AddRef on this proxy is an AddRef that can be cached.
// 2) Interfaces, returned in the proxy code are called outbound. Outbound proxies are referenced by definition, and thus
// should be released.
//
// Characteristics of proxies:
// Inbound proxies can cache a pending AddRef if required.
// Inbound and Outbound do require a remote Release on a transaition from 1 -> 0 AddRef.
// -------------------------------------------------------------------------------------------
class EXTERNAL UnknownProxy {
private:
enum mode : uint8_t {
CACHING_ADDREF = 0x01,
CACHING_RELEASE = 0x02
};
public:
UnknownProxy() = delete;
UnknownProxy(UnknownProxy&&) = delete;
UnknownProxy(const UnknownProxy&) = delete;
UnknownProxy& operator=(UnknownProxy&&) = delete;
UnknownProxy& operator=(const UnknownProxy&) = delete;
UnknownProxy(const Core::ProxyType<Core::IPCChannel>& channel, const Core::instance_id& implementation, const uint32_t interfaceId, const bool outbound, Core::IUnknown& parent, const char* name)
: _adminLock()
, _refCount(1)
, _mode(outbound ? 0 : CACHING_ADDREF)
, _interfaceId(interfaceId)
, _implementation(implementation)
, _parent(parent)
, _channel(channel)
, _remoteReferences(1)
, _name(name)
{
}
virtual ~UnknownProxy() = default;
public:
uint32_t ReferenceCount() const {
// Subtract 1, as that is for the holding in the administration system here of the created Proxies
return(_refCount - 1);
}
// -------------------------------------------------------------------------------------------------------------------------------
// Proxy/Stub (both) environment calls
// -------------------------------------------------------------------------------------------------------------------------------
void* Acquire(const bool outbound, const uint32_t id) {
void* result = nullptr;
_adminLock.Lock();
if (_refCount > 1) {
if (outbound == false) {
_mode |= CACHING_RELEASE;
}
else {
// This is an additional call to an interface for which we already have a Proxy issued.
// This is triggerd by the other side, offering us an interface. So once this proxy goes
// out of scope, we also need to "release" the AddRef associated with this on the other
// side..
_remoteReferences++;
}
// This will increment the refcount of this PS, on behalf of the user.
result = _parent.QueryInterface(id);
}
_adminLock.Unlock();
return(result);
}
uint32_t AddRef() const {
_adminLock.Lock();
_refCount++;
_adminLock.Unlock();
return (Core::ERROR_NONE);
}
uint32_t Release() const {
uint32_t result = Core::ERROR_NONE;
_adminLock.Lock();
ASSERT(_refCount > 0);
_refCount--;
if (_refCount > 1 ) { // note this proxy is also held in the administrator list for non happy day scenario's so we should already release with refcount one, the UnregisterProxy will remove it from the list
_adminLock.Unlock();
}
else if( _refCount == 0 ) {
_adminLock.Unlock();
result = Core::ERROR_DESTRUCTION_SUCCEEDED;
ASSERT(_channel.IsValid() == false);
}
else {
if ((_channel.IsValid() == true) && ((_mode & (CACHING_RELEASE|CACHING_ADDREF)) == 0)) {
// We have reached "0", signal the other side..
Core::ProxyType<RPC::InvokeMessage> message(RPC::Administrator::Instance().Message());
message->Parameters().Set(_implementation, _interfaceId, 1);
// Pass on the number of reference we need to lower, since it is indicated by the amount of times this proxy had to be created
message->Parameters().Writer().Number<uint32_t>(_remoteReferences);
// Just try the destruction for few Seconds...
result = _channel->Invoke(message, RPC::CommunicationTimeOut);
if (result != Core::ERROR_NONE) {
TRACE_L1("Could not remote release the Proxy.");
result |= COM_ERROR;
}
else {
// Pass the remote release return value through
result = message->Response().Reader().Number<uint32_t>();
}
}
_adminLock.Unlock();
// Remove our selves from the Administration, we are done..
if (RPC::Administrator::Instance().UnregisterUnknownProxy(*this) == true ) {
ASSERT(_refCount == 1);
_refCount = 0;
result = Core::ERROR_DESTRUCTION_SUCCEEDED;
}
}
return (result);
}
inline void* RemoteInterface(const uint32_t id) const
{
void* result = nullptr;
Core::ProxyType<RPC::InvokeMessage> message(RPC::Administrator::Instance().Message());
RPC::Data::Frame::Writer parameters(message->Parameters().Writer());
message->Parameters().Set(_implementation, _interfaceId, 2);
parameters.Number<uint32_t>(id);
_adminLock.Lock();
if ((_channel.IsValid() == false) || (_channel->Invoke(message, RPC::CommunicationTimeOut) != Core::ERROR_NONE)) {
_adminLock.Unlock();
}
else {
RPC::Data::Frame::Reader response(message->Response().Reader());
Core::instance_id impl = response.Number<Core::instance_id>();
_adminLock.Unlock();
// From what is returned, we need to create a proxy
RPC::Administrator::Instance().ProxyInstance(_channel, impl, true, id, result);
}
return (result);
}
inline Core::IUnknown* Parent()
{
return (&_parent);
}
void* Interface(const Core::instance_id& implementation, const uint32_t id) const
{
void* result = nullptr;
RPC::Administrator::Instance().ProxyInstance(_channel, implementation, true, id, result);
return (result);
}
inline uint32_t InterfaceId() const
{
return (_interfaceId);
}
inline const Core::instance_id& Implementation() const
{
return (_implementation);
}
inline uint32_t ChannelId() const
{
Core::SafeSyncType<Core::CriticalSection> lock(_adminLock);
return (_channel.IsValid() ? _channel->Id() : ~0);
}
// Required by proxystubs!
const Core::ProxyType<Core::IPCChannel>& Channel() const {
return (_channel);
}
// -------------------------------------------------------------------------------------------------------------------------------
// Proxy environment calls
// -------------------------------------------------------------------------------------------------------------------------------
inline Core::ProxyType<RPC::InvokeMessage> Message(const uint8_t methodId) const
{
Core::ProxyType<RPC::InvokeMessage> message(RPC::Administrator::Instance().Message());
message->Parameters().Set(_implementation, _interfaceId, methodId + 3);
return (message);
}
inline uint32_t Invoke(Core::ProxyType<RPC::InvokeMessage>& message, const uint32_t waitTime = RPC::CommunicationTimeOut) const
{
uint32_t result = Core::ERROR_UNAVAILABLE | COM_ERROR;
_adminLock.Lock();
Core::ProxyType<Core::IPCChannel> channel (_channel);
_adminLock.Unlock();
if (channel.IsValid() == true) {
result = channel->Invoke(message, waitTime);
if (result != Core::ERROR_NONE) {
result |= COM_ERROR;
// Oops something failed on the communication. Report it.
TRACE_L1("IPC method invocation failed for 0x%X, error: %d", message->Parameters().InterfaceId(), result);
}
}
return (result);
}
inline void Complete(RPC::Data::Setup& response)
{
uint32_t result = Release();
_adminLock.Lock();
if ((_mode & CACHING_ADDREF) != 0) {
_mode ^= CACHING_ADDREF;
if (_refCount > 1) {
response.Action(RPC::Data::Output::mode::CACHED_ADDREF);
}
}
else if ((_mode & CACHING_RELEASE) != 0) {
_mode ^= CACHING_RELEASE;
if (result == Core::ERROR_DESTRUCTION_SUCCEEDED) {
response.Action(RPC::Data::Output::mode::CACHED_RELEASE);
}
}
_adminLock.Unlock();
if (result == Core::ERROR_DESTRUCTION_SUCCEEDED) {
delete &_parent;
}
}
inline void Complete(RPC::Data::Output& response)
{
uint32_t result = Release();
_adminLock.Lock();
if ((_mode & CACHING_ADDREF) != 0) {
// We completed the first cycle. Clear Pending, if it was active..
_mode ^= CACHING_ADDREF;
if (_refCount > 1) {
response.AddImplementation(_implementation, _interfaceId, RPC::Data::Output::mode::CACHED_ADDREF);
}
}
else if ((_mode & CACHING_RELEASE) != 0) {
// We completed the current cycle. Clear the CACHING_RELEASE, if it was active..
_mode ^= CACHING_RELEASE;
if (result == Core::ERROR_DESTRUCTION_SUCCEEDED) {
response.AddImplementation(_implementation, _interfaceId, RPC::Data::Output::mode::CACHED_RELEASE);
}
}
_adminLock.Unlock();
if (result == Core::ERROR_DESTRUCTION_SUCCEEDED) {
delete &_parent;
}
}
inline uint32_t Complete(const Core::instance_id& impl, const uint32_t id, const RPC::Data::Output::mode how)
{
// This method is called from the stubs.
uint32_t result = Core::ERROR_NONE;
if (how == RPC::Data::Output::mode::CACHED_ADDREF) {
// Just AddRef this implementation
RPC::Administrator::Instance().AddRef(_channel, reinterpret_cast<void*>(impl), id);
}
else if (how == RPC::Data::Output::mode::CACHED_RELEASE) {
// Just Release this implementation
RPC::Administrator::Instance().Release(_channel, reinterpret_cast<void*>(impl), id, 1);
}
else {
ASSERT(!"Invalid caching data");
result = Core::ERROR_INVALID_RANGE;
}
return (result);
}
inline string Name() const {
return Core::ClassName(_name).Text();
}
private:
friend RPC::Administrator;
// -------------------------------------------------------------------------------------------------------------------------------
// Stub environment calls
// -------------------------------------------------------------------------------------------------------------------------------
// This method should only be called from the administrator from stub implementation methods
// It should be called through the Administrator::Release(ProxyStub*, Message::Response) !!
// Concurrent access trhough this code is prevented by the CriticalSection in the Administrator
template <typename ACTUAL_INTERFACE>
inline ACTUAL_INTERFACE* QueryInterface() const
{
return (reinterpret_cast<ACTUAL_INTERFACE*>(QueryInterface(ACTUAL_INTERFACE::ID)));
}
inline void* QueryInterface(const uint32_t id) const
{
ASSERT (_interfaceId == id);
return (_parent.QueryInterface(id));
}
// The RPC::Administrator uses this to identifiy to what link this
// proxy belongs. The LinkId is always called within the lock of the
// RPC::Administrator, and since it is onl used and called from there
// and the clearing of the _channel is also only called from there,
// Invalidate(), It is safe to use it on the _channel in an unlocked
// fashion!!
uint32_t Id() const;
void Invalidate() {
ASSERT(_refCount > 0);
_adminLock.Lock();
_channel.Release();
_adminLock.Unlock();
}
private:
mutable Core::CriticalSection _adminLock;
mutable uint32_t _refCount;
uint8_t _mode;
const uint32_t _interfaceId;
Core::instance_id _implementation;
Core::IUnknown& _parent;
mutable Core::ProxyType<Core::IPCChannel> _channel;
uint32_t _remoteReferences;
const char* _name;
};
template <typename INTERFACE>
class UnknownProxyType : public INTERFACE {
public:
using BaseClass = UnknownProxyType<INTERFACE>;
using IPCMessage = Core::ProxyType<RPC::InvokeMessage>;
public:
UnknownProxyType(UnknownProxyType<INTERFACE>&&) = delete;
UnknownProxyType(const UnknownProxyType<INTERFACE>&) = delete;
UnknownProxyType<INTERFACE>& operator=(UnknownProxyType<INTERFACE>&&) = delete;
UnknownProxyType<INTERFACE>& operator=(const UnknownProxyType<INTERFACE>&) = delete;
PUSH_WARNING(DISABLE_WARNING_THIS_IN_MEMBER_INITIALIZER_LIST)
UnknownProxyType(const Core::ProxyType<Core::IPCChannel>& channel, const Core::instance_id& implementation, const bool outbound)
: _unknown(channel, implementation, INTERFACE::ID, outbound, *this, typeid(INTERFACE).name())
{
}
POP_WARNING()
~UnknownProxyType() override = default;
public:
UnknownProxy* Administration()
{
return(&_unknown);
}
// -------------------------------------------------------------------------------------------------------------------------------
// Proxy environment calls
// -------------------------------------------------------------------------------------------------------------------------------
IPCMessage Message(const uint8_t methodId) const
{
return (_unknown.Message(methodId));
}
PUSH_WARNING(DISABLE_WARNING_OVERLOADED_VIRTUALS)
uint32_t Invoke(Core::ProxyType<RPC::InvokeMessage>& message, const uint32_t waitTime = RPC::CommunicationTimeOut) const
{
return (_unknown.Invoke(message, waitTime));
}
POP_WARNING()
void* Interface(const Core::instance_id& implementation, const uint32_t id) const
{
return (_unknown.Interface(implementation, id));
}
uint32_t Complete(const Core::instance_id& instance, const uint32_t id, const RPC::Data::Output::mode how)
{
return (_unknown.Complete(instance, id, how));
}
// Required by proxystubs!
const Core::ProxyType<Core::IPCChannel>& Channel() const {
return (_unknown.Channel());
}
// -------------------------------------------------------------------------------------------------------------------------------
// Applications calls to the Proxy
// -------------------------------------------------------------------------------------------------------------------------------
uint32_t AddRef() const override
{
return (_unknown.AddRef());
}
uint32_t Release() const override
{
uint32_t result = _unknown.Release();
if (result == Core::ERROR_DESTRUCTION_SUCCEEDED) {
delete (this);
}
return (result);
}
void* QueryInterface(const uint32_t interfaceNumber) override
{
void* result = nullptr;
if (interfaceNumber == INTERFACE::ID) {
// Just AddRef and return..
_unknown.AddRef();
result = static_cast<INTERFACE*>(this);
} else if (interfaceNumber == Core::IUnknown::ID) {
// Just AddRef and return..
_unknown.AddRef();
result = static_cast<Core::IUnknown*>(this);
} else {
result = _unknown.RemoteInterface(interfaceNumber);
}
return (result);
}
private:
UnknownProxy _unknown;
};
}
} // namespace ProxyStub
#endif // __COM_IUNKNOWN_H