private void asyncReadAllReadCallback(IAsyncResult ar)
        {
            AsyncResult myar = (AsyncResult)ar.AsyncState;

            try {
                int len = baseStream.EndRead(ar);
                if (len <= 0)
                {
                    throw new EndOfStreamException();
                }
                myar.Offset += len;
                myar.Left   -= len;
                myar.Count  += len;
                if (myar.Left > 0)
                {
                    baseStream.BeginRead(myar.Buffer, myar.Offset, myar.Left, asyncReadAllReadCallback, ar);
                }
                else
                {
                    myar.SetCompleted(false, myar.Count, null);
                }
            } catch (Exception ex) {
                myar.SetCompleted(false, ex);
            }
        }
        private void asyncPrebufferReadCallback(IAsyncResult ar)
        {
            AsyncResult myar = (AsyncResult)ar.AsyncState;

            try {
                int len = baseStream.EndRead(ar);
                if (len <= 0)
                {
                    myar.SetCompleted(false, prebuffercount, null);
                }
                else
                {
                    myar.Left      -= len;
                    prebuffercount += len;
                    if (myar.Left > 0)
                    {
                        int off = prebufferoffset + prebuffercount;
                        baseStream.BeginRead(prebuffer, off, prebuffer.Length - off, asyncPrebufferReadCallback, myar);
                    }
                    else
                    {
                        myar.SetCompleted(false, prebuffercount, null);
                    }
                }
            } catch (Exception ex) {
                myar.SetCompleted(false, prebuffercount, ex);
            }
        }
Beispiel #3
0
        private IAsyncResult BeginAsyncReadOperation(AsyncResult ar)
        {
            Boolean synccompleted = false;

            lock (ReceiveQueue) {
                if (AsyncReceiveOperation != null)
                {
                    throw new InvalidOperationException("Another asynchronous operation is in progress");
                }
                if (ReceiveBuffer != null || ReceiveQueue.Count > 0)
                {
                    synccompleted = true;
                }
                else
                {
                    if (Closed)
                    {
                        throw new ObjectDisposedException("QueuedPacketStream", "The connection has been closed");
                    }
                    AsyncReceiveOperation = ar;
                }
            }
            if (synccompleted)
            {
                ar.SetCompleted(true);
            }
            return(ar);
        }
        public IAsyncResult BeginReadAll(byte[] buffer, int offset, int count, AsyncCallback callback, object state)
        {
            AsyncResult ar = new AsyncResult(callback, state);

            ar.Buffer = buffer;
            ar.Offset = 0;
            ar.Left   = count;
            ar.Count  = 0;
            if (prebuffercount > 0)
            {
                int read = Math.Min(ar.Left, prebuffercount);
                Buffer.BlockCopy(prebuffer, prebufferoffset, ar.Buffer, ar.Offset, read);
                prebufferoffset += read;
                prebuffercount  -= read;
                ar.Offset       += read;
                ar.Left         -= read;
                ar.Count        += read;
            }
            if (ar.Left > 0)
            {
                baseStream.BeginRead(ar.Buffer, ar.Offset, ar.Left, asyncReadAllReadCallback, ar);
            }
            else
            {
                ar.SetCompleted(true, count, null);
            }
            return(ar);
        }
Beispiel #5
0
        public static AsyncResult CreateCompleted(Exception ex)
        {
            AsyncResult ar = new AsyncResult();

            ar.SetCompleted(true, ex);
            return(ar);
        }
Beispiel #6
0
        public static AsyncResult CreateCompleted()
        {
            AsyncResult ar = new AsyncResult();

            ar.SetCompleted(true, null);
            return(ar);
        }
Beispiel #7
0
 protected void AddReadBufferNoCopy(Byte[] store)
 {
     if (Closed)
     {
         return;
     }
     lock (ReceiveQueue) {
         ReceiveQueue.Enqueue(store);
         Interlocked.Add(ref ReceiveWaiting, store.Length);
         if (AsyncReceiveOperation != null && (store.Length > 0 || AsyncReceiveOperation.IsReadPacket))
         {
             AsyncReceiveOperation.SetCompleted(false);
             AsyncReceiveOperation = null;
         }
         else
         {
             Monitor.Pulse(ReceiveQueue);
         }
     }
 }
        public IAsyncResult BeginPrebuffering(int count, AsyncCallback callback, Object state)
        {
            AsyncResult ar = new AsyncResult(callback, state);

            if (prebuffercount >= count)
            {
                ar.SetCompleted(true, prebuffercount, null);
            }
            else
            {
                PrepareBuffer(count);
                ar.Left = count - prebuffercount;
                int off = prebufferoffset + prebuffercount;
                baseStream.BeginRead(prebuffer, off, prebuffer.Length - off, asyncPrebufferReadCallback, ar);
            }
            return(ar);
        }
 public override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback callback, object state)
 {
     if (prebuffercount > 0)
     {
         if (count > prebuffercount)
         {
             count = prebuffercount;
         }
         Buffer.BlockCopy(prebuffer, prebufferoffset, buffer, offset, count);
         prebufferoffset += count;
         prebuffercount  -= count;
         AsyncResult ar = new AsyncResult(callback, state);
         ar.SetCompleted(true, count, null);
         return(ar);
     }
     else
     {
         return(baseStream.BeginRead(buffer, offset, count, callback, state));
     }
 }