Esempio n. 1
0
 public ObjectCheck genNullableChecker(ObjectCheck oc)
 {
     return((RealStatePtr L, int idx) =>
     {
         return LuaAPI.lua_isnil(L, idx) || oc(L, idx);
     });
 }
Esempio n. 2
0
        /// <summary>
        /// 生成有效的密钥
        /// </summary>
        /// <param name="key">密钥</param>
        /// <param name="minLength">密钥最小长度(字节数),默认16字节</param>
        /// <param name="maxLength">密钥最大长度(字节数),默认32字节</param>
        /// <returns></returns>
        public static byte[] GenerateValidKey(string key, int minLength = 16, int maxLength = 32)
        {
            minLength = minLength < 16 ? 16 : minLength;
            maxLength = maxLength < minLength ? minLength : maxLength;

            byte[] keys;
            if (ObjectCheck.IsBase64(key))
            {
                keys = Convert.FromBase64String(key);
            }
            else
            {
                keys = Encoding.UTF8.GetBytes(key);
            }

            int len = minLength;

            if (keys.Length > maxLength) // 如果密钥超过maxLength长度则截取
            {
                byte[] temp = new byte[maxLength];
                Array.Copy(keys, 0, temp, 0, maxLength);
                keys = temp;
            }
            else if ((keys.Length % len) != 0) // 如果密钥不足minLength长度就补足
            {
                int    groups = (keys.Length / len) + ((keys.Length % len) != 0 ? 1 : 0);
                byte[] temp   = new byte[groups * len];
                BitConverter.ToInt32(temp, 0); // 补足
                Array.Copy(keys, 0, temp, 0, keys.Length);
                keys = temp;
            }

            return(keys);
        }
Esempio n. 3
0
        private RedisOption _option; // 选项配置

        #endregion

        #region 构造与析构

        /// <summary>
        /// 实例化
        /// </summary>
        /// <param name="option">配置</param>
        public RedisClient(RedisOption option)
        {
            _option = new RedisOption()
            {
                Server         = ObjectCheck.IsNullOrEmpty(option.Server) ? "127.0.0.1" : option.Server,
                Port           = option.Port <= 0 ? 6379 : option.Port,
                Password       = option.Password,
                Db             = option.Db < 0 ? 0 : option.Db,
                ConnectTimeout = option.ConnectTimeout,
                SendTimeout    = option.SendTimeout,
                ReceiveTimeout = option.ReceiveTimeout,
                RetryCount     = option.RetryCount
            };
        }
Esempio n. 4
0
        private ObjectCheck genChecker(Type type)
        {
            ObjectCheck fixTypeCheck = (RealStatePtr L, int idx) =>
            {
                if (LuaAPI.lua_type(L, idx) == LuaTypes.LUA_TUSERDATA)
                {
                    object obj = translator.SafeGetCSObj(L, idx);
                    if (obj != null)
                    {
                        return(type.IsAssignableFrom(obj.GetType()));
                    }
                    else
                    {
                        Type type_of_obj = translator.GetTypeOf(L, idx);
                        if (type_of_obj != null)
                        {
                            return(type.IsAssignableFrom(type_of_obj));
                        }
                    }
                }
                return(false);
            };

            if (!type.IsAbstract && typeof(Delegate).IsAssignableFrom(type))
            {
                return((RealStatePtr L, int idx) =>
                {
                    return LuaAPI.lua_isnil(L, idx) || LuaAPI.lua_isfunction(L, idx) || fixTypeCheck(L, idx);
                });
            }
            else if (type.IsEnum)
            {
                return(fixTypeCheck);
            }
            else if (type.IsInterface)
            {
                return((RealStatePtr L, int idx) =>
                {
                    return LuaAPI.lua_isnil(L, idx) || LuaAPI.lua_istable(L, idx) || fixTypeCheck(L, idx);
                });
            }
            else
            {
                if ((type.IsClass && type.GetConstructor(System.Type.EmptyTypes) != null)) //class has default construtor
                {
                    return((RealStatePtr L, int idx) =>
                    {
                        return LuaAPI.lua_isnil(L, idx) || LuaAPI.lua_istable(L, idx) || fixTypeCheck(L, idx);
                    });
                }
                else if (type.IsValueType)
                {
                    return((RealStatePtr L, int idx) =>
                    {
                        return LuaAPI.lua_istable(L, idx) || fixTypeCheck(L, idx);
                    });
                }
                else if (type.IsArray)
                {
                    return((RealStatePtr L, int idx) =>
                    {
                        return LuaAPI.lua_isnil(L, idx) || LuaAPI.lua_istable(L, idx) || fixTypeCheck(L, idx);
                    });
                }
                else
                {
                    return((RealStatePtr L, int idx) =>
                    {
                        return LuaAPI.lua_isnil(L, idx) || fixTypeCheck(L, idx);
                    });
                }
            }
        }
