The event arguments provided when a keep alive response arrives.
Наследование: System.EventArgs
Пример #1
0
 /// <summary>
 /// Raised when a keep alive response is returned from the server.
 /// </summary>
 static void Session_KeepAlive(Session session, KeepAliveEventArgs e)
 {
     Console.WriteLine("===>>> Session KeepAlive: {0} ServerTime: {1:HH:MM:ss}", e.CurrentState, e.CurrentTime.ToLocalTime());
 }
Пример #2
0
        /// <summary>
        /// Updates the status control when a keep alive event occurs.
        /// </summary>
        void StandardClient_KeepAlive(Session sender, KeepAliveEventArgs e)
        {
            if (InvokeRequired)
            {
                BeginInvoke(new KeepAliveEventHandler(StandardClient_KeepAlive), sender, e);
                return;
            }
            else if (!IsHandleCreated)
            {
                return;
            }

            if (sender != null && sender.Endpoint != null)
            {
                ServerUrlLB.Text = Utils.Format(
                    "{0} ({1}) {2}",
                    sender.Endpoint.EndpointUrl,
                    sender.Endpoint.SecurityMode,
                    (sender.EndpointConfiguration.UseBinaryEncoding)?"UABinary":"XML");
            }
            else
            {
                ServerUrlLB.Text = "None";
            }

            if (e != null && m_session != null)
            {
                if (ServiceResult.IsGood(e.Status))
                {
                    ServerStatusLB.Text = Utils.Format(
                        "Server Status: {0} {1:yyyy-MM-dd HH:mm:ss} {2}/{3}",
                        e.CurrentState,
                        e.CurrentTime.ToLocalTime(),
                        m_session.OutstandingRequestCount,
                        m_session.DefunctRequestCount);

                    ServerStatusLB.ForeColor = Color.Empty;
                    ServerStatusLB.Font = new Font(ServerStatusLB.Font, FontStyle.Regular);
                }
                else
                {
                    ServerStatusLB.Text = String.Format(
                        "{0} {1}/{2}", e.Status,
                        m_session.OutstandingRequestCount,
                        m_session.DefunctRequestCount);

                    ServerStatusLB.ForeColor = Color.Red;
                    ServerStatusLB.Font = new Font(ServerStatusLB.Font, FontStyle.Bold);

                    if (m_reconnectPeriod <= 0)
                    {
                        return;
                    }

                    if (m_reconnectHandler == null && m_reconnectPeriod > 0)
                    {
                        m_reconnectHandler = new SessionReconnectHandler();
                        m_reconnectHandler.BeginReconnect(m_session, m_reconnectPeriod * 1000, StandardClient_Server_ReconnectComplete);
                    }
                }
            }
        }
        /// <summary>
        /// Updates the status control when a keep alive event occurs.
        /// </summary>
        async void StandardClient_KeepAlive(Session sender, KeepAliveEventArgs e)
        {
            if (!Dispatcher.HasThreadAccess)
            {
                await Dispatcher.RunAsync(
                CoreDispatcherPriority.Normal,
                () =>
                {
                    StandardClient_KeepAlive( sender, e);
                });
                return;
            }

            if (sender != null && sender.Endpoint != null)
            {
                ServerUrlTB.Text = Utils.Format(
                    "{0} ({1}) {2}",
                    sender.Endpoint.EndpointUrl,
                    sender.Endpoint.SecurityMode,
                    (sender.EndpointConfiguration.UseBinaryEncoding) ? "UABinary" : "XML");
            }
            else
            {
                ServerUrlTB.Text = "None";
            }

            if (e != null && m_session != null)
            {
                SessionsCTRL.UpdateSessionNode(m_session);

                if (ServiceResult.IsGood(e.Status))
                {
                    ServerStatusTB.Text = Utils.Format(
                        "Server Status: {0} {1:yyyy-MM-dd HH:mm:ss} {2}/{3}",
                        e.CurrentState,
                        e.CurrentTime.ToLocalTime(),
                        m_session.OutstandingRequestCount,
                        m_session.DefunctRequestCount);
                    ServerStatusTB.Foreground = new SolidColorBrush(Colors.Black);
                    ServerStatusTB.FontWeight = FontWeights.Normal;
                }
                else
                {
                    ServerStatusTB.Text = String.Format(
                        "{0} {1}/{2}", e.Status,
                        m_session.OutstandingRequestCount,
                        m_session.DefunctRequestCount);
                    ServerStatusTB.Foreground = new SolidColorBrush(Colors.Red);
                    ServerStatusTB.FontWeight = FontWeights.Bold;
                }
            }
        }
