public PendingIORequest(byte[] buffer, int offset, int count, IIORequestEventsHandler handler) { this.buffer = buffer; this.offset = offset; this.count = count; this.numOfBytesTransferred = 0; this.handler = handler; }
bool TryCompleteFlushRequest(IIORequestEventsHandler handler) { lock (this.thisLock) { if (this.writeQueue.HasPendingRequests) { this.writeQueue.Enqueue(null, 0, 0, handler); return(false); } else { handler.OnCompletion(true, 0); return(true); } } }
public void Enqueue(byte[] buffer, int offset, int count, IIORequestEventsHandler eventsHandler) { eventsHandler.OnEnquing(); if (this.hasPendingRequests) { if (this.pendingRequests == null) { this.pendingRequests = new Queue <PendingIORequest>(); } this.pendingRequests.Enqueue(new PendingIORequest(buffer, offset, count, eventsHandler)); } else { this.nextRequest = new PendingIORequest(buffer, offset, count, eventsHandler); this.hasPendingRequests = true; } }
bool TryCompleteWriteRequest(byte[] buffer, int offset, int count, IIORequestEventsHandler handler) { int numOfBytesWritten = 0; lock (this.thisLock) { while ((count > 0) && this.readQueue.HasPendingRequests) { if (this.readQueue.Head.Count < count) { int bytesToCopy = this.readQueue.Head.Count; Buffer.BlockCopy(buffer, offset, this.readQueue.Head.Buffer, this.readQueue.Head.Offset, bytesToCopy); numOfBytesWritten += bytesToCopy; offset += bytesToCopy; count -= bytesToCopy; this.readQueue.Dequeue().Complete(bytesToCopy); } else { Buffer.BlockCopy(buffer, offset, this.readQueue.Head.Buffer, this.readQueue.Head.Offset, count); this.readQueue.Dequeue().Complete(count); handler.OnCompletion(true, numOfBytesWritten + count); return(true); } } if (count > 0) { this.writeQueue.Enqueue(buffer, offset, count, handler); return(false); } else { handler.OnCompletion(true, numOfBytesWritten); return(true); } } }
public void Enqueue(byte[] buffer, int offset, int count, IIORequestEventsHandler eventsHandler) { eventsHandler.OnEnquing(); if (this.hasPendingRequests) { if (this.pendingRequests == null) { this.pendingRequests = new Queue<PendingIORequest>(); } this.pendingRequests.Enqueue(new PendingIORequest(buffer, offset, count, eventsHandler)); } else { this.nextRequest = new PendingIORequest(buffer, offset, count, eventsHandler); this.hasPendingRequests = true; } }
bool TryCompleteFlushRequest(IIORequestEventsHandler handler) { lock (this.thisLock) { if (this.writeQueue.HasPendingRequests) { this.writeQueue.Enqueue(null, 0, 0, handler); return false; } else { handler.OnCompletion(true, 0); return true; } } }
bool TryCompleteWriteRequest(byte[] buffer, int offset, int count, IIORequestEventsHandler handler) { int numOfBytesWritten = 0; lock (this.thisLock) { while ((count > 0) && this.readQueue.HasPendingRequests) { if (this.readQueue.Head.Count < count) { int bytesToCopy = this.readQueue.Head.Count; Buffer.BlockCopy(buffer, offset, this.readQueue.Head.Buffer, this.readQueue.Head.Offset, bytesToCopy); numOfBytesWritten += bytesToCopy; offset += bytesToCopy; count -= bytesToCopy; this.readQueue.Dequeue().Complete(bytesToCopy); } else { Buffer.BlockCopy(buffer, offset, this.readQueue.Head.Buffer, this.readQueue.Head.Offset, count); this.readQueue.Dequeue().Complete(count); handler.OnCompletion(true, numOfBytesWritten + count); return true; } } if (count > 0) { this.writeQueue.Enqueue(buffer, offset, count, handler); return false; } else { handler.OnCompletion(true, numOfBytesWritten); return true; } } }
bool TryCompleteReadRequest(byte[] buffer, int offset, int count, IIORequestEventsHandler handler, out bool shouldInvokeWriter) { int numOfBytesRead = 0; shouldInvokeWriter = false; lock (this.thisLock) { while ((count > 0) && this.writeQueue.HasPendingRequests) { if (this.writeQueue.Head.Buffer == null) { // This is a pending flush request, we just need to complete it this.writeQueue.Dequeue().Complete(0); } else if (this.writeQueue.Head.Count <= count) { int bytesToCopy = this.writeQueue.Head.Count; Buffer.BlockCopy(this.writeQueue.Head.Buffer, this.writeQueue.Head.Offset, buffer, offset, bytesToCopy); numOfBytesRead += bytesToCopy; offset += bytesToCopy; count -= bytesToCopy; this.writeQueue.Dequeue().Complete(bytesToCopy); } else { Buffer.BlockCopy(this.writeQueue.Head.Buffer, this.writeQueue.Head.Offset, buffer, offset, count); numOfBytesRead += count; this.writeQueue.Head.Progress(count); handler.OnCompletion(true, numOfBytesRead); return true; } } if ((this.noMoreData) || (numOfBytesRead > 0)) { if (this.writerException != null) { handler.OnException(this.writerException); this.writerException = null; } else { // Clearing the queue from leading flush request : while (this.writeQueue.HasPendingRequests && (this.writeQueue.Head.Buffer == null)) { this.writeQueue.Dequeue().Complete(0); } handler.OnCompletion(true, numOfBytesRead); } return true; } else { this.readQueue.Enqueue(buffer, offset, count, handler); if (!this.wasWriterInvoked) { this.wasWriterInvoked = true; shouldInvokeWriter = true; } return false; } } }
bool TryCompleteReadRequest(byte[] buffer, int offset, int count, IIORequestEventsHandler handler, out bool shouldInvokeWriter) { int numOfBytesRead = 0; shouldInvokeWriter = false; lock (this.thisLock) { while ((count > 0) && this.writeQueue.HasPendingRequests) { if (this.writeQueue.Head.Buffer == null) { // This is a pending flush request, we just need to complete it this.writeQueue.Dequeue().Complete(0); } else if (this.writeQueue.Head.Count <= count) { int bytesToCopy = this.writeQueue.Head.Count; Buffer.BlockCopy(this.writeQueue.Head.Buffer, this.writeQueue.Head.Offset, buffer, offset, bytesToCopy); numOfBytesRead += bytesToCopy; offset += bytesToCopy; count -= bytesToCopy; this.writeQueue.Dequeue().Complete(bytesToCopy); } else { Buffer.BlockCopy(this.writeQueue.Head.Buffer, this.writeQueue.Head.Offset, buffer, offset, count); numOfBytesRead += count; this.writeQueue.Head.Progress(count); handler.OnCompletion(true, numOfBytesRead); return(true); } } if ((this.noMoreData) || (numOfBytesRead > 0)) { if (this.writerException != null) { handler.OnException(this.writerException); this.writerException = null; } else { // Clearing the queue from leading flush request : while (this.writeQueue.HasPendingRequests && (this.writeQueue.Head.Buffer == null)) { this.writeQueue.Dequeue().Complete(0); } handler.OnCompletion(true, numOfBytesRead); } return(true); } else { this.readQueue.Enqueue(buffer, offset, count, handler); if (!this.wasWriterInvoked) { this.wasWriterInvoked = true; shouldInvokeWriter = true; } return(false); } } }