Exemplo n.º 1
0
        // Save the index to file, assuming it is loaded and sorted
        void CreateIndexFromPlaceInfos(ProgressReportDelegate pr, int nTask, int nTotal)
        {
            // initialize Writer
            using (FileStream fsIdx = File.Open(IndexFileName(), FileMode.Create, FileAccess.Write, FileShare.None))
                using (BinaryWriter bwIdx = new BinaryWriter(fsIdx, System.Text.Encoding.Default))
                {
                    // write index entry count (total overall place infos)
                    bwIdx.Write(this.m_indexedPlaces.Length);

                    // iterate over PlaceInfos
                    int nPlaceCount = m_indexedPlaces.Length;

                    for (int i = 0; i < nPlaceCount; i++)
                    {
                        IndexedPlace ip = m_indexedPlaces[i];
                        bwIdx.Write(ip.indexEntry.fileNumber);
                        bwIdx.Write(ip.indexEntry.seekOffset);
                        if (i % 100 == 0)
                        {                 // don't update progress too often as that slows down
                            if (pr != null)
                            {
                                pr(ProgressPercent(nTask, nTotal, 0, nPlaceCount, i), "Writing index");
                            }
                        }
                    }
                }
        }
        public static async Task<long> CopyWithProgressAsync(this Stream source, Stream destination,
            ProgressReportDelegate progressReport = null, long sourceLength = 0, int bufferSize = 64 * 1024)
        {
            long bytesWritten = 0;
            long totalBytesToWrite = sourceLength;

            byte[] copy_buffer = new byte[bufferSize];
            int read;
            while ((read = await source.ReadAsync(copy_buffer, 0, copy_buffer.Length)) > 0)
            {
                await destination.WriteAsync(copy_buffer, 0, read);
                bytesWritten += read;

                System.Diagnostics.Debug.WriteLine("CopyWithProgress: {0} / {1}", bytesWritten, totalBytesToWrite);
                if (null != progressReport)
                {
                    int percentComplete = 0;
                    if (sourceLength > 0)
                        percentComplete = (int)((bytesWritten/(double)totalBytesToWrite) * 100);    
                    progressReport(percentComplete, bytesWritten, totalBytesToWrite);
                }
            }

            await destination.FlushAsync();

            return bytesWritten;
        }
        public static async Task <long> CopyWithProgressAsync(this Stream source, Stream destination,
                                                              ProgressReportDelegate progressReport = null, long sourceLength = 0, int bufferSize = 64 * 1024)
        {
            long bytesWritten      = 0;
            long totalBytesToWrite = sourceLength;

            byte[] copy_buffer = new byte[bufferSize];
            int    read;

            while ((read = await source.ReadAsync(copy_buffer, 0, copy_buffer.Length)) > 0)
            {
                await destination.WriteAsync(copy_buffer, 0, read);

                bytesWritten += read;

                System.Diagnostics.Debug.WriteLine("CopyWithProgress: {0} / {1}", bytesWritten, totalBytesToWrite);
                if (null != progressReport)
                {
                    int percentComplete = 0;
                    if (sourceLength > 0)
                    {
                        percentComplete = (int)((bytesWritten / (double)totalBytesToWrite) * 100);
                    }
                    progressReport(percentComplete, bytesWritten, totalBytesToWrite);
                }
            }

            await destination.FlushAsync();

            return(bytesWritten);
        }
Exemplo n.º 4
0
        static void StreamCopy(Stream from, Stream to, ProgressReportDelegate cb, long ContentLength)
        {
            byte[] buffer           = new byte[4 * 1024];
            long   totalRead        = 0;
            double invContentLength = ContentLength == 0 ? 0 : 1d / ContentLength;

            try
            {
                while (true)
                {
                    if (!from.CanRead)
                    {
                        return;
                    }
                    //copy
                    var read = from.Read(buffer, 0, buffer.Length);
                    if (read == 0 || read == -1)
                    {
                        return;
                    }
                    to.Write(buffer, 0, read);
                    //accumulate and compute percentage
                    totalRead += read;
                    double perc = (totalRead * invContentLength).Saturate();
                    try { cb?.Invoke(totalRead, ContentLength, perc); } catch { cb = null; } //if it causes exception then be gone with it
                }
            }
            finally { try { cb?.Invoke(totalRead, ContentLength, 1); } catch { } } //closure
        }
Exemplo n.º 5
0
 /// <summary>
 /// Creates an index from placeitems
 /// </summary>
 /// <param name="pr">Callback to provide progress report</param>
 public void CreateIndex(ProgressReportDelegate pr)
 {
     LoadPlaceInfos(pr, 1, 3);
     SortPlaceInfos(pr, 2, 3);
     CreateIndexFromPlaceInfos(pr, 3, 3);
     DiscardPlaceInfos();
 }
Exemplo n.º 6
0
 public static void HardWork(ProgressReportDelegate d)
 {
     for (int i = 0; i < 10; i++)
     {
         d(i * 10);                           // Invoke delegate
         System.Threading.Thread.Sleep(1000); // Simulate hard work
     }
 }
Exemplo n.º 7
0
 // Sort Place Info structures
 void SortPlaceInfos(ProgressReportDelegate pr, int nTask, int nTotal)
 {
     if (pr != null)
     {
         pr(ProgressPercent(nTask, nTotal, 0, 1, 0), "Sorting");
     }
     Array.Sort(m_indexedPlaces, new PlaceItemComparer());
     if (pr != null)
     {
         pr(ProgressPercent(nTask, nTotal, 0, 1, 1), "Sorting");
     }
 }
