public virtual async Task ConnectAsync(string host, int port) { AssertSocket(); IPAddress[] adds = await Dns.GetHostAddressesAsync(host).ConfigureAwait(false); IPAddress ep = TcpClientAdapterHelper.GetMatchingHost(adds, _sock.AddressFamily); if (ep == default(IPAddress)) { throw new ArgumentException($"No ip address could be resolved for {host}"); } await ConnectAsync(ep, port).ConfigureAwait(false); }
public virtual async Task ConnectAsync(string host, int port) { AssertSocket(); IPAddress[] adds = await Dns.GetHostAddressesAsync(host).ConfigureAwait(false); IPAddress ep = TcpClientAdapterHelper.GetMatchingHost(adds, _sock.AddressFamily); if (ep == default(IPAddress)) { throw new ArgumentException("No ip address could be resolved for " + host); } #if NET461 await Task.Run(() => _sock.Connect(ep, port)).ConfigureAwait(false); #else await _sock.ConnectAsync(ep, port).ConfigureAwait(false); #endif }
public SocketFrameHandler(AmqpTcpEndpoint endpoint, Func <AddressFamily, ITcpClient> socketFactory, TimeSpan connectionTimeout, TimeSpan readTimeout, TimeSpan writeTimeout) { Endpoint = endpoint; _frameHeaderBuffer = new byte[6]; var channel = Channel.CreateUnbounded <ReadOnlyMemory <byte> >( new UnboundedChannelOptions { AllowSynchronousContinuations = false, SingleReader = true, SingleWriter = false }); _channelReader = channel.Reader; _channelWriter = channel.Writer; // Resolve the hostname to know if it's even possible to even try IPv6 IPAddress[] adds = Dns.GetHostAddresses(endpoint.HostName); IPAddress ipv6 = TcpClientAdapterHelper.GetMatchingHost(adds, AddressFamily.InterNetworkV6); if (ipv6 == default(IPAddress)) { if (endpoint.AddressFamily == AddressFamily.InterNetworkV6) { throw new ConnectFailureException("Connection failed", new ArgumentException($"No IPv6 address could be resolved for {endpoint.HostName}")); } } else if (ShouldTryIPv6(endpoint)) { try { _socket = ConnectUsingIPv6(new IPEndPoint(ipv6, endpoint.Port), socketFactory, connectionTimeout); } catch (ConnectFailureException) { // We resolved to a ipv6 address and tried it but it still didn't connect, try IPv4 _socket = null; } } if (_socket == null) { IPAddress ipv4 = TcpClientAdapterHelper.GetMatchingHost(adds, AddressFamily.InterNetwork); if (ipv4 == default(IPAddress)) { throw new ConnectFailureException("Connection failed", new ArgumentException($"No ip address could be resolved for {endpoint.HostName}")); } _socket = ConnectUsingIPv4(new IPEndPoint(ipv4, endpoint.Port), socketFactory, connectionTimeout); } Stream netstream = _socket.GetStream(); netstream.ReadTimeout = (int)readTimeout.TotalMilliseconds; netstream.WriteTimeout = (int)writeTimeout.TotalMilliseconds; if (endpoint.Ssl.Enabled) { try { netstream = SslHelper.TcpUpgrade(netstream, endpoint.Ssl); } catch (Exception) { Close(); throw; } } _reader = new BufferedStream(netstream, _socket.Client.ReceiveBufferSize); _writer = new BufferedStream(netstream, _socket.Client.SendBufferSize); WriteTimeout = writeTimeout; _writerTask = Task.Run(WriteLoop, CancellationToken.None); }