/// <summary> /// Invokes method in proxied service passing parameters specified. /// Optionally, persists and retries the method until successful. /// </summary> /// <param name="methodName">Name of method in proxied service to invoke</param> /// <param name="wifiOnly">Indicates if message is to be sent via WiFi only</param> /// <param name="recurringId">Will mark a recurring message with and Id that can be used to stop the call from recurring. /// Method must have RecurringAttribute or id will not be record. Pass Guid.Empty if you don't wish to set this value.</param> /// <param name="parameters">Optional list of parameters to include in method invocation</param> /// <returns>If call is recurring will return an id that can be used to stop to recurrence; Otherwise will return null.</returns> public Guid CallService(String methodName, bool wifiOnly, Guid recurringId, params Object[] parameters) { _logger.Debug("CallService invoked, methodName is: " + methodName + ", wifiOnly: " + wifiOnly + ", Guid: " + recurringId); var proxyType = ProxiedService.GetType(); _logger.Debug("proxyType: " + proxyType); return(CallService(ProxiedService.GetType().GetRuntimeMethod(methodName, Util.GetTypes(parameters)), wifiOnly, recurringId, parameters)); }
/// <summary> /// Starts the task that will makes sure pending messages /// are sent. /// </summary> /// <returns>Returns true if task is running</returns> public bool StartWorker() { var svc = ProxiedService.IsNull() ? "Null" : ProxiedService.GetType().FullName; _logger.Debug("Starting worker thread: " + svc); try { if (_workerProcTask == null || _workerProcTask.Status != TaskStatus.Running) { Task.Run(async() => { try { _logger.Debug($"Starting task. Id={ Task.CurrentId.GetValueOrDefault() }, Managed Thread Id {Environment.CurrentManagedThreadId}"); _workerToken = new CancellationTokenSource(); _workerProcTask = WorkerProcAsync(_workerToken.Token); await _workerProcTask.ConfigureAwait(false); _logger.Debug($"Task Status: {_workerProcTask.Status}"); } catch (OperationCanceledException) { _logger.Debug("WorkProc has been Canceled"); } }); } } catch (Exception ex) { _logger.Error("Failed to start worker: " + ex.Message, ex); return(false); } return(true); }
/// <summary> /// Send all pending messages. /// </summary> /// <param name="forceSend">If true WiFi only will not be observed.</param> /// <param name="token"></param> public async Task SendPendingMessages(bool forceSend = false, CancellationToken token = default(CancellationToken)) { var pendingMessages = RetrievePendingMessages(); if (pendingMessages != null && pendingMessages.Count > 0) { var svc = ProxiedService.IsNull() ? "Null" : ProxiedService.GetType().FullName; _logger.Debug($"Pending message count: {pendingMessages.Count} / ProxiedService Type == {svc} / WiFiConnected == {WifiConnected} / ForceSend == {forceSend}"); foreach (var call in pendingMessages.Where(call => !call.WifiOnly || WifiConnected || forceSend)) { if (token.IsCancellationRequested) { token.ThrowIfCancellationRequested(); } try { _logger.Debug($"Replaying message {call.MessageId} {call.MethodName}"); call.Target = ProxiedService; call.Method = ProxiedService.GetType() .GetRuntimeMethod(call.MethodName, Util.GetTypes(call.Parameters)); CallService(call); await Task.Delay(TimeSpan.FromSeconds(5), token).ConfigureAwait(false); } catch (OperationCanceledException) { } catch (Exception e) { _logger.Warn(e.Message, e); } } } }
/// <summary> /// Invokes method in proxied service passing parameters specified. /// Optionally, persists and retries the method until successful. /// </summary> /// <param name="methodName">Name of method in proxied service to invoke</param> /// <param name="wifiOnly">Indicates if message is to be sent via WiFi only</param> /// <param name="parameters">Optional list of parameters to include in method invocation</param> /// <returns>If call is recurring will return an id that can be used to stop to recurrence; Otherwise will return null.</returns> public Guid CallService(String methodName, bool wifiOnly, params Object[] parameters) { _logger.Debug("CallService invoked, methodName is: " + methodName + ", wifiOnly: " + wifiOnly); return(CallService(ProxiedService.GetType().GetRuntimeMethod(methodName, Util.GetTypes(parameters)), wifiOnly, Guid.Empty, parameters)); }
/// <summary> /// Invokes method in proxied service passing parameters specified. /// Optionally, persists and retries the method until successful. /// </summary> /// <param name="methodName">Name of method in proxied service to invoke</param> /// <param name="recurringId">Will mark a recurring message with and Id that can be used to stop the call from recurring. /// Method must have RecurringAttribute or id will not be record. Pass Guid.Empty if you don't wish to set this value.</param> /// <param name="parameters">Optional list of parameters to include in method invocation</param> /// <returns>If call is recurring will return an id that can be used to stop to recurrence; Otherwise will return null.</returns> public Guid CallService(String methodName, Guid recurringId, params Object[] parameters) { _logger.Debug("CallService invoked, methodName is: " + methodName + ", Guid: " + recurringId); return(CallService(ProxiedService.GetType().GetRuntimeMethod(methodName, Util.GetTypes(parameters)), false, recurringId, parameters)); }