Exemplo n.º 8
0
        public static TraceResult Render(TraceParams parameters, ProgressReportDelegate progressReport = null, float[] backbuffer = null)
        {
            // Allocate backbuffer
            if (ReferenceEquals(backbuffer, null) || backbuffer.Length != (parameters.width * parameters.height * 3))
            {
                backbuffer = new float[parameters.width * parameters.height * 3];
            }

            TraceResult result = new TraceResult()
            {
                parameters = parameters,
                backbuffer = backbuffer
            };

            TraceRuntimeParams runtimeParams = new TraceRuntimeParams();

            runtimeParams.invWidth           = 1.0f / parameters.width;
            runtimeParams.invHeight          = 1.0f / parameters.height;
            runtimeParams.invSamplesPerPixel = 1.0f / parameters.samplesPerPixel;
            runtimeParams.sampleBatchCount   = (int)Mathf.Ceiling(parameters.samplesPerPixel / (float)runtimeParams.batchSize);

            List <TileTraceParameters> traceTiles = new List <TileTraceParameters>();

            for (int y = 0; y < parameters.height; y += parameters.traceTileDimension)
            {
                for (int x = 0; x < parameters.width; x += parameters.traceTileDimension)
                {
                    var tt = new TileTraceParameters()
                    {
                        xStart        = x,
                        yStart        = y,
                        parameters    = parameters,
                        runtimeParams = runtimeParams
                    };

                    tt.xEnd = Math.Min(tt.xStart + parameters.traceTileDimension, parameters.width);
                    tt.yEnd = Math.Min(tt.yStart + parameters.traceTileDimension, parameters.height);
                    traceTiles.Add(tt);
                }
            }

            parameters.tracingProcessor.Trace(traceTiles, ref result, progressReport);
            return(result);
        }
Exemplo n.º 9
0
        // load place infos for all wwp files belonging to this index / layer
        void LoadPlaceInfos(ProgressReportDelegate pr, int nTask, int nTotal)
        {
            // count number of places in all wwp
            int nSize = this.PlaceCount;

            // allocate an array of matching size
            m_indexedPlaces = new IndexedPlace [nSize];

            int nEntryCount = 0; // keep track of current position
            int nWwpCount   = this.m_strWwpNames.Length;

            for (int i = 0; i < nWwpCount; i++)
            { // loop over Wwp
                // handle a single Wwp - will increment nEntryCount
                AddSingleWwpPlaces(i, ref nEntryCount);
                if (pr != null)
                {
                    pr(ProgressPercent(nTask, nTotal, 0, nWwpCount, i), "Loading place infos");
                }
            }
        }
Exemplo n.º 10
0
        /// <summary>
        /// Loads an index from file (implicitly known via the name of the wpl file)
        /// </summary>
        /// <param name="pr">Callback to provide progress report</param>
        public void Load(ProgressReportDelegate pr)
        {
            // initialize Reader
            using (BinaryReader brIdx = OpenBinReader(IndexFileName()))
            {
                // read index entry count
                int nCount = brIdx.ReadInt32();

                // allocate an array of that size
                this.m_indexEntries = new WplIndexEntry [nCount];

                // read index entries
                for (int i = 0; i < nCount; i++)
                {
                    WplIndexEntry ie = new WplIndexEntry();

                    ie.fileNumber     = brIdx.ReadInt16(); // read file number
                    ie.seekOffset     = brIdx.ReadInt32(); // read seek offset
                    m_indexEntries[i] = ie;
                }
            }
        }
Exemplo n.º 11
0
        /// <summary>
        /// Locks the index: makes sure it is loaded and referenced
        /// </summary>
        /// <param name="pr">Callback to provide progress report</param>
        public void Lock(ProgressReportDelegate pr)
        {
            if (this.IsLoaded)
            {
                return;            // seems fine and "locked" already
            }
            // not loaded, try the weak reference
            if (this.weakReferenceIndexEntries == null)
            {
                // that doesn't look good, weakref not there. Allocate it
                this.weakReferenceIndexEntries = new WeakReference(null);
            }

            // weakref still valid ?
            if (this.weakReferenceIndexEntries.IsAlive)
            { // yes, restore the array from the weakref
                m_indexEntries = (WplIndexEntry [])this.weakReferenceIndexEntries.Target;
            }
            else
            { // no - load again, and save as weakref as well
                Load(pr);
                this.weakReferenceIndexEntries = new WeakReference(m_indexEntries);
            }
        }
Exemplo n.º 12
0
 public static RequestResult RequestGET(string url, Dictionary <string, string> data, IEnumerable <Cookie> Cookies, ICredentials Credentials, ProgressReportDelegate progressReport)
 {
     return(Request(HttpMethods.Get, url + "?" + BuildParameters(data), null, HttpRequestDataFormat.Text, null, Cookies, Credentials, null, progressReport));
 }
Exemplo n.º 13
0
 public static RequestResult RequestGET(string url, IEnumerable <Cookie> Cookies, ICredentials Credentials, ProgressReportDelegate progressReport)
 {
     return(Request(HttpMethods.Get, url, null, HttpRequestDataFormat.FormData, null, Cookies, Credentials, null, progressReport));
 }
Exemplo n.º 14
0
 public static RequestResult RequestGET(string url, ICredentials Credentials, ProgressReportDelegate progressReport)
 {
     return Request(HttpMethods.Get, url, null, HttpRequestDataFormat.FormData, null, null, Credentials, null, progressReport);
 }
