예제 #1
0
        public static async Task <T> DeSerializeFromPathAsync <T>(string filepath)
        {
            FileUtils.CreateAllFilepathDirectories(filepath);

            if (!File.Exists(filepath))
            {
                return(default(T));
            }

            StreamReader reader = new StreamReader(filepath);

            Task <string> read_task   = reader.ReadToEndAsync();
            AwaitResult   read_result = await AwaitUtils.AwaitTask(read_task);

            if (read_result.HasErrors)
            {
                return(default(T));
            }

            T deserialized_object = Newtonsoft.Json.JsonConvert.DeserializeObject <T>(read_task.Result);

            reader.Dispose();

            return(deserialized_object);
        }
예제 #2
0
 /// <summary>
 /// Handles the objectReceived event of the client.
 /// </summary>
 /// <param name="sender">The source of the event.</param>
 /// <param name="e">The <see cref="ObjectReceivedEventArgs"/> instance containing the event data.</param>
 private void _client_objectReceived(object sender, ObjectReceivedEventArgs e)
 {
     if (_awaitResult == null)
     {
         if (!_results.TryDequeue(out _awaitResult))
         {
             _awaitResult = null;
         }
     }
     if (_awaitResult != null)
     {
         _awaitResult.result = e.Object;
         if (_awaitResult.allResultsAcquired)
         {
             _awaitResult.waiter.Set();
             _awaitResult = null;
         }
     }
     _resultConsumed.Reset();
 }
예제 #3
0
        /// <summary>
        /// Sends the next command.
        /// </summary>
        /// <param name="command">The command.</param>
        /// <returns>The result of the command.</returns>
        /// <exception cref="System.Exception">Connection Error</exception>
        private Queue sendCommand(Command command)
        {
            int numberOfResultsToExpect      = command.numberOfResultsToExpect;
            ManualResetEventSlim waiter      = new ManualResetEventSlim(false);
            AwaitResult          awaitResult = new AwaitResult(waiter, numberOfResultsToExpect);

            lock (_lock) {
                _client.send(command);
                _results.Enqueue(awaitResult);
            }

            waiter.Wait();
            object result = awaitResult.result;

            _resultConsumed.Set();
            if (result is ConnectionError)
            {
                throw new Exception("Connection Error");
            }

            return((Queue)result);
        }
예제 #4
0
        public static async Task <bool> SerializeToPathAsync(string filepath, object to_serialize)
        {
            FileUtils.CreateAllFilepathDirectories(filepath);

            string data = Newtonsoft.Json.JsonConvert.SerializeObject(to_serialize, Newtonsoft.Json.Formatting.Indented);

            FileUtils.DeleteFileIfExists(filepath);

            StreamWriter writer = File.CreateText(filepath);

            Task        write_task   = writer.WriteAsync(data);
            AwaitResult write_result = await AwaitUtils.AwaitTask(write_task);

            writer.Dispose();

            if (write_result.HasErrors)
            {
                return(false);
            }

            return(true);
        }
예제 #5
0
 /// <summary>
 /// Initializes a new <see cref="AsyncWaitTCoroutine{TValue}"/>.
 /// </summary>
 /// <param name="result">Container for storing the task result.</param>
 /// <param name="taskFactory">Task factory function.</param>
 /// <exception cref="ArgumentNullException">
 ///     The <paramref name="result"/> parameter is null.
 ///     The <paramref name="taskFactory"/> parameter is null.
 /// </exception>
 public AsyncWaitTCoroutine(AwaitResult <TValue> result, Func <AwaitResult <TValue>, Task <TValue> > taskFactory)
 {
     _result      = result ?? throw new ArgumentNullException(nameof(result));
     _taskFactory = taskFactory ?? throw new ArgumentNullException(nameof(taskFactory));
 }
 public AwaiterStruct(AwaitResult result)
 {
     _result = result;
 }
 public AwaiterClass(AwaitResult result)
 {
     _result = result;
 }
 public AwaitableStructWithAwaiterStruct(AwaitResult result)
 {
     _result = result;
 }
		/// <summary>
		/// Sends the next command.
		/// </summary>
		/// <param name="command">The command.</param>
		/// <returns>The result of the command.</returns>
		/// <exception cref="System.Exception">Connection Error</exception>
		private Queue sendCommand(Command command) {
			int numberOfResultsToExpect = command.numberOfResultsToExpect;
			ManualResetEventSlim waiter = new ManualResetEventSlim(false);
			AwaitResult awaitResult = new AwaitResult(waiter, numberOfResultsToExpect);
			lock (_lock) {
				_client.send(command);
				_results.Enqueue(awaitResult);
			}

			waiter.Wait();
			object result = awaitResult.result;
			_resultConsumed.Set();
			if (result is ConnectionError)
				throw new Exception("Connection Error");

			return (Queue)result;
		}
		/// <summary>
		/// Handles the objectReceived event of the client.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="ObjectReceivedEventArgs"/> instance containing the event data.</param>
		private void _client_objectReceived(object sender, ObjectReceivedEventArgs e) {
			if (_awaitResult == null) {
				if (!_results.TryDequeue(out _awaitResult))
					_awaitResult = null;
			}
			if (_awaitResult != null) {
				_awaitResult.result = e.Object;
				if (_awaitResult.allResultsAcquired) {
					_awaitResult.waiter.Set();
					_awaitResult = null;
				}
			}
			_resultConsumed.Reset();
		}
예제 #11
0
 public AwaiterStruct(AwaitResult result)
 {
     _result = result;
 }
예제 #12
0
 public AwaiterClass(AwaitResult result)
 {
     _result = result;
 }
