unsafe private static void AsyncFSCallback(uint errorCode, uint numBytes, NativeOverlapped *pOverlapped) { // Unpack overlapped Overlapped overlapped = Overlapped.Unpack(pOverlapped); // Free the overlapped struct in EndRead/EndWrite. // Extract async result from overlapped PipeAsyncResult asyncResult = (PipeAsyncResult)overlapped.AsyncResult; asyncResult._numBytes = (int)numBytes; // Handle reading from & writing to closed pipes. While I'm not sure // this is entirely necessary anymore, maybe it's possible for // an async read on a pipe to be issued and then the pipe is closed, // returning this error. This may very well be necessary. if (errorCode == NativePipe.ERROR_BROKEN_PIPE) { errorCode = 0; } asyncResult._errorCode = (int)errorCode; // Call the user-provided callback. It can and often should // call EndRead or EndWrite. There's no reason to use an async // delegate here - we're already on a threadpool thread. // IAsyncResult's completedSynchronously property must return // false here, saying the user callback was called on another thread. AsyncCallback userCallback = asyncResult._userCallback; userCallback(asyncResult); }
internal unsafe IAsyncResult BeginRead(byte[] data, int offset, int size, AsyncCallback callback, object state) { bool flag; PipeAsyncResult ar = new PipeAsyncResult(callback); NativeOverlapped *lpOverlapped = new Overlapped(0, 0, IntPtr.Zero, ar).UnsafePack(IOCallback, data); ar._overlapped = lpOverlapped; fixed(byte *numRef = data) { flag = NativePipe.ReadFile(this._handle, numRef + offset, size, IntPtr.Zero, lpOverlapped); } if (!flag) { int errorCode = Marshal.GetLastWin32Error(); if (errorCode == 0x6dL) { ar.CallUserCallback(); return(ar); } if (errorCode != 0x3e5L) { throw new RemotingException(string.Format(CultureInfo.CurrentCulture, CoreChannel.GetResourceString("Remoting_Ipc_ReadFailure"), new object[] { GetMessage(errorCode) })); } } return(ar); }
internal unsafe IAsyncResult BeginRead(byte[] data, int offset, int size, AsyncCallback callback, object state) { bool flag; PipeAsyncResult ar = new PipeAsyncResult(callback); NativeOverlapped* lpOverlapped = new Overlapped(0, 0, IntPtr.Zero, ar).UnsafePack(IOCallback, data); ar._overlapped = lpOverlapped; fixed (byte* numRef = data) { flag = NativePipe.ReadFile(this._handle, numRef + offset, size, IntPtr.Zero, lpOverlapped); } if (!flag) { int errorCode = Marshal.GetLastWin32Error(); if (errorCode == 0x6dL) { ar.CallUserCallback(); return ar; } if (errorCode != 0x3e5L) { throw new RemotingException(string.Format(CultureInfo.CurrentCulture, CoreChannel.GetResourceString("Remoting_Ipc_ReadFailure"), new object[] { GetMessage(errorCode) })); } } return ar; }
private static unsafe void AsyncFSCallback(uint errorCode, uint numBytes, NativeOverlapped *pOverlapped) { PipeAsyncResult asyncResult = (PipeAsyncResult)Overlapped.Unpack(pOverlapped).AsyncResult; asyncResult._numBytes = (int)numBytes; if (errorCode == 0x6dL) { errorCode = 0; } asyncResult._errorCode = (int)errorCode; asyncResult._userCallback(asyncResult); }
internal unsafe int EndRead(IAsyncResult iar) { PipeAsyncResult result = iar as PipeAsyncResult; NativeOverlapped *nativeOverlappedPtr = result._overlapped; if (nativeOverlappedPtr != null) { Overlapped.Free(nativeOverlappedPtr); } if (result._errorCode != 0) { throw new RemotingException(string.Format(CultureInfo.CurrentCulture, CoreChannel.GetResourceString("Remoting_Ipc_ReadFailure"), new object[] { GetMessage(result._errorCode) })); } return(result._numBytes); }
internal unsafe int EndRead(IAsyncResult iar) { PipeAsyncResult ar = iar as PipeAsyncResult; // Free memory & GC handles. NativeOverlapped *overlappedPtr = ar._overlapped; if (overlappedPtr != null) { Overlapped.Free(overlappedPtr); } // Now check for any error during the read. if (ar._errorCode != 0) { throw new RemotingException(String.Format(CultureInfo.CurrentCulture, CoreChannel.GetResourceString("Remoting_Ipc_ReadFailure"), GetMessage(ar._errorCode))); } return(ar._numBytes); }
internal unsafe IAsyncResult BeginRead(byte[] data, int offset, int size, AsyncCallback callback, object state) { PipeAsyncResult asyncResult = new PipeAsyncResult(callback); // Create a managed overlapped class // We will set the file offsets later Overlapped overlapped = new Overlapped(0, 0, IntPtr.Zero, asyncResult); // Pack the Overlapped class, and store it in the async result NativeOverlapped *intOverlapped; intOverlapped = overlapped.UnsafePack(IOCallback, data); asyncResult._overlapped = intOverlapped; bool status; // pin the buffer and read data with overlapped fixed(byte *p = data) { status = NativePipe.ReadFile(_handle, p + offset, size, IntPtr.Zero, intOverlapped); } if (!status) { int error = Marshal.GetLastWin32Error(); // For pipes, when they hit EOF, they will come here. if (error == NativePipe.ERROR_BROKEN_PIPE) { // Not an error, but EOF. AsyncFSCallback will NOT be // called. Call the user callback here. asyncResult.CallUserCallback(); // EndRead will free the Overlapped struct correctly. } else if (error != NativePipe.ERROR_IO_PENDING) { throw new RemotingException(String.Format(CultureInfo.CurrentCulture, CoreChannel.GetResourceString("Remoting_Ipc_ReadFailure"), GetMessage(error))); } } return(asyncResult); }
internal unsafe IAsyncResult BeginRead(byte[] data, int offset, int size, AsyncCallback callback, object state) { PipeAsyncResult asyncResult = new PipeAsyncResult(callback); // Create a managed overlapped class // We will set the file offsets later Overlapped overlapped = new Overlapped(0, 0, IntPtr.Zero, asyncResult); // Pack the Overlapped class, and store it in the async result NativeOverlapped* intOverlapped; intOverlapped = overlapped.UnsafePack(IOCallback, data); asyncResult._overlapped = intOverlapped; bool status; // pin the buffer and read data with overlapped fixed(byte* p = data) { status = NativePipe.ReadFile(_handle, p + offset, size, IntPtr.Zero, intOverlapped); } if (!status) { int error = Marshal.GetLastWin32Error(); // For pipes, when they hit EOF, they will come here. if (error == NativePipe.ERROR_BROKEN_PIPE) { // Not an error, but EOF. AsyncFSCallback will NOT be // called. Call the user callback here. asyncResult.CallUserCallback(); // EndRead will free the Overlapped struct correctly. } else if (error != NativePipe.ERROR_IO_PENDING) throw new RemotingException(String.Format(CultureInfo.CurrentCulture, CoreChannel.GetResourceString("Remoting_Ipc_ReadFailure"), GetMessage(error))); } return asyncResult; }