Exemplo n.º 15
0
 static void StreamCopy(Stream from, Stream to, ProgressReportDelegate cb, long ContentLength)
 {
     byte[] buffer = new byte[4 * 1024];
     long totalRead = 0;
     double invContentLength = ContentLength == 0 ? 0 : 1d / ContentLength;
     try
     {
         while (true)
         {
             if (!from.CanRead)
                 return;
             //copy
             var read = from.Read(buffer, 0, buffer.Length);
             if (read == 0 || read == -1)
                 return;
             to.Write(buffer, 0, read);
             //accumulate and compute percentage
             totalRead += read;
             double perc = (totalRead * invContentLength).Saturate();
             try { cb?.Invoke(totalRead, ContentLength, perc); } catch { cb = null; } //if it causes exception then be gone with it
         }
     }
     finally { try { cb?.Invoke(totalRead, ContentLength, 1); } catch { } } //closure
 }
Exemplo n.º 16
0
        private static RequestResult _Request(HttpMethods method, string url, byte[] data, HttpRequestDataFormat dataFormat, Dictionary<string, string> headers, IEnumerable<Cookie> Cookies, ICredentials Credentials, Action<RequestResult> callback, ProgressReportDelegate progressReportCallback)
        {
            //enable protocols
#if NETFX
#if __MonoCS__
            try{ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;}catch{}
#warning WARNING the system is not secure when using only TLS1 !!!! you must use visual studio.
#elif UNIVERSAL
            try{ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;} catch{}
#endif
#endif

            //add request cookies
            var cookieContainer = new CookieContainer();
            if (Cookies != null)
                foreach (var entry in Cookies)
#if NETFX
                    cookieContainer.Add(entry);
#elif UNIVERSAL
                    cookieContainer.Add(new Uri(url), entry);
#endif

            //creat request
            HttpWebRequest webRequest;
            try { webRequest = (HttpWebRequest)WebRequest.Create(url); }
            catch (Exception ex)
            {
                DebugEx.TraceError(ex, "Http request failed");
                if (callback != null)
                    callback(default(RequestResult));
                return default(RequestResult);
            }

            //setup request
            webRequest.CookieContainer = cookieContainer;
            if (Credentials == null)
                webRequest.UseDefaultCredentials = true;
            else
                webRequest.Credentials = Credentials;
#if NETFX
            webRequest.PreAuthenticate = true;
            webRequest.ServicePoint.Expect100Continue = false;

            //collect certificates
            /*
            if (!EnvironmentEx.IsRunningOnMono) 
                webRequest.ClientCertificates = Tools.Certificates.CollectCertificates();
            */

            webRequest.AllowAutoRedirect = true;
            webRequest.MaximumAutomaticRedirections = 10;
            webRequest.AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip | DecompressionMethods.None;
            webRequest.UserAgent = UserAgent;
#elif UNIVERSAL
            webRequest.Headers[HttpRequestHeader.UserAgent] = UserAgent;
#endif
            webRequest.Accept = "*/*";

            try
            {
#if NETFX
                //build headers
                if (headers != null)
                    foreach (var header in headers)
                        webRequest.Headers.Add(header.Key, header.Value);
#endif
                //setup method
                switch (method)
                {
                    case HttpMethods.Get:
                        webRequest.Method = "GET";
                        break;
                    case HttpMethods.Post:
                        webRequest.Method = "POST";
                        break;
                    case HttpMethods.Put:
                        webRequest.Method = "PUT";
                        break;
                    default:
                        DebugEx.Assert("Invalid Method");
                        throw new NotImplementedException("Invalid Method");
                }

                //build request
                if (method == HttpMethods.Get)
                {
#if NETFX
                    webRequest.ContentLength = 0;
                    webRequest.ContentType = webRequest.MediaType = "application/x-www-form-urlencoded";
#endif
                }
                else
                {
                    string contentType = null;
                    switch (dataFormat)
                    {
                        case HttpRequestDataFormat.Json:
                            contentType = "application/json";
                            break;
                        case HttpRequestDataFormat.Xml:
                            contentType = "application/xhtml+xml";
                            break;
                        case HttpRequestDataFormat.FormData:
                            contentType = "application/x-www-form-urlencoded";
                            break;
                        case HttpRequestDataFormat.Text:
                            contentType = "mime/text";
                            break;
                        case HttpRequestDataFormat.Soap:
                            if (headers.ContainsKey("SoapContent-Type"))
                            {
                                contentType = headers["SoapContent-Type"];
                                webRequest.Headers.Remove("SoapContent-Type");
                            }
                            break;
                        case HttpRequestDataFormat.Binary:
                            contentType = "application/octet-stream";
                            break;
                    }
                    webRequest.ContentType = contentType;
#if NETFX
                    webRequest.MediaType = contentType;
#endif

                    //write body data
                    var body_data = data ?? new byte[0];
#if NETFX
                    webRequest.ContentLength = body_data.Length;
                    using (var stream = webRequest.GetRequestStream())
                        stream.Write(body_data, 0, body_data.Length);
#elif UNIVERSAL
                    using (var stream = webRequest.GetRequestStreamAsync().GetResults())
                        stream.Write(body_data, 0, body_data.Length);
#endif
                }

                //get response
                HttpWebResponse response = null;
                Stream respstream = null;
                try
                {
#if NETFX
                    var _response = webRequest.GetResponse();
#elif UNIVERSAL
                    var _response = webRequest.GetResponseAsync().GetResults();
#endif
                    response = (HttpWebResponse)_response;
                    respstream = response.GetResponseStream();
                }
                catch (WebException ex)
                {
                    response = ex.Response as HttpWebResponse;
                    if (response != null && response.StatusCode == HttpStatusCode.RedirectMethod)
                        DebugEx.TraceLog("Redirected from url " + url + " to url " + response.ResponseUri);
                    else if (response != null && response.StatusCode == HttpStatusCode.NotFound)
                        DebugEx.TraceLog("404 - Not Found. Url=" + url);
                    else
                        DebugEx.TraceError(ex, "Unhandled exception during webRequest.GetResponse() for url " + url);
                    //try get response stream
                    try
                    {
                        if (response != null)
                            respstream = response.GetResponseStream();
                    }
                    catch (WebException ex2)
                    {
                        DebugEx.TraceError(ex2, "Unhandled exception during redirect webRequest.GetResponseStream() for url " + url);
                    }
                }


                //Read results and execute callback                
                if (response != null)
                {
                    //find encoding
                    var encoding = response.Headers["content-encoding"];
                    var contentType = response.Headers["content-type"];
                    var contentTypeEntries = contentType?.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
                    if (contentTypeEntries != null)
                        for (int n = 0; n > contentTypeEntries.Length; n++)
                            contentTypeEntries[n] = contentTypeEntries[n].Trim();
                    //find charset
                    string charset = "";
                    if (contentTypeEntries != null && contentType != null && contentType.Contains("charset="))
                        try
                        {
                            var entry = contentTypeEntries.FirstOrDefault(e => e.ToLowerInvariant().StartsWith("charset="));
                            if (!string.IsNullOrWhiteSpace(entry))
                                charset = entry.Split(new[] { "=" }, StringSplitOptions.RemoveEmptyEntries)[1].ToLowerInvariant();
                        }
                        catch { charset = "utf-8"; }

                    //consume stream
                    byte[] bytes = null;
                    if (respstream != null)
                        try
                        {
                            using (var memStream = new MemoryStream())
                            {
                                if (progressReportCallback == null)
                                {
                                    //unzip or just copy to memstream
                                    if (encoding != null && encoding.ToLowerInvariant() == "gzip")
                                        using (var defStream = new GZipStream(respstream, CompressionMode.Decompress))
                                            defStream.CopyTo(memStream);
                                    else
                                        respstream.CopyTo(memStream);
                                }
                                else
                                {
                                    //unzip or just copy to memstream
                                    if (encoding != null && encoding.ToLowerInvariant() == "gzip")
                                        using (var defStream = new GZipStream(respstream, CompressionMode.Decompress))
                                            StreamCopy(defStream, memStream, progressReportCallback, response.ContentLength);
                                    else
                                        StreamCopy(respstream, memStream, progressReportCallback, response.ContentLength);
                                }

                                //get bytes
                                bytes = memStream.ToArray();
                            }
                        }
                        catch (Exception ex) { DebugEx.Assert(ex, "Unhandled exception while reading response stream"); }

                    //build result
                    var result = new RequestResult()
                    {
                        IsValid = true,
                        ResponseUri = response.ResponseUri,
                        ResponseBodyBinary = bytes,
                        ContentTypeEntries = contentTypeEntries,
                        Charset = charset,
                        Cookies = cookieContainer.GetCookies(new Uri(url)).Cast<Cookie>().ToList(),
                        StatusCode = response.StatusCode,
                        IsSuccessStatusCode = ((int)response.StatusCode >= 200 && (int)response.StatusCode <= 299),
                    };
                    if (callback != null)
                        callback(result);
                    //close and dispose response
#if NETFX
                    response.Close();
#endif
                    response.Dispose();
                    //give result back
                    return result;
                }
                else
                    return default(RequestResult);
            }
            catch (Exception ex)
            {
                DebugEx.TraceError(ex, "Http request failed");
                return default(RequestResult);
            }
        }
