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(); } }
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; 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__ || UNITY_STANDALONE) 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, errorCallback, callback); } } #endif catch (Exception ex) { #if (__MonoCS__ || UNITY_STANDALONE) _status = false; #endif ParseCheckSocketConnectException <T> (ex, channels, errorCallback, callback); } finally { #if (UNITY_IOS || UNITY_ANDROID) if (response != null) { response.Close(); response = null; } if (request != null) { request = null; } #elif (__MonoCS__ || UNITY_STANDALONE) #endif #if (UNITY_IOS) GC.Collect(); #endif } #if (!UNITY_ANDROID && !UNITY_IOS && !UNITY_STANDALONE) mres.Set(); #endif }
public void ResetChannelsAndChannelGroupsAndBuildState() { AllPresenceChannelsOrChannelGroups.Clear(); AllNonPresenceChannelsOrChannelGroups.Clear(); ChannelsAndChannelGroupsAwaitingConnectCallback.Clear(); AllChannels.Clear(); AllChannelGroups.Clear(); AllSubscribedChannelsAndChannelGroups.Clear(); HasChannelGroups = false; HasChannels = false; HasPresenceChannels = false; HasChannelsOrChannelGroups = false; CurrentSubscribedChannelsCount = 0; CurrentSubscribedChannelGroupsCount = 0; foreach (var ci in channelEntitiesDictionary) { if (ci.Value.IsSubscribed) { if (ci.Key.IsChannelGroup) { CurrentSubscribedChannelGroupsCount++; AllChannelGroups.Add(new ChannelEntity(ci.Key, ci.Value)); } else { CurrentSubscribedChannelsCount++; AllChannels.Add(new ChannelEntity(ci.Key, ci.Value)); } AllSubscribedChannelsAndChannelGroups.Add(new ChannelEntity(ci.Key, ci.Value)); if (ci.Key.IsPresenceChannel) { AllPresenceChannelsOrChannelGroups.Add(new ChannelEntity(ci.Key, ci.Value)); } else { AllNonPresenceChannelsOrChannelGroups.Add(new ChannelEntity(ci.Key, ci.Value)); } if (ci.Value.IsAwaitingConnectCallback) { ChannelsAndChannelGroupsAwaitingConnectCallback.Add(new ChannelEntity(ci.Key, ci.Value)); } } #if (ENABLE_PUBNUB_LOGGING) LoggingMethod.WriteToLog(string.Format("DateTime {0}, ResetChannelsAndChannelGroupsAndBuildState: channelEntities subscription key/val {1} {2}", DateTime.Now.ToString(), ci.Key.ChannelOrChannelGroupName, ci.Value.IsSubscribed), LoggingMethod.LevelInfo); #endif } if (CurrentSubscribedChannelGroupsCount > 0) { HasChannelGroups = true; } if (CurrentSubscribedChannelsCount > 0) { HasChannels = true; } if (AllPresenceChannelsOrChannelGroups.Count > 0) { HasPresenceChannels = true; } if (HasChannels || HasChannelGroups) { HasChannelsOrChannelGroups = true; } CompiledUserState = Helpers.BuildJsonUserState(AllSubscribedChannelsAndChannelGroups); }
private void OnTelemetryIntervalTimeout(System.Object telemetryState) { LoggingMethod.WriteToLog(pubnubLog, string.Format("DateTime {0}, TelemetryManager - OnTelemetryIntervalTimeout => CleanupTelemetryData", DateTime.Now.ToString(CultureInfo.InvariantCulture)), pubnubConfig.LogVerbosity); CleanupTelemetryData(); }
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 void StartCoroutinesByName <T> (string url, RequestState <T> pubnubRequestState, int timeout, int pause, CurrentRequestType crt) { CoroutineParams <T> cp = new CoroutineParams <T> (url, timeout, pause, crt, typeof(T), pubnubRequestState); if (crt == CurrentRequestType.Subscribe) { if ((SubTimeoutCoroutine != null) && (!isSubscribeComplete)) { StopCoroutine(SubTimeoutCoroutine); #if (ENABLE_PUBNUB_LOGGING) LoggingMethod.WriteToLog(string.Format("DateTime {0}, Stopped existing timeout coroutine {1}", DateTime.Now.ToString(), cp.crt.ToString()), LoggingMethod.LevelInfo); #endif } SubTimeoutCoroutine = CheckTimeoutSub <T> (cp); SubCoroutine = SendRequestSub <T> (cp); StartCoroutine(SubTimeoutCoroutine); StartCoroutine(SubCoroutine); } else if (crt == CurrentRequestType.NonSubscribe) { if ((NonSubTimeoutCoroutine != null) && (!isNonSubscribeComplete)) { StopCoroutine(NonSubTimeoutCoroutine); #if (ENABLE_PUBNUB_LOGGING) LoggingMethod.WriteToLog(string.Format("DateTime {0}, Stopped existing timeout coroutine {1}", DateTime.Now.ToString(), cp.crt.ToString()), LoggingMethod.LevelInfo); #endif } NonSubTimeoutCoroutine = CheckTimeoutNonSub <T> (cp); NonSubCoroutine = SendRequestNonSub <T> (cp); StartCoroutine(NonSubTimeoutCoroutine); StartCoroutine(NonSubCoroutine); } else if (crt == CurrentRequestType.PresenceHeartbeat) { if ((PresenceHeartbeatTimeoutCoroutine != null) && (!isPresenceHeartbeatComplete)) { StopCoroutine(PresenceHeartbeatTimeoutCoroutine); #if (ENABLE_PUBNUB_LOGGING) LoggingMethod.WriteToLog(string.Format("DateTime {0}, Stopped existing timeout coroutine {1}", DateTime.Now.ToString(), cp.crt.ToString()), LoggingMethod.LevelInfo); #endif } PresenceHeartbeatTimeoutCoroutine = CheckTimeoutPresenceHeartbeat <T> (cp); PresenceHeartbeatCoroutine = SendRequestPresenceHeartbeat <T> (cp); StartCoroutine(PresenceHeartbeatTimeoutCoroutine); StartCoroutine(PresenceHeartbeatCoroutine); } else if (crt == CurrentRequestType.Heartbeat) { if ((HeartbeatTimeoutCoroutine != null) && (!isHearbeatComplete)) { StopCoroutine(HeartbeatTimeoutCoroutine); #if (ENABLE_PUBNUB_LOGGING) LoggingMethod.WriteToLog(string.Format("DateTime {0}, Stopped existing timeout coroutine {1}", DateTime.Now.ToString(), cp.crt.ToString()), LoggingMethod.LevelInfo); #endif } HeartbeatTimeoutCoroutine = CheckTimeoutHeartbeat <T> (cp); HeartbeatCoroutine = SendRequestHeartbeat <T> (cp); StartCoroutine(HeartbeatTimeoutCoroutine); StartCoroutine(HeartbeatCoroutine); } }
protected override string EncryptOrDecrypt(bool type, string plainStr) { { #if (SILVERLIGHT || WINDOWS_PHONE) AesManaged aesEncryption = new AesManaged(); aesEncryption.KeySize = 256; aesEncryption.BlockSize = 128; //get ASCII bytes of the string aesEncryption.IV = System.Text.Encoding.UTF8.GetBytes("0123456789012345"); aesEncryption.Key = System.Text.Encoding.UTF8.GetBytes(GetEncryptionKey()); #else RijndaelManaged aesEncryption = new RijndaelManaged(); aesEncryption.KeySize = 256; aesEncryption.BlockSize = 128; //Mode CBC aesEncryption.Mode = CipherMode.CBC; //padding aesEncryption.Padding = PaddingMode.PKCS7; //get ASCII bytes of the string aesEncryption.IV = System.Text.Encoding.ASCII.GetBytes("0123456789012345"); aesEncryption.Key = System.Text.Encoding.ASCII.GetBytes(GetEncryptionKey()); #endif if (type) { ICryptoTransform crypto = aesEncryption.CreateEncryptor(); plainStr = EncodeNonAsciiCharacters(plainStr); #if (SILVERLIGHT || WINDOWS_PHONE) byte[] plainText = Encoding.UTF8.GetBytes(plainStr); #else byte[] plainText = Encoding.ASCII.GetBytes(plainStr); #endif //encrypt byte[] cipherText = crypto.TransformFinalBlock(plainText, 0, plainText.Length); return(Convert.ToBase64String(cipherText)); } else { try { ICryptoTransform decrypto = aesEncryption.CreateDecryptor(); //decode byte[] decryptedBytes = Convert.FromBase64CharArray(plainStr.ToCharArray(), 0, plainStr.Length); //decrypt #if (SILVERLIGHT || WINDOWS_PHONE) var data = decrypto.TransformFinalBlock(decryptedBytes, 0, decryptedBytes.Length); string decrypted = Encoding.UTF8.GetString(data, 0, data.Length); #else string decrypted = System.Text.Encoding.ASCII.GetString(decrypto.TransformFinalBlock(decryptedBytes, 0, decryptedBytes.Length)); #endif return(decrypted); } catch (Exception ex) { LoggingMethod.WriteToLog(string.Format("DateTime {0} Decrypt Error. {1}", DateTime.Now.ToString(), ex.ToString()), LoggingMethod.LevelVerbose); throw ex; //LoggingMethod.WriteToLog(string.Format("DateTime {0} Decrypt Error. {1}", DateTime.Now.ToString(), ex.ToString()), LoggingMethod.LevelVerbose); //return "**DECRYPT ERROR**"; } } } }