Example #1
0
        public SmartUri(IPEndPoint endpoint)
        {
            if (endpoint == null)
            {
                throw new ArgumentNullException("endpoint");
            }
            this.HostNameType = SmartUri.GetHostNameType(endpoint.Address);
            this.Host         = endpoint.Address.ToString();
            string str;

            if (this.HostNameType != UriHostNameType.IPv6)
            {
                str = this.Host;
            }
            else
            {
                str = this.Host.Trim('[', ']');
            }
            this.DnsFriendlyHost = str;
            this.Port            = endpoint.Port;
            this.IPEndPoint      = endpoint;
        }
Example #2
0
        public static bool TryParse(string value, int defaultPort, out SmartUri uri)
        {
            uri = new SmartUri();
            if (string.IsNullOrEmpty(value))
            {
                return(false);
            }
            int startIndex1 = value.IndexOf('[');
            int startIndex2 = 0;
            int length1     = value.Length;
            int num;

            if (startIndex1 >= 0)
            {
                startIndex2 = startIndex1;
                int startIndex3 = value.IndexOf(']', startIndex1);
                if (startIndex3 < 0)
                {
                    return(false);
                }
                length1 = startIndex3 - startIndex1 + 1;
                num     = value.IndexOf(':', startIndex3);
                if (num > 0 && startIndex3 + 1 != num)
                {
                    return(false);
                }
            }
            else
            {
                num = value.LastIndexOf(':');
                if (num >= 0)
                {
                    if (value.IndexOf(':') == num)
                    {
                        length1 = num - startIndex2;
                    }
                    else
                    {
                        num = -1;
                    }
                }
            }
            string    ipString = value.Substring(startIndex2, length1);
            IPAddress address;

            IPAddress.TryParse(ipString, out address);
            int result;

            if (num >= 0)
            {
                int startIndex3 = num + 1;
                int length2     = value.Length - startIndex3;
                if (!int.TryParse(value.Substring(startIndex3, length2), out result))
                {
                    return(false);
                }
            }
            else
            {
                result = defaultPort;
            }
            UriHostNameType hostNameType = SmartUri.GetHostNameType(address);


            SmartUri local     = @uri;
            SmartUri smartUri1 = new SmartUri();

            smartUri1.HostNameType = hostNameType;
            smartUri1.Host         = ipString;
            SmartUri smartUri2 = smartUri1;
            string   str;

            if (hostNameType != UriHostNameType.IPv6)
            {
                str = ipString;
            }
            else
            {
                str = ipString.Trim('[', ']');
            }
            smartUri2.DnsFriendlyHost = str;
            smartUri1.Port            = result;
            smartUri1.IPEndPoint      = address != null ? new IPEndPoint(address, result) : (IPEndPoint)null;
            SmartUri smartUri3 = smartUri1;

            local = smartUri3;
            return(true);
        }