Exemplo n.º 17
0
      /// <summary>
      /// Loads an index from file (implicitly known via the name of the wpl file)
      /// </summary>
      /// <param name="pr">Callback to provide progress report</param>
      public void Load(ProgressReportDelegate pr) 
      {
         // initialize Reader
         using(BinaryReader brIdx = OpenBinReader(IndexFileName())) 
         {
            // read index entry count
            int nCount = brIdx.ReadInt32();

            // allocate an array of that size
            this.m_indexEntries = new WplIndexEntry [nCount];

            // read index entries
            for(int i = 0; i < nCount; i++) 
            {
               WplIndexEntry ie = new WplIndexEntry();

               ie.fileNumber = brIdx.ReadInt16(); // read file number
               ie.seekOffset = brIdx.ReadInt32(); // read seek offset
               m_indexEntries[i] = ie;
            }
         }
      }
Exemplo n.º 18
0
        public void Trace(List <TileTraceParameters> traceTiles, ref TraceResult result, ProgressReportDelegate progressReport = null)
        {
            long rc = 0;

            for (int i = 0; i < traceTiles.Count; i++)
            {
                Tracer.TraceTile(traceTiles[i], result.backbuffer, out rc);
                progressReport?.Invoke(traceTiles.Count, i);
                result.rayCount += rc;
            }
        }
Exemplo n.º 19
0
 public static RequestResult RequestGET(string url, IEnumerable<Cookie> Cookies, ProgressReportDelegate progressReport)
 {
     return Request(HttpMethods.Get, url, null, HttpRequestDataFormat.FormData, null, Cookies, null, null, progressReport);
 }
