Ejemplo n.º 1
0
        public virtual void Update(GameTime gameTime)
        {
            if (!Stopped)
            {
                timespan++;
                if (timespan >= interval)
                {
                    timespan = 0;
                    if (index + 1 >= length)
                    {
                        Finished = true;
                    }
                    else
                    {
                        index++;
                    }
                }
            }

            if (Finished)
            {
                TaskFinished?.Invoke(this);
            }
        }
Ejemplo n.º 2
0
        public static void ProgsToExec(ExecProgsType taskName)
        {
            try
            {
                _report = "Выполнение программ...\n\n";
                ReportUpdated?.Invoke(_report);
                if (QMediator.PathToProgDest != null)
                {
                    string str;
                    if (taskName == ExecProgsType.Oktel)
                    {
                        str = "\\post!\\oktel";
                        QMediator.CurrentTaskName = TaskName.Oktel;
                        DbNotification.ResultWaiter();
                    }
                    else
                    {
                        str = taskName == ExecProgsType.PredProgs ? "\\!pred" : "\\post!";
                    }

                    string path = QMediator.PathToProgDest + str;
                    if (!new DirectoryInfo(path).Exists)
                    {
                        Directory.CreateDirectory(path);
                        _report = "Нет программ для выполнения.";
                    }
                    else
                    {
                        List <string> filePathList = GetFilesForExec(path);
                        if (filePathList.Count < 1)
                        {
                            _report = "Нет программ для выполнения.";
                            ReportUpdated?.Invoke(_report);

                            if (taskName == ExecProgsType.PredProgs)
                            {
                                TaskFinished?.Invoke(TaskName.PredProgs);
                            }
                            else if (taskName == ExecProgsType.PostProgs)
                            {
                                TaskFinished?.Invoke(TaskName.PostProgs);
                            }
                            else
                            {
                                TaskFinished?.Invoke(TaskName.Oktel);
                            }

                            return;
                        }
                        foreach (var item in filePathList)
                        {
                            _report += Environment.NewLine + "Файл:\t\t" + item;
                            bool   isOk     = true;
                            string fileText = File.ReadAllText(item, Encoding.Default);
                            try
                            {
                                if (fileText.ToLower().Contains("begin"))
                                {
                                    ManagerDB.ExecCommand(fileText);
                                }
                                else
                                {
                                    isOk = SplitAndExec(fileText);
                                }
                            }
                            catch (Exception ex)
                            {
                                isOk = false;
                                ExceptionHandler("ProgsToExec()", ex.Message);
                            }
                            _report += ("\t" + (isOk == true ? "отработал нормально." : "отработал с ошибками.") + Environment.NewLine);
                        }
                    }
                }
                else
                {
                    _report = "Не определен путь к программам.";
                }

                ReportUpdated?.Invoke(_report);
                if (taskName == ExecProgsType.PredProgs)
                {
                    TaskFinished?.Invoke(TaskName.PredProgs);
                }
                else if (taskName == ExecProgsType.PostProgs)
                {
                    TaskFinished?.Invoke(TaskName.PostProgs);
                }
                else
                {
                    TaskFinished?.Invoke(TaskName.Oktel);
                }
            }
            catch (Exception ex)
            {
                ExceptionHandler("ProgsToExec()", ex.Message);
            }
        }
