예제 #1
0
        /// <summary>
        ///   Closes the connection
        /// </summary>
        public override void Close()
        {
            if (!IsDisposed)
            {
                try
                {
                    Send(EndStream);
                }
                    // TODO: empty try-catch bad, martial arts good
            // ReSharper disable EmptyGeneralCatchClause
                catch
            // ReSharper restore EmptyGeneralCatchClause
                {
                }
                finally
                {
                    if (tlsProceedEvent != null)
                    {
                        tlsProceedEvent.Set();
                        tlsProceedEvent = null;
                    }

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

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

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

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

                base.Close();
            }
        }
예제 #2
0
        /// <summary>
        ///   Opens the connection to the XMPP server.
        /// </summary>
        private void Connect()
        {
            try
            {
                if (ConnectionString.ResolveHostName)
                {
                    // ReSharper disable RedundantBaseQualifier
                    base.ResolveHostName();
                    // ReSharper restore RedundantBaseQualifier
                }

                var hostadd = Dns.GetHostEntry(HostName).AddressList[0];
                var hostEndPoint = new IPEndPoint(hostadd, ConnectionString.PortNumber);

                socket = new ProxySocket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

                if (ConnectionString.UseProxy)
                {
                    IPAddress proxyadd = Dns.GetHostEntry(ConnectionString.ProxyServer).AddressList[0];
                    var proxyEndPoint = new IPEndPoint(proxyadd, ConnectionString.ProxyPortNumber);

                    switch (ConnectionString.ProxyType)
                    {
                        case "SOCKS4":
                            socket.ProxyType = ProxyTypes.Socks4;
                            break;

                        case "SOCKS5":
                            socket.ProxyType = ProxyTypes.Socks5;
                            break;

                        default:
                            socket.ProxyType = ProxyTypes.None;
                            break;
                    }

                    socket.ProxyEndPoint = proxyEndPoint;
                    socket.ProxyUser = ConnectionString.ProxyUserName;

                    if (!String.IsNullOrWhiteSpace(ConnectionString.ProxyPassword))
                    {
                        socket.ProxyPass = ConnectionString.ProxyPassword;
                    }
                }

                // Disables the Nagle algorithm for send coalescing.
                socket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.NoDelay, 1);

                // Make the socket to connect to the Server
                socket.Connect(hostEndPoint);

                // Create the Stream Instance
                networkStream = new NetworkStream(socket, false);
            }
            catch (Exception ex)
            {
                throw new XmppException(
                    String.Format("Unable to connect to XMPP server {0}", ConnectionString.HostName), ex);
            }
        }