Exemplo n.º 20
0
      // load place infos for all wwp files belonging to this index / layer
      void LoadPlaceInfos(ProgressReportDelegate pr, int nTask, int nTotal) 
      {
         // count number of places in all wwp
         int nSize = this.PlaceCount;

         // allocate an array of matching size
         m_indexedPlaces = new IndexedPlace [nSize];

         int nEntryCount = 0; // keep track of current position
         int nWwpCount = this.m_strWwpNames.Length;
         for(int i=0; i < nWwpCount;  i++) 
         { // loop over Wwp
            // handle a single Wwp - will increment nEntryCount
            AddSingleWwpPlaces(i, ref nEntryCount);
            if(pr != null) pr(ProgressPercent(nTask, nTotal, 0, nWwpCount, i), "Loading place infos");
         }
      }
Exemplo n.º 21
0
        public void Trace(List <TileTraceParameters> traceTiles, ref TraceResult result, ProgressReportDelegate progressReport = null)
        {
            AutoResetEvent evt = new AutoResetEvent(false);

            float[] backbuffer     = result.backbuffer;
            long[]  rayCounts      = new long[traceTiles.Count];
            int     tilesProcessed = 0;
            int     c = traceTiles.Count;

            for (int i = 0; i < c; i++)
            {
                int j  = i; // Move to local scope for lambda
                var tt = traceTiles[i];
                ThreadPool.QueueUserWorkItem(_ =>
                {
                    Tracer.TraceTile(tt, backbuffer, out rayCounts[j]);
                    int processed = Interlocked.Increment(ref tilesProcessed);

                    progressReport?.Invoke(traceTiles.Count, processed);
                    if (processed == traceTiles.Count)
                    {
                        evt.Set();
                    }
                });
            }

            evt.WaitOne();
            result.rayCount = 0;
            for (int i = 0; i < rayCounts.Length; i++)
            {
                result.rayCount += rayCounts[i];
            }
        }
Exemplo n.º 22
0
 // Sort Place Info structures
 void SortPlaceInfos(ProgressReportDelegate pr, int nTask, int nTotal) 
 {
    if(pr != null) pr(ProgressPercent(nTask, nTotal, 0, 1, 0), "Sorting");
    Array.Sort(m_indexedPlaces, new PlaceItemComparer());
    if(pr != null) pr(ProgressPercent(nTask, nTotal, 0, 1, 1), "Sorting");
 }
Exemplo n.º 23
0
      // Save the index to file, assuming it is loaded and sorted
      void CreateIndexFromPlaceInfos(ProgressReportDelegate pr, int nTask, int nTotal) 
      {
         // initialize Writer
         using( FileStream fsIdx = File.Open(IndexFileName(), FileMode.Create, FileAccess.Write, FileShare.None) )
			using( BinaryWriter bwIdx = new BinaryWriter(fsIdx, System.Text.Encoding.Default) )
			{
				// write index entry count (total overall place infos)
				bwIdx.Write(this.m_indexedPlaces.Length);

				// iterate over PlaceInfos
				int nPlaceCount = m_indexedPlaces.Length;

				for(int i = 0; i < nPlaceCount; i++) 
				{
					IndexedPlace ip = m_indexedPlaces[i];
					bwIdx.Write(ip.indexEntry.fileNumber);
					bwIdx.Write(ip.indexEntry.seekOffset);
					if(i % 100 == 0) 
					{ // don't update progress too often as that slows down
						if(pr != null) pr(ProgressPercent(nTask, nTotal, 0, nPlaceCount, i), "Writing index");
					}
				}
			}
      }
Exemplo n.º 24
0
 /// <summary>
 /// Creates an index from placeitems
 /// </summary>
 /// <param name="pr">Callback to provide progress report</param>
 public void CreateIndex(ProgressReportDelegate pr) 
 {
    LoadPlaceInfos(pr, 1, 3);
    SortPlaceInfos(pr, 2, 3);
    CreateIndexFromPlaceInfos(pr, 3, 3);
    DiscardPlaceInfos();
 }
Exemplo n.º 25
0
 public static RequestResult Request(HttpMethods method, string url, string data, HttpRequestDataFormat dataFormat, Dictionary <string, string> headers, IEnumerable <Cookie> Cookies, ICredentials Credentials, Action <RequestResult> callback, ProgressReportDelegate progressReportCallback)
 {
     return(_Request(method, url, data != null ? Encoding.UTF8.GetBytes(data) : null, dataFormat, headers, Cookies, Credentials, callback, progressReportCallback));
 }
Exemplo n.º 26
0
 public static RequestResult RequestGET(string url, Dictionary<string, string> data, IEnumerable<Cookie> Cookies, ICredentials Credentials, ProgressReportDelegate progressReport)
 {
     return Request(HttpMethods.Get, url + "?" + BuildParameters(data), null, HttpRequestDataFormat.Text, null, Cookies, Credentials, null, progressReport);
 }