Ejemplo n.º 3
0
        //RAM stream -> File stream
        private void _monitor_thread_callback()
        {
            //设置线程启动相关变量
            _monitor_thread_created.Set();
            _start_time = DateTime.Now;
            //管理任务状态
            bool pause_to_start = false;

            lock (_thread_flag_lock)
            {
                pause_to_start        = ((_download_thread_flag & 0xffffff) & (_DOWNLOAD_THREAD_FLAG_PAUSED | _DOWNLOAD_THREAD_FLAG_PAUSE_REQUESTED)) != 0;
                _download_thread_flag = (_download_thread_flag | _DOWNLOAD_THREAD_FLAG_STARTED) & ~_DOWNLOAD_THREAD_FLAG_START_REQUESTED;
                if (pause_to_start)
                {
                    _download_thread_flag = (_download_thread_flag & ~_DOWNLOAD_THREAD_FLAG_PAUSED);
                }
            }
            //缓存最大线程数,避免中途更改
            var max_thread = _max_thread;

            //预分配空间(todo:改为set-length,提高效率)
            _downloaded_size = 0;
            try { PreAllocBlockStarted?.Invoke(this, new EventArgs()); } catch { }
            if (_file_stream.Length != (long)_data.Size)
            {
                if (_file_stream.Length > (long)_data.Size)
                {
                    _file_stream.SetLength((long)_data.Size); //文件长度大于预分配长度,直接截断
                }
                else
                {
                    //从当前长度开始按0填充
                    _file_stream.Seek(0, SeekOrigin.End);
                    _downloaded_size = _file_stream.Length;
                    var  blank_buffer = new byte[_MIN_IO_FLUSH_DATA_LENGTH];
                    long write_length = 0;
                    do
                    {
                        write_length = (long)_data.Size - _file_stream.Length;
                        int len = (int)Math.Min(_MIN_IO_FLUSH_DATA_LENGTH, write_length);
                        _file_stream.Write(blank_buffer, 0, len);
                        _downloaded_size += len;
                    } while (write_length > 0 && ((_download_thread_flag & 0xffffff) & (_DOWNLOAD_THREAD_FLAG_PAUSE_REQUESTED | _DOWNLOAD_THREAD_FLAG_STOP_REQUESTED)) == 0);
                }
                _file_stream.Seek(0, SeekOrigin.Begin);
            }
            try { PreAllocBlockFinished?.Invoke(this, new EventArgs()); } catch { }
            _downloaded_size = 0;

            //分配多线程的变量数组
            _guid_list        = new Guid[max_thread];
            _last_receive     = new DateTime[max_thread];
            _buffer_stream    = new QueueStream[max_thread];
            _thread_data_lock = new object[max_thread];
            _request          = new NetStream[max_thread];
            _position         = new ulong[max_thread];
            _io_position      = new long[max_thread];
            for (int i = 0; i < max_thread; i++)
            {
                _buffer_stream[i]     = new QueueStream();
                _thread_data_lock[i]  = new object();
                _request[i]           = new NetStream();
                _request[i].CookieKey = _cookie_key;
            }

            var buffer         = new byte[_MIN_IO_FLUSH_DATA_LENGTH];
            var next_loop_time = DateTime.Now.AddSeconds(1);

            //监控循环
            #region monitor loop
            while (true)
            {
                //当url的有效时间已过时,刷新url
                if (_url_expire_time < DateTime.Now)
                {
                    _url_expire_time         = _url_expire_time.AddSeconds(30);
                    _url_fail_to_fetch_count = 0;
                    _api.GetAccount(_data.AccountID).GetLocateDownloadLinkAsync(_data.Path, _main_url_refresh_callback);
                }

                //管理任务标识
                #region thread flag handling
                bool ispause = false, iscancel = false;

                if (((_download_thread_flag & 0xffffff) & _DOWNLOAD_THREAD_FLAG_PAUSE_REQUESTED) != 0)
                {
                    ispause = true;
                }
                if (((_download_thread_flag & 0xffffff) & _DOWNLOAD_THREAD_FLAG_STOP_REQUESTED) != 0)
                {
                    iscancel = true;
                }

                if (ispause)
                {
                    //pause event
                    for (int i = 0; i < _request.Length; i++)
                    {
                        lock (_thread_data_lock[i])
                        {
                            _request[i].Close();
                            if (_guid_list[i] != Guid.Empty)
                            {
                                _dispatcher.ReleaseTask(_guid_list[i]);
                                _guid_list[i] = Guid.Empty;
                            }
                            //flushing buffer stream
                            if ((_guid_list[i] == Guid.Empty && _buffer_stream[i].Length > 0) || (_buffer_stream[i].Length > _MIN_IO_FLUSH_DATA_LENGTH))
                            {
                                _file_stream.Seek(_io_position[i], SeekOrigin.Begin);
                                while ((_guid_list[i] == Guid.Empty && _buffer_stream[i].Length > 0) || (_buffer_stream[i].Length > _MIN_IO_FLUSH_DATA_LENGTH))
                                {
                                    int rc = _buffer_stream[i].Read(buffer, 0, _MIN_IO_FLUSH_DATA_LENGTH);
                                    _file_stream.Write(buffer, 0, rc);
                                    _io_position[i] += rc;
                                }
                            }
                        }

                        _file_stream.Flush();
                    }
                    _request              = null;
                    _urls                 = null;
                    _guid_list            = null;
                    _last_receive         = null;
                    _position             = null;
                    _thread_data_lock     = null;
                    _monitor_thread       = null;
                    _end_time             = DateTime.Now;
                    _download_thread_flag = (_download_thread_flag | _DOWNLOAD_THREAD_FLAG_PAUSED) & ~(_DOWNLOAD_THREAD_FLAG_PAUSE_REQUESTED | _DOWNLOAD_THREAD_FLAG_STARTED);
                    return;
                }
                if (iscancel)
                {
                    //cancel event
                    _file_stream.Close();
                    for (int i = 0; i < _request.Length; i++)
                    {
                        lock (_thread_data_lock[i])
                        {
                            _request[i].Close();
                            if (_guid_list[i] != Guid.Empty)
                            {
                                _dispatcher.ReleaseTask(_guid_list[i]);
                                _guid_list[i] = Guid.Empty;
                            }
                        }
                    }
                    _request          = null;
                    _urls             = null;
                    _guid_list        = null;
                    _last_receive     = null;
                    _position         = null;
                    _thread_data_lock = null;
                    _file_stream.Dispose();
                    _file_stream          = null;
                    _monitor_thread       = null;
                    _end_time             = DateTime.Now;
                    _download_thread_flag = (_download_thread_flag | _DOWNLOAD_THREAD_FLAG_STOPPED) & ~(_DOWNLOAD_THREAD_FLAG_STOP_REQUESTED | _DOWNLOAD_THREAD_FLAG_STARTED);
                    return;
                }
                #endregion

                //url合理检测
                if (_urls == null || _urls.Length == 0)
                {
                    Thread.Sleep(100);
                    continue;
                }

                int started_tasks = 0;
                for (int i = 0; i < _request.Length; i++)
                {
                    lock (_thread_data_lock[i])
                    {
                        //将内存的缓存数据流写入到硬盘中
                        if ((_guid_list[i] == Guid.Empty && _buffer_stream[i].Length > 0) || (_buffer_stream[i].Length > _MIN_IO_FLUSH_DATA_LENGTH))
                        {
                            _file_stream.Seek(_io_position[i], SeekOrigin.Begin);
                            while ((_guid_list[i] == Guid.Empty && _buffer_stream[i].Length > 0) || (_buffer_stream[i].Length > _MIN_IO_FLUSH_DATA_LENGTH))
                            {
                                int rc = _buffer_stream[i].Read(buffer, 0, _MIN_IO_FLUSH_DATA_LENGTH);
                                _file_stream.Write(buffer, 0, rc);
                                _io_position[i] += rc;
                            }
                        }

                        //检测任务分配状态,自动开始新的分段下载
                        if (_guid_list[i] == Guid.Empty && (DateTime.Now - _last_receive[i]).TotalSeconds > 3.0)
                        {
                            if (started_tasks < _PARALLEL_START_REQUEST_COUNT)
                            {
                                _guid_list[i] = _dispatcher.AllocateNewTask(out _position[i]);
                                if (_guid_list[i] != Guid.Empty)
                                {
                                    _last_receive[i] = DateTime.Now;
                                    try
                                    {
                                        _request[i].UserAgent = "netdisk;5.7.2.3;PC;PC-Windows;10.0.16299;WindowsBaiduYunGuanJia";
                                        _request[i].HttpGetAsync(_urls[i % _urls.Length], _data_transfer_callback, new _temp_strcut {
                                            id = _guid_list[i], index = i
                                        }, range: (long)_position[i]);
                                        _io_position[i] = (long)_position[i];
                                    }
                                    catch { }

                                    started_tasks++;
                                }
                            }
                        }

                        //自动中断超时的请求
                        else if (_guid_list[i] != Guid.Empty && (DateTime.Now - _last_receive[i]).TotalSeconds > 35.0)
                        {
                            _request[i].Close();
                            _request[i] = new NetStream();
                            _dispatcher.ReleaseTask(_guid_list[i]);
                            _guid_list[i] = Guid.Empty;
                        }
                    }
                }

                _current_bytes = 0;

                //更新速度
                var cur_len = _dispatcher.CompletedLength;
                _last_5s_length.RemoveFirst();
                _last_5s_length.AddLast(cur_len);

                _average_speed_total = cur_len / (DateTime.Now - _start_time).TotalSeconds;
                _average_speed_5s    = (_last_5s_length.Last.Value - _last_5s_length.First.Value) / 5.0;
                _downloaded_size     = (long)cur_len;
                Tracer.GlobalTracer.TraceInfo("Downloading: " + cur_len + "/" + _data.Size + " [" + (_average_speed_5s / 1024).ToString("0.00") + "KB/s]");

                if (cur_len == _data.Size)
                {
                    break;
                }
                //时钟控制(1s)
                var ts = (next_loop_time - DateTime.Now).TotalMilliseconds;
                next_loop_time = next_loop_time.AddSeconds(1);
                if (ts >= 1.0)
                {
                    Thread.Sleep((int)ts);
                }
            }
            #endregion

            //下载完成
            _file_stream.Close();
            for (int i = 0; i < _request.Length; i++)
            {
                lock (_thread_data_lock[i])
                {
                    _request[i].Close();
                }
            }
            _request          = null;
            _urls             = null;
            _guid_list        = null;
            _last_receive     = null;
            _position         = null;
            _thread_data_lock = null;
            _file_stream.Dispose();
            _file_stream    = null;
            _monitor_thread = null;
            _end_time       = DateTime.Now;

            //文件解密
            if (_data.Path.EndsWith(".bcsd"))
            {
                _download_thread_flag |= _DOWNLOAD_THREAD_FLAG_DECRYPTING;
                try { DecryptStarted?.Invoke(this, new EventArgs()); } catch { }
                _decrypt_file();
                _download_thread_flag = _download_thread_flag & ~_DOWNLOAD_THREAD_FLAG_DECRYPTING;
                try { DecryptFinished?.Invoke(this, new EventArgs()); } catch { }
            }
            _download_thread_flag = (_download_thread_flag | _DOWNLOAD_THREAD_FLAG_FINISHED) & ~_DOWNLOAD_THREAD_FLAG_STARTED;

            try { TaskFinished?.Invoke(this, new EventArgs()); }
            catch { }
        }
 // ReSharper disable once UnusedMember.Local
 private void OnTaskFinished(TaskFinishedEventArgs e)
 {
     TaskFinished?.Invoke(this, e);
 }
