/// <exclude/> protected async Task <HttpResponseMessage> ExecuteRequestAsync(HttpRequestMessage request, bool logError = true) { try { if (request.Method != HttpMethod.Get && request.Content == null) { // Work around current Mono bug. request.Content = new ByteArrayContent(new byte[0]); } var response = await _client.SendAsync(request).ConfigureAwait(false); if (response.StatusCode == System.Net.HttpStatusCode.InternalServerError) { var error = SimpleJson.DeserializeObject <RestError>(await response.Content.ReadAsStringAsync().ConfigureAwait(false)); throw new ArgusTVException(error.detail); } if (response.StatusCode >= HttpStatusCode.BadRequest) { throw new ArgusTVException(response.ReasonPhrase); } return(response); } catch (AggregateException ex) { if (IsConnectionError(ex.InnerException)) { WakeOnLan.EnsureServerAwake(Proxies.ServerSettings); throw new ArgusTVNotFoundException(ex.InnerException.InnerException.Message); } throw; } catch (ArgusTVException) { throw; } catch (Exception ex) { if (logError) { Proxies.Logger.Error(ex.ToString()); } throw new ArgusTVUnexpectedErrorException("An unexpected error occured."); } }
private async Task <bool> InternalInitialize(ServerSettings serverSettings, bool throwError, IServiceProxyLogger logger, SourceLevels logLevel = SourceLevels.Error) #endif { #if DOTNET4 lock (Instance._syncLock) #else using (await _asyncLock.LockAsync().ConfigureAwait(false)) #endif { ServerSettings previousServerSettings = _serverSettings; bool previousIsInitialized = _isInitialized; IServiceProxyLogger previousLogger = _logger; try { _isInitialized = false; _serverSettings = serverSettings; _logger = logger ?? new DefaultLogger(); WakeOnLan.EnsureServerAwake(serverSettings); DateTime firstTryTime = DateTime.Now; for (; ;) { try { #if DOTNET4 PingAndCheckServer(serverSettings).Wait(); #else await PingAndCheckServer(serverSettings).ConfigureAwait(false); #endif break; } catch { TimeSpan span = DateTime.Now - firstTryTime; if (!serverSettings.WakeOnLan.Enabled || span.TotalSeconds >= serverSettings.WakeOnLan.TimeoutSeconds) { throw; } System.Threading.Thread.Sleep(1000); } } } catch { _serverSettings = previousServerSettings; _logger = previousLogger; _isInitialized = previousIsInitialized; if (!throwError) { return(false); } throw; } ResetProxies(); _isInitialized = true; return(true); } }