Exemplo n.º 1
0
 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;
 }
Exemplo 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);
         }
     }
 }
Exemplo n.º 3
0
            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;
                }
            }
Exemplo 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);
                }
            }
        }
Exemplo n.º 5
0
            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;
                }
            }
Exemplo n.º 6
0
 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;
 }
Exemplo n.º 7
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;
         }
     }
 }
Exemplo n.º 8
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;
                }
            }
        }
Exemplo n.º 9
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;
                }
            }
        }
Exemplo n.º 10
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);
                }
            }
        }