Ejemplo n.º 5
0
        private void InformationRefresh(JRCtler.JsonRpcRes e)
        {
            //计算当前下载速度
            double SpeedLong = e.Result.downloadSpeed;

            if (SpeedLong / 1024 == 0)
            {
                Speed = Math.Round(SpeedLong, 2).ToString() + "B/S";
            }
            else if (SpeedLong / 1048576 == 0)
            {
                Speed = Math.Round((SpeedLong / 1024), 2).ToString() + "KB/S";
            }
            else
            {
                Speed = Math.Round((SpeedLong / 1048578), 2).ToString() + "MB/S";
            }
            OnPropertyChanged("Speed");

            //更新状态
            string NewStatus = e.Result.status;

            if (Status != NewStatus)
            {
                Status = NewStatus;
                switch (Status)
                {
                case "active":
                    State = "none";
                    break;

                case "waiting":
                    State          = "wait";
                    OughtToRefresh = 5;
                    break;

                case "paused":
                    State          = "wait";
                    OughtToRefresh = 5;
                    break;

                case "error":
                    State          = "error";
                    OughtToRefresh = 10;
                    break;

                case "complete":
                    State = "none";
                    TaskFinished?.Invoke(this);
                    OughtToRefresh = 10;
                    break;

                case "removed":
                    State          = "error";
                    OughtToRefresh = 10;
                    break;

                default:
                    State          = "wait";
                    OughtToRefresh = 5;
                    break;
                }
                OnPropertyChanged("State");
            }

            //更新进度
            string CompletedNew = e.Result.completedLength;

            Total = e.Result.totalLength;
            if (CompletedNew != Completed)
            {
                Completed = CompletedNew;
                OnPropertyChanged("Progress");
            }
            else if (CompletedNew == "0")
            {
                State = "wait";
                OnPropertyChanged("State");
            }

            Gid = e.Result.gid;
            if (FileName == null || FileNameSource != FileNameSources.Path)
            {
                GetFileInfo();
            }
            e = null;
        }