Exemplo n.º 27
0
        private static RequestResult _Request(HttpMethods method, string url, byte[] data, HttpRequestDataFormat dataFormat, Dictionary <string, string> headers, IEnumerable <Cookie> Cookies, ICredentials Credentials, Action <RequestResult> callback, ProgressReportDelegate progressReportCallback)
        {
            //enable protocols
#if NETFX
#if __MonoCS__
            try{ ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls; }catch {}
#warning WARNING the system is not secure when using only TLS1 !!!! you must use visual studio.
#elif UNIVERSAL
            try{ ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls; } catch {}
#endif
#endif

            //add request cookies
            var cookieContainer = new CookieContainer();
            if (Cookies != null)
            {
                foreach (var entry in Cookies)
#if NETFX
                { cookieContainer.Add(entry); }
            }
#elif UNIVERSAL
                { cookieContainer.Add(new Uri(url), entry); }
#endif

            //creat request
            HttpWebRequest webRequest;
            try { webRequest = (HttpWebRequest)WebRequest.Create(url); }
            catch (Exception ex)
            {
                DebugEx.TraceError(ex, "Http request failed");
                if (callback != null)
                {
                    callback(default(RequestResult));
                }
                return(default(RequestResult));
            }

            //setup request
            try
            {
                webRequest.CookieContainer = cookieContainer;
                if (Credentials == null)
                {
                    webRequest.UseDefaultCredentials = true;
                }
                else
                {
                    webRequest.Credentials = Credentials;
                }
            }
            catch (Exception) { } //throw away credential errors (propably due to permission errors)

#if NETFX
            webRequest.PreAuthenticate = true;
            webRequest.ServicePoint.Expect100Continue = false;

            //collect certificates

            /*
             * if (!EnvironmentEx.IsRunningOnMono)
             *  webRequest.ClientCertificates = Tools.Certificates.CollectCertificates();
             */

            webRequest.AllowAutoRedirect            = true;
            webRequest.MaximumAutomaticRedirections = 10;
            webRequest.AutomaticDecompression       = DecompressionMethods.Deflate | DecompressionMethods.GZip | DecompressionMethods.None;
            webRequest.UserAgent = UserAgent;
#elif UNIVERSAL
            webRequest.Headers[HttpRequestHeader.UserAgent] = UserAgent;
#endif
            webRequest.Accept = "*/*";

            try
            {
#if NETFX
                //build headers
                if (headers != null)
                {
                    foreach (var header in headers)
                    {
                        webRequest.Headers.Add(header.Key, header.Value);
                    }
                }
#endif
                //setup method
                switch (method)
                {
                case HttpMethods.Get:
                    webRequest.Method = "GET";
                    break;

                case HttpMethods.Post:
                    webRequest.Method = "POST";
                    break;

                case HttpMethods.Put:
                    webRequest.Method = "PUT";
                    break;

                default:
                    DebugEx.Assert("Invalid Method");
                    throw new NotImplementedException("Invalid Method");
                }

                //build request
                if (method == HttpMethods.Get)
                {
#if NETFX
                    webRequest.ContentLength = 0;
                    webRequest.ContentType   = webRequest.MediaType = "application/x-www-form-urlencoded";
#endif
                }
                else
                {
                    string contentType = null;
                    switch (dataFormat)
                    {
                    case HttpRequestDataFormat.Json:
                        contentType = "application/json";
                        break;

                    case HttpRequestDataFormat.Xml:
                        contentType = "application/xhtml+xml";
                        break;

                    case HttpRequestDataFormat.FormData:
                        contentType = "application/x-www-form-urlencoded";
                        break;

                    case HttpRequestDataFormat.Text:
                        contentType = "mime/text";
                        break;

                    case HttpRequestDataFormat.Soap:
                        if (headers.ContainsKey("SoapContent-Type"))
                        {
                            contentType = headers["SoapContent-Type"];
                            webRequest.Headers.Remove("SoapContent-Type");
                        }
                        break;

                    case HttpRequestDataFormat.Binary:
                        contentType = "application/octet-stream";
                        break;
                    }
                    webRequest.ContentType = contentType;
#if NETFX
                    webRequest.MediaType = contentType;
#endif

                    //write body data
                    var body_data = data ?? new byte[0];
#if NETFX
                    webRequest.ContentLength = body_data.Length;
                    using (var stream = webRequest.GetRequestStream())
                        stream.Write(body_data, 0, body_data.Length);
#elif UNIVERSAL
                    using (var stream = webRequest.GetRequestStreamAsync().GetResults())
                        stream.Write(body_data, 0, body_data.Length);
#endif
                }

                //get response
                HttpWebResponse response   = null;
                Stream          respstream = null;
                try
                {
#if NETFX
                    var _response = webRequest.GetResponse();
#elif UNIVERSAL
                    var _response = webRequest.GetResponseAsync().GetResults();
#endif
                    response   = (HttpWebResponse)_response;
                    respstream = response.GetResponseStream();
                }
                catch (WebException ex)
                {
                    response = ex.Response as HttpWebResponse;
                    if (response != null && response.StatusCode == HttpStatusCode.RedirectMethod)
                    {
                        DebugEx.TraceLog("Redirected from url " + url + " to url " + response.ResponseUri);
                    }
                    else if (response != null && response.StatusCode == HttpStatusCode.NotFound)
                    {
                        DebugEx.TraceLog("404 - Not Found. Url=" + url);
                    }
                    else
                    {
                        DebugEx.TraceError(ex, "Unhandled exception during webRequest.GetResponse() for url " + url);
                    }
                    //try get response stream
                    try
                    {
                        if (response != null)
                        {
                            respstream = response.GetResponseStream();
                        }
                    }
                    catch (WebException ex2)
                    {
                        DebugEx.TraceError(ex2, "Unhandled exception during redirect webRequest.GetResponseStream() for url " + url);
                    }
                }


                //Read results and execute callback
                if (response != null)
                {
                    //find encoding
                    var encoding           = response.Headers["content-encoding"];
                    var contentType        = response.Headers["content-type"];
                    var contentTypeEntries = contentType?.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
                    if (contentTypeEntries != null)
                    {
                        for (int n = 0; n > contentTypeEntries.Length; n++)
                        {
                            contentTypeEntries[n] = contentTypeEntries[n].Trim();
                        }
                    }
                    //find charset
                    string charset = "";
                    if (contentTypeEntries != null && contentType != null && contentType.Contains("charset="))
                    {
                        try
                        {
                            var entry = contentTypeEntries.FirstOrDefault(e => e.ToLowerInvariant().StartsWith("charset="));
                            if (!string.IsNullOrWhiteSpace(entry))
                            {
                                charset = entry.Split(new[] { "=" }, StringSplitOptions.RemoveEmptyEntries)[1].ToLowerInvariant();
                            }
                        }
                        catch { charset = "utf-8"; }
                    }

                    //consume stream
                    byte[] bytes = null;
                    if (respstream != null)
                    {
                        try
                        {
                            using (var memStream = new MemoryStream())
                            {
                                if (progressReportCallback == null)
                                {
                                    //unzip or just copy to memstream
                                    if (encoding != null && encoding.ToLowerInvariant() == "gzip")
                                    {
                                        using (var defStream = new GZipStream(respstream, CompressionMode.Decompress))
                                            defStream.CopyTo(memStream);
                                    }
                                    else
                                    {
                                        respstream.CopyTo(memStream);
                                    }
                                }
                                else
                                {
                                    //unzip or just copy to memstream
                                    if (encoding != null && encoding.ToLowerInvariant() == "gzip")
                                    {
                                        using (var defStream = new GZipStream(respstream, CompressionMode.Decompress))
                                            StreamCopy(defStream, memStream, progressReportCallback, response.ContentLength);
                                    }
                                    else
                                    {
                                        StreamCopy(respstream, memStream, progressReportCallback, response.ContentLength);
                                    }
                                }

                                //get bytes
                                bytes = memStream.ToArray();
                            }
                        }
                        catch (Exception ex) { DebugEx.Assert(ex, "Unhandled exception while reading response stream"); }
                    }

                    //build result
                    var result = new RequestResult()
                    {
                        IsValid             = true,
                        ResponseUri         = response.ResponseUri,
                        ResponseBodyBinary  = bytes,
                        ContentTypeEntries  = contentTypeEntries,
                        Charset             = charset,
                        Cookies             = cookieContainer.GetCookies(new Uri(url)).Cast <Cookie>().ToList(),
                        StatusCode          = response.StatusCode,
                        IsSuccessStatusCode = ((int)response.StatusCode >= 200 && (int)response.StatusCode <= 299),
                    };
                    if (callback != null)
                    {
                        callback(result);
                    }
                    //close and dispose response
#if NETFX
                    response.Close();
#endif
                    response.Dispose();
                    //give result back
                    return(result);
                }
                else
                {
                    return(default(RequestResult));
                }
            }
            catch (Exception ex)
            {
                DebugEx.TraceError(ex, "Http request failed");
                return(default(RequestResult));
            }
        }
