private async Task <bool> CheckClientNetworkAvailability <T>(Action <bool> internalCallback, PNOperationType type, PNCallback <T> callback, string[] channels, string[] channelGroups) { lock (internetCheckLock) { if (isInternetCheckRunning) { LoggingMethod.WriteToLog(pubnubLog, string.Format("DateTime {0} InternetCheckRunning Already running", DateTime.Now.ToString()), pubnubConfig.LogVerbosity); return(networkStatus); } } //mres = new ManualResetEvent(false); InternetState <T> state = new InternetState <T>(); state.InternalCallback = internalCallback; state.PubnubCallbacck = callback; state.ResponseType = type; //state.ErrorCallback = errorCallback; state.Channels = channels; state.ChannelGroups = channelGroups; networkStatus = await CheckSocketConnect <T>(state); return(networkStatus); //ThreadPool.QueueUserWorkItem(CheckSocketConnect<T>, state); //mres.WaitOne(500); }
private static async Task <bool> CheckClientNetworkAvailability <T>(Action <bool> internalCallback, PNOperationType type, PNCallback <T> callback, string[] channels, string[] channelGroups) { lock (internetCheckLock) { if (isInternetCheckRunning) { LoggingMethod.WriteToLog(pubnubLog, string.Format("DateTime {0} InternetCheckRunning Already running", DateTime.Now.ToString(CultureInfo.InvariantCulture)), pubnubConfig.LogVerbosity); return(networkStatus); } } InternetState <T> state = new InternetState <T>(); state.InternalCallback = internalCallback; state.PubnubCallbacck = callback; state.ResponseType = type; state.Channels = channels; state.ChannelGroups = channelGroups; networkStatus = await CheckSocketConnect <T>(state).ConfigureAwait(false); return(networkStatus); }
private static async Task <bool> CheckSocketConnect <T>(object internetState) { lock (internetCheckLock) { isInternetCheckRunning = true; } LoggingMethod.WriteToLog(pubnubLog, string.Format("DateTime {0} CheckSocketConnect Entered", DateTime.Now.ToString(CultureInfo.InvariantCulture)), pubnubConfig.LogVerbosity); Action <bool> internalCallback = null; PNCallback <T> pubnubCallback = null; PNOperationType type = PNOperationType.None; string[] channels = null; string[] channelGroups = null; var t = new TaskCompletionSource <bool>(); InternetState <T> state = internetState as InternetState <T>; if (state != null) { internalCallback = state.InternalCallback; type = state.ResponseType; pubnubCallback = state.PubnubCallbacck; channels = state.Channels; channelGroups = state.ChannelGroups; } PubnubApi.Interface.IUrlRequestBuilder urlBuilder = new UrlRequestBuilder(pubnubConfig, jsonLib, unit, pubnubLog, null); Uri requestUri = urlBuilder.BuildTimeRequest(null); try { bool gotTimeResp = false; if (pubnubConfig.UseClassicHttpWebRequest) { gotTimeResp = await GetTimeWithClassicHttp <T>(requestUri).ConfigureAwait(false); t.TrySetResult(gotTimeResp); } else { #if !NET35 && !NET40 && !NET45 && !NET461 && !NETSTANDARD10 if (pubnubConfig.UseTaskFactoryAsyncInsteadOfHttpClient) { gotTimeResp = await GetTimeWithTaskFactoryAsync(requestUri).ConfigureAwait(false); } else { gotTimeResp = await GetTimeWithHttpClient(requestUri).ConfigureAwait(false); } t.TrySetResult(gotTimeResp); #else gotTimeResp = await GetTimeWithTaskFactoryAsync(requestUri).ConfigureAwait(false); t.TrySetResult(gotTimeResp); #endif } } catch (Exception ex) { networkStatus = false; LoggingMethod.WriteToLog(pubnubLog, string.Format("DateTime {0} CheckSocketConnect (HttpClient Or Task.Factory) Failed {1}", DateTime.Now.ToString(CultureInfo.InvariantCulture), ex.Message), pubnubConfig.LogVerbosity); if (!networkStatus) { t.TrySetResult(false); isInternetCheckRunning = false; ParseCheckSocketConnectException <T>(ex, type, pubnubCallback, internalCallback); } } finally { isInternetCheckRunning = false; } return(await t.Task.ConfigureAwait(false)); }
private async Task <bool> CheckSocketConnect <T>(object internetState) { lock (internetCheckLock) { isInternetCheckRunning = true; } LoggingMethod.WriteToLog(pubnubLog, string.Format("DateTime {0} CheckSocketConnect Entered", DateTime.Now.ToString(CultureInfo.InvariantCulture)), pubnubConfig.LogVerbosity); Action <bool> internalCallback = null; PNCallback <T> pubnubCallback = null; PNOperationType type = PNOperationType.None; string[] channels = null; string[] channelGroups = null; var t = new TaskCompletionSource <bool>(); InternetState <T> state = internetState as InternetState <T>; if (state != null) { internalCallback = state.InternalCallback; type = state.ResponseType; pubnubCallback = state.PubnubCallbacck; channels = state.Channels; channelGroups = state.ChannelGroups; } PubnubApi.Interface.IUrlRequestBuilder urlBuilder = new UrlRequestBuilder(pubnubConfig, jsonLib, unit, pubnubLog, null); Uri requestUri = urlBuilder.BuildTimeRequest(); try { #if !NET35 && !NET40 && !NET45 && !NET461 && !NETSTANDARD10 var response = await httpClient.GetAsync(requestUri); if (response.IsSuccessStatusCode) { LoggingMethod.WriteToLog(pubnubLog, string.Format("DateTime {0} HttpClient CheckSocketConnect Resp {1}", DateTime.Now.ToString(CultureInfo.InvariantCulture), response.StatusCode.ToString()), pubnubConfig.LogVerbosity); networkStatus = true; t.TrySetResult(true); } else { LoggingMethod.WriteToLog(pubnubLog, string.Format("DateTime {0} HttpClient CheckSocketConnect Resp {1}", DateTime.Now.ToString(CultureInfo.InvariantCulture), response.StatusCode.ToString()), pubnubConfig.LogVerbosity); networkStatus = false; t.TrySetResult(false); } #else HttpWebRequest myRequest = null; myRequest = (HttpWebRequest)System.Net.WebRequest.Create(requestUri); myRequest.Method = "GET"; using (HttpWebResponse response = await Task.Factory.FromAsync <HttpWebResponse>(myRequest.BeginGetResponse, asyncPubnubResult => (HttpWebResponse)myRequest.EndGetResponse(asyncPubnubResult), null)) { if (response != null && response.StatusCode == HttpStatusCode.OK) { LoggingMethod.WriteToLog(pubnubLog, string.Format("DateTime {0} WebRequest CheckSocketConnect Resp {1}", DateTime.Now.ToString(CultureInfo.InvariantCulture), response.StatusCode.ToString()), pubnubConfig.LogVerbosity); networkStatus = true; t.TrySetResult(true); } } #endif } catch (Exception ex) { networkStatus = false; LoggingMethod.WriteToLog(pubnubLog, string.Format("DateTime {0} CheckSocketConnect (HttpClient Or Task.Factory) Failed {1}", DateTime.Now.ToString(CultureInfo.InvariantCulture), ex.Message), pubnubConfig.LogVerbosity); if (!networkStatus) { t.TrySetResult(false); isInternetCheckRunning = false; ParseCheckSocketConnectException <T>(ex, type, pubnubCallback, internalCallback); } } finally { isInternetCheckRunning = false; } return(await t.Task.ConfigureAwait(false)); }