Esempio n. 5
0
        //Загрузить
        private void button2_Click(object sender, EventArgs e)
        {
            FileStream fs = new FileStream(textPath.Text, FileMode.Open, FileAccess.Read);

            byte[] xmlParameter = new byte[fs.Length];
            fs.Read(xmlParameter, 0, xmlParameter.Length);
            fs.Close();

            byte[] rez;
            try
            {
                // Обработка и сохранение полученного массива xmlInfo
                rez = UnpackXML(xmlParameter);
            }
            catch
            {
                MessageBox.Show("Файл повреждён.");
                return;
            }

            ArrayList     masArray = new ArrayList();
            MemoryStream  ms       = new MemoryStream(rez);
            XmlTextReader reader   = new XmlTextReader(ms);
            ColorCheck    whoF     = ColorCheck.disable;

            ObjectCheck[,] saveMas = new ObjectCheck[8, 8];

            try
            {
                while (!reader.EOF)
                {
                    reader.Read();
                    if (reader.NodeType == XmlNodeType.Element && reader.Name.ToLower() == "whofirst")
                    {
                        reader.Read();
                        if (reader.Value == "black")
                        {
                            whoF = ColorCheck.black;
                        }
                        else
                        {
                            whoF = ColorCheck.white;
                        }
                        reader.Read();
                    }

                    if (reader.NodeType == XmlNodeType.Element && reader.Name.ToLower() == "board")
                    {
                        int w = 0, q = 0;
                        reader.Read();
                        string str;
                        while (!(reader.NodeType == XmlNodeType.EndElement && reader.Name.ToLower() == "board"))
                        {
                            if (reader.NodeType == XmlNodeType.Element)
                            {
                                reader.Read();
                                str = reader.Value;
                                if (str == "check_black")
                                {
                                    saveMas[w, q] = ObjectCheck.check_black;
                                }
                                else if (str == "check_black_dam")
                                {
                                    saveMas[w, q] = ObjectCheck.check_black_dam;
                                }
                                else if (str == "check_white")
                                {
                                    saveMas[w, q] = ObjectCheck.check_white;
                                }
                                else if (str == "check_white_dam")
                                {
                                    saveMas[w, q] = ObjectCheck.check_white_dam;
                                }
                                else if (str == "empty")
                                {
                                    saveMas[w, q] = ObjectCheck.empty;
                                }
                                else
                                {
                                    saveMas[w, q] = ObjectCheck.full;
                                }

                                q++;
                                if (q > 7)
                                {
                                    w++;
                                    q = 0;
                                }
                            }
                            reader.Read();
                        }
                    }

                    if (reader.NodeType == XmlNodeType.Element && reader.Name.ToLower() == "parameter")
                    {
                        reader.Read();
                        string[] str = new string[6];
                        while (!(reader.NodeType == XmlNodeType.EndElement && reader.Name.ToLower() == "parameter"))
                        {
                            if (reader.NodeType == XmlNodeType.Element)
                            {
                                string type = reader.Name;
                                reader.Read();
                                switch (type.ToLower())
                                {
                                case "par0":
                                    str[0] = reader.Value;
                                    reader.Read();
                                    break;

                                case "par1":
                                    str[1] = reader.Value;
                                    reader.Read();
                                    break;

                                case "par2":
                                    if (reader.Value != " ")
                                    {
                                        str[2] = reader.Value;
                                    }
                                    else
                                    {
                                        str[2] = "";
                                    }
                                    reader.Read();
                                    break;

                                case "par3":
                                    str[3] = reader.Value;
                                    reader.Read();
                                    break;

                                case "par4":
                                    str[4] = reader.Value;
                                    reader.Read();
                                    break;

                                case "par5":
                                    if (reader.Value != " ")
                                    {
                                        str[5] = reader.Value;
                                    }
                                    else
                                    {
                                        str[5] = "";
                                    }
                                    reader.Read();
                                    break;
                                }
                            }
                            reader.Read();
                        }
                        masArray.Add(str);
                    }
                }
            }
            catch
            {
            }
            finally
            {
                if (reader != null)
                {
                    reader.Close();
                }
            }

            mf.whoFirst   = whoF;
            mf.masSave    = saveMas;
            mf.historyMas = masArray;
            this.Close();
        }
