From 5a1feade0d158cd9997f5989b34844c55d2b230c Mon Sep 17 00:00:00 2001 From: Pandu Masabathula Date: Sun, 18 Nov 2012 16:12:46 +0530 Subject: [PATCH] used UDPClient and added OVERRIDE_TCP_KEEP_ALIVE by removing config key --- .../3.3.0.1/PubNub-Messaging.Tests/App.config | 3 - .../PubNub-Messaging/PubNub-Messaging.csproj | 1 - csharp/3.3.0.1/PubNub-Messaging/Pubnub.cs | 136 ++++-------------- csharp/3.3.0.1/PubNub-Messaging/app.config | 3 - csharp/3.3.0.1/PubNub.suo | Bin 132096 -> 132096 bytes 5 files changed, 27 insertions(+), 116 deletions(-) diff --git a/csharp/3.3.0.1/PubNub-Messaging.Tests/App.config b/csharp/3.3.0.1/PubNub-Messaging.Tests/App.config index 796c7023..12fdc9f3 100644 --- a/csharp/3.3.0.1/PubNub-Messaging.Tests/App.config +++ b/csharp/3.3.0.1/PubNub-Messaging.Tests/App.config @@ -3,9 +3,6 @@ - - - diff --git a/csharp/3.3.0.1/PubNub-Messaging/PubNub-Messaging.csproj b/csharp/3.3.0.1/PubNub-Messaging/PubNub-Messaging.csproj index ac9c02cd..316897d8 100644 --- a/csharp/3.3.0.1/PubNub-Messaging/PubNub-Messaging.csproj +++ b/csharp/3.3.0.1/PubNub-Messaging/PubNub-Messaging.csproj @@ -58,7 +58,6 @@ - diff --git a/csharp/3.3.0.1/PubNub-Messaging/Pubnub.cs b/csharp/3.3.0.1/PubNub-Messaging/Pubnub.cs index 11d01df6..a039a03d 100644 --- a/csharp/3.3.0.1/PubNub-Messaging/Pubnub.cs +++ b/csharp/3.3.0.1/PubNub-Messaging/Pubnub.cs @@ -32,6 +32,7 @@ public class Pubnub : INotifyPropertyChanged const int PUBNUB_NETWORK_TCP_CHECK_INTERVAL_IN_SEC = 15; const int PUBNUB_NETWORK_CHECK_RETRIES = 50; const int PUBNUB_WEBREQUEST_RETRY_INTERVAL_IN_SEC = 10; + const bool OVERRIDE_TCP_KEEP_ALIVE = false; // Common property changed event public event PropertyChangedEventHandler PropertyChanged; @@ -74,8 +75,6 @@ public void RaisePropertyChanged(string propertyName) // Timestamp private List _Time = new List(); - private bool _overrideTcpKeepAlive = false; - // Pubnub Core API implementation private string ORIGIN = "pubsub.pubnub.com"; private string PUBLISH_KEY = ""; @@ -110,8 +109,6 @@ private void init(string publish_key, string subscribe_key, string secret_key, s else this.ORIGIN = "http://" + this.ORIGIN; - _overrideTcpKeepAlive = Boolean.Parse(ConfigurationManager.AppSettings["OverrideTcpKeepAlive"].ToString()); - //Initiate System Events for PowerModeChanged - to monitor suspend/resume initiatePowerModeCheck(); } @@ -232,14 +229,14 @@ void SystemEvents_PowerModeChanged(object sender, PowerModeChangedEventArgs e) { _pubnetSystemActive = false; TerminatePendingWebRequest(); - if (_overrideTcpKeepAlive) + if (OVERRIDE_TCP_KEEP_ALIVE) { heartBeatTimer.Change(Timeout.Infinite, Timeout.Infinite); } if (appSwitch.TraceInfo) { Trace.WriteLine(string.Format("DateTime {0}, System entered into Suspend Mode.", DateTime.Now.ToString())); - if (_overrideTcpKeepAlive) + if (OVERRIDE_TCP_KEEP_ALIVE) { Trace.WriteLine(string.Format("DateTime {0}, Disabled Timer for heartbeat ", DateTime.Now.ToString())); } @@ -248,7 +245,7 @@ void SystemEvents_PowerModeChanged(object sender, PowerModeChangedEventArgs e) else if (e.Mode == PowerModes.Resume) { _pubnetSystemActive = true; - if (_overrideTcpKeepAlive) + if (OVERRIDE_TCP_KEEP_ALIVE) { heartBeatTimer.Change( (-1 == PUBNUB_NETWORK_TCP_CHECK_INTERVAL_IN_SEC) ? -1 : PUBNUB_NETWORK_TCP_CHECK_INTERVAL_IN_SEC * 1000, @@ -257,7 +254,7 @@ void SystemEvents_PowerModeChanged(object sender, PowerModeChangedEventArgs e) if (appSwitch.TraceInfo) { Trace.WriteLine(string.Format("DateTime {0}, System entered into Resume/Awake Mode.", DateTime.Now.ToString())); - if (_overrideTcpKeepAlive) + if (OVERRIDE_TCP_KEEP_ALIVE) { Trace.WriteLine(string.Format("DateTime {0}, Enabled Timer for heartbeat ", DateTime.Now.ToString())); } @@ -684,7 +681,7 @@ void OnPubnubWebRequestTimeout(object state, bool timeout) } } - if (_overrideTcpKeepAlive) + if (OVERRIDE_TCP_KEEP_ALIVE) { //reset heart beat time because http request already timedout heartBeatTimer.Change( @@ -712,7 +709,7 @@ void OnPubnubHeartBeatTimeoutCallback(Object heartbeatState) if (_channelInternetStatus.ContainsKey(currentState.channel) && (currentState.type == ResponseType.Subscribe || currentState.type == ResponseType.Presence) - && _overrideTcpKeepAlive) + && OVERRIDE_TCP_KEEP_ALIVE) { _channelInternetStatus[currentState.channel] = networkConnection; @@ -917,7 +914,7 @@ private void _subscribe(string channel, object timetoken, Action usercallb return; } - if (_overrideTcpKeepAlive) + if (OVERRIDE_TCP_KEEP_ALIVE) { if (appSwitch.TraceInfo) { @@ -1042,7 +1039,7 @@ private void _presence(string channel, object timetoken, Action usercallba return; } - if (_overrideTcpKeepAlive) + if (OVERRIDE_TCP_KEEP_ALIVE) { if (appSwitch.TraceInfo) { @@ -1337,7 +1334,7 @@ private bool _urlRequest(List url_components, ResponseType type, Acti _channelRequest.AddOrUpdate(channelName, pubnubRequestState, (key, oldState) => pubnubRequestState); } - if (_overrideTcpKeepAlive) + if (OVERRIDE_TCP_KEEP_ALIVE) { //Eventhough heart-beat is disabled, run one time to check internet connection by setting dueTime=0 heartBeatTimer = new System.Threading.Timer( @@ -1378,7 +1375,7 @@ private bool _urlRequest(List url_components, ResponseType type, Acti string jsonString = streamReader.ReadToEnd(); streamReader.Close(); - if (_overrideTcpKeepAlive) + if (OVERRIDE_TCP_KEEP_ALIVE) { heartBeatTimer.Change( (-1 == PUBNUB_NETWORK_TCP_CHECK_INTERVAL_IN_SEC) ? -1 : PUBNUB_NETWORK_TCP_CHECK_INTERVAL_IN_SEC * 1000, @@ -1439,7 +1436,7 @@ private bool _urlRequest(List url_components, ResponseType type, Acti || webEx.Status == WebExceptionStatus.ConnectFailure //Sending Keep-alive packet failed (No network)/Server is down. || webEx.Status == WebExceptionStatus.ServerProtocolViolation//Problem with proxy or ISP || webEx.Status == WebExceptionStatus.ProtocolError - ) && (!_overrideTcpKeepAlive)) + ) && (!OVERRIDE_TCP_KEEP_ALIVE)) { //internet connection problem. if (appSwitch.TraceError) @@ -2816,116 +2813,37 @@ private static void callbackClientNetworkStatus(bool status) private static void checkClientNetworkAvailability(Action callback) { - #if (__MonoCS__) - bool connected = false; - if (NetworkInterface.GetIsNetworkAvailable()) - { - Ping netMon = new Ping(); - - try - { - PingReply response = netMon.Send("pubsub.pubnub.com"); - if (response != null) - { - if (appSwitch.TraceError) - { - Trace.WriteLine(response.RoundtripTime); - Trace.WriteLine(response.Status); - } - if(response.Status == IPStatus.Success) - { - connected = true; - } - } - } - catch (Exception ex) - { - if (appSwitch.TraceError) - { - Trace.WriteLine(string.Format("DateTime {0} checkSocketConnect Error. {1}", DateTime.Now.ToString(), ex.ToString())); - } - } - callback(connected); - } - else - { - callback(connected); - } - #else - if (NetworkInterface.GetIsNetworkAvailable()) - { - NetworkInterface[] netInterfaces = NetworkInterface.GetAllNetworkInterfaces(); - foreach (NetworkInterface netInterface in netInterfaces) - { - IPInterfaceProperties ip = netInterface.GetIPProperties(); - if (netInterface.OperationalStatus == OperationalStatus.Up) - { - if (netInterface.NetworkInterfaceType != NetworkInterfaceType.Tunnel - && netInterface.NetworkInterfaceType != NetworkInterfaceType.Loopback) - { - IPInterfaceProperties prop = netInterface.GetIPProperties(); - UnicastIPAddressInformationCollection unicast = prop.UnicastAddresses; - - foreach (UnicastIPAddressInformation uniIP in unicast) - { - IPAddress addrip = uniIP.Address; - if (addrip.AddressFamily != AddressFamily.InterNetwork) continue; - - InternetState state = new InternetState(); - state.ipaddr = addrip; - state.callback = callback; - ThreadPool.QueueUserWorkItem(checkSocketConnect, state); - mres.Wait(); - } - } - } - } - } - else - { - callback(false); - } - #endif + InternetState state = new InternetState(); + state.callback = callback; + ThreadPool.QueueUserWorkItem(checkSocketConnect, state); + mres.Wait(); } private static void checkSocketConnect(object internetState) { - bool connected = false; InternetState state = internetState as InternetState; - IPAddress ipaddr = state.ipaddr; Action callback = state.callback; try { - using (Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) + using (UdpClient udp = new UdpClient("pubsub.pubnub.com", 80)) { - socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Linger, false); + IPAddress localAddr = ((IPEndPoint)udp.Client.LocalEndPoint).Address; + udp.Close(); - IPEndPoint local = new IPEndPoint(ipaddr, 0); - socket.Bind(local); - socket.Connect("pubsub.pubnub.com", 80); - connected = true; - socket.Shutdown(SocketShutdown.Both); - socket.Disconnect(true); - socket.Close(); - } - } - catch (ObjectDisposedException objEx) - { - if (appSwitch.TraceVerbose) - { - Trace.WriteLine(string.Format("DateTime {0} checkSocketConnect Error. {1}", DateTime.Now.ToString(), objEx.ToString())); + if (appSwitch.TraceVerbose) + { + Trace.WriteLine(string.Format("DateTime {0} checkInternetStatus LocalIP: {1}", DateTime.Now.ToString(), localAddr.ToString())); + } + callback(true); } } catch (Exception ex) { - if (appSwitch.TraceVerbose) + if (appSwitch.TraceError) { - Trace.WriteLine(string.Format("DateTime {0} checkSocketConnect Error. {1}", DateTime.Now.ToString(), ex.ToString())); + Trace.WriteLine(string.Format("DateTime {0} checkInternetStatus Error. {1}", DateTime.Now.ToString(), ex.ToString())); } - } - if (callback != null) - { - callback(connected); + callback(false); } mres.Set(); } diff --git a/csharp/3.3.0.1/PubNub-Messaging/app.config b/csharp/3.3.0.1/PubNub-Messaging/app.config index 16134363..4eff6f1e 100644 --- a/csharp/3.3.0.1/PubNub-Messaging/app.config +++ b/csharp/3.3.0.1/PubNub-Messaging/app.config @@ -2,9 +2,6 @@ - - - diff --git a/csharp/3.3.0.1/PubNub.suo b/csharp/3.3.0.1/PubNub.suo index 4b74a7891787b6c03b8f4ccc4466fb144b089604..5a2ddfe81d7f879774625cc75bd08078e72b5250 100644 GIT binary patch del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