示例#1
0
 public void Cut(string mFilePath, List <FilePiece> mQueeue)
 {
     using (var reader = new FileStream(mFilePath, FileMode.Open, FileAccess.Read))
     {
         var  counter = 0;
         long oldPs   = 0;
         while (reader.Position < reader.Length)
         {
             var buffer = new byte[8];
             //читаем заголовок файла
             reader.Read(buffer, 0, 8);
             //выбираем из прочитанного размер блока
             var compressedBlockLength = BitConverter.ToInt32(buffer, 4);
             //Console.WriteLine(compressedBlockLength);
             var comressedBytes = new byte[compressedBlockLength + 1];
             buffer.CopyTo(comressedBytes, 0);
             reader.Read(comressedBytes, 8, compressedBlockLength - 8);
             var blockSize         = BitConverter.ToInt32(comressedBytes, compressedBlockLength - 4);
             var decompressedBytes = ProcessUnPacking.ProcessArchive(comressedBytes, blockSize);
             mQueeue.Add(new FilePiece(counter, decompressedBytes));
             counter++;
             long ps = reader.Position * 100 / reader.Length;
             if (ps != oldPs)
             {
                 oldPs = ps;
                 NotifyProgress?.Invoke(ps.ToString());
             }
         }
     }
 }
示例#2
0
 public void Cut(string mFilePath, List <FilePiece> mQueeue)
 {
     using (var reader = new BinaryReader(new FileStream(mFilePath, FileMode.Open, FileAccess.Read)))
     {
         int  counter     = 0;
         var  BUFFER_SIZE = AppPropertiesSingle.GetInstance().m_BufferSize;
         long oldPs       = 0;
         while (reader.BaseStream.Position < reader.BaseStream.Length)
         {
             reader.BaseStream.Seek(counter * BUFFER_SIZE, SeekOrigin.Begin);
             var bufferSize = BUFFER_SIZE;
             if (reader.BaseStream.Length - reader.BaseStream.Position <= BUFFER_SIZE)
             {
                 bufferSize = (int)(reader.BaseStream.Length - reader.BaseStream.Position);
             }
             var arBytes         = reader.ReadBytes(bufferSize);
             var compressedBytes = ProcessPacking.ProcessArchive(arBytes);
             mQueeue.Add(new FilePiece(counter, compressedBytes));
             counter++;
             long ps = reader.BaseStream.Position * 100 / reader.BaseStream.Length;
             if (ps != oldPs)
             {
                 NotifyProgress?.Invoke(ps.ToString());
             }
             oldPs = ps;
         }
     }
 }
示例#3
0
        public void Cut(string mFilePath, List <FilePiece> mQueeue)
        {
            m_ThreadPieceList = mQueeue;
            m_FilePath        = mFilePath;
            // найдем все якоря, чтобы потом считывать инфу в потоках
            var anchorList = new List <long>();

            using (var reader = new FileStream(mFilePath, FileMode.Open, FileAccess.Read))
            {
                while (reader.Position < reader.Length)
                {
                    long anchor = reader.Position;
                    var  buffer = new byte[8];
                    reader.Read(buffer, 0, 8);
                    var compressedBlockLength = BitConverter.ToInt32(buffer, 4);
                    reader.Position += compressedBlockLength - 8;
                    anchorList.Add(anchor);
                }
            }

            var prop = AppPropertiesSingle.GetInstance();
            var elementsEachThread = anchorList.Count / prop.ProcessorCount;

            // распределим на разные списки для распараллеливания
            m_AnchorListThread = anchorList
                                 .Select((x, i) => new { Index = i, Value = x })
                                 .GroupBy(x => x.Index / elementsEachThread)
                                 .Select(x => x.Select(v => v.Value).ToList())
                                 .ToList();

            // параллелим процесс
            foreach (var unused in m_AnchorListThread)
            {
                m_ThreadPieceList.Add(new FilePiece(""));
                m_ProgressList.Add(1);
            }
            int pathsCount = m_AnchorListThread.Count;
            var threadList = new List <Thread>();

            for (int i = 0; i < pathsCount; i++)
            {
                threadList.Add(new Thread(CutPath));
                threadList[i].IsBackground = true;
                threadList[i].Start(i);
            }
            while (threadList.Any(w => w.IsAlive))
            {
                NotifyProgress?.Invoke(PercentageCalculate.GetPercentAverage(m_ProgressList).ToString());
                Thread.Sleep(100);
            }
            foreach (var thread in threadList)
            {
                thread.Join();
            }
        }
