Exemplo n.º 1
0
        /// <summary>
        /// Creates a <see cref="ForwardSpec"/> from its <see cref="string"/> representation.
        /// </summary>
        /// <param name="spec">
        /// A <see cref="string"/> which represents a <see cref="ForwardSpec"/>.
        /// </param>
        /// <returns>
        /// A <see cref="ForwardSpec"/> which represents <paramref name="spec"/>.
        /// </returns>
        public static ForwardSpec Parse(string spec)
        {
            if (spec == null)
            {
                throw new ArgumentNullException(nameof(spec));
            }

            var parts = spec.Split(new char[] { ':' }, 2, StringSplitOptions.RemoveEmptyEntries);

            if (parts.Length != 2)
            {
                throw new ArgumentOutOfRangeException(nameof(spec));
            }

            if (!Mappings.ContainsKey(parts[0]))
            {
                throw new ArgumentOutOfRangeException(nameof(spec));
            }

            var protocol = Mappings[parts[0]];

            ForwardSpec value = new ForwardSpec()
            {
                Protocol = protocol,
            };

            int  intValue;
            bool isInt = int.TryParse(parts[1], out intValue);

            switch (protocol, isInt)
            {
Exemplo n.º 2
0
        /// <summary>
        /// Creates a new instance of the <seealso cref="ForwardData"/> class by parsing
        /// a <see cref="string"/>.
        /// </summary>
        /// <param name="value">
        /// The <see cref="string"/> value to parse.
        /// </param>
        /// <returns>
        /// A <see cref="ForwardData"/> object that represents the port forwarding information
        /// contained in <paramref name="value"/>.
        /// </returns>
        public static ForwardData FromString(string value)
        {
            if (value == null)
            {
                throw new ArgumentNullException(nameof(value));
            }

            string[] parts = value.Split(' ');

            if (parts.Length != 3)
            {
                throw new ArgumentOutOfRangeException(nameof(value), $"Cannot parse {value}");
            }

            var serial = parts[0].Trim();
            var local  = ForwardSpec.Parse(parts[1].Trim());
            var remote = ForwardSpec.Parse(parts[2].Trim());

            // swap if it is a reverse forward.
            if (serial.Contains("reverse"))
            {
                var r = local;
                local  = remote;
                remote = r;
            }

            return(new ForwardData()
            {
                SerialNumber = parts[0],
                LocalSpec = local,
                RemoteSpec = remote,
            });
        }