public override object GetProxyObject(IRpcChannel channel, Type[] interfaceTypes, int remoteInstanceId)
        {
            lock (_rpcObjects)
            {
                Purge();
                if (_rpcObjects.TryGetValue(RpcHandle.ComparisonHandle(remoteInstanceId), out var obj))
                {
                    if (((RpcObjectHandle)obj).Object.TryGetTarget(out var inst))
                    {
                        return(inst);
                    }
                }

                bool isDelegate = interfaceTypes.Length == 1 && interfaceTypes[0].IsSubclassOf(typeof(Delegate));

                var result = new RpcObjectHandle(null, pinned: /*isDelegate && !_clientRepository,*/ false, instanceId: remoteInstanceId);
                _rpcObjects.Add(result);

                var instance = isDelegate
                    ? ImplementDelegate(interfaceTypes[0], channel, remoteInstanceId, result.InstanceId)
                    : ImplementInterfaces(interfaceTypes, channel, remoteInstanceId, result.InstanceId);



                result.Object = new WeakReference <object>(instance);
                return(instance);
            }
        }
 public void RemoveInstance(int instanceId)
 {
     lock (_rpcObjects)
     {
         Purge();
         var ch       = RpcHandle.ComparisonHandle(instanceId);
         var toRemove = _rpcObjects.FirstOrDefault(o => !o.IsSingleton /*(_clientRepository || !o.IsPinned)*/ && o.Equals(ch));
         if (toRemove != null)
         {
             _rpcObjects.Remove(toRemove);
         }
     }
 }
Example #3
0
        public override object GetProxyObject(IRpcChannel channel, Type[] interfaceTypes, int remoteInstanceId)
        {
            lock (_rpcObjects)
            {
                Purge();
                if (_rpcObjects.TryGetValue(RpcHandle.ComparisonHandle(remoteInstanceId), out var obj))
                {
                    if (((RpcObjectHandle)obj).Object.TryGetTarget(out var inst))
                    {
                        return(inst);
                    }
                }

                bool isDelegate = interfaceTypes.Length == 1 && interfaceTypes[0].IsSubclassOf(typeof(Delegate));

                var result = new RpcObjectHandle(null, pinned: /*isDelegate && !_clientRepository,*/ false, instanceId: remoteInstanceId);
                _rpcObjects.Add(result);

                object instance;
                if (isDelegate)
                {
                    throw new NotSupportedException("We do not support delegates for now.");
                }
                else
                {
                    var typesHash = CreateTypesHash(interfaceTypes);
                    if (!_typeImplementations.TryGetValue(typesHash, out var implementationType))
                    {
                        throw new InvalidOperationException($"Implementation type for interfaces {string.Join(", ", interfaceTypes.Select(t => t.ToString()))} not found.");
                    }
                    var constructor = implementationType.GetConstructor(
                        BindingFlags.NonPublic | BindingFlags.Instance,
                        null,
                        new Type[] { typeof(IRpcChannel), typeof(int), typeof(int) },
                        null);
                    instance = constructor.Invoke(new object[] { channel, result.InstanceId, remoteInstanceId });
                }


                result.Object = new WeakReference <object>(instance);
                return(instance);
            }
        }
 public object GetInstance(int instanceId)
 {
     lock (_rpcObjects)
     {
         Purge();
         if (_rpcObjects.TryGetValue(RpcHandle.ComparisonHandle(instanceId), out var obj))
         {
             if (obj is RpcObjectHandle objectHandle)
             {
                 if (objectHandle.Object.TryGetTarget(out var instance))
                 {
                     return(instance);
                 }
             }
             else
             {
                 return(GetInstance(CreateObjectHandleFromTypeHandle((RpcObjectTypeHandle)obj).InstanceId));
             }
         }
     }
     return(null);
 }