// This is the truly async version that uses AcceptEx. internal IAsyncResult BeginAccept(Socket acceptSocket, int receiveSize, AsyncCallback callback, object state) { if (s_loggingEnabled) { Logging.Enter(Logging.Sockets, this, "BeginAccept", ""); } if (CleanedUp) { throw new ObjectDisposedException(this.GetType().FullName); } // Validate input parameters. if (receiveSize < 0) { throw new ArgumentOutOfRangeException("size"); } // Set up the async result with flowing. AcceptOverlappedAsyncResult asyncResult = new AcceptOverlappedAsyncResult(this, state, callback); asyncResult.StartPostingAsyncOp(false); // Start the accept. DoBeginAccept(acceptSocket, receiveSize, asyncResult); // Finish the flow capture, maybe complete here. asyncResult.FinishPostingAsyncOp(ref Caches.AcceptClosureCache); if (s_loggingEnabled) { Logging.Exit(Logging.Sockets, this, "BeginAccept", asyncResult); } return asyncResult; }
private void DoBeginAccept(Socket acceptSocket, int receiveSize, AcceptOverlappedAsyncResult asyncResult) { if (_rightEndPoint == null) { throw new InvalidOperationException(SR.net_sockets_mustbind); } if (!_isListening) { throw new InvalidOperationException(SR.net_sockets_mustlisten); } SafeCloseSocket acceptHandle; asyncResult.AcceptSocket = GetOrCreateAcceptSocket(acceptSocket, false, "acceptSocket", out acceptHandle); GlobalLog.Print("Socket#" + Logging.HashString(this) + "::DoBeginAccept() AcceptSocket:" + Logging.HashString(acceptSocket)); int socketAddressSize = _rightEndPoint.Serialize().Size; SocketError errorCode = SocketPal.AcceptAsync(this, _handle, acceptHandle, receiveSize, socketAddressSize, asyncResult); errorCode = asyncResult.CheckAsyncCallOverlappedResult(errorCode); GlobalLog.Print("Socket#" + Logging.HashString(this) + "::DoBeginAccept() Interop.Winsock.AcceptEx returns:" + errorCode.ToString() + Logging.HashString(asyncResult)); // Throw an appropriate SocketException if the native call fails synchronously. if (errorCode != SocketError.Success) { SocketException socketException = new SocketException((int)errorCode); UpdateStatusAfterSocketError(socketException); if (s_loggingEnabled) { Logging.Exception(Logging.Sockets, this, "BeginAccept", socketException); } throw socketException; } }
public static SocketError AcceptAsync(Socket socket, SafeCloseSocket handle, SafeCloseSocket acceptHandle, int receiveSize, int socketAddressSize, AcceptOverlappedAsyncResult asyncResult) { Debug.Assert(acceptHandle == null); byte[] socketAddressBuffer = new byte[socketAddressSize]; return handle.AsyncContext.AcceptAsync(socketAddressBuffer, socketAddressSize, asyncResult.CompletionCallback); }
public static unsafe SocketError AcceptAsync(Socket socket, SafeCloseSocket handle, SafeCloseSocket acceptHandle, int receiveSize, int socketAddressSize, AcceptOverlappedAsyncResult asyncResult) { // The buffer needs to contain the requested data plus room for two sockaddrs and 16 bytes // of associated data for each. int addressBufferSize = socketAddressSize + 16; byte[] buffer = new byte[receiveSize + ((addressBufferSize) * 2)]; // Set up asyncResult for overlapped AcceptEx. // This call will use completion ports on WinNT. asyncResult.SetUnmanagedStructures(buffer, addressBufferSize); // This can throw ObjectDisposedException. int bytesTransferred; SocketError errorCode = SocketError.Success; if (!socket.AcceptEx( handle, acceptHandle, Marshal.UnsafeAddrOfPinnedArrayElement(asyncResult.Buffer, 0), receiveSize, addressBufferSize, addressBufferSize, out bytesTransferred, asyncResult.OverlappedHandle)) { errorCode = GetLastSocketError(); } return(errorCode); }
public static unsafe SocketError AcceptAsync(Socket socket, SafeCloseSocket handle, SafeCloseSocket acceptHandle, int receiveSize, int socketAddressSize, AcceptOverlappedAsyncResult asyncResult) { // The buffer needs to contain the requested data plus room for two sockaddrs and 16 bytes // of associated data for each. int addressBufferSize = socketAddressSize + 16; byte[] buffer = new byte[receiveSize + ((addressBufferSize) * 2)]; // Set up asyncResult for overlapped AcceptEx. // This call will use completion ports on WinNT. asyncResult.SetUnmanagedStructures(buffer, addressBufferSize); try { // This can throw ObjectDisposedException. int bytesTransferred; bool success = socket.AcceptEx( handle, acceptHandle, Marshal.UnsafeAddrOfPinnedArrayElement(asyncResult.Buffer, 0), receiveSize, addressBufferSize, addressBufferSize, out bytesTransferred, asyncResult.DangerousOverlappedPointer); // SafeHandle was just created in SetUnmanagedStructures return(asyncResult.ProcessOverlappedResult(success, 0)); } catch { asyncResult.ReleaseUnmanagedStructures(); throw; } }
private void DoBeginAccept(Socket acceptSocket, int receiveSize, AcceptOverlappedAsyncResult asyncResult) { if (m_RightEndPoint==null) { throw new InvalidOperationException(SR.GetString(SR.net_sockets_mustbind)); } if(!isListening){ throw new InvalidOperationException(SR.GetString(SR.net_sockets_mustlisten)); } // if a acceptSocket isn't specified, then we need to create it. if (acceptSocket == null) { acceptSocket = new Socket(addressFamily,socketType,protocolType); } else { if (acceptSocket.m_RightEndPoint != null) { throw new InvalidOperationException(SR.GetString(SR.net_sockets_namedmustnotbebound, "acceptSocket")); } } asyncResult.AcceptSocket = acceptSocket; GlobalLog.Print("Socket#" + ValidationHelper.HashString(this) + "::DoBeginAccept() AcceptSocket:" + ValidationHelper.HashString(acceptSocket)); //the buffer needs to contain the requested data plus room for two sockaddrs and 16 bytes //of associated data for each. int addressBufferSize = m_RightEndPoint.Serialize().Size + 16; byte[] buffer = new byte[receiveSize + ((addressBufferSize) * 2)]; // // Set up asyncResult for overlapped AcceptEx. // This call will use // completion ports on WinNT // asyncResult.SetUnmanagedStructures(buffer, addressBufferSize); // This can throw ObjectDisposedException. int bytesTransferred; SocketError errorCode = SocketError.Success; if (!AcceptEx( m_Handle, acceptSocket.m_Handle, Marshal.UnsafeAddrOfPinnedArrayElement(asyncResult.Buffer, 0), receiveSize, addressBufferSize, addressBufferSize, out bytesTransferred, asyncResult.OverlappedHandle)) { errorCode = (SocketError)Marshal.GetLastWin32Error(); } errorCode = asyncResult.CheckAsyncCallOverlappedResult(errorCode); GlobalLog.Print("Socket#" + ValidationHelper.HashString(this) + "::DoBeginAccept() UnsafeNclNativeMethods.OSSOCK.AcceptEx returns:" + errorCode.ToString() + ValidationHelper.HashString(asyncResult)); // // if the asynchronous native call fails synchronously // we'll throw a SocketException // if (errorCode!=SocketError.Success) { SocketException socketException = new SocketException(errorCode); UpdateStatusAfterSocketError(socketException); if(s_LoggingEnabled)Logging.Exception(Logging.Sockets, this, "BeginAccept", socketException); throw socketException; } }
public static unsafe SocketError AcceptAsync(Socket socket, SafeCloseSocket handle, SafeCloseSocket acceptHandle, int receiveSize, int socketAddressSize, AcceptOverlappedAsyncResult asyncResult) { // The buffer needs to contain the requested data plus room for two sockaddrs and 16 bytes // of associated data for each. int addressBufferSize = socketAddressSize + 16; byte[] buffer = new byte[receiveSize + ((addressBufferSize) * 2)]; // Set up asyncResult for overlapped AcceptEx. // This call will use completion ports on WinNT. asyncResult.SetUnmanagedStructures(buffer, addressBufferSize); // This can throw ObjectDisposedException. int bytesTransferred; SocketError errorCode = SocketError.Success; if (!socket.AcceptEx( handle, acceptHandle, Marshal.UnsafeAddrOfPinnedArrayElement(asyncResult.Buffer, 0), receiveSize, addressBufferSize, addressBufferSize, out bytesTransferred, asyncResult.OverlappedHandle)) { errorCode = GetLastSocketError(); } return errorCode; }
private void DoBeginAccept(Socket acceptSocket, int receiveSize, AcceptOverlappedAsyncResult asyncResult) { 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 (acceptSocket == null) acceptSocket = new Socket(this.addressFamily, this.socketType, this.protocolType); else if (acceptSocket.m_RightEndPoint != null) throw new InvalidOperationException(SR.GetString("net_sockets_namedmustnotbebound", new object[1] { (object) "acceptSocket" })); asyncResult.AcceptSocket = acceptSocket; int num = this.m_RightEndPoint.Serialize().Size + 16; byte[] buffer = new byte[receiveSize + num * 2]; asyncResult.SetUnmanagedStructures(buffer, num); SocketError errorCode = SocketError.Success; int bytesReceived; if (!this.AcceptEx(this.m_Handle, acceptSocket.m_Handle, Marshal.UnsafeAddrOfPinnedArrayElement((Array) asyncResult.Buffer, 0), receiveSize, num, num, out bytesReceived, asyncResult.OverlappedHandle)) errorCode = (SocketError) Marshal.GetLastWin32Error(); SocketError socketError = asyncResult.CheckAsyncCallOverlappedResult(errorCode); if (socketError == SocketError.Success) return; SocketException socketException = new SocketException(socketError); this.UpdateStatusAfterSocketError(socketException); if (Socket.s_LoggingEnabled) Logging.Exception(Logging.Sockets, (object) this, "BeginAccept", (Exception) socketException); throw socketException; }
public IAsyncResult BeginAccept(Socket acceptSocket, int receiveSize, AsyncCallback callback, object state) { if (Socket.s_LoggingEnabled) Logging.Enter(Logging.Sockets, (object) this, "BeginAccept", ""); if (this.CleanedUp) throw new ObjectDisposedException(this.GetType().FullName); if (receiveSize < 0) throw new ArgumentOutOfRangeException("size"); AcceptOverlappedAsyncResult asyncResult = new AcceptOverlappedAsyncResult(this, state, callback); asyncResult.StartPostingAsyncOp(false); this.DoBeginAccept(acceptSocket, receiveSize, asyncResult); asyncResult.FinishPostingAsyncOp(ref this.Caches.AcceptClosureCache); if (Socket.s_LoggingEnabled) Logging.Exit(Logging.Sockets, (object) this, "BeginAccept", (object) asyncResult); return (IAsyncResult) asyncResult; }
private void DoBeginAccept(Socket acceptSocket, int receiveSize, AcceptOverlappedAsyncResult asyncResult) { if (_rightEndPoint == null) { throw new InvalidOperationException(SR.net_sockets_mustbind); } if (!_isListening) { throw new InvalidOperationException(SR.net_sockets_mustlisten); } SafeCloseSocket acceptHandle; asyncResult.AcceptSocket = GetOrCreateAcceptSocket(acceptSocket, false, nameof(acceptSocket), out acceptHandle); if (NetEventSource.IsEnabled) NetEventSource.Info(this, $"AcceptSocket:{acceptSocket}"); int socketAddressSize = _rightEndPoint.Serialize().Size; SocketError errorCode = SocketPal.AcceptAsync(this, _handle, acceptHandle, receiveSize, socketAddressSize, asyncResult); errorCode = asyncResult.CheckAsyncCallOverlappedResult(errorCode); if (NetEventSource.IsEnabled) NetEventSource.Info(this, $"Interop.Winsock.AcceptEx returns:{errorCode} {asyncResult}"); // Throw an appropriate SocketException if the native call fails synchronously. if (errorCode != SocketError.Success) { SocketException socketException = new SocketException((int)errorCode); UpdateStatusAfterSocketError(socketException); if (NetEventSource.IsEnabled) NetEventSource.Error(this, socketException); throw socketException; } }
private void DoBeginAccept(Socket acceptSocket, int receiveSize, AcceptOverlappedAsyncResult asyncResult) { int num2; if (!ComNetOS.IsWinNt) { throw new PlatformNotSupportedException(SR.GetString("WinNTRequired")); } 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 (acceptSocket == null) { acceptSocket = new Socket(this.addressFamily, this.socketType, this.protocolType); } else if (acceptSocket.m_RightEndPoint != null) { throw new InvalidOperationException(SR.GetString("net_sockets_namedmustnotbebound", new object[] { "acceptSocket" })); } asyncResult.AcceptSocket = acceptSocket; int addressBufferLength = this.m_RightEndPoint.Serialize().Size + 0x10; byte[] buffer = new byte[receiveSize + (addressBufferLength * 2)]; asyncResult.SetUnmanagedStructures(buffer, addressBufferLength); SocketError success = SocketError.Success; if (!this.AcceptEx(this.m_Handle, acceptSocket.m_Handle, Marshal.UnsafeAddrOfPinnedArrayElement(asyncResult.Buffer, 0), receiveSize, addressBufferLength, addressBufferLength, out num2, asyncResult.OverlappedHandle)) { success = (SocketError) Marshal.GetLastWin32Error(); } success = asyncResult.CheckAsyncCallOverlappedResult(success); if (success != SocketError.Success) { SocketException socketException = new SocketException(success); this.UpdateStatusAfterSocketError(socketException); if (s_LoggingEnabled) { Logging.Exception(Logging.Sockets, this, "BeginAccept", socketException); } throw socketException; } }