Ejemplo n.º 1
0
        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);
                }
            }
        }
Ejemplo n.º 2
0
 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);
         }
     }
 }
Ejemplo n.º 3
0
 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;
         }
     }
 }
Ejemplo n.º 4
0
        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;
                }
            }
        }
Ejemplo n.º 5
0
        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;
                }
            }
        }
Ejemplo n.º 6
0
        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);
                }
            }
        }