Exemplo n.º 1
0
        /// <summary>
        /// Reestablish connection to server.
        /// </summary>
        /// <remarks>
        /// This method checks if the session is valid. If not, a new logon in perfomed automatically.
        /// Handle the NewLogonNeeded event to provide credentials.
        /// </remarks>
        /// <returns>True, if reconnecting was successfull, otherwis false </returns>
        internal bool InternalReconnect()
        {
            // When the session isn´t valid, the server process must have been restarted
            if (!RemoteDispatcher.ExistSession(_sessionID))
            {
                AuthCredentials credentials     = null;
                var             performNewLogon = true;

                // If cached auto login credentials are present
                if (_autoLoginOnExpiredSession)
                {
                    credentials = _autoLoginCredentials;
                }
                else
                {
                    var newLogonNeededEventArgs = new NewLogonNeededEventArgs();
                    if (OnNewLogonNeeded(newLogonNeededEventArgs))
                    {
                        performNewLogon = !newLogonNeededEventArgs.Cancel;
                        credentials     = newLogonNeededEventArgs.Credentials;
                    }
                    else
                    {
                        performNewLogon = false;
                    }
                }

                if (performNewLogon)
                {
                    if (credentials == null)
                    {
                        credentials = new AuthCredentials();
                    }

                    credentials.Authenticate(_sessionID, RemoteDispatcher);
                    ReconnectRemoteEvents();

                    RemoteDispatcher.ReceiveClientHeartbeat(_sessionID);
                    return(true);
                }
            }
            else
            {
                RemoteDispatcher.ReceiveClientHeartbeat(_sessionID);
                return(true);
            }
            return(false);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Will be called from polling timer on every interval.
        /// </summary>
        /// <param name="state">State (not used)</param>
        internal void SendHeartbeat(object state)
        {
            if (_sendingHeartbeat)
            {
                // if polling timer interval is less than
                // channel timeout, skip sending a heartbeat
                return;
            }

            try
            {
                _sendingHeartbeat = true;
                try
                {
                    PrepareCallContext(false);
                    RemoteDispatcher.ReceiveClientHeartbeat(_sessionID);
                }
                finally
                {
                    _sendingHeartbeat = false;
                }
            }
            catch (Exception ex)
            {
                PollingEnabled = false;
                bool problemSolved = false;

                DisconnectedEventArgs e = new DisconnectedEventArgs()
                {
                    Exception  = ex,
                    RetryCount = 0,
                    Retry      = false
                };

                OnDisconnected(e);

                while (e.Retry)
                {
                    e.Retry = false;

                    Thread.Sleep(Convert.ToInt32(_pollingInterval.TotalMilliseconds));

                    try
                    {
                        problemSolved = InternalReconnect();
                    }
                    catch (Exception retryEx)
                    {
                        e.Exception = retryEx;
                        e.RetryCount++;
                        OnDisconnected(e);
                    }
                }

                if (problemSolved)
                {
                    PollingEnabled = true;
                    OnReconnected(EventArgs.Empty);
                }
                else
                {
                    // connection wasn't restored, make sure
                    // that the polling timer is stopped
                    PollingEnabled = false;
                }
            }
        }