コード例 #1
0
        public async Task <IWorkerResult> Run()
        {
            var httpRequest = new HttpRequest(Uri);

            return(await httpRequest.GetRequestStreamAsync().ContinueWith(request =>
            {
                var length = Convert.ToInt32(request.Result.Length);
                _profiler.OnBytesWrote(null, length);
            }).ContinueWith((task) =>
            {
                return httpRequest.GetResponseAsync().ContinueWith(response => new Response(response.Result)).Result;
            }));
        }
コード例 #2
0
        /// <summary>
        ///     Start using the current instance BinaryProfiler for the given stream
        /// </summary>
        /// <param name="stream">The stream to monitor</param>
        /// <param name="cancellationToken"></param>
        public Task <IBinaryProfiler> Monitor(Stream stream, CancellationToken cancellationToken)
        {
            if (stream == null)
            {
                throw new ArgumentNullException(nameof(stream));
            }

            return(Task.Run(() =>
            {
                if (cancellationToken.IsCancellationRequested)
                {
                    return _profiler;
                }

                var isSeekable = stream.CanSeek;
                long previousStreamPosition = -1;
                long nextPositionToRead = 0;
                do
                {
                    int readValue;

                    if (cancellationToken.IsCancellationRequested)
                    {
                        return _profiler;
                    }

                    lock (_streamSyncRoot)
                    {
                        if (isSeekable)
                        {
                            previousStreamPosition = stream.Position;
                            stream.Position = nextPositionToRead;
                        }

                        readValue = stream.ReadByte();
                        if (readValue > -1)
                        {
                            ++nextPositionToRead;
                        }

                        if (isSeekable)
                        {
                            stream.Position = previousStreamPosition;
                        }
                    }


                    if (readValue == -1)
                    {
                        if (nextPositionToRead != 0)
                        {
                            //End of stream reached:
                            return _profiler;
                        }

                        //Stream not yet feed, wait for input
                        if (cancellationToken.IsCancellationRequested)
                        {
                            return _profiler;
                        }

                        Thread.Sleep(500);
                        continue;
                    }

                    var bytes = new[] { Convert.ToByte(readValue) };

                    if (_isWriting)
                    {
                        _profiler.OnBytesWrote(bytes, 1);
                    }
                    else
                    {
                        _profiler.OnBytesRead(bytes, 1);
                    }
                } while (true);
            }, cancellationToken));
        }