public void DestroyProxy(ClientProxy clientProxy)
        {
            var ns = new DistributedObjectInfo(clientProxy.Name, clientProxy.ServiceName);

            if (_proxies.TryRemove(ns, out var registeredProxyLazy))
            {
                ClientProxy registeredProxy = null;
                try
                {
                    registeredProxy = registeredProxyLazy.Value;
                    if (registeredProxy != null)
                    {
                        try
                        {
                            registeredProxy.DestroyLocally();
                        }
                        finally
                        {
                            registeredProxy.DestroyRemotely();
                        }
                    }
                }
                finally
                {
                    if (clientProxy != registeredProxy)
                    {
                        // The given proxy is stale and was already destroyed, but the caller
                        // may have allocated local resources in the context of this stale proxy
                        // instance after it was destroyed, so we have to cleanup it locally one
                        // more time to make sure there are no leaking local resources.
                        clientProxy.DestroyLocally();
                    }
                }
            }
        }
        private void InitializeWithRetry(ClientProxy clientProxy)
        {
            var  clientInvocationService = (ClientInvocationService)_client.GetInvocationService();
            long retryCountLimit         = clientInvocationService.InvocationRetryCount;

            for (var retryCount = 0; retryCount < retryCountLimit; retryCount++)
            {
                try
                {
                    InitializeOnServer(clientProxy);
                    return;
                }
                catch (Exception e)
                {
                    Logger.Warning("Got error initializing proxy", e);
                    if (IsRetryable(e))
                    {
                        try
                        {
                            Thread.Sleep(clientInvocationService.InvocationRetryWaitTime);
                        }
                        catch (ThreadInterruptedException)
                        {
                        }
                    }
                    else
                    {
                        throw;
                    }
                }
            }
        }
        private void InitializeOnServer(ClientProxy clientProxy)
        {
            var initializationTarget = FindNextAddressToCreateARequest();
            var invocationTarget     = initializationTarget;

            if (initializationTarget != null && _client.GetConnectionManager().GetConnection(initializationTarget) == null)
            {
                invocationTarget = _client.GetClientClusterService().GetOwnerConnectionAddress();
            }

            if (invocationTarget == null)
            {
                throw new IOException("Not able to setup owner connection!");
            }

            var request =
                ClientCreateProxyCodec.EncodeRequest(clientProxy.GetName(), clientProxy.GetServiceName(), initializationTarget);

            try
            {
                ThreadUtil.GetResult(_client.GetInvocationService().InvokeOnTarget(request, invocationTarget));
            }
            catch (Exception e)
            {
                throw ExceptionUtil.Rethrow(e);
            }
        }
        public ClientProxy GetProxy(string service, string id)
        {
            var         ns    = new ObjectNamespace(service, id);
            ClientProxy proxy = null;

            _proxies.TryGetValue(ns, out proxy);
            if (proxy != null)
            {
                return(proxy);
            }
            return(null);
        }
        public string AddDistributedObjectListener(IDistributedObjectListener listener)
        {
            var request = ClientAddDistributedObjectListenerCodec.EncodeRequest(false);
            var context = new ClientContext(_client.GetSerializationService(), _client.GetClientClusterService(),
                                            _client.GetClientPartitionService(), _client.GetInvocationService(), _client.GetClientExecutionService(),
                                            _client.GetListenerService(),
                                            this, _client.GetClientConfig());
            //EventHandler<PortableDistributedObjectEvent> eventHandler = new _EventHandler_211(this, listener);

            DistributedEventHandler eventHandler = delegate(IClientMessage message)
            {
                ClientAddDistributedObjectListenerCodec.AbstractEventHandler.Handle(message,
                                                                                    (name, serviceName, type) =>
                {
                    var ns            = new ObjectNamespace(serviceName, name);
                    ClientProxy proxy = null;
                    _proxies.TryGetValue(ns, out proxy);
                    if (proxy == null)
                    {
                        proxy = GetProxy(serviceName, name);
                    }
                    var _event = new DistributedObjectEvent(type, serviceName, proxy);
                    if (DistributedObjectEvent.EventType.Created.Equals(type))
                    {
                        listener.DistributedObjectCreated(_event);
                    }
                    else
                    {
                        if (DistributedObjectEvent.EventType.Destroyed.Equals(type))
                        {
                            listener.DistributedObjectDestroyed(_event);
                        }
                    }
                });
            };

            //PortableDistributedObjectEvent
            return(context.GetListenerService()
                   .StartListening(request, eventHandler,
                                   m => ClientAddDistributedObjectListenerCodec.DecodeResponse(m).response));
        }
        public ClientProxy GetOrCreateProxy <T>(string service, string id)
        {
            var         ns    = new ObjectNamespace(service, id);
            ClientProxy proxy = null;

            _proxies.TryGetValue(ns, out proxy);
            if (proxy != null)
            {
                return(proxy);
            }
            ClientProxyFactory factory = null;

            _proxyFactories.TryGetValue(service, out factory);
            if (factory == null)
            {
                throw new ArgumentException("No factory registered for service: " + service);
            }
            var clientProxy = factory(typeof(T), id);

            InitializeWithRetry(clientProxy);
            return(_proxies.GetOrAdd(ns, clientProxy));
        }
        private void Initialize(ClientProxy clientProxy)
        {
            var request = ClientCreateProxyCodec.EncodeRequest(clientProxy.Name, clientProxy.ServiceName);

            ThreadUtil.GetResult(_client.InvocationService.InvokeOnRandomTarget(request));
        }
 public void RemoveProxy(ClientProxy proxy)
 {
     proxyManager.RemoveProxy(proxy.GetServiceName(), proxy.GetName());
 }