Ejemplo n.º 1
0
        private static Stream GetTcpStream(MySqlConnectionStringBuilder settings)
        {
            TcpClient client = new TcpClient(AddressFamily.InterNetwork);
            Task      task   = null;

            if (!settings.IsSshEnabled())
            {
                task = client.ConnectAsync(settings.Server, (int)settings.Port);
            }
            else
            {
                task = client.ConnectAsync(
                    settings.Server == "127.0.0.1" || settings.Server == "::1"
            ? "localhost"
            : settings.Server,
                    3306);
            }

            if (!task.Wait(((int)settings.ConnectionTimeout * 1000)))
            {
                throw new MySqlException(Resources.Timeout);
            }
            if (settings.Keepalive > 0)
            {
                SetKeepAlive(client.Client, settings.Keepalive);
            }

            return(client.GetStream());
        }
Ejemplo n.º 2
0
        private static Stream GetTcpStream(MySqlConnectionStringBuilder settings)
        {
            Task <IPAddress[]> dnsTask = Dns.GetHostAddressesAsync(settings.Server);

            dnsTask.Wait();
            if (dnsTask.Result == null || dnsTask.Result.Length == 0)
            {
                throw new ArgumentException(Resources.InvalidHostNameOrAddress);
            }
            IPAddress addr = dnsTask.Result.FirstOrDefault(c => c.AddressFamily == AddressFamily.InterNetwork);

            if (addr == null)
            {
                addr = dnsTask.Result[0];
            }
            TcpClient client = new TcpClient(addr.AddressFamily);
            Task      task   = null;

            if (!settings.IsSshEnabled())
            {
                task = client.ConnectAsync(settings.Server, (int)settings.Port);
            }
            else
            {
                task = client.ConnectAsync(
                    settings.Server == "127.0.0.1" || settings.Server == "::1"
            ? "localhost"
            : settings.Server,
                    3306);
            }

            if (!task.Wait(((int)settings.ConnectionTimeout * 1000)))
            {
                throw new MySqlException(Resources.Timeout);
            }
            if (settings.Keepalive > 0)
            {
                SetKeepAlive(client.Client, settings.Keepalive);
            }

            var result = client.GetStream();

            GC.SuppressFinalize(result);

            return(result);
        }