Пример #4
0
        /// <summary>
        /// Reports keep alive responses from the server.
        /// </summary>
        void Session_Reconnect(Session session, KeepAliveEventArgs e)
        {
            m_keepAliveCount++;

            if (ServiceResult.IsBad(e.Status))
            {
                if (m_keepAliveCount > 2)
                {
                    m_error = e.Status;
                    m_errorEvent.Set();
                }
            }
        }
Пример #5
0
 /// <summary>
 /// Reports keep alive responses from the server.
 /// </summary>
 void Session_KeepAlive(Session session, KeepAliveEventArgs e)
 {
     if (ServiceResult.IsBad(e.Status))
     {
         m_error = e.Status;
         m_errorEvent.Set();
         return;
     }
     
     m_keepAliveCount++;
 }
Пример #6
0
        /// <summary>
        /// Handles a keep alive notification for a session by updating the status bar.
        /// </summary>
        private void Session_KeepAlive(Session session, KeepAliveEventArgs e)
        {
            if (this.InvokeRequired)
            {
                this.BeginInvoke(new KeepAliveEventHandler(Session_KeepAlive), session, e);
                return;
            }

            try
            {
                // check for events from discarded sessions.
                if (!Object.ReferenceEquals(session, m_session))
                {
                    return;
                }

                if (ServiceResult.IsBad(e.Status))
                {
                    if (m_reconnectHandler == null)
                    {
                        LastKeepAliveTimeLB.Text = e.Status.ToString();
                        LastKeepAliveTimeLB.ForeColor = Color.Red;

                        ConnectedLB.Text = "Reconnecting";

                        m_reconnectHandler = new SessionReconnectHandler();
                        m_reconnectHandler.BeginReconnect(m_session, 10000, Server_ReconnectComplete);
                    }

                    return;
                }

                LastKeepAliveTimeLB.Text = Utils.Format("{0:HH:mm:ss}", e.CurrentTime.ToLocalTime());
                LastKeepAliveTimeLB.ForeColor = Color.Empty;
            }
            catch (Exception exception)
            {
                MessageBox.Show(exception.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
Пример #7
0
        /// <summary>
        /// Called when a error occurs during a keep alive.
        /// </summary>
        protected virtual bool OnKeepAliveError(ServiceResult result)
        {
            long delta = 0;

            lock (m_eventLock)
            {   
                delta = DateTime.UtcNow.Ticks - m_lastKeepAliveTime.Ticks;       
            }

            Utils.Trace(
                "KEEP ALIVE LATE: {0}s, EndpointUrl={1}, RequestCount={3}/{2}", 
                ((double)delta)/TimeSpan.TicksPerSecond,
                this.Endpoint.EndpointUrl,
                this.OutstandingRequestCount,
                this.GoodPublishRequestCount);  
                        
            KeepAliveEventHandler callback = null;
            
            lock (m_eventLock)
            {
                callback = m_KeepAlive;
            }

            if (callback != null)
            {
                try
                {
                    KeepAliveEventArgs args = new KeepAliveEventArgs(result, ServerState.Unknown, DateTime.UtcNow);
                    callback(this, args);
                    return !args.CancelKeepAlive;
                }
                catch (Exception e)
                {
                    Utils.Trace(e, "Session: Unexpected error invoking KeepAliveCallback.");
                }
            }

            return true;
        }
Пример #8
0
        /// <summary>
        /// The session the keep alive handler.
        /// </summary>
        /// <param name="session">The session.</param>
        /// <param name="e">The <see cref="Opc.Ua.Client.KeepAliveEventArgs"/> instance containing the event data.</param>
        private void Session_KeepAlive(Session session, KeepAliveEventArgs e)
        {
            int missedKeepAlives = 0;

            lock (m_lock)
            {
                // check if the session is closed.
                if (!m_running || !Object.ReferenceEquals(m_session, session))
                {
                    return;
                }

                // check if everything is ok.
                if (ServiceResult.IsGood(e.Status))
                {
                    m_missedKeepAlives = 0;
                    m_lastKeepAliveTime = DateTime.UtcNow;
                    return;
                }
                
                // increment miss count.                    
                missedKeepAlives = ++m_missedKeepAlives;
            }

            // attempt to reconnect after two misses.
            if (missedKeepAlives == 2)
            {
                ThreadPool.QueueUserWorkItem(OnReconnectSession, session);
                Utils.Trace("Calling OnReconnectSession NOW.");
            }
        }
Пример #9
0
 /// <summary>
 /// Raised when a keep alive response is returned from the server.
 /// </summary>
 private void Session_KeepAlive(Session session, KeepAliveEventArgs e)
 {
     if (ServiceResult.IsBad(e.Status))
     {
         Report("KEEP ALIVE LATE: {0}", e.Status);
     }
 }
Пример #10
0
        /// <summary>
        /// Handles a keep alive event from a session.
        /// </summary>
        private void Session_KeepAlive(Session session, KeepAliveEventArgs e)
        {
            if (this.InvokeRequired)
            {
                this.BeginInvoke(new KeepAliveEventHandler(Session_KeepAlive), session, e);
                return;
            }

            try
            {
                // check for events from discarded sessions.
                if (!Object.ReferenceEquals(session, m_session))
                {
                    return;
                }

                // start reconnect sequence on communication error.
                if (ServiceResult.IsBad(e.Status))
                {
                    if (m_reconnectPeriod <= 0)
                    {
                        UpdateStatus(true, e.CurrentTime, "Communication Error ({0})", e.Status);
                        return;
                    }

                    UpdateStatus(true, e.CurrentTime, "Reconnecting in {0}s", m_reconnectPeriod);

                    if (m_reconnectHandler == null)
                    {
                        if (m_ReconnectStarting != null)
                        {
                            m_ReconnectStarting(this, e);
                        }

                        m_reconnectHandler = new SessionReconnectHandler();
                        m_reconnectHandler.BeginReconnect(m_session, m_reconnectPeriod * 1000, Server_ReconnectComplete);
                    }

                    return;
                }

                // update status.
                UpdateStatus(false, e.CurrentTime, "Connected [{0}]", session.Endpoint.EndpointUrl);
                                
                // raise any additional notifications.
                if (m_KeepAliveComplete != null)
                {
                    m_KeepAliveComplete(this, e);
                }
            }
            catch (Exception exception)
            {
                ClientUtils.HandleException(this.Text, exception);
            }
        }
Пример #11
0
        /// <summary>
        /// Updates the server listview item when a keep alive event occurs from the server.
        /// </summary>
        void StandardClient_KeepAlive(Session sender, KeepAliveEventArgs e)
        {
            if (InvokeRequired)
            {
                BeginInvoke(new KeepAliveEventHandler(StandardClient_KeepAlive), sender, e);
                return;
            }
            else if (!IsHandleCreated)
            {
                return;
            }

            if (sender != null)
            {
                ListViewItem[] lvis = ServerListView.Items.Find(sender.SessionId.ToString(), false);
                if (lvis.Length == 1)
                {
                    lvis[0].SubItems[(int)ServerColumnHeader.URL].Text = sender.Endpoint.EndpointUrl.ToString();
                    lvis[0].SubItems[(int)ServerColumnHeader.SecurityMode].Text = sender.Endpoint.SecurityMode.ToString();
                    lvis[0].SubItems[(int)ServerColumnHeader.SecurityPolicy].Text = SecurityPolicies.GetDisplayName(sender.Endpoint.SecurityPolicyUri);
                    lvis[0].SubItems[(int)ServerColumnHeader.Encoding].Text = (sender.InnerChannel.UseBinaryEncoding) ? "Binary" : "XML";
                    if (e != null)
                    {
                        if (ServiceResult.IsGood(e.Status))
                        {
                            lvis[0].SubItems[(int)ServerColumnHeader.KeepAlive].Text = Utils.Format("{0} {1:yyyy-MM-dd HH:mm:ss}", e.CurrentState, e.CurrentTime.ToLocalTime());
                        }
                        else
                        {
                            lvis[0].SubItems[(int)ServerColumnHeader.KeepAlive].Text = e.Status.SymbolicId;
                        }
                    }
                }
            }
            else
            {
                Utils.Trace("{0} sender is null", MethodBase.GetCurrentMethod());
            }
        }
Пример #12
0
        /// <summary>
        /// Handles a keep alive event from a session.
        /// </summary>
        private void Session_KeepAlive(Session session, KeepAliveEventArgs e)
        {
            try
            {
                // check for events from discarded sessions.
                if (!Object.ReferenceEquals(session, m_session))
                {
                    return;
                }

                // start reconnect sequence on communication error.
                if (ServiceResult.IsBad(e.Status))
                {
                    lock (m_lock)
                    {
                        if (m_reconnectPeriod <= 0)
                        {
                            return;
                        }

                        BeginReconnect();
                    }
                }
            }
            catch (Exception exception)
            {
                Utils.Trace(exception, "Unexpected error handling keep alive.");
            }
        }