private void ProcessDownloadedData(HTTPRequest request, HTTPResponse response)
    {
        //Debug.Log("Processing downloaded data");
        if (request == null || response == null)
        {
            return;
        }

        if (!mRequestToBytesDownloaded.ContainsKey(request))
        {
            mRequestToBytesDownloaded[request] = 0;
        }

        if (!response.IsStreamed)
        {
            byte[] data = response.Data;

            // Copy data into memory
            int offset = response.GetRange().FirstBytePos + mRequestToBytesDownloaded[request];
            int length = data.Length;
            CopyDownloadedFragment(offset, length, ref data);

            // Keep track of how many bytes this connection downloaded & consecutive bytes
            Interlocked.Add(ref mDownloadedContentSize, length);
            Interlocked.Add(ref mConsecutiveBytesCount, length);
            mDownloadComplete = mConsecutiveBytesCount >= mContentSize &&
                                mContentSize > 0;
        }
        else
        {
            List <byte[]> fragments = response.GetStreamedFragments();
            if (fragments == null)
            {
                return;
            }

            for (int i = 0; i < fragments.Count; i++)
            {
                byte[] fragment = fragments[i];

                // Copy data into memory
                int offset = response.GetRange().FirstBytePos + mRequestToBytesDownloaded[request];
                int length = fragment.Length;
                CopyDownloadedFragment(offset, length, ref fragment);

                // Keep track of how many bytes this connection downloaded & consecutive bytes
                Interlocked.Add(ref mDownloadedContentSize, length);
                mRequestToBytesDownloaded[request] += length;
                Interlocked.Add(ref mConsecutiveBytesCount, length);
                mDownloadComplete = mConsecutiveBytesCount >= mContentSize &&
                                    mContentSize > 0;
            }
        }
    }
Example #2
0
        private void OnHeadersReceived(HTTPRequest req, HTTPResponse resp, Dictionary <string, List <string> > newHeaders)
        {
            var range = resp.GetRange();

            if (range != null)
            {
                this.DownloadLength = range.ContentLength;
            }
            else
            {
                var contentLength = resp.GetFirstHeaderValue("content-length");
                if (contentLength != null)
                {
                    long length = 0;
                    if (long.TryParse(contentLength, out length))
                    {
                        this.DownloadLength = length;
                    }
                }
            }
        }