Exemplo n.º 28
0
 public static RequestResult Request(HttpMethods method, string url, string data, HttpRequestDataFormat dataFormat, Dictionary<string, string> headers, IEnumerable<Cookie> Cookies, ICredentials Credentials, Action<RequestResult> callback, ProgressReportDelegate progressReportCallback)
 {
     return _Request(method, url, data != null ? Encoding.UTF8.GetBytes(data) : null, dataFormat, headers, Cookies, Credentials, callback, progressReportCallback);
 }
Exemplo n.º 29
0
 public static RequestResult RequestGET(string url, ProgressReportDelegate progressReport)
 {
     return(Request(HttpMethods.Get, url, null, HttpRequestDataFormat.FormData, null, null, null, null, progressReport));
 }
Exemplo n.º 30
0
      /// <summary>
      /// Locks the index: makes sure it is loaded and referenced
      /// </summary>
      /// <param name="pr">Callback to provide progress report</param>
      public void Lock(ProgressReportDelegate pr) 
      {
         if(this.IsLoaded) return; // seems fine and "locked" already

         // not loaded, try the weak reference
         if(this.weakReferenceIndexEntries == null) 
         {
            // that doesn't look good, weakref not there. Allocate it
            this.weakReferenceIndexEntries = new WeakReference(null);
         }

         // weakref still valid ?
         if(this.weakReferenceIndexEntries.IsAlive) 
         {  // yes, restore the array from the weakref
            m_indexEntries = (WplIndexEntry [])this.weakReferenceIndexEntries.Target;
         }
         else 
         {  // no - load again, and save as weakref as well
            Load(pr);
            this.weakReferenceIndexEntries = new WeakReference(m_indexEntries);
         }
      }
