/// <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> /// 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); } }
/// <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); } }
private void BeginReconnect() { if (m_subscription != null) { foreach (MonitoredItem monitoredItem in m_subscription.MonitoredItems) { Opc.Ua.Server.MonitoredItem localItem = (Opc.Ua.Server.MonitoredItem)monitoredItem.Handle; localItem.QueueValue(null, StatusCodes.BadDeviceFailure); } } if (m_reconnectHandler == null) { m_reconnectHandler = new SessionReconnectHandler(); m_reconnectHandler.BeginReconnect(m_session, m_reconnectPeriod * 1000, Server_ReconnectComplete); } m_session = null; m_subscription = null; }