Esempio n. 1
0
        /// <summary>
        ///   Opens the connection
        /// </summary>
        /// <param name = "connectionString">The connection string used for authentication.</param>
        public void Open(string connectionString)
        {
            if (state == XmppConnectionState.Open)
            {
                throw new XmppException("Connection must be closed first.");
            }

            try
            {
                // Initialization
                Initialize();

                // Build the connection string
                this.connectionString = new XmppConnectionString(connectionString);
                userId = this.connectionString.UserId;

                // Connect to the server
                if (this.connectionString.UseHttpBinding)
                {
                    transport = new HttpTransport();
                }
                else
                {
                    transport = new TcpTransport();
                }

                transportMessageSubscription = transport
                    .OnMessageReceived
                    .Subscribe(message => OnTransportMessageReceived(message));

                transportStreamInitializedSubscription = transport
                    .OnXmppStreamInitialized
                    .Subscribe(message => OnTransportXmppStreamInitialized(message));

                transportStreamClosedSubscription = transport
                    .OnXmppStreamClosed
                    .Subscribe(message => OnTransportXmppStreamClosed(message));

                transport.Open(this.connectionString);

                // Initialize XMPP Stream
                InitializeXmppStream();

                // Wait until we receive the Stream features
                WaitForStreamFeatures();

                if (transport is ISecureTransport)
                {
                    if (this.connectionString.PortNumber != 443 &&
                        this.connectionString.PortNumber != 5223)
                    {
                        if (SupportsFeature(XmppStreamFeatures.SecureConnection))
                        {
                            ((ISecureTransport) transport).OpenSecureConnection();

                            // Wait until we receive the Stream features
                            WaitForStreamFeatures();
                        }
                    }
                }

                // Perform authentication
                bool authenticationDone = Authenticate();

                if (authenticationDone)
                {
                    // Resource Binding.
                    BindResource();

                    // Open the session
                    OpenSession();

                    // Update state
                    state = XmppConnectionState.Open;
                }
            }
            catch (Exception ex)
            {
                if (AuthenticationFailiure != null)
                {
                    AuthenticationFailiure(this, new XmppAuthenticationFailiureEventArgs(ex.ToString()));
                }

                Close();
            }
        }
Esempio n. 2
0
        /// <summary>
        ///   Closes the connection
        /// </summary>
        public void Close()
        {
            if (state != XmppConnectionState.Closed)
            {
                if (ConnectionClosing != null)
                {
                    ConnectionClosing(this, new EventArgs());
                }

                try
                {
                    state = XmppConnectionState.Closing;

                    if (transport != null)
                    {
                        transport.Close();
                    }
                }
                catch
                {
                }
                finally
                {
                    if (initializedStreamEvent != null)
                    {
                        initializedStreamEvent.Set();
                        initializedStreamEvent = null;
                    }

                    if (streamFeaturesEvent != null)
                    {
                        streamFeaturesEvent.Set();
                        streamFeaturesEvent = null;
                    }

                    if (bindResourceEvent != null)
                    {
                        bindResourceEvent.Set();
                        bindResourceEvent = null;
                    }

                    if (initializedStreamEvent != null)
                    {
                        initializedStreamEvent.Close();
                        initializedStreamEvent = null;
                    }

                    if (streamFeaturesEvent != null)
                    {
                        streamFeaturesEvent.Close();
                        streamFeaturesEvent = null;
                    }

                    if (bindResourceEvent != null)
                    {
                        bindResourceEvent.Close();
                        bindResourceEvent = null;
                    }

                    if (transportMessageSubscription != null)
                    {
                        transportMessageSubscription.Dispose();
                        transportMessageSubscription = null;
                    }

                    if (transportStreamInitializedSubscription != null)
                    {
                        transportStreamInitializedSubscription.Dispose();
                        transportStreamInitializedSubscription = null;
                    }

                    if (transportStreamClosedSubscription != null)
                    {
                        transportStreamClosedSubscription.Dispose();
                        transportStreamClosedSubscription = null;
                    }

                    if (transport != null)
                    {
                        transport = null;
                    }

                    streamFeatures   = streamFeatures & (~streamFeatures);
                    state            = XmppConnectionState.Closed;
                    connectionString = null;
                    userId           = null;
                }

                if (ConnectionClosed != null)
                {
                    ConnectionClosed(this, new EventArgs());
                }
            }
        }
