Пример #1
0
        public void NumericUtility_TryParseInt32_1()
        {
            int value;

            Assert.IsTrue(NumericUtility.TryParseInt32("123", out value));
            Assert.AreEqual(123, value);

            Assert.IsFalse(NumericUtility.TryParseInt32("hello", out value));
            Assert.IsFalse(NumericUtility.TryParseInt32(string.Empty, out value));
            Assert.IsFalse(NumericUtility.TryParseInt32(null, out value));
        }
Пример #2
0
        /// <summary>
        /// Get 32-bit integer value.
        /// </summary>
        public int GetInt32
        (
            int defaultValue
        )
        {
            string line = GetAnsiString();

            if (!NumericUtility.TryParseInt32(line, out int result))
            {
                result = defaultValue;
            }

            return(result);
        }
        /// <summary>
        /// Get 32-bit integer value
        /// from application configuration.
        /// </summary>
        public static int GetInt32
        (
            [NotNull] string key,
            int defaultValue = 0
        )
        {
            string setting = CM.AppSettings[key];

            if (!NumericUtility.TryParseInt32(setting, out int result))
            {
                result = defaultValue;
            }

            return(result);
        }
Пример #4
0
        /// <summary>
        /// Require 32-bit integer.
        /// </summary>
        public int RequireInt32()
        {
            string line = GetAnsiString();

            int result;

            if (!NumericUtility.TryParseInt32(line, out result))
            {
                Log.Error
                (
                    "ServerResponse::RequireInt32: "
                    + "bad format="
                    + line.ToVisibleString()
                );

                throw new IrbisNetworkException();
            }

            return(result);
        }
Пример #5
0
        /// <summary>
        /// Get 32-bit integer value
        /// from application configuration.
        /// </summary>
        public static int GetInt32
        (
            [NotNull] string key,
            int defaultValue
        )
        {
#if DROID || ANDROID || UAP
            return(defaultValue);
#else
            int    result;
            string s = CM.AppSettings[key];

            if (!NumericUtility.TryParseInt32(s, out result))
            {
                result = defaultValue;
            }

            return(result);
#endif
        }
Пример #6
0
        public static MenuSpecification Parse
        (
            [CanBeNull] string text
        )
        {
            MenuSpecification result = new MenuSpecification
            {
                Path = IrbisPath.MasterFile
            };

            if (!ReferenceEquals(text, null) && text.Length != 0)
            {
                TextNavigator navigator = new TextNavigator(text);
                result.FileName = navigator.ReadUntil('\\');
                if (navigator.PeekChar() == '\\')
                {
                    navigator.ReadChar();
                }
                if (!navigator.IsEOF)
                {
                    string db = navigator.ReadUntil('\\');
                    if (navigator.PeekChar() == '\\')
                    {
                        navigator.ReadChar();
                    }

                    result.Database = db;

                    if (!navigator.IsEOF)
                    {
                        string sortText = navigator.GetRemainingText();
                        NumericUtility.TryParseInt32(sortText, out int sortMode);
                        result.SortMode = sortMode;
                    }
                }
            }

            return(result);
        }
Пример #7
0
        /// <summary>
        /// Apply settings to the <see cref="IrbisConnection" />.
        /// </summary>
        public void ApplyToConnection
        (
            [NotNull] IrbisConnection connection
        )
        {
            Sure.NotNull(connection, nameof(connection));

            connection.Host        = _Select(Host, connection.Host);
            connection.Port        = _Select(Port, connection.Port);
            connection.Username    = _Select(Username, connection.Username);
            connection.Password    = _Select(Password, connection.Password);
            connection.Database    = _Select(Database, connection.Database);
            connection.Workstation = (IrbisWorkstation)_Select
                                     (
                (int)Workstation,
                (int)connection.Workstation
                                     );

            if (!string.IsNullOrEmpty(EngineTypeName))
            {
                connection.SetEngine(EngineTypeName);
            }

            if (!string.IsNullOrEmpty(SocketTypeName))
            {
                ClientSocketUtility.CreateSocket
                (
                    connection,
                    SocketTypeName
                );
            }

            if (!string.IsNullOrEmpty(NetworkLogging))
            {
                connection.SetNetworkLogging(NetworkLogging);
            }

            if (!string.IsNullOrEmpty(FactoryTypeName))
            {
                connection.SetCommandFactory(FactoryTypeName);
            }

            if (!string.IsNullOrEmpty(Smart))
            {
                SmartClientSocket smartSocket
                    = new SmartClientSocket(connection);
                connection.SetSocket(smartSocket);
            }

            if (!string.IsNullOrEmpty(Slow))
            {
                SlowSocket slowSocket = new SlowSocket
                                        (
                    connection,
                    connection.Socket
                                        );
                int delay;
                if (NumericUtility.TryParseInt32(Slow, out delay) &&
                    delay > 0)
                {
                    slowSocket.Delay = delay;
                }
                connection.SetSocket(slowSocket);
            }

            if (!string.IsNullOrEmpty(Broken))
            {
                BrokenSocket brokenSocket = new BrokenSocket
                                            (
                    connection,
                    connection.Socket
                                            );
                double probability;
                if (NumericUtility.TryParseDouble(Broken, out probability) &&
                    probability > 0.0 &&
                    probability < 1.0)
                {
                    brokenSocket.Probability = probability;
                }
                connection.SetSocket(brokenSocket);
            }

            if (RetryLimit != 0)
            {
                connection.SetRetry(RetryLimit, null);
            }

            if (!string.IsNullOrEmpty(UserData))
            {
                connection.UserData = UserData;
            }

            connection._connected = Connected;
        }
Пример #8
0
        private static byte[] _SmartRead
        (
            [NotNull] NetworkStream stream
        )
        {
            byte[] head = new byte[10 * 1024];
            byte[] body, result;

            int readed1 = stream.Read(head, 0, head.Length);

            if (readed1 == 0)
            {
                Log.Error
                (
                    "SmartClientSocket::_SmartRead: "
                    + "empty response"
                );

                throw new IrbisNetworkException("Empty response");
            }

            // Ожидаемый ответ сервера:
            //
            // Команда
            // Идентификатор клиента
            // Порядковый номер
            // Длина ответа
            // Прочие данные

            ByteNavigator navigator = new ByteNavigator(head);

            navigator.SkipLine();
            navigator.SkipLine();
            navigator.SkipLine();

            string text = navigator.ReadLine();

            if (ReferenceEquals(text, null))
            {
                Log.Error
                (
                    "SmartClientSocket::_SmartRead: "
                    + "can't read first line of the response"
                );

                return(head);
            }

            int length;

            if (!NumericUtility.TryParseInt32(text, out length))
            {
                if (readed1 < head.Length)
                {
                    return(head.GetSpan(0, readed1));
                }
                body = stream.ReadToEnd();

                result = ArrayUtility.Merge(head, body);

                return(result);
            }

            int remaining = length + text.Length - readed1;

            if (remaining <= 0)
            {
                return(head.GetSpan(0, readed1));
            }

            body = new byte[remaining];
            int readed2 = stream.Read(body, 0, remaining);

            if (readed2 != remaining)
            {
                Log.Error
                (
                    "SmartClientSocket::SmartRead: "
                    + "expected="
                    + remaining
                    + ", readed="
                    + readed2
                );

                throw new IrbisNetworkException();
            }

            result = ArrayUtility.Merge(head, body);

            return(result);
        }