BeginConnect() public method

Connects to an endpoint.
public BeginConnect ( Uri endpointUrl, EventHandler callback, object state ) : Task
endpointUrl System.Uri
callback EventHandler
state object
return Task
Esempio n. 1
0
        /// <summary>
        /// Called when it is time to do a handshake.
        /// </summary>
        private void OnScheduledHandshake(object state)
        {
            try
            {
                // Utils.Trace("Channel {0}: Scheduled Handshake Starting: TokenId={1}", ChannelId, CurrentToken.TokenId);

                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);
                    Socket.BeginConnect(m_via, m_ConnectCallback, m_handshakeOperation);
                }
            }
            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."));
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Creates a connection with the server.
        /// </summary>
        public IAsyncResult BeginConnect(Uri url, int timeout, AsyncCallback callback, object state)
        {
            if (url == null) throw new ArgumentNullException("url");
            if (timeout <= 0) throw new ArgumentException("Timeout must be greater than zero.", "timeout");

            lock (DataLock)
            {
                if (State != TcpChannelState.Closed)
                {
                    throw new InvalidOperationException("Channel is already connected.");
                }

                m_url = url;
                m_via = url;

                // check if configured to use a proxy.
                if (EndpointDescription != null && EndpointDescription.ProxyUrl != null)
                {
                    m_via = EndpointDescription.ProxyUrl;
                }

                // do not attempt reconnect on failure.
                m_waitBetweenReconnects = Timeout.Infinite;
                
                WriteOperation operation = BeginOperation(timeout, callback, state);
                m_handshakeOperation = operation;

                State = TcpChannelState.Connecting;
                Socket = new TcpMessageSocket(this, BufferManager, Quotas.MaxBufferSize);

                try
                {
                    Socket.BeginConnect(m_via, m_ConnectCallback, operation);
                }
                catch (SocketException e)
                {
                    Shutdown(StatusCodes.Bad);
                    throw e;
                }                

                return operation;
            }
        }