Opc.Ua.Bindings.TcpClientChannel.OnScheduledHandshake C# (CSharp) Method

OnScheduledHandshake() private method

Called when it is time to do a handshake.
private OnScheduledHandshake ( object state ) : void
state object
return void
        private void OnScheduledHandshake(object state)
        {
            try
            {
                Utils.Trace("Channel {0}: Scheduled Handshake Starting: TokenId={1}", ChannelId, CurrentToken.TokenId);

                Task t;
                lock (DataLock)
                {
                    // check if renewing a token.
                    TcpChannelToken token = state as TcpChannelToken;

                    if (token == CurrentToken)
                    {
                        Utils.Trace("TCP CHANNEL {0}: Attempting Renew Token Now: TokenId={1}", ChannelId, token.TokenId);

                        // do nothing if not connected.
                        if (State != TcpChannelState.Open)
                        {
                            return;
                        }

                        // begin the operation.
                        m_handshakeOperation = BeginOperation(Int32.MaxValue, m_HandshakeComplete, token);

                        // send the request.
                        SendOpenSecureChannelRequest(true);
                        return;
                    }

                    // must be reconnecting - check if successfully reconnected.
                    if (!m_reconnecting)
                    {
                        return;
                    }

                    Utils.Trace("Channel {0}: Attempting Reconnect Now.", ChannelId);

                    // cancel any previous attempt.
                    if (m_handshakeOperation != null)
                    {
                        m_handshakeOperation.Fault(StatusCodes.BadTimeout);
                        m_handshakeOperation = null;
                    }

                    // close the socket and reconnect.
                    State = TcpChannelState.Closed;

                    if (Socket != null)
                    {
                        Utils.Trace("TCPCLIENTCHANNEL SOCKET CLOSED: {0:X8}, ChannelId={1}", Socket.Handle, ChannelId);
                        Socket.Close();
                        Socket = null;
                    }

                    // create an operation.
                    m_handshakeOperation = BeginOperation(Int32.MaxValue, m_HandshakeComplete, null);

                    State = TcpChannelState.Connecting;
                    Socket = new TcpMessageSocket(this, BufferManager, Quotas.MaxBufferSize);
                    t = Task.Run(async () =>
                    {
                        await Socket.BeginConnect(m_via, m_ConnectCallback, m_handshakeOperation);
                    });
                }
                t.Wait();
            }
            catch (Exception e)
            {
                Utils.Trace("Channel {0}: Reconnect Failed {1}.", ChannelId, e.Message);
                ForceReconnect(ServiceResult.Create(e, StatusCodes.BadUnexpectedError, "Unexpected error reconnecting or renewing a token."));
            }
        }