private static void CheckClientNetworkAvailability <T>(Action <bool> callback, Action <PubnubClientError> errorCallback, string[] channels, string[] channelGroups) { lock (_internetCheckLock) { if (isInternetCheckRunning) { LoggingMethod.WriteToLog(string.Format("DateTime {0} InternetCheckRunning Already running", DateTime.Now.ToString()), LoggingMethod.LevelInfo); return; } } mres = new ManualResetEvent(false); InternetState <T> state = new InternetState <T>(); state.Callback = callback; state.ErrorCallback = errorCallback; state.Channels = channels; state.ChannelGroups = channelGroups; #if !PORTABLE151 && !PORTABLE259 && !PORTABLE111 && !WP81 ThreadPool.QueueUserWorkItem(CheckSocketConnect <T>, state); #else CheckSocketConnect <T>(state); #endif mres.WaitOne(500); }
private static void CheckSocketConnect <T>(object internetState) { InternetState <T> state = internetState as InternetState <T>; Action <bool> callback = state.Callback; Action <PubnubClientError> errorCallback = state.ErrorCallback; string[] channels = state.Channels; string[] channelGroups = state.ChannelGroups; try { //_status = true; //callback(true); //System.Net.WebRequest test = System.Net.WebRequest.Create("http://pubsub.pubnub.com"); HttpWebRequest myRequest = (HttpWebRequest)System.Net.WebRequest.Create("http://pubsub.pubnub.com"); myRequest.Method = "HEAD"; myRequest.BeginGetResponse(cb => { try { //Just want to check whether we can hit server to check internet connection. //Expecting webexception with code 404. No response is expected. myRequest.EndGetResponse(cb); _status = true; } catch (WebException webEx) { if (webEx.Response != null) { HttpStatusCode currentHttpStatusCode = ((HttpWebResponse)webEx.Response).StatusCode; if ((int)currentHttpStatusCode == 404) { //The remote server returned an error: (404) Nothing _status = true; } } else { _status = false; } } mres.Set(); }, null); mres.WaitOne(100); // using (UdpSocketClient socket = new UdpSocketClient()) // { // await socket.ConnectAsync("pubsub.pubnub.com", 80); // } } catch (Exception ex) { _status = false; ParseCheckSocketConnectException <T>(ex, channels, channelGroups, errorCallback, callback); } finally { } }
private static void CheckClientNetworkAvailability <T>(Action <bool> callback, Action <PubnubClientError> errorCallback, string[] channels, string[] channelGroups) { InternetState <T> state = new InternetState <T>(); state.Callback = callback; state.ErrorCallback = errorCallback; state.Channels = channels; state.ChannelGroups = channelGroups; CheckSocketConnect <T>(state); }
static void socketAsync_Completed <T>(object sender, SocketAsyncEventArgs e) { if (e.LastOperation == SocketAsyncOperation.Connect) { Socket skt = sender as Socket; InternetState <T> state = e.UserToken as InternetState <T>; if (state != null) { LoggingMethod.WriteToLog(string.Format("DateTime {0} socketAsync_Completed.", DateTime.Now.ToString()), LoggingMethod.LevelInfo); state.Callback(true); } mreSocketAsync.Set(); } }
private static void CheckClientNetworkAvailability <T> (Action <bool> callback, Action <PubnubClientError> errorCallback, string[] channels) { InternetState <T> state = new InternetState <T> (); state.Callback = callback; state.ErrorCallback = errorCallback; state.Channels = channels; #if (UNITY_ANDROID || UNITY_IOS) CheckSocketConnect <T>(state); #elif (__MonoCS__) CheckSocketConnect <T> (state); #else ThreadPool.QueueUserWorkItem(CheckSocketConnect <T>, state); #endif #if (SILVERLIGHT || WINDOWS_PHONE) mres.WaitOne(); #elif (!UNITY_ANDROID && !UNITY_IOS) mres.Wait(); #endif }
private static void CheckSocketConnect <T> (object internetState) { InternetState <T> state = internetState as InternetState <T>; Action <bool> callback = state.Callback; Action <PubnubClientError> errorCallback = state.ErrorCallback; string[] channels = state.Channels; string[] channelGroups = state.ChannelGroups; try { #if (SILVERLIGHT || WINDOWS_PHONE) using (Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) { SocketAsyncEventArgs sae = new SocketAsyncEventArgs(); sae.UserToken = state; sae.RemoteEndPoint = new DnsEndPoint("pubsub.pubnub.com", 80); sae.Completed += new EventHandler <SocketAsyncEventArgs>(socketAsync_Completed <T>); bool test = socket.ConnectAsync(sae); mreSocketAsync.WaitOne(1000); sae.Completed -= new EventHandler <SocketAsyncEventArgs>(socketAsync_Completed <T>); socket.Close(); } #elif (UNITY_IOS || UNITY_ANDROID) if (request != null) { request.Abort(); request = null; } request = (HttpWebRequest)WebRequest.Create("http://pubsub.pubnub.com"); if (request != null) { request.Timeout = HeartbeatInterval * 1000; request.ContentType = "application/json"; if (response != null) { response.Close(); response = null; } response = request.GetResponse(); if (response != null) { if (((HttpWebResponse)response).ContentLength <= 0) { _status = false; throw new Exception("Failed to connect"); } else { using (Stream dataStream = response.GetResponseStream()){ using (StreamReader reader = new StreamReader(dataStream)){ string responseFromServer = reader.ReadToEnd(); LoggingMethod.WriteToLog(string.Format("DateTime {0}, Response:{1}", DateTime.Now.ToString(), responseFromServer), LoggingMethod.LevelInfo); _status = true; callback(true); reader.Close(); } dataStream.Close(); } } } } #elif (__MonoCS__) using (UdpClient udp = new UdpClient("pubsub.pubnub.com", 80)) { IPAddress localAddress = ((IPEndPoint)udp.Client.LocalEndPoint).Address; if (udp != null && udp.Client != null && udp.Client.RemoteEndPoint != null) { udp.Client.SendTimeout = HeartbeatInterval * 1000; EndPoint remotepoint = udp.Client.RemoteEndPoint; string remoteAddress = (remotepoint != null) ? remotepoint.ToString() : ""; LoggingMethod.WriteToLog(string.Format("DateTime {0} checkInternetStatus LocalIP: {1}, RemoteEndPoint:{2}", DateTime.Now.ToString(), localAddress.ToString(), remoteAddress), LoggingMethod.LevelVerbose); _status = true; callback(true); } } #else using (UdpClient udp = new UdpClient("pubsub.pubnub.com", 80)) { IPAddress localAddress = ((IPEndPoint)udp.Client.LocalEndPoint).Address; EndPoint remotepoint = udp.Client.RemoteEndPoint; string remoteAddress = (remotepoint != null) ? remotepoint.ToString() : ""; udp.Close(); LoggingMethod.WriteToLog(string.Format("DateTime {0} checkInternetStatus LocalIP: {1}, RemoteEndPoint:{2}", DateTime.Now.ToString(), localAddress.ToString(), remoteAddress), LoggingMethod.LevelVerbose); callback(true); } #endif } #if (UNITY_IOS || UNITY_ANDROID) catch (WebException webEx) { if (webEx.Message.Contains("404")) { _status = true; callback(true); } else { _status = false; ParseCheckSocketConnectException <T>(webEx, channels, channelGroups, errorCallback, callback); } } #endif catch (Exception ex) { #if (__MonoCS__) _status = false; #endif ParseCheckSocketConnectException <T> (ex, channels, channelGroups, errorCallback, callback); } finally { #if (UNITY_IOS || UNITY_ANDROID) if (response != null) { response.Close(); response = null; } if (request != null) { request = null; } #elif (__MonoCS__) #endif #if (UNITY_IOS) GC.Collect(); #endif } #if (!UNITY_ANDROID && !UNITY_IOS) mres.Set(); #endif }
private static void CheckSocketConnect <T>(object internetState) { lock (_internetCheckLock) { isInternetCheckRunning = true; } HttpWebRequest myRequest = null; Action <bool> callback = null; Action <PubnubClientError> errorCallback = null; string[] channels = null; string[] channelGroups = null; try { InternetState <T> state = internetState as InternetState <T>; if (state != null) { callback = state.Callback; errorCallback = state.ErrorCallback; channels = state.Channels; channelGroups = state.ChannelGroups; } mreSocketAsync = new ManualResetEvent(false); myRequest = (HttpWebRequest)System.Net.WebRequest.Create("https://pubsub.pubnub.com/time/0"); myRequest.BeginGetResponse(cb => { try { using (HttpWebResponse resp = myRequest.EndGetResponse(cb) as HttpWebResponse) { if (resp != null && resp.StatusCode == HttpStatusCode.OK) { LoggingMethod.WriteToLog(string.Format("DateTime {0} CheckSocketConnect Resp {1}", DateTime.Now.ToString(), HttpStatusCode.OK.ToString()), LoggingMethod.LevelInfo); _status = true; } } } catch (Exception ex) { _status = false; ParseCheckSocketConnectException <T>(ex, channels, channelGroups, errorCallback, callback); LoggingMethod.WriteToLog(string.Format("DateTime {0} CheckSocketConnect Failed {1}", DateTime.Now.ToString(), ex.ToString()), LoggingMethod.LevelInfo); } finally { mreSocketAsync.Set(); } }, null); mreSocketAsync.WaitOne(330); } catch (Exception ex) { _status = false; ParseCheckSocketConnectException <T>(ex, channels, channelGroups, errorCallback, callback); } finally { isInternetCheckRunning = false; mres.Set(); } }
private static void CheckSocketConnect <T>(object internetState) { lock (_internetCheckLock) { isInternetCheckRunning = true; } HttpWebRequest myRequest = null; Action <bool> callback = null; Action <PubnubClientError> errorCallback = null; string[] channels = null; string[] channelGroups = null; try { InternetState <T> state = internetState as InternetState <T>; if (state != null) { callback = state.Callback; errorCallback = state.ErrorCallback; channels = state.Channels; channelGroups = state.ChannelGroups; } mreSocketAsync = new ManualResetEvent(false); myRequest = (HttpWebRequest)System.Net.WebRequest.Create("https://pubsub.pubnub.com/time/0"); LoggingMethod.WriteToLog(string.Format("DateTime {0} CheckSocketConnect Req {1}", DateTime.Now.ToString(), myRequest.RequestUri.ToString()), LoggingMethod.LevelInfo); ServicePointManager.DefaultConnectionLimit = 200; myRequest.BeginGetResponse(cb => { try { if (myRequest == null) { return; } using (HttpWebResponse resp = myRequest.EndGetResponse(cb) as HttpWebResponse) { if (resp != null && resp.StatusCode == HttpStatusCode.OK) { LoggingMethod.WriteToLog(string.Format("DateTime {0} CheckSocketConnect Resp {1}", DateTime.Now.ToString(), HttpStatusCode.OK.ToString()), LoggingMethod.LevelInfo); _status = true; //System.IO.Stream stream = resp.GetResponseStream(); //using (System.IO.StreamReader streamReader = new System.IO.StreamReader(stream)) //{ // stream.Flush(); // string jsonString = streamReader.ReadToEnd(); // LoggingMethod.WriteToLog(string.Format("DateTime {0} CheckSocketConnect Resp {1}", DateTime.Now.ToString(), jsonString), LoggingMethod.LevelInfo); // _status = true; // stream.Close(); //} } resp.Close(); } } catch (Exception ex) { LoggingMethod.WriteToLog(string.Format("DateTime {0} CheckSocketConnect Failed {1}", DateTime.Now.ToString(), ex.ToString()), LoggingMethod.LevelInfo); } finally { if (myRequest != null) { myRequest = null; } mreSocketAsync.Set(); } }, null); mreSocketAsync.WaitOne(330, false); } catch (Exception ex) { _status = false; LoggingMethod.WriteToLog(string.Format("DateTime {0} CheckSocketConnectTime FAILED {1}", DateTime.Now.ToString(), ex.ToString()), LoggingMethod.LevelInfo); ParseCheckSocketConnectException <T>(ex, channels, channelGroups, errorCallback, callback); } finally { isInternetCheckRunning = false; mres.Set(); } }