示例#4
0
 public static void LoadLevelAdditive(string path, int version, string name, NotifyProgress callback)
 {
     if (manager == null)
     {
         if (callback != null)
         {
             callback(LOAD_FAIL);
         }
         return;
     }
     manager.LoadScene(path, version, name, callback);
 }
示例#5
0
 void Update()
 {
     if (notifyCallback != null && oper != null)
     {
         notifyCallback(oper.progress);
         if (oper.isDone)
         {
             notifyCallback = null;
             oper           = null;
         }
     }
 }
示例#6
0
        /// <summary>
        /// Run a lot of commands on one connection
        /// </summary>
        /// <param name="commands">List of commands</param>
        /// <returns>0 if there was no error, command's index if there was an error</returns>
        public override int ExecMultiple(IList <DbCommand> commands)
        {
            var index = 0;

            try
            {
                using (var conn = new SqlConnection(ConnectionString))
                {
                    if (SendStatus != null)
                    {
                        conn.InfoMessage += ConnOnInfoMessage;
                    }

                    conn.Open();

                    for (index = 0; index < commands.Count; index++)
                    {
                        if (index % NotifyProgressSteps == 0)
                        {
                            NotifyProgress?.Invoke(index);
                        }

                        var cmd = commands[index];
                        cmd.Connection = conn;
                        if (_commandTimeOut != -1)
                        {
                            cmd.CommandTimeout = _commandTimeOut;
                        }
                        //cmd.Connection.Open();
                        //cmd.ExecuteNonQuery();
                        cmd.ExecuteScalar();
                    }


                    conn.Close();

                    return(0);
                }
            }
            catch //(Exception e)
            {
                return(index);
            }
        }
        public void Collect(List <FilePiece> mQueue, string mPathToSave)
        {
            m_PathToSave = mPathToSave;

            NotifyProgress?.Invoke("Читаю свободную оперативку...");
            long freeMemory = FreeRamMemory.GetFreeRamMemoryMb() * 1024 * 1024;

            // соберем все файлы вместе
            NotifyProgress?.Invoke("Соберем все из потоков вместе...0");
            long BUFFER_SIZE = freeMemory / 4;

            if (BUFFER_SIZE > int.MaxValue)
            {
                BUFFER_SIZE = int.MaxValue - 1;
            }

            using (var outFile = new FileStream(m_PathToSave, FileMode.Append, FileAccess.Write))
            {
                int fileCount = 0;
                foreach (var threadPath in mQueue)
                {
                    fileCount++;
                    NotifyProgress?.Invoke("Соберем все из потоков вместе..." + fileCount * 100 / mQueue.Count);
                    long counter = 0;
                    using (var reader = new BinaryReader(new FileStream(threadPath.FilePath, FileMode.Open, FileAccess.Read)))
                    {
                        while (reader.BaseStream.Position < reader.BaseStream.Length)
                        {
                            reader.BaseStream.Seek(counter * BUFFER_SIZE, SeekOrigin.Begin);
                            var bufferSize = BUFFER_SIZE;
                            if (reader.BaseStream.Length - reader.BaseStream.Position <= BUFFER_SIZE)
                            {
                                bufferSize = (int)(reader.BaseStream.Length - reader.BaseStream.Position);
                            }
                            var arBytes = reader.ReadBytes((int)bufferSize);
                            outFile.Write(arBytes, 0, arBytes.Length);
                            counter++;
                        }
                    }
                    File.Delete(threadPath.FilePath);
                }
            }
        }
示例#8
0
 public void LoadScene(string path, int version, string name, NotifyProgress callback)
 {
     if (string.IsNullOrEmpty(path) || version < 0 || string.IsNullOrEmpty(name) || callback == null)
     {
         if (callback != null)
         {
             callback(LOAD_FAIL);
         }
         return;
     }
     if (!Caching.IsVersionCached(path, version) &&
         (Application.internetReachability == NetworkReachability.NotReachable ||
          (Application.internetReachability == NetworkReachability.ReachableViaCarrierDataNetwork &&
           AppSetting.getInstance().wifiLimit)))
     {
         Debug.Log("can't download");
         callback(LOAD_FAIL);
     }
     StartCoroutine(LoadSceneProgram(path, version, name, callback));
 }
        public void Collect(List <FilePiece> mQueue, string mPathToSave)
        {
            long oldPs = 0;

            using (var outFile = new FileStream(mPathToSave, FileMode.Create))
            {
                int counter = 0;
                foreach (var queueOfPart in mQueue)
                {
                    var buf = queueOfPart.GetContent();
                    outFile.Write(buf, 0, buf.Length);
                    counter++;
                    long ps = counter * 100 / mQueue.Count;
                    if (ps != oldPs)
                    {
                        oldPs = ps;
                        NotifyProgress?.Invoke(ps.ToString());
                    }
                }
            }
        }