Exemplo n.º 31
0
        public static IEnumerator InitAsync(ProgressReportDelegate reportProgress)
        {
            if (reportProgress != null)
            {
                reportProgress("Desc", "Startup", 0);
            }
            AsyncWorkTimer.Check();

            var list = _InitList;

            if (list != null)
            {
                list.Sort((ia, ib) => ia.Order - ib.Order);
                int   totalStep  = 0;
                int   totalPhase = 0;
                int[] workSteps  = null;
                if (reportProgress != null)
                {
                    workSteps = new int[list.Count];
                    for (int i = 0; i < list.Count; ++i)
                    {
                        var pr = list[i] as IInitProgressReporter;
                        if (pr != null && pr is IInitAsync)
                        {
                            pr.ReportProgress += reportProgress;
                            var step = pr.CountWorkStep();
                            totalStep += step;
                            ++totalPhase;
                            workSteps[i] = step;
                        }
                        else
                        {
                            list[i].Prepare();
                        }
                    }
                    if (totalStep > 0)
                    {
                        reportProgress("HaveWorkToDo", null, 1);
                    }
                    reportProgress("TotalPhase", null, totalPhase);
                    reportProgress("TotalStep", null, totalStep);
                    reportProgress("WorkingPhase", null, 0);
                    reportProgress("WorkingStep", null, 0);
                }
                else
                {
                    for (int i = 0; i < list.Count; ++i)
                    {
                        list[i].Prepare();
                    }
                }
                int workingPhase = 0;
                int workingStep  = 0;
                for (int i = 0; i < list.Count; ++i)
                {
                    var init  = list[i];
                    var inita = init as IInitAsync;
                    if (inita != null)
                    {
                        if (AsyncWorkTimer.Check())
                        {
                            yield return(null);
                        }
                        if (reportProgress != null)
                        {
                            var pr = init as IInitProgressReporter;
                            if (pr != null)
                            {
                                reportProgress("WorkingPhase", null, ++workingPhase);
                                reportProgress("Desc", pr.GetPhaseDesc(), 0);
                            }
                        }
                        var work = inita.InitAsync();
                        yield return(work);

                        if (reportProgress != null)
                        {
                            if (workSteps[i] > 0)
                            {
                                workingStep += workSteps[i];
                                reportProgress("WorkingStep", null, workingStep);
                            }
                        }
                    }
                    else
                    {
                        list[i].Init();
                    }
                }
                if (reportProgress != null)
                {
                    for (int i = 0; i < list.Count; ++i)
                    {
                        var pr = list[i] as IInitProgressReporter;
                        if (pr != null && pr is IInitAsync)
                        {
                            pr.ReportProgress -= reportProgress;
                        }
                    }
                    reportProgress("AllDone", null, 1);
                }
            }
        }
Exemplo n.º 32
0
        public BWCompletedParam DoWork(BWStartParams param, ProgressReportDelegate ReportProgress)
        {
            int rv = 0;

            var flacR = new WWFlacRWCS.FlacRW();

            do
            {
                // FLACファイルからメタデータ、画像、音声を取り出す。
                WWFlacRWCS.Metadata metaR;
                rv = flacR.DecodeAll(param.inputFile);
                if (rv < 0)
                {
                    // do-whileを抜けたところでflacR.DecodeEndする。
                    break;
                }

                flacR.GetDecodedMetadata(out metaR);

                byte[] picture = null;
                if (0 < metaR.pictureBytes)
                {
                    rv = flacR.GetDecodedPicture(out picture, metaR.pictureBytes);
                    if (rv < 0)
                    {
                        // do-whileを抜けたところでflacR.DecodeEndする。
                        break;
                    }
                }

                // flacRはまだ使用するのでここではDecodeEndしない。

                long lcm             = WWMath.Functions.LCM(metaR.sampleRate, param.targetSampleRate);
                int  upsampleScale   = (int)(lcm / metaR.sampleRate);
                int  downsampleScale = (int)(lcm / param.targetSampleRate);

                // IIRフィルターを設計。

                // ストップバンド最小周波数fs。
                double fs = metaR.sampleRate / 2 * STOPBAND_FREQ_RATIO;
                if (param.targetSampleRate / 2 * STOPBAND_FREQ_RATIO < fs)
                {
                    fs = param.targetSampleRate / 2 * STOPBAND_FREQ_RATIO;
                }

                // カットオフ周波数fc。
                double fc = CUTOFF_STOPBAND_RATIO * fs;

                mIIRFilterDesign = new IIRFilterDesign();
                mIIRFilterDesign.Design(fc, fs, lcm, param.method);

                ReportProgress(CONVERT_START_PERCENT, new BWProgressParam(State.FilterDesigned,
                                                                          string.Format("Read FLAC completed.\nSource sample rate = {0}kHz.\nTarget sample rate = {1}kHz. ratio={2}/{3}\n",
                                                                                        metaR.sampleRate / 1000.0, param.targetSampleRate / 1000.0,
                                                                                        lcm / metaR.sampleRate, lcm / param.targetSampleRate)));

                double RESAMPLE_RATIO = param.targetSampleRate / metaR.sampleRate;

                mSw.Restart();

                // 書き込み準備。
                WWFlacRWCS.Metadata metaW = new WWFlacRWCS.Metadata(metaR);
                metaW.sampleRate    = param.targetSampleRate;
                metaW.pictureBytes  = metaR.pictureBytes;
                metaW.bitsPerSample = 24;
                metaW.totalSamples  = metaR.totalSamples * upsampleScale / downsampleScale;

                if (param.isTargetPcm)
                {
                    mFlacWrite = new FlacWrite();
                    mFlacWrite.Setup(metaW, picture);
                }
                else
                {
                    mDsfWrite = new DsfWrite();
                    mDsfWrite.Setup(metaW, picture);
                }

                var  stat = new SampleValueStatistics();
                long totalSamplesOfAllChannels = metaR.totalSamples * metaR.channels;
                long processedSamples          = 0;

                // Mathematica用の設定。
                WWComplex.imaginaryUnit = "I";

                // 変換する
                for (int ch = 0; ch < metaR.channels; ++ch)
                {
                    //Parallel.For(0, metaR.channels, (int ch) => {
                    var pcmW = new WWUtil.LargeArray <byte>(metaW.totalSamples * metaW.BytesPerSample);

#if USE_ZOH_UPSAMPLE
                    // 零次ホールドのハイ落ち補償フィルター。
# if USE_CPP
                    var zohCompensation = new WWFilterCpp();
                    zohCompensation.BuildZohCompensation();
# else
                    var zohCompensation = new WWIIRFilterDesign.ZohNosdacCompensation(33);
# endif