Esempio n. 1
0
        public async Task <bool> ConnectAsync(IEnumerable <string> hostnames, int port)
        {
            foreach (var hostname in hostnames)
            {
                IPAddress[] ipAddresses;
                try
                {
                    ipAddresses = await Dns.GetHostAddressesAsync(hostname).ConfigureAwait(false);
                }
                catch (SocketException)
                {
                    // name couldn't be resolved
                    continue;
                }

                // need to try IP Addresses one at a time: https://github.com/dotnet/corefx/issues/5829
                foreach (var ipAddress in ipAddresses)
                {
                    Socket socket = null;
                    try
                    {
                        socket = new Socket(SocketType.Stream, ProtocolType.Tcp);
#if NETSTANDARD1_3
                        await socket.ConnectAsync(ipAddress, port).ConfigureAwait(false);
#else
                        await Task.Factory.FromAsync(socket.BeginConnect, socket.EndConnect, hostname, port, null).ConfigureAwait(false);
#endif
                    }
                    catch (SocketException)
                    {
                        Utility.Dispose(ref socket);
                        continue;
                    }

                    m_socket      = socket;
                    m_transmitter = new PacketTransmitter(m_socket);
                    m_state       = State.Connected;
                    return(true);
                }
            }

            return(false);
        }
Esempio n. 2
0
        private async Task <bool> OpenSocketAsync(IEnumerable <string> hostnames, int port, IOBehavior ioBehavior, CancellationToken cancellationToken)
        {
            foreach (var hostname in hostnames)
            {
                IPAddress[] ipAddresses;
                try
                {
#if NETSTANDARD1_3
                    // Dns.GetHostAddresses isn't available until netstandard 2.0: https://github.com/dotnet/corefx/pull/11950
                    ipAddresses = await Dns.GetHostAddressesAsync(hostname).ConfigureAwait(false);
#else
                    if (ioBehavior == IOBehavior.Asynchronous)
                    {
                        ipAddresses = await Dns.GetHostAddressesAsync(hostname).ConfigureAwait(false);
                    }
                    else
                    {
                        ipAddresses = Dns.GetHostAddresses(hostname);
                    }
#endif
                }
                catch (SocketException)
                {
                    // name couldn't be resolved
                    continue;
                }

                // need to try IP Addresses one at a time: https://github.com/dotnet/corefx/issues/5829
                foreach (var ipAddress in ipAddresses)
                {
                    Socket socket = null;
                    try
                    {
                        socket = new Socket(ipAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
                        using (cancellationToken.Register(() => socket.Dispose()))
                        {
                            try
                            {
                                if (ioBehavior == IOBehavior.Asynchronous)
                                {
#if NETSTANDARD1_3
                                    await socket.ConnectAsync(ipAddress, port).ConfigureAwait(false);
#else
                                    await Task.Factory.FromAsync(socket.BeginConnect, socket.EndConnect, ipAddress, port, null).ConfigureAwait(false);
#endif
                                }
                                else
                                {
#if NETSTANDARD1_3
                                    await socket.ConnectAsync(ipAddress, port).ConfigureAwait(false);
#else
                                    socket.Connect(ipAddress, port);
#endif
                                }
                            }
                            catch (ObjectDisposedException ex) when(cancellationToken.IsCancellationRequested)
                            {
                                throw new MySqlException("Connect Timeout expired.", ex);
                            }
                        }
                    }
                    catch (SocketException)
                    {
                        Utility.Dispose(ref socket);
                        continue;
                    }

                    m_socket      = socket;
                    m_transmitter = new PacketTransmitter(m_socket);
                    m_state       = State.Connected;
                    return(true);
                }
            }
            return(false);
        }