/// <summary> /// Opens the channel before sending the request. /// </summary> /// <param name="connection">A reverse connection, null otherwise.</param> private void CreateChannel(ITransportWaitingConnection connection = null) { IMessageSocket socket = null; if (connection != null) { socket = connection.Handle as IMessageSocket; if (socket == null) { throw new ArgumentException("Connection Handle is not of type IMessageSocket."); } } // create the channel. m_channel = new UaSCUaBinaryClientChannel( Guid.NewGuid().ToString(), m_bufferManager, m_messageSocketFactory, m_quotas, m_settings.ClientCertificate, m_settings.ClientCertificateChain, m_settings.ServerCertificate, m_settings.Description); // use socket for reverse connections, ignore otherwise if (socket != null) { m_channel.Socket = socket; m_channel.Socket.ChangeSink(m_channel); m_channel.ReverseSocket = true; } }
/// <summary> /// Initializes a secure channel with the endpoint identified by the connection. /// </summary> /// <param name="connection">The connection to use.</param> /// <param name="settings">The settings to use when creating the channel.</param> /// <exception cref="ServiceResultException">Thrown if any communication error occurs.</exception> public void Initialize( ITransportWaitingConnection connection, TransportChannelSettings settings) { SaveSettings(connection.EndpointUrl, settings); var socket = connection.Handle as IMessageSocket; if (socket == null) { throw new ArgumentException("Connection Handle is not of type IMessageSocket."); } m_channel = new UaSCUaBinaryClientChannel( Guid.NewGuid().ToString(), m_bufferManager, m_messageSocketFactory, m_quotas, m_settings.ClientCertificate, m_settings.ClientCertificateChain, m_settings.ServerCertificate, m_settings.Description); m_channel.Socket = socket; m_channel.Socket.ChangeSink(m_channel); }
/// <summary> /// Completes an asynchronous operation to send a request over the secure channel. /// </summary> /// <param name="result">The result returned from the BeginSendRequest call.</param> /// <returns></returns> /// <exception cref="ServiceResultException">Thrown if any communication error occurs.</exception> /// <seealso cref="SendRequest"/> public IServiceResponse EndSendRequest(IAsyncResult result) { UaSCUaBinaryClientChannel channel = m_channel; if (channel == null) { throw ServiceResultException.Create(StatusCodes.BadSecureChannelClosed, "Channel has been closed."); } return(channel.EndSendRequest(result)); }
/// <summary> /// Opens the channel before sending the request. /// </summary> private void OpenOnDemand() { // create the channel. m_channel = new UaSCUaBinaryClientChannel( Guid.NewGuid().ToString(), m_bufferManager, m_messageSocketFactory, m_quotas, m_settings.ClientCertificate, m_settings.ServerCertificate, m_settings.Description); }
/// <summary> /// Initializes a secure channel with the endpoint identified by the URL. /// </summary> /// <param name="url">The URL for the endpoint.</param> /// <param name="settings">The settings to use when creating the channel.</param> /// <exception cref="ServiceResultException">Thrown if any communication error occurs.</exception> public void Initialize( Uri url, TransportChannelSettings settings) { SaveSettings(url, settings); m_channel = new UaSCUaBinaryClientChannel( Guid.NewGuid().ToString(), m_bufferManager, m_messageSocketFactory, m_quotas, m_settings.ClientCertificate, m_settings.ClientCertificateChain, m_settings.ServerCertificate, m_settings.Description); }
/// <summary> /// Begins an asynchronous operation to open a secure channel with the endpoint identified by the URL. /// </summary> /// <param name="callback">The callback to call when the operation completes.</param> /// <param name="callbackData">The callback data to return with the callback.</param> /// <returns> /// The result which must be passed to the EndOpen method. /// </returns> /// <exception cref="ServiceResultException">Thrown if any communication error occurs.</exception> /// <seealso cref="Open"/> public IAsyncResult BeginOpen(AsyncCallback callback, object callbackData) { lock (m_lock) { // create the channel. m_channel = new UaSCUaBinaryClientChannel( Guid.NewGuid().ToString(), m_bufferManager, m_messageSocketFactory, m_quotas, m_settings.ClientCertificate, m_settings.ServerCertificate, m_settings.Description); // begin connect operation. return(m_channel.BeginConnect(this.m_url, m_operationTimeout, callback, callbackData)); } }
/// <summary> /// Begins an asynchronous operation to send a request over the secure channel. /// </summary> /// <param name="request">The request to send.</param> /// <param name="callback">The callback to call when the operation completes.</param> /// <param name="callbackData">The callback data to return with the callback.</param> /// <returns> /// The result which must be passed to the EndSendRequest method. /// </returns> /// <exception cref="ServiceResultException">Thrown if any communication error occurs.</exception> /// <seealso cref="SendRequest"/> public IAsyncResult BeginSendRequest(IServiceRequest request, AsyncCallback callback, object callbackData) { UaSCUaBinaryClientChannel channel = m_channel; if (channel == null) { lock (m_lock) { if (m_channel == null) { OpenOnDemand(); } channel = m_channel; } } return(channel.BeginSendRequest(request, m_operationTimeout, callback, callbackData)); }
/// <summary> /// Closes any existing secure channel and opens a new one. /// </summary> /// <param name="connection">A reverse connection, null otherwise.</param> /// <exception cref="ServiceResultException">Thrown if any communication error occurs.</exception> /// <remarks> /// Calling this method will cause outstanding requests over the current secure channel to fail. /// </remarks> public void Reconnect(ITransportWaitingConnection connection) { Utils.Trace("TransportChannel RECONNECT: Reconnecting to {0}.", m_url); lock (m_lock) { // the new channel must be created first because WinSock will reuse sockets and this // can result in messages sent over the old socket arriving as messages on the new socket. // if this happens the new channel is shutdown because of a security violation. UaSCUaBinaryClientChannel channel = m_channel; m_channel = null; try { // reconnect. CreateChannel(connection); // begin connect operation. IAsyncResult result = m_channel.BeginConnect(m_url, m_operationTimeout, null, null); m_channel.EndConnect(result); } finally { // close existing channel. if (channel != null) { try { channel.Close(1000); } catch (Exception e) { // do nothing. Utils.Trace(e, "Ignoring exception while closing transport channel during Reconnect."); } finally { channel.Dispose(); } } } } }