Example #1
0
        internal static int ConvertToPortNum(ConversionStorage <MutableString> /*!*/ stringCast, ConversionStorage <int> /*!*/ fixnumCast, object port)
        {
            // conversion protocol: if it's a Fixnum, return it
            // otherwise, convert to string & then convert the result to a Fixnum
            if (port is int)
            {
                return((int)port);
            }

            if (port == null)
            {
                return(0);
            }

            MutableString serviceName = Protocols.CastToString(stringCast, port);
            ServiceName   service     = SearchForService(serviceName);

            if (service != null)
            {
                return(service.Port);
            }

            int i = 0;

            if (Int32.TryParse(serviceName.ToString(), out i))
            {
                return(i);
            }

            throw SocketErrorOps.Create(MutableString.FormatMessage("Invalid port number or service name: `{0}'.", serviceName));
        }
Example #2
0
        public static int GetServiceByName(RubyClass /*!*/ self,
                                           [DefaultProtocol, NotNull] MutableString /*!*/ name,
                                           [DefaultProtocol, Optional] MutableString protocol)
        {
            if (protocol == null)
            {
                protocol = _DefaultProtocol;
            }

            ServiceName service = SearchForService(name, protocol);

            if (service != null)
            {
                return(service.Port);
            }

            // Cannot use: object port = Protocols.TryConvertToInteger(context, name);
            // Since the conversion process returns 0 if the string is not a valid number
            try {
                return(ParseInteger(self.Context, name.ConvertToString()));
            } catch (InvalidOperationException) {
                throw SocketErrorOps.Create(
                          String.Format("no such service {0} {1}", name.ConvertToString(), protocol.ConvertToString())
                          );
            }
        }