void OpenControlConnection() { Exception exception = null; Socket sock = null; foreach (IPAddress address in hostEntry.AddressList) { sock = new Socket(address.AddressFamily, SocketType.Stream, ProtocolType.Tcp); IPEndPoint remote = new IPEndPoint(address, requestUri.Port); if (!ServicePoint.CallEndPointDelegate(sock, remote)) { sock.Close(); sock = null; } else { try { sock.Connect(remote); localEndPoint = (IPEndPoint)sock.LocalEndPoint; break; } catch (SocketException exc) { exception = exc; sock.Close(); sock = null; } } } // Couldn't connect to any address if (sock == null) { throw new WebException("Unable to connect to remote server", exception, WebExceptionStatus.UnknownError, ftpResponse); } controlStream = new NetworkStream(sock); controlReader = new StreamReader(controlStream, Encoding.ASCII); State = RequestState.Authenticating; Authenticate(); FtpStatus status = SendCommand("OPTS", "utf8", "on"); if ((int)status.StatusCode < 200 || (int)status.StatusCode > 300) { dataEncoding = Encoding.Default; } else { dataEncoding = Encoding.UTF8; } status = SendCommand(WebRequestMethods.Ftp.PrintWorkingDirectory); initial_path = GetInitialPath(status); }
void OpenControlConnection() { Socket sock = null; foreach (IPAddress address in hostEntry.AddressList) { sock = new Socket(address.AddressFamily, SocketType.Stream, ProtocolType.Tcp); IPEndPoint remote = new IPEndPoint(address, requestUri.Port); if (!ServicePoint.CallEndPointDelegate(sock, remote)) { sock.Close(); sock = null; } else { try { sock.Connect(remote); localEndPoint = (IPEndPoint)sock.LocalEndPoint; break; } catch (SocketException) { sock.Close(); sock = null; } } } // Couldn't connect to any address if (sock == null) { throw new WebException("Unable to connect to remote server", null, WebExceptionStatus.UnknownError, ftpResponse); } controlStream = new NetworkStream(sock); controlReader = new StreamReader(controlStream, Encoding.ASCII); State = RequestState.Authenticating; Authenticate(); }
private void Connect(HttpWebRequest request) { lock (socketLock) { if (this.socket != null && this.socket.Connected && status == WebExceptionStatus.Success && CanReuse() && CompleteChunkedRead()) { reused = true; } else { reused = false; if (this.socket != null) { this.socket.Close(); this.socket = null; } chunkStream = null; IPHostEntry hostEntry = sPoint.HostEntry; if (hostEntry == null) { status = ((!sPoint.UsesProxy) ? WebExceptionStatus.NameResolutionFailure : WebExceptionStatus.ProxyNameResolutionFailure); } else { WebConnectionData data = Data; IPAddress[] addressList = hostEntry.AddressList; foreach (IPAddress iPAddress in addressList) { this.socket = new Socket(iPAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp); IPEndPoint iPEndPoint = new IPEndPoint(iPAddress, sPoint.Address.Port); this.socket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.Debug, (!sPoint.UseNagleAlgorithm) ? 1 : 0); this.socket.NoDelay = !sPoint.UseNagleAlgorithm; if (!sPoint.CallEndPointDelegate(this.socket, iPEndPoint)) { this.socket.Close(); this.socket = null; status = WebExceptionStatus.ConnectFailure; } else { try { if (!request.Aborted) { CheckUnityWebSecurity(request); this.socket.Connect(iPEndPoint, requireSocketPolicy: false); status = WebExceptionStatus.Success; } return; IL_01a1 :; } catch (ThreadAbortException) { Socket socket = this.socket; this.socket = null; socket?.Close(); return; IL_01c9 :; } catch (ObjectDisposedException) { return; IL_01d5 :; } catch (Exception ex3) { Socket socket2 = this.socket; this.socket = null; socket2?.Close(); if (!request.Aborted) { status = WebExceptionStatus.ConnectFailure; } connect_exception = ex3; } } } } } } }
void Connect(HttpWebRequest request) { lock (socketLock) { if (socket != null && socket.Connected && status == WebExceptionStatus.Success) { // Take the chunked stream to the expected state (State.None) if (CanReuse() && CompleteChunkedRead()) { reused = true; return; } } reused = false; if (socket != null) { socket.Close(); socket = null; } chunkStream = null; IPHostEntry hostEntry = sPoint.HostEntry; if (hostEntry == null) { #if MONOTOUCH if (start_wwan != null) { start_wwan.Invoke(null, new object [1] { sPoint.Address }); hostEntry = sPoint.HostEntry; } if (hostEntry == null) { #endif status = sPoint.UsesProxy ? WebExceptionStatus.ProxyNameResolutionFailure : WebExceptionStatus.NameResolutionFailure; return; #if MONOTOUCH } #endif } WebConnectionData data = Data; foreach (IPAddress address in hostEntry.AddressList) { socket = new Socket(address.AddressFamily, SocketType.Stream, ProtocolType.Tcp); IPEndPoint remote = new IPEndPoint(address, sPoint.Address.Port); #if NET_1_1 socket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.NoDelay, sPoint.UseNagleAlgorithm ? 0 : 1); #endif #if NET_2_0 socket.NoDelay = !sPoint.UseNagleAlgorithm; if (!sPoint.CallEndPointDelegate(socket, remote)) { socket.Close(); socket = null; status = WebExceptionStatus.ConnectFailure; } else { #endif try { if (request.Aborted) { return; } socket.Connect(remote); status = WebExceptionStatus.Success; break; } catch (ThreadAbortException) { // program exiting... Socket s = socket; socket = null; if (s != null) { s.Close(); } return; } catch (ObjectDisposedException exc) { // socket closed from another thread return; } catch (Exception exc) { Socket s = socket; socket = null; if (s != null) { s.Close(); } if (!request.Aborted) { status = WebExceptionStatus.ConnectFailure; } connect_exception = exc; } #if NET_2_0 } #endif } } }
void Connect() { lock (socketLock) { if (socket != null && socket.Connected && status == WebExceptionStatus.Success) { // Take the chunked stream to the expected state (State.None) if (CanReuse() && CompleteChunkedRead()) { reused = true; return; } } reused = false; if (socket != null) { socket.Close(); socket = null; } chunkStream = null; IPHostEntry hostEntry = sPoint.HostEntry; if (hostEntry == null) { status = sPoint.UsesProxy ? WebExceptionStatus.ProxyNameResolutionFailure : WebExceptionStatus.NameResolutionFailure; return; } foreach (IPAddress address in hostEntry.AddressList) { socket = new Socket(address.AddressFamily, SocketType.Stream, ProtocolType.Tcp); IPEndPoint remote = new IPEndPoint(address, sPoint.Address.Port); #if NET_2_0 if (!sPoint.CallEndPointDelegate(socket, remote)) { socket.Close(); socket = null; status = WebExceptionStatus.ConnectFailure; } else { #endif try { socket.Connect(remote); status = WebExceptionStatus.Success; break; } catch (SocketException) { // This might be null if the request is aborted if (socket != null) { socket.Close(); socket = null; status = WebExceptionStatus.ConnectFailure; } } #if NET_2_0 } #endif } } }
void Connect(HttpWebRequest request) { lock (socketLock) { if (socket != null && socket.Connected && status == WebExceptionStatus.Success) { // Take the chunked stream to the expected state (State.None) if (CanReuse() && CompleteChunkedRead()) { reused = true; return; } } reused = false; if (socket != null) { socket.Close(); socket = null; } chunkStream = null; IPHostEntry hostEntry = sPoint.HostEntry; if (hostEntry == null) { #if MONOTOUCH xamarin_start_wwan(sPoint.Address.ToString()); hostEntry = sPoint.HostEntry; if (hostEntry == null) { #endif status = sPoint.UsesProxy ? WebExceptionStatus.ProxyNameResolutionFailure : WebExceptionStatus.NameResolutionFailure; return; #if MONOTOUCH } #endif } //WebConnectionData data = Data; foreach (IPAddress address in hostEntry.AddressList) { try { socket = new Socket(address.AddressFamily, SocketType.Stream, ProtocolType.Tcp); } catch (Exception se) { // The Socket ctor can throw if we run out of FD's if (!request.Aborted) { status = WebExceptionStatus.ConnectFailure; } connect_exception = se; return; } IPEndPoint remote = new IPEndPoint(address, sPoint.Address.Port); socket.NoDelay = !sPoint.UseNagleAlgorithm; try { sPoint.KeepAliveSetup(socket); } catch { // Ignore. Not supported in all platforms. } if (!sPoint.CallEndPointDelegate(socket, remote)) { socket.Close(); socket = null; status = WebExceptionStatus.ConnectFailure; } else { try { if (request.Aborted) { return; } socket.Connect(remote); status = WebExceptionStatus.Success; break; } catch (ThreadAbortException) { // program exiting... Socket s = socket; socket = null; if (s != null) { s.Close(); } return; } catch (ObjectDisposedException) { // socket closed from another thread return; } catch (Exception exc) { Socket s = socket; socket = null; if (s != null) { s.Close(); } if (!request.Aborted) { status = WebExceptionStatus.ConnectFailure; } connect_exception = exc; } } } } }
async Task Connect(WebOperation operation, CancellationToken cancellationToken) { IPHostEntry hostEntry = ServicePoint.HostEntry; if (hostEntry == null || hostEntry.AddressList.Length == 0) { #if MONOTOUCH && !MONOTOUCH_TV && !MONOTOUCH_WATCH xamarin_start_wwan(ServicePoint.Address.ToString()); hostEntry = ServicePoint.HostEntry; if (hostEntry == null) { #endif throw GetException(ServicePoint.UsesProxy ? WebExceptionStatus.ProxyNameResolutionFailure : WebExceptionStatus.NameResolutionFailure, null); #if MONOTOUCH && !MONOTOUCH_TV && !MONOTOUCH_WATCH } #endif } Exception connectException = null; foreach (IPAddress address in hostEntry.AddressList) { operation.ThrowIfDisposed(cancellationToken); try { socket = new Socket(address.AddressFamily, SocketType.Stream, ProtocolType.Tcp); } catch (Exception se) { // The Socket ctor can throw if we run out of FD's throw GetException(WebExceptionStatus.ConnectFailure, se); } IPEndPoint remote = new IPEndPoint(address, ServicePoint.Address.Port); socket.NoDelay = !ServicePoint.UseNagleAlgorithm; try { ServicePoint.KeepAliveSetup(socket); } catch { // Ignore. Not supported in all platforms. } if (!ServicePoint.CallEndPointDelegate(socket, remote)) { Interlocked.Exchange(ref socket, null)?.Close(); continue; } else { try { operation.ThrowIfDisposed(cancellationToken); await socket.ConnectAsync(remote).ConfigureAwait(false); } catch (ObjectDisposedException) { throw; } catch (Exception exc) { Interlocked.Exchange(ref socket, null)?.Close(); // Something went wrong, but we might have multiple IP Addresses // and need to probe them all. connectException = GetException(WebExceptionStatus.ConnectFailure, exc); continue; } } if (socket != null) { return; } } if (connectException == null) { connectException = GetException(WebExceptionStatus.ConnectFailure, null); } throw connectException; }
async Task Connect(WebOperation operation, CancellationToken cancellationToken) { IPHostEntry hostEntry = ServicePoint.HostEntry; if (hostEntry == null || hostEntry.AddressList.Length == 0) { #if MONOTOUCH && !MONOTOUCH_TV && !MONOTOUCH_WATCH xamarin_start_wwan(ServicePoint.Address.ToString()); hostEntry = ServicePoint.HostEntry; if (hostEntry == null) { #endif throw GetException(ServicePoint.UsesProxy ? WebExceptionStatus.ProxyNameResolutionFailure : WebExceptionStatus.NameResolutionFailure, null); #if MONOTOUCH && !MONOTOUCH_TV && !MONOTOUCH_WATCH } #endif } Exception connectException = null; foreach (IPAddress address in hostEntry.AddressList) { operation.ThrowIfDisposed(cancellationToken); try { socket = new Socket(address.AddressFamily, SocketType.Stream, ProtocolType.Tcp); } catch (Exception se) { // The Socket ctor can throw if we run out of FD's throw GetException(WebExceptionStatus.ConnectFailure, se); } IPEndPoint remote = new IPEndPoint(address, ServicePoint.Address.Port); socket.NoDelay = !ServicePoint.UseNagleAlgorithm; try { ServicePoint.KeepAliveSetup(socket); } catch { // Ignore. Not supported in all platforms. } if (!ServicePoint.CallEndPointDelegate(socket, remote)) { Interlocked.Exchange(ref socket, null)?.Close(); continue; } else { try { operation.ThrowIfDisposed(cancellationToken); /* * Socket.Tasks.cs from CoreFX introduces a new internal * BeginConnect(EndPoint) overload, which will replace * the one we're using from SocketTaskExtensions.cs. * * Our implementation of Socket.BeginConnect() does not * invoke the callback when the request failed synchronously. * * Explicitly use our implementation from SocketTaskExtensions.cs here. */ await Task.Factory.FromAsync( (targetEndPoint, callback, state) => ((Socket)state).BeginConnect (targetEndPoint, callback, state), asyncResult => ((Socket)asyncResult.AsyncState).EndConnect(asyncResult), remote, socket).ConfigureAwait(false); } catch (ObjectDisposedException) { throw; } catch (Exception exc) { Interlocked.Exchange(ref socket, null)?.Close(); // Something went wrong, but we might have multiple IP Addresses // and need to probe them all. connectException = GetException(WebExceptionStatus.ConnectFailure, exc); continue; } } if (socket != null) { return; } } if (connectException == null) { connectException = GetException(WebExceptionStatus.ConnectFailure, null); } throw connectException; }