コード例 #1
0
        public void ReadStreamData()
        {
            data_read_event.WaitOne();              // make sure there's no pending data.

            data_mutex.WaitOne();
            data.Length = (int)http_stream.Read(data.Buffer, 0, data.Buffer.Length);
            data_mutex.ReleaseMutex();

            data_event.Set();
        }
コード例 #2
0
        async Task <bool> ReadFromServer(bool exitContext)
        {
            int index, count;
            var buffer = currentOperation.GetBuffer(out index, out count);

            nint ret;

            try {
                ret = stream.Read(buffer, index, count);
            } catch (Exception ex) {
                OnError(ex);
                return(false);
            }

            /*
             * If there are still bytes available to be read, then we'll immediately
             * get another BytesAvailable event, whereas calling stream.Read() again
             * could block.
             */

            if (ret < 0)
            {
                OnError(stream.GetError());
                return(false);
            }
            else if (ret == 0)
            {
                OnCompleted();
                return(false);
            }

            /*
             * We're normally called from the CFReadStream's OnBytesAvailableEvent
             * on the main thread, though OperationStarted() may also call us from
             * a ThreadPool thread.
             *
             * Release the lock while we're writing the data and re-acquire it when
             * done with that.  The server may send us a OnBytesAvailableEvent while
             * we're await'ing - if that happens, 'onBytesAvailable' will be set.
             */

            busy = true;
            if (exitContext)
            {
                Monitor.Exit(syncRoot);
            }

            bool keepGoing;

            try {
                keepGoing = await currentOperation.Write((int)ret);
            } finally {
                if (exitContext)
                {
                    Monitor.Enter(syncRoot);
                }
                busy = false;
            }

            /*
             * 'keepGoing' specifies whether the client wants more data from us.
             */

            if (keepGoing)
            {
                return(true);
            }

            var operation = Interlocked.Exchange(ref currentOperation, null);

            operation.SetCompleted();
            return(false);
        }