예제 #13
0
 public AwaitableStructWithAwaiterStruct(AwaitResult result)
 {
     _result = result;
 }
예제 #14
0
 public AwaitableClassWithAwaiterClass(AwaitResult result)
 {
     _result = result;
 }
예제 #15
0
 /// <summary>
 /// Initializes a new <see cref="SyncWaitTCoroutine"/>.
 /// </summary>
 /// <param name="result">Container for storing the task result.</param>
 /// <param name="coroutine">Implementation of the <see cref="ICoroutine"/></param>
 /// <exception cref="ArgumentNullException">
 ///     The <paramref name="result"/> parameter is null.
 ///     The <paramref name="coroutine"/> parameter is null.
 /// </exception>
 public SyncWaitTCoroutine(AwaitResult <object?> result, ICoroutine coroutine)
 {
     _result    = result ?? throw new ArgumentNullException(nameof(result));
     _coroutine = coroutine ?? throw new ArgumentNullException(nameof(coroutine));
 }
예제 #16
0
        protected override async Task RunRequestInternal()
        {
            // For Github Api Issues documentation visit https://developer.github.com/v3/issues/

            if (user == null)
            {
                has_errors   = true;
                error_result = new CreateIssueErrorObject("Github user is null", null);

                return;
            }

            if (repo == null)
            {
                error_result = new CreateIssueErrorObject("Github repo is null", null);

                return;
            }

            if (issue == null)
            {
                error_result = new CreateIssueErrorObject("Github issue is null", null);

                return;
            }

            string json_issue_object = Fast.Parsers.JSONParser.ComposeObject(issue);

            RestClient client = new RestClient("https://api.github.com/");

            client.Authenticator = new HttpBasicAuthenticator(user.GithubUsername, user.GithubAccessToken);

            string url = "/repos/" + repo.GithubRepoOwnerUsername + "/" + repo.GithubRepoName.ToLower() + "/issues";

            RestRequest request = new RestRequest(url, Method.POST);

            request.AddHeader("content-type", "application/vnd.github.symmetra-preview+json");
            request.AddJsonBody(json_issue_object);

            Task <IRestResponse> task = client.ExecuteTaskAsync(request);

            AwaitResult result = await AwaitUtils.AwaitTask(task);

            if (result.HasErrors)
            {
                has_errors   = true;
                error_result = new CreateIssueErrorObject(result.Exception.Message, result.Exception);

                return;
            }

            if (task.Result == null)
            {
                has_errors   = true;
                error_result = new CreateIssueErrorObject("Result is null", null);

                return;
            }

            if (!task.Result.IsSuccessful)
            {
                has_errors   = true;
                error_result = new CreateIssueErrorObject(task.Result.Content, task.Result.ErrorException);

                return;
            }

            success_result = new CreateIssueSuccessObject();
        }
예제 #17
0
        protected override async Task RunRequestInternal()
        {
            // For Google Drive Api documentation visit https://developers.google.com/sheets/api/quickstart/dotnet

            string[] scopes = { SheetsService.Scope.SpreadsheetsReadonly };

            ClientSecrets secrets = new ClientSecrets();

            secrets.ClientId     = client_id;
            secrets.ClientSecret = client_secret;

            Task <UserCredential> user_credential_task        = GoogleWebAuthorizationBroker.AuthorizeAsync(secrets, scopes, "user", CancellationToken.None);
            AwaitResult           user_credential_task_result = await AwaitUtils.AwaitTask(user_credential_task);

            if (user_credential_task_result.HasErrors)
            {
                has_errors   = true;
                error_result = new DownloadSpreadsheetErrorObject(
                    user_credential_task_result.Exception.Message, user_credential_task_result.Exception);

                return;
            }

            UserCredential credential = user_credential_task.Result;

            if (credential == null)
            {
                has_errors   = true;
                error_result = new DownloadSpreadsheetErrorObject("Credentials are null", null);

                return;
            }

            BaseClientService.Initializer service_in = new BaseClientService.Initializer();
            service_in.HttpClientInitializer = credential;
            service_in.ApplicationName       = "Download spreadhseet GoogleDrive";

            SheetsService service = new SheetsService(service_in);

            SpreadsheetsResource.ValuesResource.GetRequest get_values_request =
                service.Spreadsheets.Values.Get(document_id, document_data_range);

            Task <Google.Apis.Sheets.v4.Data.ValueRange> get_values_task = get_values_request.ExecuteAsync();
            AwaitResult get_values_task_result = await AwaitUtils.AwaitTask(get_values_task);

            if (get_values_task_result.HasErrors)
            {
                has_errors   = true;
                error_result = new DownloadSpreadsheetErrorObject(
                    get_values_task_result.Exception.Message, get_values_task_result.Exception);

                return;
            }

            Google.Apis.Sheets.v4.Data.ValueRange values = get_values_task.Result;

            if (values == null)
            {
                has_errors   = true;
                error_result = new DownloadSpreadsheetErrorObject("Values are null", null);

                return;
            }

            IList <IList <object> > values_data = values.Values;

            List <List <object> > data = new List <List <object> >();

            List <IList <object> > values_data_list = values_data.ToList();

            for (int i = 0; i < values_data_list.Count; ++i)
            {
                List <object> data_row = values_data_list[i].ToList();

                data.Add(data_row);
            }

            Data.GridData grid_data = new Data.GridData(data);

            success_result = new DownloadSpreadsheetSuccessObject(grid_data);
        }
 public AwaitableClassWithAwaiterClass(AwaitResult result)
 {
     _result = result;
 }