Ejemplo n.º 6
0
 public object Post(TaskFinished taskFinished)
 {
     return(serviceStackTemplate.DoAction(() => masterService.TaskFinished(taskFinished.DelegationId, taskFinished.TaskResult)));
 }
Ejemplo n.º 7
0
 private void FireTaskFinished()
 {
     TaskFinished?.Invoke(this, EventArgs.Empty);
 }
Ejemplo n.º 8
0
 /// <summary>
 /// Use this method to finish effect, rather than turning <see cref="Finished"/> variable to true.
 /// </summary>
 public virtual void Finish()
 {
     Finished = true; TaskFinished?.Invoke(this);
 }
Ejemplo n.º 9
0
 private void OnTaskFinished()
 {
     this.IsComplete = true;
     TaskFinished?.Invoke(this, null);
 }
Ejemplo n.º 10
0
        private void _monitor_thread_callback(object _ = null)
        {
            _monitor_thread_created.Set();
            _start_time = DateTime.Now;
            try
            {
                _upload_thread_flag = (_upload_thread_flag | _UPLOAD_THREAD_FLAG_STARTED) & ~(_UPLOAD_THREAD_FLAG_START_REQUESTED | _UPLOAD_THREAD_FLAG_PAUSED);

                //local io
                #region file io
                if (string.IsNullOrEmpty(_local_data.Path))
                {
                    _local_cacher.FileIORequest(_local_path);
                    _upload_thread_flag |= _UPLOAD_THREAD_FLAG_DIGEST_REQUESTED;
                    try { FileDigestStarted?.Invoke(this, new EventArgs()); } catch { }
                    _file_io_response.Wait();
                    _file_io_response.Reset();
                    try { FileDigestFinished?.Invoke(this, new EventArgs()); } catch { }
                }
                _uploaded_size = 0;
                #endregion

                #region status check
                if ((_upload_thread_flag & _UPLOAD_THREAD_FLAG_CANCEL_REQUESTED) != 0)
                {
                    _upload_thread_flag = (_upload_thread_flag | _UPLOAD_THREAD_FLAG_CANCELLED) & ~(_UPLOAD_THREAD_FLAG_CANCEL_REQUESTED | _UPLOAD_THREAD_FLAG_STARTED);
                    _end_time           = DateTime.Now;
                    return;
                }
                if ((_upload_thread_flag & _UPLOAD_THREAD_FLAG_PAUSE_REQUESTED) != 0)
                {
                    _upload_thread_flag = (_upload_thread_flag | _UPLOAD_THREAD_FLAG_PAUSED) & ~(_UPLOAD_THREAD_FLAG_PAUSE_REQUESTED | _UPLOAD_THREAD_FLAG_STARTED);
                    _end_time           = DateTime.Now;
                    return;
                }
                if ((_upload_thread_flag & _UPLOAD_THREAD_FLAG_ERROR) != 0)
                {
                    _upload_thread_flag = (_upload_thread_flag | _UPLOAD_THREAD_FLAG_PAUSED) & ~_UPLOAD_THREAD_FLAG_STARTED;
                    _end_time           = DateTime.Now;
                    try { TaskError?.Invoke(this, new EventArgs()); } catch { }
                    return;
                }
                #endregion

                #region encryption
                if (_enable_encryption)
                {
                    _upload_thread_flag |= _UPLOAD_THREAD_FLAG_FILE_ENCRYPTING;
                    try { EncryptStarted?.Invoke(this, new EventArgs()); } catch { }
                    _file_encrypt();
                    _upload_thread_flag = _upload_thread_flag & ~_UPLOAD_THREAD_FLAG_FILE_ENCRYPTING;
                    try { EncryptFinished?.Invoke(this, new EventArgs()); } catch { }
                    _uploaded_size = 0;


                    //handling IO
                    _local_cacher.FileIORequest(_local_path);
                    _upload_thread_flag |= _UPLOAD_THREAD_FLAG_DIGEST_REQUESTED;
                    try { EncryptFileDigestStarted?.Invoke(this, new EventArgs()); } catch { }
                    _file_io_response.Wait();
                    _file_io_response.Reset();
                    try { EncryptFileDigestFinished?.Invoke(this, new EventArgs()); } catch { }

                    //handling file slice data
                    _uploaded_size = 0;
                    _file_size     = _local_data.Size;
                    _slice_count   = (int)Math.Ceiling(_file_size * 1.0 / BaiduPCS.UPLOAD_SLICE_SIZE);
                }
                #endregion

                //status check
                #region status check
                if ((_upload_thread_flag & _UPLOAD_THREAD_FLAG_CANCEL_REQUESTED) != 0)
                {
                    _upload_thread_flag = (_upload_thread_flag | _UPLOAD_THREAD_FLAG_CANCELLED) & ~(_UPLOAD_THREAD_FLAG_CANCEL_REQUESTED | _UPLOAD_THREAD_FLAG_STARTED);
                    _end_time           = DateTime.Now;
                    return;
                }
                if ((_upload_thread_flag & _UPLOAD_THREAD_FLAG_PAUSE_REQUESTED) != 0)
                {
                    _upload_thread_flag = (_upload_thread_flag | _UPLOAD_THREAD_FLAG_PAUSED) & ~(_UPLOAD_THREAD_FLAG_PAUSE_REQUESTED | _UPLOAD_THREAD_FLAG_STARTED);
                    _end_time           = DateTime.Now;
                    return;
                }
                if ((_upload_thread_flag & _UPLOAD_THREAD_FLAG_ERROR) != 0)
                {
                    _upload_thread_flag = (_upload_thread_flag | _UPLOAD_THREAD_FLAG_PAUSED) & ~_UPLOAD_THREAD_FLAG_STARTED;
                    _end_time           = DateTime.Now;
                    try { TaskError?.Invoke(this, new EventArgs()); } catch { }
                    return;
                }
                #endregion

                //rapid upload test
                var  sync_lock        = new ManualResetEventSlim();
                var  rapid_param      = _local_data;
                bool rapid_upload_suc = false;
                if (_enable_rapid_upload)
                {
                    _remote_cacher.RapidUploadAsync(_remote_path, (ulong)rapid_param.Size, rapid_param.MD5, rapid_param.CRC32.ToString("X2").ToLower(), rapid_param.Slice_MD5, (suc, data, e) =>
                    {
                        rapid_upload_suc = suc;
                        _remote_data     = data;
                        sync_lock.Set();
                    }, _overwrite ? BaiduPCS.ondup.overwrite : BaiduPCS.ondup.newcopy, _selected_account_id);
                    sync_lock.Wait(120000);
                    sync_lock.Reset();
                }

                if (rapid_upload_suc == false)
                {
                    //deleting existed file if overwrite is true
                    if (_overwrite)
                    {
                        var temp_struct = new _temp_struct {
                            lck = sync_lock, suc = false
                        };
                        for (int i = 0; i < 10; i++)
                        {
                            _remote_cacher.DeletePathAsync(_remote_path, _delete_callback, _selected_account_id, temp_struct);
                            sync_lock.Wait(60000);
                            sync_lock.Reset();
                            if (temp_struct.suc)
                            {
                                break;
                            }
                        }
                        if (temp_struct.suc == false)
                        {
                            Tracer.GlobalTracer.TraceWarning("delete file " + _remote_path + " failed, using newcopy instead (reached max. retry times)");
                        }
                    }
                    //pre create file request
                    if (string.IsNullOrEmpty(_upload_id))
                    {
                        var temp_struct = new _temp_struct {
                            lck = sync_lock, suc = false
                        };
                        for (int i = 0; i < 3; i++)
                        {
                            _remote_cacher.PreCreateFileAsync(_remote_path, _slice_count, _pre_create_request_callback, _selected_account_id, temp_struct);
                            sync_lock.Wait(120000);
                            sync_lock.Reset();
                            if (temp_struct.suc)
                            {
                                break;
                            }
                        }
                        if (temp_struct.suc == false)
                        {
                            //precreate failed
                            _upload_thread_flag |= _UPLOAD_THREAD_FLAG_ERROR;
                            _end_time            = DateTime.Now;
                            try { TaskError?.Invoke(this, new EventArgs()); } catch { }
                            return;
                        }
                        #region status check
                        if ((_upload_thread_flag & _UPLOAD_THREAD_FLAG_CANCEL_REQUESTED) != 0)
                        {
                            _upload_thread_flag = (_upload_thread_flag | _UPLOAD_THREAD_FLAG_CANCELLED) & ~(_UPLOAD_THREAD_FLAG_CANCEL_REQUESTED | _UPLOAD_THREAD_FLAG_STARTED);
                            _end_time           = DateTime.Now;
                            return;
                        }
                        if ((_upload_thread_flag & _UPLOAD_THREAD_FLAG_PAUSE_REQUESTED) != 0)
                        {
                            _upload_thread_flag = (_upload_thread_flag | _UPLOAD_THREAD_FLAG_PAUSED) & ~(_UPLOAD_THREAD_FLAG_PAUSE_REQUESTED | _UPLOAD_THREAD_FLAG_STARTED);
                            _end_time           = DateTime.Now;
                            return;
                        }
                        #endregion
                    }

                    //initializing multi thread data
                    var max_thread = _max_thread;
                    _task_id   = new Guid[max_thread];
                    _task_seq  = new int[max_thread];
                    _last_sent = new DateTime[max_thread];

                    //adding slice sequence
                    _slice_seq = new ConcurrentQueue <int>();
                    for (int i = 0; i < _slice_count; i++)
                    {
                        if (_slice_result.ContainsKey(i) == false)
                        {
                            _slice_seq.Enqueue(i);
                        }
                    }

                    //upload start, multi thread
                    var next_time = DateTime.Now.AddSeconds(1);
                    #region loop
                    while (true)
                    {
                        //handling finish state
                        if (_slice_result.Count == _slice_count)
                        {
                            break;
                        }
                        //handling other state
                        #region status check
                        if ((_upload_thread_flag & _UPLOAD_THREAD_FLAG_CANCEL_REQUESTED) != 0)
                        {
                            _upload_thread_flag = (_upload_thread_flag | _UPLOAD_THREAD_FLAG_CANCELLED) & ~(_UPLOAD_THREAD_FLAG_CANCEL_REQUESTED | _UPLOAD_THREAD_FLAG_STARTED);
                            for (int i = 0; i < _task_id.Length; i++)
                            {
                                if (_task_id[i] != Guid.Empty)
                                {
                                    _remote_cacher.UploadSliceCancelAsync(_task_id[i]);
                                }
                            }
                            _end_time = DateTime.Now;
                            return;
                        }
                        if ((_upload_thread_flag & _UPLOAD_THREAD_FLAG_PAUSE_REQUESTED) != 0)
                        {
                            _upload_thread_flag = (_upload_thread_flag | _UPLOAD_THREAD_FLAG_PAUSED) & ~(_UPLOAD_THREAD_FLAG_PAUSE_REQUESTED | _UPLOAD_THREAD_FLAG_STARTED);
                            for (int i = 0; i < _task_id.Length; i++)
                            {
                                if (_task_id[i] != Guid.Empty)
                                {
                                    _remote_cacher.UploadSliceCancelAsync(_task_id[i]);
                                }
                            }
                            _end_time = DateTime.Now;
                            return;
                        }
                        if ((_upload_thread_flag & (_UPLOAD_THREAD_FLAG_ERROR | _UPLOAD_THREAD_FLAG_FILE_MODIFIED)) != 0)
                        {
                            _upload_thread_flag = _upload_thread_flag & ~_UPLOAD_THREAD_FLAG_STARTED;
                            _end_time           = DateTime.Now;
                            try { TaskError?.Invoke(this, new EventArgs()); } catch { }
                            return;
                        }
                        #endregion

                        lock (_thread_data_lock)
                        {
                            for (int i = 0; i < max_thread; i++)
                            {
                                if ((DateTime.Now - _last_sent[i]).TotalSeconds > 120)
                                {
                                    if (_task_id[i] != Guid.Empty)
                                    {
                                        _last_sent[i] = DateTime.Now;
                                        _remote_cacher.UploadSliceCancelAsync(_task_id[i], _selected_account_id);
                                        _task_id[i] = Guid.Empty;
                                        continue;
                                    }
                                    if (_slice_seq.Count == 0)
                                    {
                                        continue;
                                    }
                                    //error handling for dequeue failure
                                    if (_slice_seq.TryDequeue(out _task_seq[i]) == false)
                                    {
                                        _upload_thread_flag |= _UPLOAD_THREAD_FLAG_ERROR;
                                        _end_time            = DateTime.Now;
                                        try { TaskError?.Invoke(this, new EventArgs()); } catch { }
                                        return;
                                    }
                                    try
                                    {
                                        _remote_cacher.UploadSliceBeginAsync((ulong)Math.Min(_file_size - BaiduPCS.UPLOAD_SLICE_SIZE * (long)_task_seq[i], BaiduPCS.UPLOAD_SLICE_SIZE), _remote_path, _upload_id, _task_seq[i], _on_slice_upload_request_callback, _selected_account_id, i);
                                    }
                                    catch { _slice_seq.Enqueue(_task_seq[i]); }
                                    _last_sent[i] = DateTime.Now;
                                }
                            }
                        }

                        //speed calculation
                        long size = Interlocked.Read(ref _uploaded_size);
                        _upload_size_5s.RemoveFirst();
                        _upload_size_5s.AddLast(size);
                        _average_speed_5s    = (_upload_size_5s.Last.Value - _upload_size_5s.First.Value) / 5.0;
                        _average_speed_total = size / (DateTime.Now - _start_time).TotalSeconds;

                        _current_bytes = 0;
                        Tracer.GlobalTracer.TraceInfo("Uploaded " + _uploaded_size + "/" + _file_size + " [" + (_average_speed_5s / 1024.0).ToString("0.00") + "KB/s]");

                        //monitor loop
                        var ts = next_time - DateTime.Now;
                        next_time = next_time.AddSeconds(1);
                        if (ts.TotalMilliseconds > 1)
                        {
                            Thread.Sleep((int)ts.TotalMilliseconds);
                        }
                    }
                    #endregion

                    //merging slice request
                    var temp_struct1 = new _temp_struct {
                        lck = sync_lock, suc = false
                    };
                    for (int i = 0; i < 3; i++)
                    {
                        _remote_cacher.CreteSuperFileAsync(_remote_path, _upload_id, from item in _slice_result orderby item.Key ascending select item.Value, (ulong)_file_size, _create_superfile_request_callback, _selected_account_id, temp_struct1);
                        sync_lock.Wait(120000);
                        sync_lock.Reset();
                        if (temp_struct1.suc)
                        {
                            break;
                        }
                    }

                    if (temp_struct1.suc)
                    {
                        _upload_thread_flag = (_upload_thread_flag | _UPLOAD_THREAD_FLAG_FINISHED) & ~_UPLOAD_THREAD_FLAG_STARTED;
                        try { TaskFinished?.Invoke(this, new EventArgs()); } catch { }
                    }
                    else
                    {
                        _upload_thread_flag = (_upload_thread_flag | _UPLOAD_THREAD_FLAG_ERROR | _UPLOAD_THREAD_FLAG_PAUSED) & ~_UPLOAD_THREAD_FLAG_STARTED;
                        try { TaskError?.Invoke(this, new EventArgs()); } catch { }
                    }
                    _end_time = DateTime.Now;
                }
                else
                {
                    //rapid upload succeeded
                    _upload_thread_flag = (_upload_thread_flag | _UPLOAD_THREAD_FLAG_FINISHED) & ~_UPLOAD_THREAD_FLAG_STARTED;
                    _end_time           = DateTime.Now;
                    try { TaskFinished?.Invoke(this, new EventArgs()); } catch { }
                }
            }
            catch (Exception)
            {
                _upload_thread_flag = (_upload_thread_flag | _UPLOAD_THREAD_FLAG_ERROR | _UPLOAD_THREAD_FLAG_PAUSED) & ~_UPLOAD_THREAD_FLAG_STARTED;
                _end_time           = DateTime.Now;
                try { TaskError?.Invoke(this, new EventArgs()); } catch { }
            }
            finally
            {
                _file_watcher.EnableRaisingEvents = false;
                _file_watcher.Dispose();
                _file_watcher = null;
                //deleting temporary encrypted file
                if (_enable_encryption && File.Exists(_local_path) && _local_path.EndsWith(".encrypted"))
                {
                    File.Delete(_local_path);
                }
                _monitor_thread_exited.Set();
            }
        }
