/// <summary>
        /// Tries to parse a string to <see cref="HostEndPoint"/>
        /// </summary>
        /// <param name="input">The input string.</param>
        /// <param name="hostEndpoint">The parsed HostEndPoint</param>
        /// <returns>true if <paramref name="input"/> converted successfully; otherwise, false.</returns>
        public static bool TryParse(string input, out HostEndPoint hostEndpoint)
        {
            var entries = input.Split(':');

            if (entries.Length != 2)
            {
                _logger.InfoFormat("Failed to parse HostEndPoint because input has not exactly two parts splitting by ':'. ({0})", input);
                hostEndpoint = null;
                return(false);
            }
            if (!int.TryParse(entries[1], out var port))
            {
                _logger.InfoFormat("Failed to parse HostEndPoint because port is invalid. ({0})", input);
                hostEndpoint = null;
                return(false);
            }
            if (port < 0 || 65535 < port)
            {
                _logger.InfoFormat("Failed to parse HostEndPoint because port is out of range. ({0})", input);
                hostEndpoint = null;
                return(false);
            }

            /*
             * Almost anything can be a hostname which makes further validation here hard.
             * Accept any string in entries[0] and let it fail in the DNS lookup instead.
             */

            hostEndpoint = new HostEndPoint(entries[0], port);
            _logger.InfoFormat("Using custom daemon address: {0}:{1}", hostEndpoint.Host, hostEndpoint.Port);
            return(true);
        }
Exemple #2
0
 /// <summary>
 /// Create an EndPoint representing a HostEndPoint.
 /// </summary>
 /// <param name="hostEndPoint">the host endpoint to represent.</param>
 /// <returns></returns>
 public static EndPoint Of(HostEndPoint hostEndPoint)
 {
     return(new EndPoint {
         _isHost = true, _h = hostEndPoint
     });
 }