public static bool ConnectAsync(SocketType socketType, ProtocolType protocolType, SocketAsyncEventArgs e) { bool retval; // Throw if multiple buffers specified. if (e.m_BufferList != null) { throw new ArgumentException(SR.GetString(SR.net_multibuffernotsupported), "BufferList"); } // Throw if RemoteEndPoint is null. if (e.RemoteEndPoint == null) { throw new ArgumentNullException("remoteEP"); } EndPoint endPointSnapshot = e.RemoteEndPoint; DnsEndPoint dnsEP = endPointSnapshot as DnsEndPoint; if (dnsEP != null) { Socket attemptSocket = null; MultipleConnectAsync multipleConnectAsync = null; if (dnsEP.AddressFamily == AddressFamily.Unspecified) { multipleConnectAsync = new MultipleSocketMultipleConnectAsync(socketType, protocolType); } else { attemptSocket = new Socket(dnsEP.AddressFamily, socketType, protocolType); multipleConnectAsync = new SingleSocketMultipleConnectAsync(attemptSocket, false); } e.StartOperationCommon(attemptSocket); e.StartOperationWrapperConnect(multipleConnectAsync); retval = multipleConnectAsync.StartConnectAsync(e, dnsEP); } else { Socket attemptSocket = new Socket(endPointSnapshot.AddressFamily, socketType, protocolType); retval = attemptSocket.ConnectAsync(e); } return(retval); }
public static bool ConnectAsync(SocketType socketType, ProtocolType protocolType, SocketAsyncEventArgs e) { bool retval; if (s_loggingEnabled) { Logging.Enter(Logging.Sockets, null, "ConnectAsync", ""); } if (e == null) { throw new ArgumentNullException("e"); } if (e._bufferList != null) { throw new ArgumentException(SR.net_multibuffernotsupported, "BufferList"); } if (e.RemoteEndPoint == null) { throw new ArgumentNullException("remoteEP"); } EndPoint endPointSnapshot = e.RemoteEndPoint; DnsEndPoint dnsEP = endPointSnapshot as DnsEndPoint; if (dnsEP != null) { Socket attemptSocket = null; MultipleConnectAsync multipleConnectAsync = null; if (dnsEP.AddressFamily == AddressFamily.Unspecified) { // Disable CS0162 and CS0429: Unreachable code detected // // SuportsMultipleConnectAttempts is a constant; when false, the following lines will trigger CS0162 or CS0429. #pragma warning disable 162, 429 multipleConnectAsync = SocketPal.SupportsMultipleConnectAttempts ? (MultipleConnectAsync)(new DualSocketMultipleConnectAsync(socketType, protocolType)) : (MultipleConnectAsync)(new MultipleSocketMultipleConnectAsync(socketType, protocolType)); #pragma warning restore } else { attemptSocket = new Socket(dnsEP.AddressFamily, socketType, protocolType); multipleConnectAsync = new SingleSocketMultipleConnectAsync(attemptSocket, false); } e.StartOperationCommon(attemptSocket); e.StartOperationWrapperConnect(multipleConnectAsync); retval = multipleConnectAsync.StartConnectAsync(e, dnsEP); } else { Socket attemptSocket = new Socket(endPointSnapshot.AddressFamily, socketType, protocolType); retval = attemptSocket.ConnectAsync(e); } if (s_loggingEnabled) { Logging.Exit(Logging.Sockets, null, "ConnectAsync", retval); } return retval; }
public bool ConnectAsync(SocketAsyncEventArgs e) { bool retval; if (s_loggingEnabled) { Logging.Enter(Logging.Sockets, this, "ConnectAsync", ""); } if (CleanedUp) { throw new ObjectDisposedException(GetType().FullName); } if (e == null) { throw new ArgumentNullException("e"); } if (e._bufferList != null) { throw new ArgumentException(SR.net_multibuffernotsupported, "BufferList"); } if (e.RemoteEndPoint == null) { throw new ArgumentNullException("remoteEP"); } if (_isListening) { throw new InvalidOperationException(SR.net_sockets_mustnotlisten); } // Check permissions for connect and prepare SocketAddress. EndPoint endPointSnapshot = e.RemoteEndPoint; DnsEndPoint dnsEP = endPointSnapshot as DnsEndPoint; if (dnsEP != null) { if (s_loggingEnabled) { Logging.PrintInfo(Logging.Sockets, "Socket#" + Logging.HashString(this) + "::ConnectAsync " + SR.net_log_socket_connect_dnsendpoint); } if (dnsEP.AddressFamily != AddressFamily.Unspecified && !CanTryAddressFamily(dnsEP.AddressFamily)) { throw new NotSupportedException(SR.net_invalidversion); } MultipleConnectAsync multipleConnectAsync = new SingleSocketMultipleConnectAsync(this, true); e.StartOperationCommon(this); e.StartOperationWrapperConnect(multipleConnectAsync); retval = multipleConnectAsync.StartConnectAsync(e, dnsEP); } else { // Throw if remote address family doesn't match socket. if (!CanTryAddressFamily(e.RemoteEndPoint.AddressFamily)) { throw new NotSupportedException(SR.net_invalidversion); } e._socketAddress = CheckCacheRemote(ref endPointSnapshot, false); // Do wildcard bind if socket not bound. if (_rightEndPoint == null) { if (endPointSnapshot.AddressFamily == AddressFamily.InterNetwork) { InternalBind(new IPEndPoint(IPAddress.Any, 0)); } else { InternalBind(new IPEndPoint(IPAddress.IPv6Any, 0)); } } // Save the old RightEndPoint and prep new RightEndPoint. EndPoint oldEndPoint = _rightEndPoint; if (_rightEndPoint == null) { _rightEndPoint = endPointSnapshot; } // Prepare for the native call. e.StartOperationCommon(this); e.StartOperationConnect(); // Make the native call. int bytesTransferred; SocketError socketError = SocketError.Success; try { socketError = e.DoOperationConnect(this, _handle, out bytesTransferred); } catch (Exception ex) { _rightEndPoint = oldEndPoint; // Clear in-use flag on event args object. e.Complete(); throw ex; } // Handle failure where completion port is not posted. if (socketError != SocketError.Success && socketError != SocketError.IOPending) { e.FinishOperationSyncFailure(socketError, bytesTransferred, SocketFlags.None); retval = false; } else { retval = true; } } if (s_loggingEnabled) { Logging.Exit(Logging.Sockets, this, "ConnectAsync", retval); } return retval; }
public static bool ConnectAsync(SocketType socketType, ProtocolType protocolType, SocketAsyncEventArgs e) { bool retval; if (s_LoggingEnabled) Logging.Enter(Logging.Sockets, null, "ConnectAsync", ""); // Throw if multiple buffers specified. if (e.m_BufferList != null) { throw new ArgumentException(SR.GetString(SR.net_multibuffernotsupported), "BufferList"); } // Throw if RemoteEndPoint is null. if (e.RemoteEndPoint == null) { throw new ArgumentNullException("remoteEP"); } EndPoint endPointSnapshot = e.RemoteEndPoint; DnsEndPoint dnsEP = endPointSnapshot as DnsEndPoint; if (dnsEP != null) { Socket attemptSocket = null; MultipleConnectAsync multipleConnectAsync = null; if (dnsEP.AddressFamily == AddressFamily.Unspecified) { multipleConnectAsync = new MultipleSocketMultipleConnectAsync(socketType, protocolType); } else { attemptSocket = new Socket(dnsEP.AddressFamily, socketType, protocolType); multipleConnectAsync = new SingleSocketMultipleConnectAsync(attemptSocket, false); } e.StartOperationCommon(attemptSocket); e.StartOperationWrapperConnect(multipleConnectAsync); retval = multipleConnectAsync.StartConnectAsync(e, dnsEP); } else { Socket attemptSocket = new Socket(endPointSnapshot.AddressFamily, socketType, protocolType); retval = attemptSocket.ConnectAsync(e); } if (s_LoggingEnabled) Logging.Exit(Logging.Sockets, null, "ConnectAsync", retval); return retval; }
public static bool ConnectAsync(SocketType socketType, ProtocolType protocolType, SocketAsyncEventArgs e) { bool retval; if (s_loggingEnabled) { NetEventSource.Enter(NetEventSource.ComponentType.Socket, null, nameof(ConnectAsync), ""); } if (e == null) { throw new ArgumentNullException(nameof(e)); } if (e._bufferList != null) { throw new ArgumentException(SR.net_multibuffernotsupported, "BufferList"); } if (e.RemoteEndPoint == null) { throw new ArgumentNullException("remoteEP"); } EndPoint endPointSnapshot = e.RemoteEndPoint; DnsEndPoint dnsEP = endPointSnapshot as DnsEndPoint; if (dnsEP != null) { Socket attemptSocket = null; MultipleConnectAsync multipleConnectAsync = null; if (dnsEP.AddressFamily == AddressFamily.Unspecified) { // Disable CS0162 and CS0429: Unreachable code detected // // SuportsMultipleConnectAttempts is a constant; when false, the following lines will trigger CS0162 or CS0429. // // This is the only *Connect* API that actually supports multiple endpoint attempts, as it's responsible // for creating each Socket instance and can create one per attempt (with the instance methods, once a // connect fails, on unix systems that socket can't be used for subsequent connect attempts). #pragma warning disable 162, 429 multipleConnectAsync = SocketPal.SupportsMultipleConnectAttempts ? (MultipleConnectAsync)(new DualSocketMultipleConnectAsync(socketType, protocolType)) : (MultipleConnectAsync)(new MultipleSocketMultipleConnectAsync(socketType, protocolType)); #pragma warning restore } else { attemptSocket = new Socket(dnsEP.AddressFamily, socketType, protocolType); multipleConnectAsync = new SingleSocketMultipleConnectAsync(attemptSocket, false); } e.StartOperationCommon(attemptSocket); e.StartOperationWrapperConnect(multipleConnectAsync); retval = multipleConnectAsync.StartConnectAsync(e, dnsEP); } else { Socket attemptSocket = new Socket(endPointSnapshot.AddressFamily, socketType, protocolType); retval = attemptSocket.ConnectAsync(e); } if (s_loggingEnabled) { NetEventSource.Exit(NetEventSource.ComponentType.Socket, null, nameof(ConnectAsync), retval); } return retval; }
/// <summary> /// Begins an asynchronous request for a connection to a remote host. /// </summary> /// /// <returns> /// Returns true if the I/O operation is pending. The <see cref="E:System.Net.Sockets.SocketAsyncEventArgs.Completed"/> event on the <paramref name="e"/> parameter will be raised upon completion of the operation. Returns false if the I/O operation completed synchronously. In this case, The <see cref="E:System.Net.Sockets.SocketAsyncEventArgs.Completed"/> event on the <paramref name="e"/> parameter will not be raised and the <paramref name="e"/> object passed as a parameter may be examined immediately after the method call returns to retrieve the result of the operation. /// </returns> /// <param name="socketType">One of the <see cref="T:System.Net.Sockets.SocketType"/> values.</param><param name="protocolType">One of the <see cref="T:System.Net.Sockets.ProtocolType"/> values.</param><param name="e">The <see cref="T:System.Net.Sockets.SocketAsyncEventArgs"/> object to use for this asynchronous socket operation.</param><exception cref="T:System.ArgumentException">An argument is not valid. This exception occurs if multiple buffers are specified, the <see cref="P:System.Net.Sockets.SocketAsyncEventArgs.BufferList"/> property is not null. </exception><exception cref="T:System.ArgumentNullException">The <paramref name="e"/> parameter cannot be null and the <see cref="P:System.Net.Sockets.SocketAsyncEventArgs.RemoteEndPoint"/> cannot be null.</exception><exception cref="T:System.InvalidOperationException">The <see cref="T:System.Net.Sockets.Socket"/> is listening or a socket operation was already in progress using the <see cref="T:System.Net.Sockets.SocketAsyncEventArgs"/> object specified in the <paramref name="e"/> parameter.</exception><exception cref="T:System.Net.Sockets.SocketException">An error occurred when attempting to access the socket. See the Remarks section for more information.</exception><exception cref="T:System.NotSupportedException">Windows XP or later is required for this method. This exception also occurs if the local endpoint and the <see cref="P:System.Net.Sockets.SocketAsyncEventArgs.RemoteEndPoint"/> are not the same address family.</exception><exception cref="T:System.ObjectDisposedException">The <see cref="T:System.Net.Sockets.Socket"/> has been closed. </exception><exception cref="T:System.Security.SecurityException">A caller higher in the call stack does not have permission for the requested operation.</exception> public static bool ConnectAsync(SocketType socketType, ProtocolType protocolType, SocketAsyncEventArgs e) { if (Socket.s_LoggingEnabled) Logging.Enter(Logging.Sockets, (string) null, "ConnectAsync", ""); if (e.m_BufferList != null) throw new ArgumentException(SR.GetString("net_multibuffernotsupported"), "BufferList"); if (e.RemoteEndPoint == null) throw new ArgumentNullException("remoteEP"); EndPoint remoteEndPoint = e.RemoteEndPoint; DnsEndPoint endPoint = remoteEndPoint as DnsEndPoint; bool flag; if (endPoint != null) { Socket socket = (Socket) null; MultipleConnectAsync args; if (endPoint.AddressFamily == AddressFamily.Unspecified) { args = (MultipleConnectAsync) new MultipleSocketMultipleConnectAsync(socketType, protocolType); } else { socket = new Socket(endPoint.AddressFamily, socketType, protocolType); args = (MultipleConnectAsync) new SingleSocketMultipleConnectAsync(socket, false); } e.StartOperationCommon(socket); e.StartOperationWrapperConnect(args); flag = args.StartConnectAsync(e, endPoint); } else flag = new Socket(remoteEndPoint.AddressFamily, socketType, protocolType).ConnectAsync(e); if (Socket.s_LoggingEnabled) Logging.Exit(Logging.Sockets, (string) null, "ConnectAsync", (object) (bool) (flag ? 1 : 0)); return flag; }
/// <summary> /// Begins an asynchronous request for a connection to a remote host. /// </summary> /// /// <returns> /// Returns true if the I/O operation is pending. The <see cref="E:System.Net.Sockets.SocketAsyncEventArgs.Completed"/> event on the <paramref name="e"/> parameter will be raised upon completion of the operation. Returns false if the I/O operation completed synchronously. In this case, The <see cref="E:System.Net.Sockets.SocketAsyncEventArgs.Completed"/> event on the <paramref name="e"/> parameter will not be raised and the <paramref name="e"/> object passed as a parameter may be examined immediately after the method call returns to retrieve the result of the operation. /// </returns> /// <param name="e">The <see cref="T:System.Net.Sockets.SocketAsyncEventArgs"/> object to use for this asynchronous socket operation.</param><exception cref="T:System.ArgumentException">An argument is not valid. This exception occurs if multiple buffers are specified, the <see cref="P:System.Net.Sockets.SocketAsyncEventArgs.BufferList"/> property is not null. </exception><exception cref="T:System.ArgumentNullException">The <paramref name="e"/> parameter cannot be null and the <see cref="P:System.Net.Sockets.SocketAsyncEventArgs.RemoteEndPoint"/> cannot be null.</exception><exception cref="T:System.InvalidOperationException">The <see cref="T:System.Net.Sockets.Socket"/> is listening or a socket operation was already in progress using the <see cref="T:System.Net.Sockets.SocketAsyncEventArgs"/> object specified in the <paramref name="e"/> parameter.</exception><exception cref="T:System.Net.Sockets.SocketException">An error occurred when attempting to access the socket. See the Remarks section for more information.</exception><exception cref="T:System.NotSupportedException">Windows XP or later is required for this method. This exception also occurs if the local endpoint and the <see cref="P:System.Net.Sockets.SocketAsyncEventArgs.RemoteEndPoint"/> are not the same address family.</exception><exception cref="T:System.ObjectDisposedException">The <see cref="T:System.Net.Sockets.Socket"/> has been closed. </exception><exception cref="T:System.Security.SecurityException">A caller higher in the call stack does not have permission for the requested operation.</exception> public bool ConnectAsync(SocketAsyncEventArgs e) { if (Socket.s_LoggingEnabled) Logging.Enter(Logging.Sockets, (object) this, "ConnectAsync", ""); if (this.CleanedUp) throw new ObjectDisposedException(this.GetType().FullName); if (e.m_BufferList != null) throw new ArgumentException(SR.GetString("net_multibuffernotsupported"), "BufferList"); if (e.RemoteEndPoint == null) throw new ArgumentNullException("remoteEP"); if (this.isListening) throw new InvalidOperationException(SR.GetString("net_sockets_mustnotlisten")); EndPoint remoteEndPoint = e.RemoteEndPoint; DnsEndPoint endPoint1 = remoteEndPoint as DnsEndPoint; bool flag; if (endPoint1 != null) { if (Socket.s_LoggingEnabled) Logging.PrintInfo(Logging.Sockets, "Socket#" + ValidationHelper.HashString((object) this) + "::ConnectAsync " + SR.GetString("net_log_socket_connect_dnsendpoint")); if (endPoint1.AddressFamily != AddressFamily.Unspecified && !this.CanTryAddressFamily(endPoint1.AddressFamily)) throw new NotSupportedException(SR.GetString("net_invalidversion")); MultipleConnectAsync args = (MultipleConnectAsync) new SingleSocketMultipleConnectAsync(this, true); e.StartOperationCommon(this); e.StartOperationWrapperConnect(args); flag = args.StartConnectAsync(e, endPoint1); } else { if (!this.CanTryAddressFamily(e.RemoteEndPoint.AddressFamily)) throw new NotSupportedException(SR.GetString("net_invalidversion")); e.m_SocketAddress = this.CheckCacheRemote(ref remoteEndPoint, false); if (this.m_RightEndPoint == null) { if (remoteEndPoint.AddressFamily == AddressFamily.InterNetwork) this.InternalBind((EndPoint) new IPEndPoint(IPAddress.Any, 0)); else this.InternalBind((EndPoint) new IPEndPoint(IPAddress.IPv6Any, 0)); } EndPoint endPoint2 = this.m_RightEndPoint; if (this.m_RightEndPoint == null) this.m_RightEndPoint = remoteEndPoint; e.StartOperationCommon(this); e.StartOperationConnect(); this.BindToCompletionPort(); SocketError socketError = SocketError.Success; int bytesSent; try { if (!this.ConnectEx(this.m_Handle, e.m_PtrSocketAddressBuffer, e.m_SocketAddress.m_Size, e.m_PtrSingleBuffer, e.Count, out bytesSent, (SafeHandle) e.m_PtrNativeOverlapped)) socketError = (SocketError) Marshal.GetLastWin32Error(); } catch (Exception ex) { this.m_RightEndPoint = endPoint2; e.Complete(); throw ex; } if (socketError != SocketError.Success && socketError != SocketError.IOPending) { e.FinishOperationSyncFailure(socketError, bytesSent, SocketFlags.None); flag = false; } else flag = true; } if (Socket.s_LoggingEnabled) Logging.Exit(Logging.Sockets, (object) this, "ConnectAsync", (object) (bool) (flag ? 1 : 0)); return flag; }
public bool ConnectAsync(SocketAsyncEventArgs e) { bool flag; if (s_LoggingEnabled) { Logging.Enter(Logging.Sockets, this, "ConnectAsync", ""); } if (this.CleanedUp) { throw new ObjectDisposedException(base.GetType().FullName); } if (e.m_BufferList != null) { throw new ArgumentException(SR.GetString("net_multibuffernotsupported"), "BufferList"); } if (e.RemoteEndPoint == null) { throw new ArgumentNullException("remoteEP"); } if (this.isListening) { throw new InvalidOperationException(SR.GetString("net_sockets_mustnotlisten")); } EndPoint remoteEndPoint = e.RemoteEndPoint; DnsEndPoint endPoint = remoteEndPoint as DnsEndPoint; if (endPoint != null) { if (s_LoggingEnabled) { Logging.PrintInfo(Logging.Sockets, "Socket#" + ValidationHelper.HashString(this) + "::ConnectAsync Connecting to a DnsEndPoint"); } if ((endPoint.AddressFamily != System.Net.Sockets.AddressFamily.Unspecified) && (endPoint.AddressFamily != this.addressFamily)) { throw new NotSupportedException(SR.GetString("net_invalidversion")); } MultipleConnectAsync args = new SingleSocketMultipleConnectAsync(this, true); e.StartOperationCommon(this); e.StartOperationWrapperConnect(args); flag = args.StartConnectAsync(e, endPoint); } else { int num; if (this.addressFamily != e.RemoteEndPoint.AddressFamily) { throw new NotSupportedException(SR.GetString("net_invalidversion")); } e.m_SocketAddress = this.CheckCacheRemote(ref remoteEndPoint, false); if (this.m_RightEndPoint == null) { if (remoteEndPoint.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork) { this.InternalBind(new IPEndPoint(IPAddress.Any, 0)); } else { this.InternalBind(new IPEndPoint(IPAddress.IPv6Any, 0)); } } EndPoint rightEndPoint = this.m_RightEndPoint; if (this.m_RightEndPoint == null) { this.m_RightEndPoint = remoteEndPoint; } e.StartOperationCommon(this); e.StartOperationConnect(); this.BindToCompletionPort(); SocketError success = SocketError.Success; try { if (!this.ConnectEx(this.m_Handle, e.m_PtrSocketAddressBuffer, e.m_SocketAddress.m_Size, e.m_PtrSingleBuffer, e.Count, out num, e.m_PtrNativeOverlapped)) { success = (SocketError) Marshal.GetLastWin32Error(); } } catch (Exception exception) { this.m_RightEndPoint = rightEndPoint; e.Complete(); throw exception; } if ((success != SocketError.Success) && (success != SocketError.IOPending)) { e.FinishOperationSyncFailure(success, num, SocketFlags.None); flag = false; } else { flag = true; } } if (s_LoggingEnabled) { Logging.Exit(Logging.Sockets, this, "ConnectAsync", flag); } return flag; }