Esempio n. 3
0
        /// <summary>
        ///   Closes the connection
        /// </summary>
        public void Close()
        {
            if (state != XmppConnectionState.Closed)
            {
                if (ConnectionClosing != null)
                {
                    ConnectionClosing(this, new EventArgs());
                }

                try
                {
                    state = XmppConnectionState.Closing;

                    if (transport != null)
                    {
                        transport.Close();
                    }
                }
                catch
                {
                }
                finally
                {
                    if (initializedStreamEvent != null)
                    {
                        initializedStreamEvent.Set();
                        initializedStreamEvent = null;
                    }

                    if (streamFeaturesEvent != null)
                    {
                        streamFeaturesEvent.Set();
                        streamFeaturesEvent = null;
                    }

                    if (bindResourceEvent != null)
                    {
                        bindResourceEvent.Set();
                        bindResourceEvent = null;
                    }

                    if (initializedStreamEvent != null)
                    {
                        initializedStreamEvent.Close();
                        initializedStreamEvent = null;
                    }

                    if (streamFeaturesEvent != null)
                    {
                        streamFeaturesEvent.Close();
                        streamFeaturesEvent = null;
                    }

                    if (bindResourceEvent != null)
                    {
                        bindResourceEvent.Close();
                        bindResourceEvent = null;
                    }

                    if (transportMessageSubscription != null)
                    {
                        transportMessageSubscription.Dispose();
                        transportMessageSubscription = null;
                    }

                    if (transportStreamInitializedSubscription != null)
                    {
                        transportStreamInitializedSubscription.Dispose();
                        transportStreamInitializedSubscription = null;
                    }

                    if (transportStreamClosedSubscription != null)
                    {
                        transportStreamClosedSubscription.Dispose();
                        transportStreamClosedSubscription = null;
                    }

                    if (transport != null)
                    {
                        transport = null;
                    }

                    streamFeatures = streamFeatures & (~streamFeatures);
                    state = XmppConnectionState.Closed;
                    connectionString = null;
                    userId = null;
                }

                if (ConnectionClosed != null)
                {
                    ConnectionClosed(this, new EventArgs());
                }
            }
        }
Esempio n. 4
0
        /// <summary>
        ///   Opens the connection
        /// </summary>
        /// <param name = "connectionString">The connection string used for authentication.</param>
        public void Open(string connectionString)
        {
            if (state == XmppConnectionState.Open)
            {
                throw new XmppException("Connection must be closed first.");
            }

            try
            {
                // Initialization
                Initialize();

                // Build the connection string
                this.connectionString = new XmppConnectionString(connectionString);
                userId = this.connectionString.UserId;

                // Connect to the server
                if (this.connectionString.UseHttpBinding)
                {
                    transport = new HttpTransport();
                }
                else
                {
                    transport = new TcpTransport();
                }

                transportMessageSubscription = transport
                                               .OnMessageReceived
                                               .Subscribe(message => OnTransportMessageReceived(message));

                transportStreamInitializedSubscription = transport
                                                         .OnXmppStreamInitialized
                                                         .Subscribe(message => OnTransportXmppStreamInitialized(message));

                transportStreamClosedSubscription = transport
                                                    .OnXmppStreamClosed
                                                    .Subscribe(message => OnTransportXmppStreamClosed(message));

                transport.Open(this.connectionString);

                // Initialize XMPP Stream
                InitializeXmppStream();

                // Wait until we receive the Stream features
                WaitForStreamFeatures();

                if (transport is ISecureTransport)
                {
                    if (this.connectionString.PortNumber != 443 &&
                        this.connectionString.PortNumber != 5223)
                    {
                        if (SupportsFeature(XmppStreamFeatures.SecureConnection))
                        {
                            ((ISecureTransport)transport).OpenSecureConnection();

                            // Wait until we receive the Stream features
                            WaitForStreamFeatures();
                        }
                    }
                }

                // Perform authentication
                bool authenticationDone = Authenticate();

                if (authenticationDone)
                {
                    // Resource Binding.
                    BindResource();

                    // Open the session
                    OpenSession();

                    // Update state
                    state = XmppConnectionState.Open;
                }
            }
            catch (Exception ex)
            {
                if (AuthenticationFailiure != null)
                {
                    AuthenticationFailiure(this, new XmppAuthenticationFailiureEventArgs(ex.ToString()));
                }

                Close();
            }
        }