Esempio n. 1
0
        public ISessionPrx Create(string name, Current current, CancellationToken cancel)
        {
            var         session = new Session(name);
            ISessionPrx proxy   = current.Adapter.AddWithUUID(session, ISessionPrx.Factory);

            // Remove endpoints to ensure that calls are collocated-only. This way, if we invoke on the proxy during
            // shutdown, the invocation fails immediately without attempting to establish any connection.
            ISessionPrx collocProxy = proxy.Clone(endpoints: Array.Empty <Endpoint>());

            // Never close this connection from the client and turn on heartbeats with a timeout of 30s
            // TODO:
            //          current.Connection!.IdleTimeout = TimeSpan.FromSeconds(30);
            // Removed for function, but this demo is somewhat pointless without this though
            current.Connection !.KeepAlive = true;
            current.Connection !.Closed   += (sender, args) =>
            {
                try
                {
                    collocProxy.Destroy();
                    Console.WriteLine("Cleaned up dead client.");
                }
                catch
                {
                    // The client already destroyed this session, or the server is shutting down
                }
            };
            return(proxy);
        }
Esempio n. 2
0
        public ISessionPrx Create(string name, Current current)
        {
            var         session = new Session(name);
            ISessionPrx proxy   = current.Adapter.AddWithUUID(session, ISessionPrx.Factory);

            // Remove endpoints to ensure that calls are collocated-only. This way, if we invoke on the proxy during
            // shutdown, the invocation fails immediately without attempting to establish any connection.
            ISessionPrx collocProxy = proxy.Clone(endpoints: Array.Empty <Endpoint>());

            // Never close this connection from the client and turn on heartbeats with a timeout of 30s
            current.Connection !.Acm     = new Acm(TimeSpan.FromSeconds(30), AcmClose.Off, AcmHeartbeat.Always);
            current.Connection !.Closed += (sender, args) =>
            {
                try
                {
                    collocProxy.Destroy();
                    Console.WriteLine("Cleaned up dead client.");
                }
                catch
                {
                    // The client already destroyed this session, or the server is shutting down
                }
            };
            return(proxy);
        }
Esempio n. 3
0
        ConnectImpl(ConnectStrategy factory)
        {
            Debug.Assert(!_destroy);
            new Thread(new ThreadStart(() =>
            {
                try
                {
                    lock (_mutex)
                    {
                        _communicator = new Communicator(
                            properties: _properties,
                            logger: _logger,
                            observer: _observer,
                            certificates: _certificates,
                            caCertificates: _caCertificates,
                            certificateValidationCallback: _certificateValidationCallback,
                            passwordCallback: _passwordCallback);
                    }
                }
                catch (System.Exception ex)
                {
                    lock (_mutex)
                    {
                        _destroy = true;
                    }
                    _callback.ConnectFailed(this, ex);
                    return;
                }

                if (_communicator.DefaultRouter == null)
                {
                    IRouterFinderPrx?finder = null;
                    try
                    {
                        finder = IRouterFinderPrx.Parse(_finderStr, _communicator);
                        _communicator.DefaultRouter = finder.GetRouter();
                    }
                    catch (CommunicatorDestroyedException ex)
                    {
                        _callback.ConnectFailed(this, ex);
                        return;
                    }
                    catch (System.Exception ex)
                    {
                        if (finder == null)
                        {
                            _callback.ConnectFailed(this, ex);
                            return;
                        }
                        else
                        {
                            //
                            // In case of error getting router identity from RouterFinder use default identity.
                            //
                            _communicator.DefaultRouter =
                                finder.Clone(new Identity("router", "Glacier2"), Ice.IRouterPrx.Factory);
                        }
                    }
                }

                try
                {
                    _callback.CreatedCommunicator(this);
                    Ice.IRouterPrx?defaultRouter = _communicator.DefaultRouter;
                    Debug.Assert(defaultRouter != null);
                    var routerPrx       = IRouterPrx.UncheckedCast(defaultRouter);
                    ISessionPrx session = factory(routerPrx);
                    Connected(routerPrx, session);
                }
                catch (System.Exception ex)
                {
                    _communicator.Destroy();
                    _callback.ConnectFailed(this, ex);
                }
            })).Start();
        }