示例#10
0
    private IEnumerator LoadSceneProgram(string path, int version, string name, NotifyProgress callback)
    {
        WWW www = WWW.LoadFromCacheOrDownload(path, version);

        yield return(www);

        if (www.error != null)
        {
            Debug.Log(www.error);
            callback(LOAD_FAIL);
        }
        else
        {
            AssetBundle    bundle = www.assetBundle;
            AsyncOperation oper   = Application.LoadLevelAdditiveAsync(name);
            this.oper           = oper;
            this.notifyCallback = callback;
            yield return(oper);

            bundle.Unload(false);
        }
    }
示例#11
0
        public void Cut(string mFilePath, List <FilePiece> mQueeue)
        {
            m_ThreadPieceList = mQueeue;
            m_FilePath        = mFilePath;
            // разрежем файл на части, чтобы распараллелить процесс разрезания на кусочки
            var app     = AppPropertiesSingle.GetInstance();
            var prCount = app.ProcessorCount;

            var info       = new FileInfo(mFilePath);
            var fileLength = info.Length;

            m_StreamLength = long.Parse((fileLength / prCount).ToString()) + 100;

            // параллелим процесс
            int pathsCount = prCount;

            for (int i = 0; i < prCount; i++)
            {
                m_ThreadPieceList.Add(new FilePiece(""));
                m_ProgressList.Add(1);
            }
            var threadList = new List <Thread>();

            for (int i = 0; i < pathsCount; i++)
            {
                threadList.Add(new Thread(CutPath));
                threadList[i].IsBackground = true;
                threadList[i].Start(i);
            }
            while (threadList.Any(w => w.IsAlive))
            {
                NotifyProgress?.Invoke(PercentageCalculate.GetPercentAverage(m_ProgressList).ToString());
                Thread.Sleep(100);
            }
            foreach (var thread in threadList)
            {
                thread.Join();
            }
        }
        /// <summary>
        /// Run a lot of commands on one connection in order of the list
        /// </summary>
        /// <param name="commands">List of commands</param>
        /// <returns>0 if there was no error, command's index if there was an error</returns>
        public override int ExecMultiple(IList <DbCommand> commands)
        {
            var index = 0;

            try
            {
                using (var conn = new NpgsqlConnection(ConnectionString))
                {
                    conn.Open();

                    for (index = 0; index < commands.Count; index++)
                    {
                        if (index % NotifyProgressSteps == 0)
                        {
                            NotifyProgress?.Invoke(index);
                        }

                        var cmd = commands[index];
                        cmd.Connection = conn;
                        if (_commandTimeOut != -1)
                        {
                            cmd.CommandTimeout = _commandTimeOut;
                        }
                        cmd.ExecuteNonQuery();
                    }

                    conn.Close();
                }

                return(0);
            }
            catch
            {
                return(index);
            }
        }
