GetInterfaceIPStack() public static method

Derives the desired IPStack from the "interface" setting in the connection string key/value pairs.
The "interface" setting will be added to the connectionStringEntries if it doesn't exist, in this case return value will be IPStack.Default.
public static GetInterfaceIPStack ( string>.Dictionary connectionStringEntries ) : IPStack
connectionStringEntries string>.Dictionary Connection string key/value pairs.
return IPStack
Esempio n. 1
0
        /// <summary>
        /// Validates the specified <paramref name="configurationString"/>.
        /// </summary>
        /// <param name="configurationString">Configuration string to be validated.</param>
        /// <exception cref="ArgumentException">Port property is missing.</exception>
        /// <exception cref="ArgumentOutOfRangeException">Port property value is not between <see cref="Transport.PortRangeLow"/> and <see cref="Transport.PortRangeHigh"/>.</exception>
        protected override void ValidateConfigurationString(string configurationString)
        {
            m_configData = configurationString.ParseKeyValuePairs();

            // Check for "server" property, this is the preferred configuration for ZeroMQServer
            if (m_configData.ContainsKey("server"))
            {
                // Validate ZeroMQ configuration string
                Match  endpointMatch            = Regex.Match(m_configData["server"], EndpointFormatRegex);
                string port                     = null;
                bool   validConfigurationString = false;

                if (endpointMatch.Success)
                {
                    if (!Enum.TryParse(endpointMatch.Groups["protocol"].Value.Trim(), true, out m_zeroMQTransportProtocol))
                    {
                        m_zeroMQTransportProtocol = ZeroMQTransportProtocol.Tcp;
                    }

                    m_configData["interface"] = endpointMatch.Groups["host"].Value.Trim();
                    port = endpointMatch.Groups["port"].Value.Trim();
                    validConfigurationString = true;
                }
                else
                {
                    // Support traditional IClient "server" property
                    endpointMatch = Regex.Match(m_configData["server"], Transport.EndpointFormatRegex);

                    if (endpointMatch.Success)
                    {
                        m_configData["server"]    = $"{m_zeroMQTransportProtocol.ToString().ToLowerInvariant()}://{m_configData["server"]}";
                        m_configData["interface"] = endpointMatch.Groups["host"].Value.Trim();
                        port = endpointMatch.Groups["port"].Value.Trim();
                        validConfigurationString = true;
                    }
                }

                if (!validConfigurationString)
                {
                    throw new FormatException($"Server property is invalid (Example: {DefaultConfigurationString})");
                }

                if (m_zeroMQTransportProtocol != ZeroMQTransportProtocol.InProc && !Transport.IsPortNumberValid(port))
                {
                    throw new ArgumentOutOfRangeException(nameof(configurationString), $"Port number must be between {Transport.PortRangeLow} and {Transport.PortRangeHigh}");
                }
            }
            else
            {
                // Fall back on traditional server configuration strings
                Transport.GetInterfaceIPStack(m_configData);

                if (string.IsNullOrWhiteSpace(m_configData["interface"]) || m_configData["interface"].Equals("0.0.0.0", StringComparison.Ordinal))
                {
                    m_configData["interface"] = "*";
                }

                // For traditional style connection strings, also support a "zeroMQTransportProtocol" setting
                if (m_configData.ContainsKey("zeroMQTransportProtocol"))
                {
                    ZeroMQTransportProtocol protocol;

                    if (Enum.TryParse(m_configData["zeroMQTransportProtocol"].Trim(), true, out protocol))
                    {
                        m_zeroMQTransportProtocol = protocol;
                    }
                }

                // For traditional IServer connection strings, a "port" property is expected
                if (m_configData.ContainsKey("port") && m_zeroMQTransportProtocol != ZeroMQTransportProtocol.InProc)
                {
                    m_configData["server"] = $"{m_zeroMQTransportProtocol.ToString().ToLowerInvariant()}://{m_configData["interface"]}:{m_configData["port"]}";
                }
                else
                {
                    throw new FormatException($"Server property is invalid (Example: {DefaultConfigurationString})");
                }
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Validates the specified <paramref name="connectionString"/>.
        /// </summary>
        /// <param name="connectionString">Connection string to be validated.</param>
        /// <exception cref="ArgumentException">Server property is missing.</exception>
        /// <exception cref="FormatException">Server property is invalid.</exception>
        /// <exception cref="ArgumentOutOfRangeException">Server port value is not between <see cref="Transport.PortRangeLow"/> and <see cref="Transport.PortRangeHigh"/>.</exception>
        protected override void ValidateConnectionString(string connectionString)
        {
            m_connectData = connectionString.ParseKeyValuePairs();

            // Make sure "interface" setting exists
            Transport.GetInterfaceIPStack(m_connectData);

            // Check if 'server' property is missing.
            if (!m_connectData.ContainsKey("server"))
            {
                throw new ArgumentException($"Server property is missing (Example: {DefaultConnectionString})");
            }

            // Backwards compatibility adjustments.
            // New Format: Server=localhost:8888
            // Old Format: Server=localhost; Port=8888
            if (m_connectData.ContainsKey("port"))
            {
                m_connectData["server"] = $"{m_connectData["server"]}:{m_connectData["port"]}";
            }

            // For traditional style connection strings, also support a "zeroMQTransportProtocol" setting
            if (m_connectData.ContainsKey("zeroMQTransportProtocol"))
            {
                ZeroMQTransportProtocol protocol;

                if (Enum.TryParse(m_connectData["zeroMQTransportProtocol"].Trim(), true, out protocol))
                {
                    m_zeroMQTransportProtocol = protocol;
                }
            }

            // Validate ZeroMQ connection string
            Match  endpointMatch         = Regex.Match(m_connectData["server"], ZeroMQServer.EndpointFormatRegex);
            string port                  = null;
            bool   validConnectionString = false;

            if (endpointMatch.Success)
            {
                if (!Enum.TryParse(endpointMatch.Groups["protocol"].Value.Trim(), true, out m_zeroMQTransportProtocol))
                {
                    m_zeroMQTransportProtocol = ZeroMQTransportProtocol.Tcp;
                }

                port = endpointMatch.Groups["port"].Value.Trim();
                validConnectionString = true;

                if (!string.IsNullOrWhiteSpace(m_connectData["interface"]) && (m_zeroMQTransportProtocol == ZeroMQTransportProtocol.Pgm || m_zeroMQTransportProtocol == ZeroMQTransportProtocol.Epgm))
                {
                    m_connectData["server"] = $"{m_zeroMQTransportProtocol.ToString().ToLowerInvariant()}://{m_connectData["interface"]};{endpointMatch.Groups["host"].Value.Trim()}:{port}";
                }
            }
            else
            {
                // Support traditional IClient "server" property
                endpointMatch = Regex.Match(m_connectData["server"], Transport.EndpointFormatRegex);

                if (endpointMatch.Success)
                {
                    m_connectData["server"] = $"{m_zeroMQTransportProtocol.ToString().ToLowerInvariant()}://{m_connectData["server"]}";
                    port = endpointMatch.Groups["port"].Value.Trim();
                    validConnectionString = true;

                    if (!string.IsNullOrWhiteSpace(m_connectData["interface"]) && (m_zeroMQTransportProtocol == ZeroMQTransportProtocol.Pgm || m_zeroMQTransportProtocol == ZeroMQTransportProtocol.Epgm))
                    {
                        m_connectData["server"] = $"{m_zeroMQTransportProtocol.ToString().ToLowerInvariant()}://{m_connectData["interface"]};{endpointMatch.Groups["host"].Value.Trim()}:{port}";
                    }
                }
            }

            if (!validConnectionString)
            {
                throw new FormatException($"Server property is invalid (Example: {DefaultConnectionString})");
            }

            if (m_zeroMQTransportProtocol != ZeroMQTransportProtocol.InProc && !Transport.IsPortNumberValid(port))
            {
                throw new ArgumentOutOfRangeException(nameof(connectionString), $"Server port must between {Transport.PortRangeLow} and {Transport.PortRangeHigh}");
            }
        }