private static unsafe void AsyncFSCallback(uint errorCode, uint numBytes, NativeOverlapped *pOverlapped) { FileStreamAsyncResult streamAsyncResult = (FileStreamAsyncResult)Overlapped.Unpack(pOverlapped).AsyncResult; streamAsyncResult._numBytes = (int)numBytes; if (FrameworkEventSource.IsInitialized && FrameworkEventSource.Log.IsEnabled(EventLevel.Informational, (EventKeywords)16)) { FrameworkEventSource.Log.ThreadTransferReceive((long)streamAsyncResult.OverLapped, 2, string.Empty); } if ((int)errorCode == 109 || (int)errorCode == 232) { errorCode = 0U; } streamAsyncResult._errorCode = (int)errorCode; streamAsyncResult._completedSynchronously = false; streamAsyncResult._isComplete = true; Thread.MemoryBarrier(); ManualResetEvent manualResetEvent = streamAsyncResult._waitHandle; if (manualResetEvent != null && !manualResetEvent.Set()) { __Error.WinIOError(); } AsyncCallback asyncCallback = streamAsyncResult._userCallback; if (asyncCallback == null) { return; } asyncCallback((IAsyncResult)streamAsyncResult); }
internal static FileStreamAsyncResult CreateBufferedReadResult(int numBufferedBytes, AsyncCallback userCallback, Object userStateObject, bool isWrite) { FileStreamAsyncResult asyncResult = new FileStreamAsyncResult(numBufferedBytes, userCallback, userStateObject, isWrite); asyncResult.CallUserCallback(); return(asyncResult); }
private unsafe static void AsyncFSCallback(uint errorCode, uint numBytes, NativeOverlapped *pOverlapped) { Overlapped overlapped = Overlapped.Unpack(pOverlapped); FileStreamAsyncResult fileStreamAsyncResult = (FileStreamAsyncResult)overlapped.AsyncResult; fileStreamAsyncResult._numBytes = (int)numBytes; if (FrameworkEventSource.IsInitialized && FrameworkEventSource.Log.IsEnabled(EventLevel.Informational, (EventKeywords)16L)) { FrameworkEventSource.Log.ThreadTransferReceive(fileStreamAsyncResult.OverLapped, 2, string.Empty); } if (errorCode == 109U || errorCode == 232U) { errorCode = 0U; } fileStreamAsyncResult._errorCode = (int)errorCode; fileStreamAsyncResult._completedSynchronously = false; fileStreamAsyncResult._isComplete = true; Thread.MemoryBarrier(); ManualResetEvent waitHandle = fileStreamAsyncResult._waitHandle; if (waitHandle != null && !waitHandle.Set()) { __Error.WinIOError(); } AsyncCallback userCallback = fileStreamAsyncResult._userCallback; if (userCallback != null) { userCallback(fileStreamAsyncResult); } }
public override IAsyncResult BeginWrite(byte [] array, int offset, int numBytes, AsyncCallback userCallback, object stateObject) { if (handle == MonoIO.InvalidHandle) { throw new ObjectDisposedException("Stream has been closed"); } if (!CanWrite) { throw new NotSupportedException("This stream does not support writing"); } if (array == null) { throw new ArgumentNullException("array"); } if (numBytes < 0) { throw new ArgumentOutOfRangeException("numBytes", "Must be >= 0"); } if (offset < 0) { throw new ArgumentOutOfRangeException("offset", "Must be >= 0"); } // reordered to avoid possible integer overflow if (numBytes > array.Length - offset) { throw new ArgumentException("array too small. numBytes/offset wrong."); } if (!async) { return(base.BeginWrite(array, offset, numBytes, userCallback, stateObject)); } FileStreamAsyncResult result = new FileStreamAsyncResult(userCallback, stateObject); result.BytesRead = -1; result.Count = numBytes; result.OriginalCount = numBytes; if (buf_dirty) { MemoryStream ms = new MemoryStream(); FlushBuffer(ms); ms.Write(array, offset, numBytes); offset = 0; numBytes = (int)ms.Length; } WriteDelegate w = new WriteDelegate(WriteInternal); return(w.BeginInvoke(array, offset, numBytes, userCallback, stateObject)); }
public override IAsyncResult BeginWrite(byte [] buffer, int offset, int count, AsyncCallback cback, object state) { //if (handle == MonoIO.InvalidHandle) // throw new ObjectDisposedException ("Stream has been closed"); if (!CanWrite) { throw new NotSupportedException("This stream does not support writing"); } if (buffer == null) { throw new ArgumentNullException("buffer"); } if (count < 0) { throw new ArgumentOutOfRangeException("count", "Must be >= 0"); } if (offset < 0) { throw new ArgumentOutOfRangeException("offset", "Must be >= 0"); } // reordered to avoid possible integer overflow if (count > buffer.Length - offset) { throw new ArgumentException("Buffer too small. count/offset wrong."); } if (!async) { return(base.BeginWrite(buffer, offset, count, cback, state)); } FileStreamAsyncResult result = new FileStreamAsyncResult(cback, state); result.BytesRead = -1; result.Count = count; result.OriginalCount = count; if (buf_dirty) { MemoryStream ms = new MemoryStream(); FlushBufferToStream(ms); ms.Write(buffer, offset, count); offset = 0; count = (int)ms.Length; } WriteDelegate w = new WriteDelegate(WriteInternal); return(w.BeginInvoke(buffer, offset, count, cback, state)); }
// When doing IO asynchronously (ie, _isAsync==true), this callback is // called by a free thread in the threadpool when the IO operation // completes. 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 FileStreamAsyncResult asyncResult = (FileStreamAsyncResult)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 == Win32FileStream.ERROR_BROKEN_PIPE || errorCode == Win32FileStream.ERROR_NO_DATA) { 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. asyncResult._completedSynchronously = false; asyncResult._isComplete = true; Interlocked.MemoryBarrier(); // ensure _isComplete is set before reading _waitHandle // The OS does not signal this event. We must do it ourselves. ManualResetEvent wh = asyncResult._waitHandle; if (wh != null) { Debug.Assert(!wh.GetSafeWaitHandle().IsClosed, "ManualResetEvent already closed!"); bool r = wh.Set(); Debug.Assert(r, "ManualResetEvent::Set failed!"); if (!r) { throw Win32Marshal.GetExceptionForLastWin32Error(); } } AsyncCallback userCallback = asyncResult._userCallback; if (userCallback != null) { userCallback(asyncResult); } }
public override IAsyncResult BeginWrite(byte [] array, int offset, int numBytes, AsyncCallback userCallback, object stateObject) { if (safeHandle.IsClosed) { throw new ObjectDisposedException("Stream has been closed"); } if (!CanWrite) { throw new NotSupportedException("This stream does not support writing"); } if (array == null) { throw new ArgumentNullException("array"); } if (numBytes < 0) { throw new ArgumentOutOfRangeException("numBytes", "Must be >= 0"); } if (offset < 0) { throw new ArgumentOutOfRangeException("offset", "Must be >= 0"); } // reordered to avoid possible integer overflow if (numBytes > array.Length - offset) { throw new ArgumentException("array too small. numBytes/offset wrong."); } if (!async) { return(base.BeginWrite(array, offset, numBytes, userCallback, stateObject)); } FileStreamAsyncResult result = new FileStreamAsyncResult(userCallback, stateObject); result.BytesRead = -1; result.Count = numBytes; result.OriginalCount = numBytes; /* * if (buf_dirty) { * MemoryStream ms = new MemoryStream (); * FlushBuffer (ms); * ms.Write (array, offset, numBytes); * * // Set arguments to new compounded buffer * offset = 0; * array = ms.ToArray (); * numBytes = array.Length; * } */ WriteDelegate w = WriteInternal; return(w.BeginInvoke(array, offset, numBytes, userCallback, stateObject)); }
internal static FileStreamAsyncResult CreateBufferedReadResult(int numBufferedBytes, AsyncCallback userCallback, Object userStateObject) { FileStreamAsyncResult asyncResult = new FileStreamAsyncResult(); asyncResult._userCallback = userCallback; asyncResult._userStateObject = userStateObject; asyncResult._isWrite = false; asyncResult._numBufferedBytes = numBufferedBytes; return asyncResult; }
public override IAsyncResult BeginWrite (byte [] array, int offset, int numBytes, AsyncCallback userCallback, object stateObject) { if (handle == MonoIO.InvalidHandle) throw new ObjectDisposedException ("Stream has been closed"); if (!CanWrite) throw new NotSupportedException ("This stream does not support writing"); if (array == null) throw new ArgumentNullException ("array"); if (numBytes < 0) throw new ArgumentOutOfRangeException ("numBytes", "Must be >= 0"); if (offset < 0) throw new ArgumentOutOfRangeException ("offset", "Must be >= 0"); // reordered to avoid possible integer overflow if (numBytes > array.Length - offset) throw new ArgumentException ("array too small. numBytes/offset wrong."); if (!async) return base.BeginWrite (array, offset, numBytes, userCallback, stateObject); FileStreamAsyncResult result = new FileStreamAsyncResult (userCallback, stateObject); result.BytesRead = -1; result.Count = numBytes; result.OriginalCount = numBytes; if (buf_dirty) { MemoryStream ms = new MemoryStream (); FlushBuffer (ms); ms.Write (array, offset, numBytes); offset = 0; numBytes = (int) ms.Length; } WriteDelegate w = new WriteDelegate (WriteInternal); return w.BeginInvoke (array, offset, numBytes, userCallback, stateObject); }
private unsafe FileStreamAsyncResult BeginWriteCore(byte[] bytes, int offset, int numBytes, AsyncCallback userCallback, object stateObject) { NativeOverlapped* overlappedPtr; FileStreamAsyncResult ar = new FileStreamAsyncResult { _handle = this._handle, _userCallback = userCallback, _userStateObject = stateObject, _isWrite = true }; ManualResetEvent event2 = new ManualResetEvent(false); ar._waitHandle = event2; Overlapped overlapped = new Overlapped(0, 0, IntPtr.Zero, ar); if (userCallback != null) { overlappedPtr = overlapped.Pack(IOCallback, bytes); } else { overlappedPtr = overlapped.UnsafePack(null, bytes); } ar._overlapped = overlappedPtr; if (this.CanSeek) { long length = this.Length; if (this._exposedHandle) { this.VerifyOSHandlePosition(); } if ((this._pos + numBytes) > length) { this.SetLengthCore(this._pos + numBytes); } overlappedPtr->OffsetLow = (int) this._pos; overlappedPtr->OffsetHigh = (int) (this._pos >> 0x20); this.SeekCore((long) numBytes, SeekOrigin.Current); } int hr = 0; if ((this.WriteFileNative(this._handle, bytes, offset, numBytes, overlappedPtr, out hr) == -1) && (numBytes != -1)) { if (hr == 0xe8) { ar.CallUserCallback(); return ar; } if (hr == 0x3e5) { return ar; } if (!this._handle.IsClosed && this.CanSeek) { this.SeekCore(0L, SeekOrigin.Current); } if (hr == 0x26) { __Error.EndOfFile(); return ar; } __Error.WinIOError(hr, string.Empty); } return ar; }
public override IAsyncResult BeginWrite(byte[] array, int offset, int numBytes, AsyncCallback userCallback, object stateObject) { if (array == null) { throw new ArgumentNullException("array"); } if (offset < 0) { throw new ArgumentOutOfRangeException("offset", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum")); } if (numBytes < 0) { throw new ArgumentOutOfRangeException("numBytes", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum")); } if ((array.Length - offset) < numBytes) { throw new ArgumentException(Environment.GetResourceString("Argument_InvalidOffLen")); } if (this._handle.IsClosed) { __Error.FileNotOpen(); } if (!this._isAsync) { return base.BeginWrite(array, offset, numBytes, userCallback, stateObject); } if (!this.CanWrite) { __Error.WriteNotSupported(); } if (this._isPipe) { if (this._writePos > 0) { this.FlushWrite(false); } return this.BeginWriteCore(array, offset, numBytes, userCallback, stateObject); } if (this._writePos == 0) { if (this._readPos < this._readLen) { this.FlushRead(); } this._readPos = 0; this._readLen = 0; } int num = this._bufferSize - this._writePos; if (numBytes <= num) { if (this._writePos == 0) { this._buffer = new byte[this._bufferSize]; } Buffer.InternalBlockCopy(array, offset, this._buffer, this._writePos, numBytes); this._writePos += numBytes; FileStreamAsyncResult result = new FileStreamAsyncResult { _userCallback = userCallback, _userStateObject = stateObject, _waitHandle = null, _isWrite = true, _numBufferedBytes = numBytes }; result.CallUserCallback(); return result; } if (this._writePos > 0) { this.FlushWrite(false); } return this.BeginWriteCore(array, offset, numBytes, userCallback, stateObject); }
private static void CBWrapper(IAsyncResult ares) { FileStreamAsyncResult fileStreamAsyncResult = (FileStreamAsyncResult)ares; fileStreamAsyncResult.realcb.BeginInvoke(ares, null, null); }
internal static FileStreamAsyncResult CreateBufferedReadResult(int numBufferedBytes, AsyncCallback userCallback, Object userStateObject, bool isWrite) { FileStreamAsyncResult asyncResult = new FileStreamAsyncResult(numBufferedBytes, userCallback, userStateObject, isWrite); asyncResult.CallUserCallback(); return asyncResult; }
static void CBWrapper(IAsyncResult ares) { FileStreamAsyncResult res = (FileStreamAsyncResult)ares; res.realcb.BeginInvoke(ares, null, null); }