public static unsafe SocketError ReceiveFromAsync(SafeCloseSocket handle, byte[] buffer, int offset, int count, SocketFlags socketFlags, Internals.SocketAddress socketAddress, OverlappedAsyncResult asyncResult) { // Set up asyncResult for overlapped WSARecvFrom. // This call will use completion ports on WinNT and Overlapped IO on Win9x. asyncResult.SetUnmanagedStructures(buffer, offset, count, socketAddress, true); int bytesTransferred; SocketError errorCode = Interop.Winsock.WSARecvFrom( handle, ref asyncResult._singleBuffer, 1, out bytesTransferred, ref socketFlags, asyncResult.GetSocketAddressPtr(), asyncResult.GetSocketAddressSizePtr(), asyncResult.OverlappedHandle, IntPtr.Zero); if (errorCode != SocketError.Success) { errorCode = GetLastSocketError(); } return(errorCode); }
public static unsafe SocketError ReceiveFromAsync(SafeCloseSocket handle, byte[] buffer, int offset, int count, SocketFlags socketFlags, Internals.SocketAddress socketAddress, OverlappedAsyncResult asyncResult) { // Set up asyncResult for overlapped WSARecvFrom. asyncResult.SetUnmanagedStructures(buffer, offset, count, socketAddress, true); try { int bytesTransferred; SocketError errorCode = Interop.Winsock.WSARecvFrom( handle, ref asyncResult._singleBuffer, 1, out bytesTransferred, ref socketFlags, asyncResult.GetSocketAddressPtr(), asyncResult.GetSocketAddressSizePtr(), asyncResult.OverlappedHandle, IntPtr.Zero); return(asyncResult.ProcessOverlappedResult(errorCode == SocketError.Success, bytesTransferred)); } catch { asyncResult.ReleaseUnmanagedStructures(); throw; } }
public static unsafe SocketError ReceiveFromAsync(SafeCloseSocket handle, byte[] buffer, int offset, int count, SocketFlags socketFlags, Internals.SocketAddress socketAddress, OverlappedAsyncResult asyncResult) { // Set up asyncResult for overlapped WSARecvFrom. asyncResult.SetUnmanagedStructures(buffer, offset, count, socketAddress); try { int bytesTransferred; SocketError errorCode = Interop.Winsock.WSARecvFrom( handle.DangerousGetHandle(), // to minimize chances of handle recycling from misuse, this should use DangerousAddRef/Release, but it adds too much overhead ref asyncResult._singleBuffer, 1, out bytesTransferred, ref socketFlags, asyncResult.GetSocketAddressPtr(), asyncResult.GetSocketAddressSizePtr(), asyncResult.DangerousOverlappedPointer, // SafeHandle was just created in SetUnmanagedStructures IntPtr.Zero); GC.KeepAlive(handle); // small extra safe guard against handle getting collected/finalized while P/Invoke in progress return(asyncResult.ProcessOverlappedResult(errorCode == SocketError.Success, bytesTransferred)); } catch { asyncResult.ReleaseUnmanagedStructures(); throw; } }
private void DoBeginReceiveFrom(byte[] buffer, int offset, int size, SocketFlags socketFlags, EndPoint endPointSnapshot, SocketAddress socketAddress, OverlappedAsyncResult asyncResult) { EndPoint oldEndPoint = m_RightEndPoint; GlobalLog.Print("Socket#" + ValidationHelper.HashString(this) + "::DoBeginReceiveFrom() size:" + size.ToString()); // Guarantee to call CheckAsyncCallOverlappedResult if we call SetUnamangedStructures with a cache in order to // avoid a Socket leak in case of error. SocketError errorCode = SocketError.SocketError; try { // Set up asyncResult for overlapped WSARecvFrom. // This call will use completion ports on WinNT and Overlapped IO on Win9x. asyncResult.SetUnmanagedStructures(buffer, offset, size, socketAddress, true /* pin remoteEP*/, ref Caches.ReceiveOverlappedCache); // save a copy of the original EndPoint in the asyncResult asyncResult.SocketAddressOriginal = endPointSnapshot.Serialize(); if (m_RightEndPoint == null) { m_RightEndPoint = endPointSnapshot; } int bytesTransferred; errorCode = UnsafeNclNativeMethods.OSSOCK.WSARecvFrom( m_Handle, ref asyncResult.m_SingleBuffer, 1, out bytesTransferred, ref socketFlags, asyncResult.GetSocketAddressPtr(), asyncResult.GetSocketAddressSizePtr(), asyncResult.OverlappedHandle, IntPtr.Zero ); if (errorCode!=SocketError.Success) { errorCode = (SocketError)Marshal.GetLastWin32Error(); } GlobalLog.Print("Socket#" + ValidationHelper.HashString(this) + "::DoBeginReceiveFrom() UnsafeNclNativeMethods.OSSOCK.WSARecvFrom returns:" + errorCode.ToString() + " size:" + size.ToString() + " returning AsyncResult:" + ValidationHelper.HashString(asyncResult)); } catch (ObjectDisposedException) { m_RightEndPoint = oldEndPoint; throw; } finally { errorCode = asyncResult.CheckAsyncCallOverlappedResult(errorCode); } // // if the asynchronous native call fails synchronously // we'll throw a SocketException // if (errorCode!=SocketError.Success) { // // update our internal state after this socket error and throw // m_RightEndPoint = oldEndPoint; asyncResult.ExtractCache(ref Caches.ReceiveOverlappedCache); SocketException socketException = new SocketException(errorCode); UpdateStatusAfterSocketError(socketException); if(s_LoggingEnabled)Logging.Exception(Logging.Sockets, this, "BeginReceiveFrom", socketException); throw socketException; } GlobalLog.Print("Socket#" + ValidationHelper.HashString(this) + "::DoBeginReceiveFrom() size:" + size.ToString() + " returning AsyncResult:" + ValidationHelper.HashString(asyncResult)); }
public static unsafe SocketError ReceiveFromAsync(SafeCloseSocket handle, byte[] buffer, int offset, int count, SocketFlags socketFlags, Internals.SocketAddress socketAddress, OverlappedAsyncResult asyncResult) { // Set up asyncResult for overlapped WSARecvFrom. // This call will use completion ports on WinNT and Overlapped IO on Win9x. asyncResult.SetUnmanagedStructures(buffer, offset, count, socketAddress, true); int bytesTransferred; SocketError errorCode = Interop.Winsock.WSARecvFrom( handle, ref asyncResult._singleBuffer, 1, out bytesTransferred, ref socketFlags, asyncResult.GetSocketAddressPtr(), asyncResult.GetSocketAddressSizePtr(), asyncResult.OverlappedHandle, IntPtr.Zero); if (errorCode != SocketError.Success) { errorCode = GetLastSocketError(); } return errorCode; }
private void DoBeginReceiveFrom(byte[] buffer, int offset, int size, SocketFlags socketFlags, EndPoint endPointSnapshot, SocketAddress socketAddress, OverlappedAsyncResult asyncResult) { EndPoint endPoint = this.m_RightEndPoint; SocketError socketError = SocketError.SocketError; try { asyncResult.SetUnmanagedStructures(buffer, offset, size, socketAddress, true, ref this.Caches.ReceiveOverlappedCache); asyncResult.SocketAddressOriginal = endPointSnapshot.Serialize(); if (this.m_RightEndPoint == null) this.m_RightEndPoint = endPointSnapshot; int bytesTransferred; socketError = UnsafeNclNativeMethods.OSSOCK.WSARecvFrom(this.m_Handle, ref asyncResult.m_SingleBuffer, 1, out bytesTransferred, out socketFlags, asyncResult.GetSocketAddressPtr(), asyncResult.GetSocketAddressSizePtr(), asyncResult.OverlappedHandle, IntPtr.Zero); if (socketError != SocketError.Success) socketError = (SocketError) Marshal.GetLastWin32Error(); } catch (ObjectDisposedException ex) { this.m_RightEndPoint = endPoint; throw; } finally { socketError = asyncResult.CheckAsyncCallOverlappedResult(socketError); } if (socketError == SocketError.Success) return; this.m_RightEndPoint = endPoint; asyncResult.ExtractCache(ref this.Caches.ReceiveOverlappedCache); SocketException socketException = new SocketException(socketError); this.UpdateStatusAfterSocketError(socketException); if (Socket.s_LoggingEnabled) Logging.Exception(Logging.Sockets, (object) this, "BeginReceiveFrom", (Exception) socketException); throw socketException; }