public bool AcceptAsync(SocketAsyncEventArgs e) { bool retval; if (s_loggingEnabled) { Logging.Enter(Logging.Sockets, this, "AcceptAsync", ""); } 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 (_rightEndPoint == null) { throw new InvalidOperationException(SR.net_sockets_mustbind); } if (!_isListening) { throw new InvalidOperationException(SR.net_sockets_mustlisten); } // Handle AcceptSocket property. SafeCloseSocket acceptHandle; e.AcceptSocket = GetOrCreateAcceptSocket(e.AcceptSocket, true, "AcceptSocket", out acceptHandle); // Prepare for the native call. e.StartOperationCommon(this); e.StartOperationAccept(); // Local variables for sync completion. int bytesTransferred; SocketError socketError = SocketError.Success; // Make the native call. try { socketError = e.DoOperationAccept(this, _handle, acceptHandle, out bytesTransferred); } catch (Exception ex) { // Clear in-use flag on event args object. e.Complete(); throw ex; } // Handle completion when 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, "AcceptAsync", retval); } return retval; }
/// <summary> /// Begins an asynchronous operation to accept an incoming connection attempt. /// </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. 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 the buffer provided is not large enough. The buffer must be at least 2 * (sizeof(SOCKADDR_STORAGE + 16) bytes. This exception also occurs if multiple buffers are specified, the <see cref="P:System.Net.Sockets.SocketAsyncEventArgs.BufferList"/> property is not null.</exception><exception cref="T:System.ArgumentOutOfRangeException">An argument is out of range. The exception occurs if the <see cref="P:System.Net.Sockets.SocketAsyncEventArgs.Count"/> is less than 0.</exception><exception cref="T:System.InvalidOperationException">An invalid operation was requested. This exception occurs if the accepting <see cref="T:System.Net.Sockets.Socket"/> is not listening for connections or the accepted socket is bound. You must call the <see cref="M:System.Net.Sockets.Socket.Bind(System.Net.EndPoint)"/> and <see cref="M:System.Net.Sockets.Socket.Listen(System.Int32)"/> method before calling the <see cref="M:System.Net.Sockets.Socket.AcceptAsync(System.Net.Sockets.SocketAsyncEventArgs)"/> method.This exception also occurs if the socket is already connected or a socket operation was already in progress using the specified <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.</exception><exception cref="T:System.ObjectDisposedException">The <see cref="T:System.Net.Sockets.Socket"/> has been closed. </exception> public bool AcceptAsync(SocketAsyncEventArgs e) { if (Socket.s_LoggingEnabled) Logging.Enter(Logging.Sockets, (object) this, "AcceptAsync", ""); if (this.CleanedUp) throw new ObjectDisposedException(this.GetType().FullName); if (e.m_BufferList != null) throw new ArgumentException(SR.GetString("net_multibuffernotsupported"), "BufferList"); if (this.m_RightEndPoint == null) throw new InvalidOperationException(SR.GetString("net_sockets_mustbind")); if (!this.isListening) throw new InvalidOperationException(SR.GetString("net_sockets_mustlisten")); if (e.AcceptSocket == null) e.AcceptSocket = new Socket(this.addressFamily, this.socketType, this.protocolType); else if (e.AcceptSocket.m_RightEndPoint != null && !e.AcceptSocket.m_IsDisconnected) throw new InvalidOperationException(SR.GetString("net_sockets_namedmustnotbebound", new object[1] { (object) "AcceptSocket" })); e.StartOperationCommon(this); e.StartOperationAccept(); this.BindToCompletionPort(); SocketError socketError = SocketError.Success; int bytesReceived; try { if (!this.AcceptEx(this.m_Handle, e.AcceptSocket.m_Handle, e.m_PtrSingleBuffer != IntPtr.Zero ? e.m_PtrSingleBuffer : e.m_PtrAcceptBuffer, e.m_PtrSingleBuffer != IntPtr.Zero ? e.Count - e.m_AcceptAddressBufferCount : 0, e.m_AcceptAddressBufferCount / 2, e.m_AcceptAddressBufferCount / 2, out bytesReceived, (SafeHandle) e.m_PtrNativeOverlapped)) socketError = (SocketError) Marshal.GetLastWin32Error(); } catch (Exception ex) { e.Complete(); throw ex; } bool flag; if (socketError != SocketError.Success && socketError != SocketError.IOPending) { e.FinishOperationSyncFailure(socketError, bytesReceived, SocketFlags.None); flag = false; } else flag = true; if (Socket.s_LoggingEnabled) Logging.Exit(Logging.Sockets, (object) this, "AcceptAsync", (object) (bool) (flag ? 1 : 0)); return flag; }
// // AcceptAsync // public bool AcceptAsync(SocketAsyncEventArgs e) { bool retval; if(s_LoggingEnabled) Logging.Enter(Logging.Sockets, this, "AcceptAsync", ""); // Throw if socket disposed if(CleanedUp) { throw new ObjectDisposedException(GetType().FullName); } // Throw if multiple buffers specified. if(e.m_BufferList != null) { throw new ArgumentException(SR.GetString(SR.net_multibuffernotsupported), "BufferList"); } // Throw if not bound. if(m_RightEndPoint == null) { throw new InvalidOperationException(SR.GetString(SR.net_sockets_mustbind)); } // Throw if not listening. if(!isListening) { throw new InvalidOperationException(SR.GetString(SR.net_sockets_mustlisten)); } // Handle AcceptSocket property. if(e.AcceptSocket == null) { // Accept socket not specified - create it. e.AcceptSocket = new Socket(addressFamily, socketType, protocolType); } else { // Validate accept socket for use here. if(e.AcceptSocket.m_RightEndPoint != null && !e.AcceptSocket.m_IsDisconnected) { throw new InvalidOperationException(SR.GetString(SR.net_sockets_namedmustnotbebound, "AcceptSocket")); } } // Prepare for the native call. e.StartOperationCommon(this); e.StartOperationAccept(); BindToCompletionPort(); // Local variables for [....] completion. int bytesTransferred; SocketError socketError = SocketError.Success; // Make the native call. try { if(!AcceptEx( m_Handle, e.AcceptSocket.m_Handle, (e.m_PtrSingleBuffer != IntPtr.Zero) ? e.m_PtrSingleBuffer : e.m_PtrAcceptBuffer, (e.m_PtrSingleBuffer != IntPtr.Zero) ? e.Count - e.m_AcceptAddressBufferCount : 0, e.m_AcceptAddressBufferCount / 2, e.m_AcceptAddressBufferCount / 2, out bytesTransferred, e.m_PtrNativeOverlapped)) { socketError = (SocketError)Marshal.GetLastWin32Error(); } } catch (Exception ex) { // clear in-use on event arg object e.Complete(); throw ex; } // Handle completion when 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, "AcceptAsync", retval); return retval; }
public bool AcceptAsync(SocketAsyncEventArgs e) { bool flag; int num; if (s_LoggingEnabled) { Logging.Enter(Logging.Sockets, this, "AcceptAsync", ""); } if (this.CleanedUp) { throw new ObjectDisposedException(base.GetType().FullName); } if (e.m_BufferList != null) { throw new ArgumentException(SR.GetString("net_multibuffernotsupported"), "BufferList"); } if (this.m_RightEndPoint == null) { throw new InvalidOperationException(SR.GetString("net_sockets_mustbind")); } if (!this.isListening) { throw new InvalidOperationException(SR.GetString("net_sockets_mustlisten")); } if (e.AcceptSocket == null) { e.AcceptSocket = new Socket(this.addressFamily, this.socketType, this.protocolType); } else if ((e.AcceptSocket.m_RightEndPoint != null) && !e.AcceptSocket.m_IsDisconnected) { throw new InvalidOperationException(SR.GetString("net_sockets_namedmustnotbebound", new object[] { "AcceptSocket" })); } e.StartOperationCommon(this); e.StartOperationAccept(); this.BindToCompletionPort(); SocketError success = SocketError.Success; try { if (!this.AcceptEx(this.m_Handle, e.AcceptSocket.m_Handle, (e.m_PtrSingleBuffer != IntPtr.Zero) ? e.m_PtrSingleBuffer : e.m_PtrAcceptBuffer, (e.m_PtrSingleBuffer != IntPtr.Zero) ? (e.Count - e.m_AcceptAddressBufferCount) : 0, e.m_AcceptAddressBufferCount / 2, e.m_AcceptAddressBufferCount / 2, out num, e.m_PtrNativeOverlapped)) { success = (SocketError) Marshal.GetLastWin32Error(); } } catch (Exception exception) { 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, "AcceptAsync", flag); } return flag; }