Esempio n. 4
0
        Connected(IRouterPrx router, ISessionPrx session)
        {
            //
            // Remote invocation should be done without acquiring a mutex lock.
            //
            Debug.Assert(router != null);
            Debug.Assert(_communicator != null);
            Connection?conn       = router.GetCachedConnection();
            string     category   = router.GetCategoryForClient();
            int        acmTimeout = 0;

            try
            {
                acmTimeout = router.GetACMTimeout();
            }
            catch (OperationNotExistException)
            {
            }

            if (acmTimeout <= 0)
            {
                acmTimeout = (int)router.GetSessionTimeout();
            }

            //
            // We create the callback object adapter here because createObjectAdapter internally
            // makes synchronous RPCs to the router. We can't create the OA on-demand when the
            // client calls objectAdapter() or addWithUUID() because they can be called from the
            // GUI thread.
            //
            if (_useCallbacks)
            {
                Debug.Assert(_adapter == null);
                _adapter = _communicator.CreateObjectAdapterWithRouter(router);
                _adapter.Activate();
            }

            lock (_mutex)
            {
                _router = router;

                if (_destroy)
                {
                    //
                    // Run destroyInternal in a thread because it makes remote invocations.
                    //
                    var t = new Thread(new ThreadStart(DestroyInternal));
                    t.Start();
                    return;
                }

                //
                // Cache the category.
                //
                _category = category;

                //
                // Assign the session after _destroy is checked.
                //
                _session   = session;
                _connected = true;

                if (acmTimeout > 0)
                {
                    Connection?connection = _router.GetCachedConnection();
                    Debug.Assert(connection != null);
                    connection.SetACM(acmTimeout, null, ACMHeartbeat.HeartbeatAlways);
                    connection.SetCloseCallback(_ => Destroy());
                }
            }

            try
            {
                _callback.Connected(this);
            }
            catch (SessionNotExistException)
            {
                Destroy();
            }
        }
Esempio n. 5
0
        private void ConnectImpl(Func <IRouterPrx, ISessionPrx> factory)
        {
            Debug.Assert(!_destroy);
            new Thread(new ThreadStart(() =>
            {
                try
                {
                    lock (_mutex)
                    {
                        _communicator = new Communicator(_properties,
                                                         logger: _logger,
                                                         observer: _observer,
                                                         tlsClientOptions: _tlsClientOptions,
                                                         tlsServerOptions: _tlsServerOptions);
                    }
                }
                catch (Exception ex)
                {
                    lock (_mutex)
                    {
                        _destroy = true;
                    }
                    _callback.ConnectFailed(this, ex);
                    return;
                }

                if (_communicator.DefaultRouter == null)
                {
                    IRouterFinderPrx?finder = null;
                    try
                    {
                        finder = IRouterFinderPrx.Parse(_finderStr, _communicator);
                        _communicator.DefaultRouter = finder.GetRouter();
                    }
                    catch (CommunicatorDisposedException ex)
                    {
                        _callback.ConnectFailed(this, ex);
                        return;
                    }
                    catch (Exception ex)
                    {
                        if (finder == null)
                        {
                            _callback.ConnectFailed(this, ex);
                            return;
                        }
                        else
                        {
                            // In case of error getting router identity from RouterFinder use default identity.
                            _communicator.DefaultRouter =
                                finder.Clone(Ice.IRouterPrx.Factory, identity: new Identity("router", "Glacier2"));
                        }
                    }
                }

                try
                {
                    _callback.CreatedCommunicator(this);
                    IRouterPrx routerPrx = _communicator.DefaultRouter !.Clone(IRouterPrx.Factory);
                    ISessionPrx session  = factory(routerPrx);
                    Connected(routerPrx, session);
                }
                catch (Exception ex)
                {
                    _communicator.Dispose();
                    _callback.ConnectFailed(this, ex);
                }
            })).Start();
        }
Esempio n. 6
0
        private void Connected(IRouterPrx router, ISessionPrx session)
        {
            // Remote invocation should be done without acquiring a mutex lock.
            Debug.Assert(router != null);
            Debug.Assert(_communicator != null);

            // We create the callback object adapter here because createObjectAdapter internally makes synchronous
            // RPCs to the router. We can't create the OA on-demand when the client calls ObjectAdapter() or
            // AddWithUUID() because they can be called from the GUI thread.
            if (_useCallbacks)
            {
                Debug.Assert(_adapter == null);
                _adapter = _communicator.CreateObjectAdapterWithRouter(router);
                _adapter.Activate();
            }

            string category = router.GetCategoryForClient();

            lock (_mutex)
            {
                _router = router;

                if (_destroy)
                {
                    // Run destroyInternal in a thread because it makes remote invocations.
                    var t = new Thread(new ThreadStart(DestroyInternal));
                    t.Start();
                    return;
                }

                // Cache the category.
                _category = category;

                // Assign the session after _destroy is checked.
                _session   = session;
                _connected = true;
            }

            // When using Ice1, we need to figure out the router's acm/session timeout and configure the connection
            // accordingly. With Ice2, this is no longer necessary, the idle timeout is negotiated on connection
            // establishment.
            Connection connection = router.GetConnection();

            if (router.Protocol == Protocol.Ice1)
            {
                TimeSpan idleTimeout = TimeSpan.Zero;
                try
                {
                    idleTimeout = TimeSpan.FromSeconds(router.GetACMTimeout());
                }
                catch (OperationNotExistException)
                {
                }

                if (idleTimeout == TimeSpan.Zero)
                {
                    idleTimeout = TimeSpan.FromSeconds(router.GetSessionTimeout());
                }

                if (idleTimeout != TimeSpan.Zero)
                {
                    connection.IdleTimeout = idleTimeout;
                }
            }

            connection.Closed += (sender, args) => Destroy();

            try
            {
                _callback.Connected(this);
            }
            catch (SessionNotExistException)
            {
                Destroy();
            }
        }
