Example #1
0
        private static bool tryCreatePorts(string value, out int[] result, out string parseError)
        {
            var ports = value.Trim('"').Split(',');
            var len   = ports.Length;
            var res   = new int[len];

            for (var i = 0; i < len; i++)
            {
                res[i] = Int32.MinValue;

                var port = ports[i].Trim();
                if (port.Length == 0)
                {
                    continue;
                }

#if SSHARP
                if (!TryParsers.Int32TryParse(port, out res[i]))
#else
                if (!Int32.TryParse(port, out res[i]))
#endif
                {
                    result     = new int[0];
                    parseError = port;

                    return(false);
                }
            }

            result     = res;
            parseError = String.Empty;

            return(true);
        }
Example #2
0
        internal FtpStatus GetResponseStatus()
        {
            while (true)
            {
                string response = null;

                try
                {
                    response = controlReader.ReadLine();
                }
                catch (IOException)
                {
                }

                if (response == null || response.Length < 3)
                {
                    return(ServiceNotAvailable());
                }

                int code;
#if SSHARP
                if (!TryParsers.Int32TryParse(response.Substring(0, 3), out code))
#else
                if (!Int32.TryParse(response.Substring(0, 3), out code))
#endif
                { return(ServiceNotAvailable()); }

                if (response.Length > 3 && response[3] == '-')
                {
                    string line = null;
                    string find = code.ToString() + ' ';
                    while (true)
                    {
                        line = null;
                        try
                        {
                            line = controlReader.ReadLine();
                        }
                        catch (IOException)
                        {
                        }
                        if (line == null)
                        {
                            return(ServiceNotAvailable());
                        }

                        response += Environment.NewLine + line;

                        if (line.StartsWith(find, StringComparison.Ordinal))
                        {
                            break;
                        }
                    }
                }
                return(new FtpStatus((FtpStatusCode)code, response));
            }
        }
Example #3
0
        public WebConnectionStream(WebConnection cnc, WebConnectionData data)
        {
            if (data == null)
            {
                throw new InvalidOperationException("data was not initialized");
            }
            if (data.Headers == null)
            {
                throw new InvalidOperationException("data.Headers was not initialized");
            }
            if (data.request == null)
            {
                throw new InvalidOperationException("data.request was not initialized");
            }
            isRead        = true;
            cb_wrapper    = new AsyncCallback(ReadCallbackWrapper);
            pending       = new ManualResetEvent(true);
            this.request  = data.request;
            read_timeout  = request.ReadWriteTimeout;
            write_timeout = read_timeout;
            this.cnc      = cnc;
            string contentType = data.Headers["Transfer-Encoding"];
            bool   chunkedRead = (contentType != null && contentType.IndexOf("chunked", StringComparison.OrdinalIgnoreCase) != -1);
            string clength     = data.Headers["Content-Length"];

            if (!chunkedRead && clength != null && clength != "")
            {
                try
                {
                    contentLength = Int32.Parse(clength);
                    if (contentLength == 0 && !IsNtlmAuth())
                    {
                        ReadAll();
                    }
                }
                catch
                {
                    contentLength = Int32.MaxValue;
                }
            }
            else
            {
                contentLength = Int32.MaxValue;
            }

            // Negative numbers?
#if SSHARP
            if (!TryParsers.Int32TryParse(clength, out stream_length))
#else
            if (!Int32.TryParse(clength, out stream_length))
#endif
            { stream_length = -1; }
        }
        protected override void OnValueChanged()
        {
            int i;

#if NETCF
            if (TryParsers.Int32TryParse(Value, out i))
#else
            if (int.TryParse(Value, out i))
#endif
            { Enabled = i != 0; }
            else
            {
                Enabled = Convert.ToBoolean(Value);
            }
        }
Example #5
0
        // Probably we could do better having here a regex
        private Socket SetupPassiveConnection(string statusDescription)
        {
            // Current response string
            string response = statusDescription;

            if (response.Length < 4)
            {
                throw new WebException("Cannot open passive data connection");
            }

            // Look for first digit after code
            int i;

            for (i = 3; i < response.Length && !Char.IsDigit(response[i]); i++)
            {
                ;
            }
            if (i >= response.Length)
            {
                throw new WebException("Cannot open passive data connection");
            }

            // Get six elements
            string[] digits = response.Substring(i).Split(new char[] { ',' }, 6);
            if (digits.Length != 6)
            {
                throw new WebException("Cannot open passive data connection");
            }

            // Clean non-digits at the end of last element
            int j;

            for (j = digits[5].Length - 1; j >= 0 && !Char.IsDigit(digits[5][j]); j--)
            {
                ;
            }
            if (j < 0)
            {
                throw new WebException("Cannot open passive data connection");
            }

            digits[5] = digits[5].Substring(0, j + 1);

            IPAddress ip;

            try
            {
                ip = IPAddress.Parse(String.Join(".", digits, 0, 4));
            }
            catch (FormatException)
            {
                throw new WebException("Cannot open passive data connection");
            }

            // Get the port
            int p1, p2, port;

#if SSHARP
            if (!TryParsers.Int32TryParse(digits[4], out p1) || !TryParsers.Int32TryParse(digits[5], out p2))
#else
            if (!Int32.TryParse(digits[4], out p1) || !Int32.TryParse(digits[5], out p2))
#endif
            { throw new WebException("Cannot open passive data connection"); }

            port = (p1 << 8) + p2;             // p1 * 256 + p2
            //port = p1 * 256 + p2;
            if (port < IPEndPoint.MinPort || port > IPEndPoint.MaxPort)
            {
                throw new WebException("Cannot open passive data connection");
            }

            IPEndPoint ep = new IPEndPoint(ip, port);
#if SSHARP
            CrestronClientSocket sock = new CrestronClientSocket();
#else
            Socket sock = new Socket(ep.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
#endif
            try
            {
                sock.Connect(ep);
            }
            catch (SocketException)
            {
                sock.Close();
                throw new WebException("Cannot open passive data connection");
            }

            return(sock);
        }