Permalink
Browse files

used UDPClient and added OVERRIDE_TCP_KEEP_ALIVE by removing config key

  • Loading branch information...
1 parent f8eef81 commit 5a1feade0d158cd9997f5989b34844c55d2b230c @budgetpreneur budgetpreneur committed Nov 18, 2012
@@ -3,9 +3,6 @@
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
</startup>
- <appSettings>
- <add key="OverrideTcpKeepAlive" value="false"/>
- </appSettings>
<system.diagnostics>
<trace autoflush="true" indentsize="4">
<listeners>
@@ -58,7 +58,6 @@
</Reference>
<Reference Include="System" />
<Reference Include="System.ComponentModel.DataAnnotations" />
- <Reference Include="System.configuration" />
<Reference Include="System.Net" />
<Reference Include="System.XML" />
</ItemGroup>
@@ -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<object> _Time = new List<object>();
- 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<T>(string channel, object timetoken, Action<T> usercallb
return;
}
- if (_overrideTcpKeepAlive)
+ if (OVERRIDE_TCP_KEEP_ALIVE)
{
if (appSwitch.TraceInfo)
{
@@ -1042,7 +1039,7 @@ private void _presence<T>(string channel, object timetoken, Action<T> usercallba
return;
}
- if (_overrideTcpKeepAlive)
+ if (OVERRIDE_TCP_KEEP_ALIVE)
{
if (appSwitch.TraceInfo)
{
@@ -1337,7 +1334,7 @@ private bool _urlRequest<T>(List<string> 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<T>(List<string> 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<T>(List<string> 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<bool> 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<bool> 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();
}
@@ -2,9 +2,6 @@
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup>
- <appSettings>
- <add key="OverrideTcpKeepAlive" value="false"/>
- </appSettings>
<system.diagnostics>
<trace autoflush="true" indentsize="4">
<listeners>
View
Binary file not shown.

0 comments on commit 5a1fead

Please sign in to comment.