示例#13
0
        /// <summary>
        /// Calls b2_download_file_by_id
        /// 
        /// https://www.backblaze.com/b2/docs/b2_download_file_by_id.html
        /// </summary>
        public async Task<B2DownloadResult> DownloadFileContent(string fileId, string overrideAuthToken = null, NotifyProgress notifyProgress = null)
        {
            HttpRequestMessage msg = new HttpRequestMessage(HttpMethod.Get, new Uri(DownloadUri, "/b2api/v1/b2_download_file_by_id?fileId=" + fileId));

            if (!string.IsNullOrEmpty(overrideAuthToken) || !string.IsNullOrEmpty(AuthToken))
                msg.Headers.Authorization = new AuthenticationHeaderValue(overrideAuthToken ?? AuthToken);

            ProgressMessageHandler progressMessageHandler = new ProgressMessageHandler(new HttpClientHandler());

            EventHandler<HttpProgressEventArgs> progress = null;
            if (notifyProgress != null)
            {
                progress = (sender, args) =>
                {
                    notifyProgress(args.TotalBytes ?? 0, args.TotalBytes ?? 0);
                };

                progressMessageHandler.HttpSendProgress += progress;
            }

            HttpResponseMessage resp = GetHttpClient(true, progressMessageHandler).SendAsync(msg, HttpCompletionOption.ResponseHeadersRead).Result;

            if (resp.StatusCode != HttpStatusCode.OK)
                HandleErrorResponse(resp).Wait();

            B2FileDownloadResult info = new B2FileDownloadResult();

            info.FileId = resp.Headers.GetValues("X-Bz-File-Id").First();
            info.FileName = resp.Headers.GetValues("X-Bz-File-Name").First();
            info.ContentSha1 = resp.Headers.GetValues("X-Bz-Content-Sha1").First();

            Debug.Assert(resp.Content.Headers.ContentLength != null, "resp.Content.Headers.ContentLength != null");

            info.ContentLength = resp.Content.Headers.ContentLength.Value;
            info.ContentType = resp.Content.Headers.ContentType.MediaType;

            info.FileInfo = new Dictionary<string, string>();

            foreach (KeyValuePair<string, IEnumerable<string>> pair in resp.Headers)
            {
                if (!pair.Key.StartsWith("X-Bz-Info-"))
                    continue;

                info.FileInfo[pair.Key.Substring("X-Bz-Info-".Length)] = pair.Value.First();
            }

            B2DownloadResult result = new B2DownloadResult();

            result.Info = info;
            result.Stream = await resp.Content.ReadAsStreamAsync();

            return result;
        }
示例#14
0
        /// <summary>
        /// Calls b2_upload_file
        /// Needs an Upload Url
        /// 
        /// https://www.backblaze.com/b2/docs/b2_upload_file.html
        /// </summary>
        public async Task<B2FileInfo> UploadFile(Uri uploadUri, string uploadToken, Stream inputStream, string sha1, string fileName, string contentType, Dictionary<string, string> fileInfo, NotifyProgress notifyDelegate)
        {
            // Pre-checks
            if (inputStream == null)
                throw new ArgumentNullException("Stream must be set");

            if (string.IsNullOrEmpty(sha1) || sha1.Length != 40)
                throw new ArgumentException("SHA1 must be set or computed");

            if (string.IsNullOrEmpty(fileName))
                throw new ArgumentException("Filename must be set");

            if (string.IsNullOrEmpty(contentType))
                contentType = B2Constants.AutoContenType;

            // Prepare
            HttpRequestMessage msg = new HttpRequestMessage(HttpMethod.Post, uploadUri);

            msg.Headers.Authorization = new AuthenticationHeaderValue(uploadToken);
            msg.Headers.Add("X-Bz-File-Name", WebUtility.UrlEncode(fileName));
            msg.Headers.Add("X-Bz-Content-Sha1", sha1);

            // Upload
            if (fileInfo != null)
            {
                foreach (KeyValuePair<string, string> info in fileInfo)
                    msg.Headers.Add("X-Bz-Info-" + info.Key, WebUtility.UrlEncode(info.Value));
            }

            msg.Content = new StreamContent(inputStream);
            msg.Content.Headers.ContentType = new MediaTypeHeaderValue(contentType);

            ProgressMessageHandler progressMessageHandler = new ProgressMessageHandler(new HttpClientHandler());

            EventHandler<HttpProgressEventArgs> progress = null;
            if (notifyDelegate != null)
            {
                progress = (sender, args) =>
                {
                    notifyDelegate(args.TotalBytes ?? 0, args.TotalBytes ?? 0);
                };

                progressMessageHandler.HttpSendProgress += progress;
            }

            try
            {
                using (HttpClient http = GetHttpClient(true, progressMessageHandler))
                {
                    HttpResponseMessage resp = await http.SendAsync(msg).ConfigureAwait(false);

                    if (resp.StatusCode != HttpStatusCode.OK)
                        await HandleErrorResponse(resp);

                    return JsonConvert.DeserializeObject<B2FileInfo>(await resp.Content.ReadAsStringAsync().ConfigureAwait(false));
                }
            }
            finally
            {
                if (progress != null)
                    progressMessageHandler.HttpSendProgress -= progress;
            }
        }
示例#15
0
 private static void Progress_ProgressChanged(object sender, float e)
 {
     // Console.WriteLine(e.ToString()); System.Diagnostics.Debug.WriteLine(e);
     NotifyProgress?.Invoke(e);
 }
 /// <summary>
 /// Calls <see cref="IConnManager.NotifyProgress"/> (for testing purposes only)
 /// </summary>
 public virtual void TestNotifying()
 {
     NotifyProgress?.Invoke(98);
 }