示例#1
0
        /// <summary>
        /// This function will run on different thread than UI. Make sure to use Invoke for interface update.
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        void queueTimer_DoWork(object sender, QueueTimerEventArgs e)
        {
            var item = e.Item as CloudItem;

            if (item == null)
            {
                return;
            }
            item.Try++;
            Invoke((Action) delegate()
            {
                MainForm.Current.AddTask(TaskName.CloudCommand);
            });
            Exception error = null;

            try
            {
                var ws = new WebServiceClient();
                ws.Url = SettingsManager.Options.InternetDatabaseUrl;
                CloudMessage result = null;
                var          o      = SettingsManager.Options;
                // Check if user public keys are present.
                o.CheckAndFixUserRsaKeys();
                // If cloud RSA keys are missing then...
                if (string.IsNullOrEmpty(o.CloudRsaPublicKey))
                {
                    // Step 1: Get Server's Public RSA key for encryption.
                    var msg = new CloudMessage(CloudAction.GetPublicRsaKey);
                    CloudHelper.ApplySecurity(item.Message);
                    msg.Values.Add(CloudKey.RsaPublicKey, o.UserRsaPublicKey);
                    // Retrieve public RSA key.
                    var results = ws.Execute(msg);
                    if (results.ErrorCode == 0)
                    {
                        o.CloudRsaPublicKey = results.Values.GetValue <string>(CloudKey.RsaPublicKey);
                        SettingsManager.OptionsData.Save();
                    }
                    else
                    {
                        error = new Exception(result.ErrorMessage);
                    }
                }
                // If no errors till this point then...
                if (error == null)
                {
                    // Add security.
                    CloudHelper.ApplySecurity(item.Message, o.UserRsaPublicKey, o.CloudRsaPublicKey, o.Username, o.Password);
                    // Add computer Id
                    item.Message.Values.Add(CloudKey.ComputerId, o.ComputerId, true);
                    result = ws.Execute(item.Message);
                    if (result.ErrorCode > 0)
                    {
                        error = new Exception(result.ErrorMessage);
                    }
                    else
                    {
                        Invoke((Action) delegate()
                        {
                            ProcessResult(item.Message, result);
                        });
                    }
                }
                ws.Dispose();
            }
            catch (Exception ex)
            {
                error = ex;
            }
            Invoke((Action) delegate()
            {
                MainForm.Current.RemoveTask(TaskName.CloudCommand);
            });
            item.Error = error;
            item.State = error == null ? CloudState.Done : CloudState.Error;
            e.Keep     = error != null;
            e.Break    = error != null;
        }
示例#2
0
        /// <summary>
        /// This function will run on different thread than UI. Make sure to use Invoke for interface update.
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        void queueTimer_DoWork(object sender, QueueTimerEventArgs e)
        {
            var item = e.Item as CloudItem;

            if (item == null)
            {
                return;
            }
            item.Try++;
            MainForm.Current.AddTask(TaskName.CloudCommand);
            Exception error = null;

            try
            {
                var ws = new WebServiceClient();
                ws.Url = SettingsManager.Options.InternetDatabaseUrl;
                CloudMessage result = null;
                var          o      = SettingsManager.Options;
                // Check if user public keys are present.
                o.CheckAndFixUserRsaKeys();
                // If cloud RSA keys are missing then...
                if (string.IsNullOrEmpty(o.CloudRsaPublicKey))
                {
                    // Step 1: Get Server's Public RSA key for encryption.
                    var msg = new CloudMessage(CloudAction.GetPublicRsaKey);
                    // Retrieve public RSA key.
                    var results = ws.Execute(msg);
                    if (results.ErrorCode == 0)
                    {
                        o.CloudRsaPublicKey = results.Values.GetValue <string>(CloudKey.RsaPublicKey);
                        SettingsManager.OptionsData.Save();
                    }
                    else
                    {
                        error = new Exception(result.ErrorMessage);
                    }
                }
                // If no errors till this point then...
                if (error == null)
                {
                    // Add security.
                    CloudHelper.ApplySecurity(item.Message, o.UserRsaPublicKey, o.CloudRsaPublicKey, o.Username, o.Password);
                    // Add computer and profile ID.
                    item.Message.Values.Add(CloudKey.ComputerId, o.ComputerId, true, true);
                    item.Message.Values.Add(CloudKey.ProfileId, o.ProfileId, true, true);
                    // Add version so it will be possible distinguish between Library (v3.x) and Virtual (v4.x) settings.
                    item.Message.Values.Add(CloudKey.ClientVersion, Application.ProductVersion, false, true);
                    // Call web service.
                    result = ws.Execute(item.Message);
                    if (result.ErrorCode != 0)
                    {
                        // If unable to decrypt error then...
                        if (result.ErrorCode == (int)CloudErrorCode.UnableToDecrypt)
                        {
                            // Get server's RSA Public key.
                            var cloudRsaPublicKey = result.Values.GetValue <string>(CloudKey.RsaPublicKey, null);
                            // If key was set then update local key.
                            if (!string.IsNullOrEmpty(cloudRsaPublicKey))
                            {
                                o.CloudRsaPublicKey = cloudRsaPublicKey;
                            }
                        }
                        error = new Exception(result.ErrorMessage);
                    }
                    else
                    {
                        ProcessResult(item.Message, result);
                    }
                }
                ws.Dispose();
            }
            catch (Exception ex)
            {
                error = ex;
            }
            MainForm.Current.RemoveTask(TaskName.CloudCommand);
            var success = error == null;

            item.Error = error;
            item.State = success ? CloudState.Done : CloudState.Error;
            // If error or have not finished.
            e.Keep = !success;
            // error and no more retries left then...
            if (!success && item.Try >= item.Retries)
            {
                // Order to remove task.
                e.Keep = false;
            }
            // Exit thread (queue will be processed later)
            e.Cancel = !success;
        }