internal IAsyncResult BeginConnect(IPAddress[] addresses, int port, AsyncCallback requestCallback, object state) { if (s_loggingEnabled) { Logging.Enter(Logging.Sockets, this, "BeginConnect", addresses); } if (CleanedUp) { throw new ObjectDisposedException(this.GetType().FullName); } if (addresses == null) { throw new ArgumentNullException("addresses"); } if (addresses.Length == 0) { throw new ArgumentException(SR.net_invalidAddressList, "addresses"); } if (!TcpValidationHelpers.ValidatePortNumber(port)) { throw new ArgumentOutOfRangeException("port"); } if (_addressFamily != AddressFamily.InterNetwork && _addressFamily != AddressFamily.InterNetworkV6) { throw new NotSupportedException(SR.net_invalidversion); } if (_isListening) { throw new InvalidOperationException(SR.net_sockets_mustnotlisten); } // Set up the result to capture the context. No need for a lock. MultipleAddressConnectAsyncResult result = new MultipleAddressConnectAsyncResult(addresses, port, this, state, requestCallback); result.StartPostingAsyncOp(false); if (DoMultipleAddressConnectCallback(PostOneBeginConnect(result), result)) { // If the call completes synchronously, invoke the callback from here. result.InvokeCallback(); } // Finished posting async op. Possibly will call callback. result.FinishPostingAsyncOp(ref Caches.ConnectClosureCache); if (s_loggingEnabled) { Logging.Exit(Logging.Sockets, this, "BeginConnect", result); } return result; }
internal IAsyncResult BeginConnect(string host, int port, AsyncCallback requestCallback, object state) { if (s_loggingEnabled) { Logging.Enter(Logging.Sockets, this, "BeginConnect", host); } if (CleanedUp) { throw new ObjectDisposedException(this.GetType().FullName); } if (host == null) { throw new ArgumentNullException("host"); } if (!TcpValidationHelpers.ValidatePortNumber(port)) { throw new ArgumentOutOfRangeException("port"); } if (_addressFamily != AddressFamily.InterNetwork && _addressFamily != AddressFamily.InterNetworkV6) { throw new NotSupportedException(SR.net_invalidversion); } if (_isListening) { throw new InvalidOperationException(SR.net_sockets_mustnotlisten); } // Here, want to flow the context. No need to lock. MultipleAddressConnectAsyncResult result = new MultipleAddressConnectAsyncResult(null, port, this, state, requestCallback); result.StartPostingAsyncOp(false); IAsyncResult dnsResult = DnsAPMExtensions.BeginGetHostAddresses(host, new AsyncCallback(DnsCallback), result); if (dnsResult.CompletedSynchronously) { if (DoDnsCallback(dnsResult, result)) { result.InvokeCallback(); } } // Done posting. result.FinishPostingAsyncOp(ref Caches.ConnectClosureCache); if (s_loggingEnabled) { Logging.Exit(Logging.Sockets, this, "BeginConnect", result); } return result; }
public IAsyncResult BeginConnect(string host, int port, AsyncCallback requestCallback, object state) { if (NetEventSource.IsEnabled) NetEventSource.Enter(this, host); if (CleanedUp) { throw new ObjectDisposedException(this.GetType().FullName); } if (host == null) { throw new ArgumentNullException(nameof(host)); } if (!TcpValidationHelpers.ValidatePortNumber(port)) { throw new ArgumentOutOfRangeException(nameof(port)); } if (_addressFamily != AddressFamily.InterNetwork && _addressFamily != AddressFamily.InterNetworkV6) { throw new NotSupportedException(SR.net_invalidversion); } if (_isListening) { throw new InvalidOperationException(SR.net_sockets_mustnotlisten); } IPAddress parsedAddress; if (IPAddress.TryParse(host, out parsedAddress)) { IAsyncResult r = BeginConnect(parsedAddress, port, requestCallback, state); if (NetEventSource.IsEnabled) NetEventSource.Exit(this, r); return r; } ThrowIfNotSupportsMultipleConnectAttempts(); // Here, want to flow the context. No need to lock. MultipleAddressConnectAsyncResult result = new MultipleAddressConnectAsyncResult(null, port, this, state, requestCallback); result.StartPostingAsyncOp(false); IAsyncResult dnsResult = DnsAPMExtensions.BeginGetHostAddresses(host, new AsyncCallback(DnsCallback), result); if (dnsResult.CompletedSynchronously) { if (DoDnsCallback(dnsResult, result)) { result.InvokeCallback(); } } // Done posting. result.FinishPostingAsyncOp(ref Caches.ConnectClosureCache); if (NetEventSource.IsEnabled) NetEventSource.Exit(this, result); return result; }
// This is like a regular async callback worker, except the result can be an exception. This is a useful pattern when // processing should continue whether or not an async step failed. private static void DoMultipleAddressConnectCallback(object result, MultipleAddressConnectAsyncResult context) { while (result != null) { Exception ex = result as Exception; if (ex == null) { try { context.socket.EndConnect((IAsyncResult) result); } catch (Exception exception) { if (exception is ThreadAbortException || exception is StackOverflowException || exception is OutOfMemoryException) throw; ex = exception; } catch { ex = new Exception(SR.GetString(SR.net_nonClsCompliantException)); } } if (ex == null) { context.InvokeCallback(); break; } else { if (++context.index >= context.addresses.Length) throw ex; context.lastException = ex; result = PostOneBeginConnect(context); } } }
public IAsyncResult BeginConnect(IPAddress[] addresses, int port, AsyncCallback requestCallback, object state) { if (s_LoggingEnabled) { Logging.Enter(Logging.Sockets, this, "BeginConnect", addresses); } if (this.CleanedUp) { throw new ObjectDisposedException(base.GetType().FullName); } if (addresses == null) { throw new ArgumentNullException("addresses"); } if (addresses.Length == 0) { throw new ArgumentException(SR.GetString("net_invalidAddressList"), "addresses"); } if (!ValidationHelper.ValidateTcpPort(port)) { throw new ArgumentOutOfRangeException("port"); } if ((this.addressFamily != System.Net.Sockets.AddressFamily.InterNetwork) && (this.addressFamily != System.Net.Sockets.AddressFamily.InterNetworkV6)) { throw new NotSupportedException(SR.GetString("net_invalidversion")); } if (this.isListening) { throw new InvalidOperationException(SR.GetString("net_sockets_mustnotlisten")); } MultipleAddressConnectAsyncResult context = new MultipleAddressConnectAsyncResult(addresses, port, this, state, requestCallback); context.StartPostingAsyncOp(false); if (DoMultipleAddressConnectCallback(PostOneBeginConnect(context), context)) { context.InvokeCallback(); } context.FinishPostingAsyncOp(ref this.Caches.ConnectClosureCache); if (s_LoggingEnabled) { Logging.Exit(Logging.Sockets, this, "BeginConnect", context); } return context; }
public IAsyncResult BeginConnect(string host, int port, AsyncCallback requestCallback, object state) { if (s_LoggingEnabled) { Logging.Enter(Logging.Sockets, this, "BeginConnect", host); } if (this.CleanedUp) { throw new ObjectDisposedException(base.GetType().FullName); } if (host == null) { throw new ArgumentNullException("host"); } if (!ValidationHelper.ValidateTcpPort(port)) { throw new ArgumentOutOfRangeException("port"); } if ((this.addressFamily != System.Net.Sockets.AddressFamily.InterNetwork) && (this.addressFamily != System.Net.Sockets.AddressFamily.InterNetworkV6)) { throw new NotSupportedException(SR.GetString("net_invalidversion")); } if (this.isListening) { throw new InvalidOperationException(SR.GetString("net_sockets_mustnotlisten")); } MultipleAddressConnectAsyncResult result = new MultipleAddressConnectAsyncResult(null, port, this, state, requestCallback); result.StartPostingAsyncOp(false); IAsyncResult result2 = Dns.UnsafeBeginGetHostAddresses(host, new AsyncCallback(Socket.DnsCallback), result); if (result2.CompletedSynchronously && DoDnsCallback(result2, result)) { result.InvokeCallback(); } result.FinishPostingAsyncOp(ref this.Caches.ConnectClosureCache); if (s_LoggingEnabled) { Logging.Exit(Logging.Sockets, this, "BeginConnect", result); } return result; }