Ejemplo n.º 11
0
 private void EventSourceOnTaskFinished(object sender, TaskFinishedEventArgs e)
 {
     TaskFinished?.Invoke(sender, e);
 }
Ejemplo n.º 12
0
 /// <summary>
 ///     Raises the <see cref="E:TaskFinished" /> event.
 /// </summary>
 /// <param name="e">The <see cref="ParkitectNexus.Data.Tasks.QueueableTaskEventArgs" /> instance containing the event data.</param>
 protected virtual void OnTaskFinished(QueueableTaskEventArgs e)
 {
     TaskFinished?.Invoke(this, e);
 }
Ejemplo n.º 13
0
 private void OnTaskFinished(string task)
 {
     TaskFinished?.Invoke(task);
 }
Ejemplo n.º 14
0
        public Host(Action <string> loader,
                    ConsoleService console,
                    TaskScheduler taskScheduler)
        {
            _loader = loader ?? throw new ArgumentNullException(nameof(loader));

            if (taskScheduler == null)
            {
                throw new ArgumentNullException(nameof(taskScheduler));
            }

            var scheduler = _scheduler = Schedule;

            Console = console;
            Timer   = new TimerService(scheduler);
            Xhr     = new XhrService(scheduler);

            void Schedule(ILogSource source, string name, CancellationToken cancellationToken,
                          Func <CancellationToken, Task> onSchedule,
                          Action <Exception> onError,
                          Action <OperationCanceledException> onCancel,
                          Action onFinally)
            {
                var task =
                    AsyncTask.Create(name,
                                     async thisTask =>
                {
                    try
                    {
                        await onSchedule(cancellationToken);
                        TaskFinishing?.Invoke(this, thisTask);
                        thisTask.FlagSuccess();
                    }
                    catch (OperationCanceledException e)
                    {
                        try
                        {
                            source.WarnLog?.Invoke(source, e, name);
                            onCancel?.Invoke(e);
                        }
                        finally
                        {
                            TaskFinishing?.Invoke(this, thisTask);
                            thisTask.FlagCanceled(e.CancellationToken);
                        }
                    }
                    catch (Exception e)
                    {
                        try
                        {
                            source.ErrorLog?.Invoke(source, e, name);
                            onError?.Invoke(e);
                        }
                        finally
                        {
                            TaskFinishing?.Invoke(this, thisTask);
                            thisTask.FlagError(e);
                        }
                    }
                    finally
                    {
                        try
                        {
                            onFinally?.Invoke();
                        }
                        finally
                        {
                            TaskFinished?.Invoke(this, thisTask);
                        }
                    }
                });

                TaskStarting?.Invoke(this, task);
                task.Start(taskScheduler);
            }
        }
 public FileLoadingTaskInfo(List <FileDescriptor> fileDesps, bool persistErrors, TaskFinished taskFinishedCallback)
     : base(persistErrors)
 {
     if (taskFinishedCallback != null)
     {
         SetTaskFinishedCallback(taskFinishedCallback);
     }
     fileDescriptors = fileDesps;
 }