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

            try
            {
                // Initialization
                this.Initialize();

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

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

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

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

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

                this.transport.Open(this.connectionString);

                // Initialize XMPP Stream
                this.InitializeXmppStream();

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

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

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

                // Perform authentication
                bool authenticationDone = this.Authenticate();

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

                    // Open the session
                    this.OpenSession();

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

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

                try
                {
                    this.state = XmppConnectionState.Closing;

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

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

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

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

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

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

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

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

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

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

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

                if (this.ConnectionClosed != null)
                {
                    this.ConnectionClosed(this, new EventArgs());
                }
            }
        }