Esempio n. 6
0
        /// <summary>
        /// 执行命令
        /// </summary>
        /// <param name="cmd"></param>
        /// <returns></returns>
        private IList <object> Execute(RedisCommand cmd)
        {
            var i = 0;

            do
            {
                var conn = Cache.Get(); // 从缓存中取出连接
                try
                {
                    // 初始化连接
                    conn.Initialize(_option);

                    // 连接到数据库
                    if (!IsLogined)
                    {
                        // 关闭连接
                        conn.Close();

                        // 验证密码
                        if (!ObjectCheck.IsNullOrEmpty(_option.Password))
                        {
                            var val = conn.Execute(new RedisCommand(Commands.Auth, _option.Password));
                        }

                        // 选择数据库
                        if (_option.Db > 0)
                        {
                            var val = conn.Execute(new RedisCommand(Commands.Select, _option.Db));
                        }

                        // 登录成功
                        IsLogined = true;
                        LoginTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture);
                    }

                    // 执行命令
                    return(conn.Execute(cmd));
                }
                catch (RedisServerException e)
                {
                    // 如果由RedisServerException引发异常,说明成功连接到了服务器
                    if (e.Message.IndexOf("AUTH", StringComparison.InvariantCulture) < 0)
                    {
                        // 如果服务器的应答不是登录验证问题,说明客户端操作出现了问题,此时不用再重试
                        throw new RedisServerException(e.Message);
                    }
                    else
                    {
                        // 如果服务器的应答是登录验证问题,则此时必须变更登录状态,重新进行登录验证
                        IsLogined = false;
                        LoginTime = "";
                        if (i++ >= _option.RetryCount) // 超过重试次数
                        {
                            throw new RedisClientException(e, MethodBase.GetCurrentMethod());
                        }
                    }
                }
                catch (RedisClientException e)
                {
                    // 如果网络存已经关闭,则必须重新登录
                    if (conn != null && !conn.IsConnected()) // 连接已断开必须重新登录
                    {
                        IsLogined = false;
                        LoginTime = "";
                    }

                    // 如果服务器的应答要求登录验证,则必须重新登录
                    if (e.Message.IndexOf("NOAUTH", StringComparison.InvariantCulture) >= 0)
                    {
                        IsLogined = false;
                        LoginTime = "";
                    }

                    // 超过重试次数
                    if (i++ >= _option.RetryCount)
                    {
                        throw new RedisClientException(e, MethodBase.GetCurrentMethod());
                    }
                }
                catch (Exception e)
                {
                    // 如果由系统引发异常,不能识别
                    throw new RedisClientException(e, MethodBase.GetCurrentMethod());
                }
                finally
                {
                    Cache.Put(conn); // 回收连接
                }
            } while (true);
        }
Esempio n. 7
0
 public void AddChecker(Type type, ObjectCheck oc)
 {
     checkersMap[type] = oc;
 }