private static NetworkDetector.ConnectivityStatus CheckTcpConnectivity(Uri baseAddress, out Exception exception)
 {
     NetworkDetector.ConnectivityStatus connectivityStatu = NetworkDetector.ConnectivityStatus.Unavailable;
     exception = null;
     if (!RelayEnvironment.GetEnvironmentVariable("RELAYFORCEHTTP", false) && !RelayEnvironment.GetEnvironmentVariable("RELAYFORCEHTTPS", false))
     {
         try
         {
             BinaryMessageEncodingBindingElement binaryMessageEncodingBindingElement = new BinaryMessageEncodingBindingElement();
             TcpTransportBindingElement          tcpTransportBindingElement          = new TcpTransportBindingElement();
             tcpTransportBindingElement.ConnectionPoolSettings.MaxOutboundConnectionsPerEndpoint = 100;
             tcpTransportBindingElement.MaxReceivedMessageSize = (long)65536;
             CustomBinding customBinding = new CustomBinding();
             customBinding.Elements.Add(binaryMessageEncodingBindingElement);
             customBinding.Elements.Add(tcpTransportBindingElement);
             customBinding.OpenTimeout    = TimeSpan.FromSeconds(10);
             customBinding.SendTimeout    = TimeSpan.FromSeconds(10);
             customBinding.ReceiveTimeout = TimeSpan.MaxValue;
             int num = 9350;
             Uri uri = ServiceBusUriHelper.CreateServiceUri("net.tcp", string.Concat(baseAddress.DnsSafeHost, ":", num.ToString(CultureInfo.InvariantCulture)), "/");
             IChannelFactory <IDuplexSessionChannel> channelFactory = null;
             IDuplexSessionChannel duplexSessionChannel             = null;
             try
             {
                 channelFactory = customBinding.BuildChannelFactory <IDuplexSessionChannel>(new object[0]);
                 channelFactory.Open();
                 duplexSessionChannel = channelFactory.CreateChannel(new EndpointAddress(uri, new AddressHeader[0]));
                 duplexSessionChannel.Open();
                 Message message = Message.CreateMessage(MessageVersion.Default, "http://schemas.microsoft.com/netservices/2009/05/servicebus/connect/OnewayPing", new OnewayPingMessage());
                 duplexSessionChannel.Send(message, customBinding.SendTimeout);
                 duplexSessionChannel.Close();
                 duplexSessionChannel = null;
                 channelFactory.Close();
                 channelFactory = null;
             }
             finally
             {
                 if (duplexSessionChannel != null)
                 {
                     duplexSessionChannel.Abort();
                 }
                 if (channelFactory != null)
                 {
                     channelFactory.Abort();
                 }
             }
             connectivityStatu = NetworkDetector.ConnectivityStatus.Available;
         }
         catch (CommunicationException communicationException)
         {
             exception = communicationException;
         }
         catch (TimeoutException timeoutException)
         {
             exception = timeoutException;
         }
     }
     NetworkDetector.LogResult(baseAddress, "Tcp", connectivityStatu);
     return(connectivityStatu);
 }
 private static void LogResult(Uri checkUri, string checkMode, NetworkDetector.ConnectivityStatus result)
 {
     if (result == NetworkDetector.ConnectivityStatus.Available)
     {
         return;
     }
     MessagingClientEtwProvider.Provider.DetectConnectivityModeFailed(checkUri.AbsoluteUri, checkMode);
 }
 private static NetworkDetector.ConnectivityStatus CheckHttpWebStreamConnectivity(Uri baseAddress, out Exception exception)
 {
     NetworkDetector.ConnectivityStatus connectivityStatu = NetworkDetector.ConnectivityStatus.Unavailable;
     exception = null;
     if (!RelayEnvironment.GetEnvironmentVariable("RELAYFORCEHTTPS", false) && WebStream.IsSupportingScheme(ServiceBusUriHelper.CreateServiceUri("http", baseAddress.Authority, "/"), out exception))
     {
         connectivityStatu = NetworkDetector.ConnectivityStatus.Available;
     }
     NetworkDetector.LogResult(baseAddress, "Http WebStream", connectivityStatu);
     return(connectivityStatu);
 }
 private static NetworkDetector.ConnectivityStatus CheckHttpsWebStreamConnectivity(Uri baseAddress, out Exception exception)
 {
     NetworkDetector.ConnectivityStatus connectivityStatu = NetworkDetector.ConnectivityStatus.Unavailable;
     exception = null;
     if (WebStream.IsSupportingScheme(ServiceBusUriHelper.CreateServiceUri("https", baseAddress.Authority, "/"), out exception))
     {
         connectivityStatu = NetworkDetector.ConnectivityStatus.Available;
     }
     NetworkDetector.LogResult(baseAddress, "https WebStream", connectivityStatu);
     return(connectivityStatu);
 }
        private static bool IsHttpsWebSocketConnectivityAvailable(Uri baseAddress, out Exception exception)
        {
            NetworkDetector.ConnectivityStatus connectivityStatu;
            exception = null;
            while (true)
            {
                NetworkDetector.ConnectivityStatus connectivityStatu1 = (NetworkDetector.ConnectivityStatus)Interlocked.CompareExchange(ref NetworkDetector.httpsWebSocketConnectivityStatus, 1, 0);
                switch (connectivityStatu1)
                {
                case NetworkDetector.ConnectivityStatus.Unknown:
                {
                    connectivityStatu = NetworkDetector.CheckHttpsWebSocketConnectivity(baseAddress, out exception);
                    NetworkDetector.ConnectivityStatus connectivityStatu2 = (NetworkDetector.ConnectivityStatus)Interlocked.CompareExchange(ref NetworkDetector.httpsWebSocketConnectivityStatus, (int)connectivityStatu, 1);
                    if (connectivityStatu2 == NetworkDetector.ConnectivityStatus.Testing)
                    {
                        return(connectivityStatu == NetworkDetector.ConnectivityStatus.Available);
                    }
                    if (connectivityStatu2 == NetworkDetector.ConnectivityStatus.Unknown)
                    {
                        continue;
                    }
                    if (connectivityStatu == connectivityStatu2)
                    {
                        return(connectivityStatu == NetworkDetector.ConnectivityStatus.Available);
                    }
                    NetworkDetector.httpsWebSocketConnectivityStatus = 0;
                    continue;
                }

                case NetworkDetector.ConnectivityStatus.Testing:
                {
                    Thread.Sleep(5);
                    continue;
                }

                case NetworkDetector.ConnectivityStatus.Available:
                case NetworkDetector.ConnectivityStatus.Unavailable:
                {
                    return(connectivityStatu1 == NetworkDetector.ConnectivityStatus.Available);
                }

                default:
                {
                    goto case NetworkDetector.ConnectivityStatus.Unknown;
                }
                }
            }
            return(connectivityStatu == NetworkDetector.ConnectivityStatus.Available);
        }
 private static NetworkDetector.ConnectivityStatus CheckHttpConnectivity(Uri baseAddress, out Exception exception)
 {
     NetworkDetector.ConnectivityStatus connectivityStatu = NetworkDetector.ConnectivityStatus.Unavailable;
     exception = null;
     try
     {
         Uri        uri        = ServiceBusUriHelper.CreateServiceUri("http", baseAddress.Authority, "/");
         WebRequest webRequest = WebRequest.Create(uri);
         webRequest.Method = "GET";
         StreamReader streamReader = new StreamReader(webRequest.GetResponse().GetResponseStream());
         using (streamReader)
         {
             streamReader.ReadToEnd();
         }
         connectivityStatu = NetworkDetector.ConnectivityStatus.Available;
     }
     catch (WebException webException)
     {
         exception = webException;
     }
     NetworkDetector.LogResult(baseAddress, "Http", connectivityStatu);
     return(connectivityStatu);
 }