Esempio n. 7
0
        private void Connected(IRouterPrx router, ISessionPrx session)
        {
            // Remote invocation should be done without acquiring a mutex lock.
            Debug.Assert(router != null);
            Debug.Assert(_communicator != null);
            Connection?conn       = router.GetCachedConnection();
            string     category   = router.GetCategoryForClient();
            TimeSpan   acmTimeout = TimeSpan.Zero;

            try
            {
                acmTimeout = TimeSpan.FromSeconds(router.GetACMTimeout());
            }
            catch (OperationNotExistException)
            {
            }

            if (acmTimeout == TimeSpan.Zero)
            {
                acmTimeout = TimeSpan.FromSeconds(router.GetSessionTimeout());
            }

            // We create the callback object adapter here because createObjectAdapter internally makes synchronous
            // RPCs to the router. We can't create the OA on-demand when the client calls ObjectAdapter() or
            // AddWithUUID() because they can be called from the GUI thread.
            if (_useCallbacks)
            {
                Debug.Assert(_adapter == null);
                _adapter = _communicator.CreateObjectAdapterWithRouter(router);
                _adapter.Activate();
            }

            lock (_mutex)
            {
                _router = router;

                if (_destroy)
                {
                    // Run destroyInternal in a thread because it makes remote invocations.
                    var t = new Thread(new ThreadStart(DestroyInternal));
                    t.Start();
                    return;
                }

                // Cache the category.
                _category = category;

                // Assign the session after _destroy is checked.
                _session   = session;
                _connected = true;

                if (acmTimeout != TimeSpan.Zero)
                {
                    Connection?connection = _router.GetCachedConnection();
                    Debug.Assert(connection != null);
                    connection.Acm     = new Acm(acmTimeout, connection.Acm.Close, AcmHeartbeat.Always);
                    connection.Closed += (sender, args) => Destroy();
                }
            }

            try
            {
                _callback.Connected(this);
            }
            catch (SessionNotExistException)
            {
                Destroy();
            }
        }
Esempio n. 8
0
        ConnectImpl(ConnectStrategy factory)
        {
            Debug.Assert(!_destroy);
            new Thread(new ThreadStart(() =>
            {
                try
                {
                    lock (_mutex)
                    {
                        _communicator = new Communicator(
                            properties: _properties,
                            compactIdResolver: _compactIdResolver,
                            logger: _logger,
                            observer: _observer,
                            threadStart: _threadStart,
                            threadStop: _threadStop,
                            typeIdNamespaces: _typeIdNamespaces);
                    }
                }
                catch (System.Exception ex)
                {
                    lock (_mutex)
                    {
                        _destroy = true;
                    }
                    _callback.connectFailed(this, ex);
                    return;
                }

                if (_communicator.GetDefaultRouter() == null)
                {
                    var finder = IRouterFinderPrx.Parse(_finderStr, _communicator);
                    try
                    {
                        _communicator.SetDefaultRouter(finder.GetRouter());
                    }
                    catch (CommunicatorDestroyedException ex)
                    {
                        _callback.connectFailed(this, ex);
                        return;
                    }
                    catch (System.Exception)
                    {
                        //
                        // In case of error getting router identity from RouterFinder use default identity.
                        //
                        _communicator.SetDefaultRouter(
                            Ice.IRouterPrx.UncheckedCast(finder.Clone(new Identity("router", "Glacier2"))));
                    }
                }

                try
                {
                    _callback.createdCommunicator(this);
                    Ice.IRouterPrx?defaultRouter = _communicator.GetDefaultRouter();
                    Debug.Assert(defaultRouter != null);
                    var routerPrx       = IRouterPrx.UncheckedCast(defaultRouter);
                    ISessionPrx session = factory(routerPrx);
                    Connected(routerPrx, session);
                }
                catch (System.Exception ex)
                {
                    _communicator.Destroy();
                    _callback.connectFailed(this, ex);
                }
            })).Start();
        }