/// <exception cref="System.IO.IOException"></exception> public virtual void Upgrade(ManagedHttpClientConnection conn, HttpHost host, HttpContext context) { HttpClientContext clientContext = ((HttpClientContext)HttpClientContext.Adapt(context )); Lookup <ConnectionSocketFactory> registry = GetSocketFactoryRegistry(clientContext ); ConnectionSocketFactory sf = registry.Lookup(host.GetSchemeName()); if (sf == null) { throw new UnsupportedSchemeException(host.GetSchemeName() + " protocol is not supported" ); } if (!(sf is LayeredConnectionSocketFactory)) { throw new UnsupportedSchemeException(host.GetSchemeName() + " protocol does not support connection upgrade" ); } LayeredConnectionSocketFactory lsf = (LayeredConnectionSocketFactory)sf; System.Net.Sockets.Socket sock = conn.GetSocket(); int port = this.schemePortResolver.Resolve(host); sock = lsf.CreateLayeredSocket(sock, host.GetHostName(), port, context); conn.Bind(sock); }
/// <exception cref="System.IO.IOException"></exception> public virtual void Connect(ManagedHttpClientConnection conn, HttpHost host, IPEndPoint localAddress, int connectTimeout, SocketConfig socketConfig, HttpContext context ) { Lookup <ConnectionSocketFactory> registry = GetSocketFactoryRegistry(context); ConnectionSocketFactory sf = registry.Lookup(host.GetSchemeName()); if (sf == null) { throw new UnsupportedSchemeException(host.GetSchemeName() + " protocol is not supported" ); } IPAddress[] addresses = this.dnsResolver.Resolve(host.GetHostName()); int port = this.schemePortResolver.Resolve(host); for (int i = 0; i < addresses.Length; i++) { IPAddress address = addresses[i]; bool last = i == addresses.Length - 1; System.Net.Sockets.Socket sock = sf.CreateSocket(context); sock.SetReuseAddress(socketConfig.IsSoReuseAddress()); conn.Bind(sock); IPEndPoint remoteAddress = new IPEndPoint(address, port); if (this.log.IsDebugEnabled()) { this.log.Debug("Connecting to " + remoteAddress); } try { sock.ReceiveTimeout = socketConfig.GetSoTimeout(); sock = sf.ConnectSocket(connectTimeout, sock, host, remoteAddress, localAddress, context); sock.NoDelay = socketConfig.IsTcpNoDelay(); sock.SetKeepAlive(socketConfig.IsSoKeepAlive()); int linger = socketConfig.GetSoLinger(); if (linger >= 0) { sock.SetSoLinger(linger > 0, linger); } conn.Bind(sock); return; } catch (SocketTimeoutException ex) { if (last) { throw new ConnectTimeoutException(ex, host, addresses); } } catch (ConnectException ex) { if (last) { string msg = ex.Message; if ("Connection timed out".Equals(msg)) { throw new ConnectTimeoutException(ex, host, addresses); } else { throw new HttpHostConnectException(ex, host, addresses); } } } if (this.log.IsDebugEnabled()) { this.log.Debug("Connect to " + remoteAddress + " timed out. " + "Connection will be retried using another IP address" ); } } }