예제 #1
0
        /// <summary>
        /// 获取数据
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public valueType Get(keyType key)
        {
            if (isLog)
            {
                RandomKey <keyType> randomKey = key;
                KeyValue <valueType, EventWaitHandle> value;
TRYGET:
                Monitor.Enter(logLock);
                if (queue.TryGetValue(ref randomKey, out value))
                {
                    Monitor.Exit(logLock);
                    if (value.Value == null)
                    {
                        return(value.Key);
                    }
                    value.Value.WaitOne();
                    goto TRYGET;
                }
                try
                {
                    value.Value = new EventWaitHandle(false, EventResetMode.ManualReset);
                    queue.Set(ref randomKey, value);
                    if (queue.Count > maxCount)
                    {
                        queue.UnsafePopNode();
                    }
                }
                finally { Monitor.Exit(logLock); }
                try
                {
                    value.Key = getValue(key);
                }
                finally
                {
                    KeyValue <valueType, EventWaitHandle> cacheValue;
                    Monitor.Enter(logLock);
                    if (queue.TryGetOnly(key, out cacheValue) && cacheValue.Value == value.Value)
                    {
                        try
                        {
                            queue.SetOnly(key, new KeyValue <valueType, EventWaitHandle>(value.Key, null));
                        }
                        finally { Monitor.Exit(logLock); }
                    }
                    else
                    {
                        Monitor.Exit(logLock);
                    }
                    value.Value.Set();
                }
                return(value.Key);
            }
            return(getValue(key));
        }
예제 #2
0
        public override string ToString()
        {
            StringBuilder sb = new StringBuilder();

            sb.Append(RegisterKey);                               //x
            RandomKey = RandomKey.Substring(0, 5) + Timestamp5;
            sb.Append(RandomKey);                                 //10
            sb.Append(((int)TimeLimit).ToString());               //1
            sb.Append(string.Format("{0:D20}", RegistTimeTicks)); //20
            return(sb.ToString());
        }
예제 #3
0
        public ISignal CreateSignal(object value, int expireMinutes)
        {
            var signal = new RedisSignal()
            {
                TenantType = _tentantType,
                TenantId   = _tentantId,
                UId        = RandomKey.GetKey(),
                Value      = value
            };

            signal.Store(expireMinutes);
            return(signal);
        }
예제 #4
0
        /// <summary>
        /// 删除数据
        /// </summary>
        /// <param name="value">被删除的数据</param>
        private void onDeleted(valueType value)
        {
            valueType           cacheValue;
            RandomKey <keyType> key = GetKey(value);

            if (dictionary.TryGetValue(key, out cacheValue))
            {
                dictionary.Remove(key);
                callOnDeleted(cacheValue);
            }
            else
            {
                SqlTable.Log.Add(AutoCSer.Log.LogType.Fatal, typeof(valueType).FullName + " 缓存同步错误");
            }
        }
예제 #5
0
        public IActionResult NewGame(NewGameViewModel model)
        {
            if (ModelState.IsValid)
            {
                using (var db = new BlocXChangeDBContext())
                {
                    if (db.Games.FirstOrDefault(u => u.GameID == model.GameID) != null) // ID 중복 확인
                    {
                        ModelState.AddModelError("", "다른 ID를 사용해주세요");
                        return(View(model));
                    }

                    // ID 미 중복시
                    string newGameKey = RandomKey.GetRandomKey(6);

                    while (db.Games.FirstOrDefault(u => u.GameKey == newGameKey) != null)
                    {
                        newGameKey = RandomKey.GetRandomKey(6);
                    }

                    Game newGame = new Game
                    {
                        GameID         = model.GameID,
                        GamePassword   = model.GamePassword,
                        GameKey        = newGameKey,
                        InitialTime    = DateTime.Now,
                        SuspendedTicks = 0
                    };
                    db.Games.Add(newGame);

                    var GenData = new GenerateGameData(newGame);
                    foreach (var i in GenData.GetStocks()) // 주가 데이터 생성
                    {
                        db.Stocks.Add(i);
                    }
                    foreach (var i in GenData.GetFluctuations()) // 주가 변동 데이터 생성
                    {
                        db.Fluctuations.Add(i);
                    }

                    db.SaveChanges();

                    HttpContext.Session.SetString("GAME_MASTER_KEY", newGameKey);
                    return(Redirect("Play/?GameKey=" + newGameKey));
                }
            }
            return(View(model));
        }
예제 #6
0
        private void btnGenerate_Click(object sender, EventArgs e)
        {
            // get a 16 character random string that is base32 for secret
            // then decode the base32 to the real byte array secret key
            _encodedKey = RandomKey.GetRandomEncodedKey();
            _secretKey  = Base32.Decode(_encodedKey);

            // display
            lblSecretKey.Text = string.Format("{0} {1} {2} {3}",
                                              _encodedKey.Substring(0, 4), _encodedKey.Substring(4, 4),
                                              _encodedKey.Substring(8, 4), _encodedKey.Substring(12, 4));

            // get qrcode
            IBarcodeWriter barcodeWriter = new BarcodeWriter
            {
                Format  = BarcodeFormat.QR_CODE,
                Options = new QrCodeEncodingOptions
                {
                    Width  = 250,
                    Height = 250,
                    Margin = 1
                }
            };

            pbQRCode.Image = new Bitmap(barcodeWriter.Write(KeyUri.GetToptUri("TwoFactorNet", "TestUser", _encodedKey)));

            // generate code
            var totp = new Totp(_secretKey, 6, 30);

            lblCurrentVerificationCode.Text = totp.GeneratePassword(UnixTime.GetUnixTime());

            // get next cycle time
            _nextCycleTime = TimeHelper.GetNextCycleTimeUtc();

            // move progressbar to correct value
            pbCurrentVerificationCode.Value = (_nextCycleTime.Second == 0 ? 60 - DateTime.Now.Second : DateTime.Now.Second);

            // setup timer to display
            timer.Enabled = true;
        }
        /// <summary>
        /// 获取分页数据
        /// </summary>
        /// <param name="page">分页号,从 1 开始</param>
        /// <returns>分页数据</returns>
        public valueType[] Get(int page = 1)
        {
            if (page <= 0)
            {
                page = 1;
            }
            if (page <= ArrayCache.Length)
            {
                valueType[] array = ArrayCache[page - 1].Get(dictionary.Version);
                if (array == null)
                {
                    ArrayCache[page - 1].Set(array = dictionary.GetPage(pageSize, page), dictionary.Version);
                }
                return(array);
            }
            RandomKey <int> pageKey = page;
            PageCacheVersion <valueType> cache;

            if (Queue.TryGetValue(ref pageKey, out cache))
            {
                valueType[] array = cache.Get(dictionary.Version);
                if (array != null)
                {
                    return(array);
                }
                cache.Set(dictionary.GetPage(pageSize, page), dictionary.Version);
                Queue.Set(ref pageKey, cache);
            }
            else
            {
                cache.Set(dictionary.GetPage(pageSize, page), dictionary.Version);
                Queue.UnsafeAdd(ref pageKey, cache);
                if (Queue.Count > fifoPageCount)
                {
                    Queue.UnsafePopNode();
                }
            }
            return(cache.Array);
        }
        /// <summary>
        /// 登录并且记录当前用户的登录标记
        /// </summary>
        /// <param name="userRedisModel">当前登录信息</param>
        /// <returns>登录标记</returns>
        public string SignIn(UserRedisModel userRedisModel)
        {
            var    signal   = internalConsumer.GetSignal(userRedisModel.UserName);
            string loginKey = RandomKey.GetKey();

            #region 释放已经登录信号,创建新的登录信号
            if (signal != null)
            {
                LoginChannel.ChannelInstance.Logout(userRedisModel);
                internalConsumer.ReleaseSignal(signal);
            }
            internalConsumer.ApplyFor(userRedisModel.UserName, loginKey);
            #endregion
            #region 客户端写入SignalRId
            var signalRId = new HttpCookie(CookieKey.SignalRId)
            {
                Name    = CookieKey.SignalRId,
                Expires = DateTime.Now.AddDays(1),
                Value   = loginKey
            };
            HttpContext.Current.Response.Cookies.Add(signalRId);
            #endregion
            return(loginKey);
        }
예제 #9
0
        /// <summary>
        /// 日志流数据
        /// </summary>
        /// <param name="data"></param>
        private void onLog(AutoCSer.Net.TcpServer.ReturnValue <Log <valueType, modelType> .Data> data)
        {
            if (isError == 0)
            {
                if (data.Type == Net.TcpServer.ReturnType.Success)
                {
                    try
                    {
                        valueType           value = data.Value.Value.Value;
                        RandomKey <keyType> key   = getKey(data.Value.Value.Value);
                        KeyValue <valueType, EventWaitHandle> queueValue;
                        switch (data.Value.Type)
                        {
                        case LogType.Insert:
                            Monitor.Enter(logLock);
                            if (queue.TryGetOnly(key, out queueValue))
                            {
                                queue.SetOnly(key, new KeyValue <valueType, EventWaitHandle>(value, null));
                                Monitor.Exit(logLock);
                            }
                            else
                            {
                                try
                                {
                                    queue.Set(ref key, new KeyValue <valueType, EventWaitHandle>(value, null));
                                    if (queue.Count > maxCount)
                                    {
                                        queue.UnsafePopNode();
                                    }
                                }
                                finally { Monitor.Exit(logLock); }
                            }
                            return;

                        case LogType.Update:
                            Monitor.Enter(logLock);
                            if (queue.TryGetOnly(key, out queueValue))
                            {
                                if (queueValue.Value == null)
                                {
                                    try
                                    {
                                        AutoCSer.MemberCopy.Copyer <modelType> .Copy(queueValue.Key, value, data.Value.Value.MemberMap);
                                    }
                                    finally { Monitor.Exit(logLock); }
                                    if (data.Value.Value.MemberMap == null && !isErrorMemberMap)
                                    {
                                        isErrorMemberMap = true;
                                        log.add(AutoCSer.Log.LogType.Warn, "客户端缓存数据缺少成员位图信息 " + typeof(valueType).fullName());
                                    }
                                    return;
                                }
                                queue.Remove(ref key, out queueValue);
                            }
                            Monitor.Exit(logLock);
                            MemberMapValueLinkPool <valueType> .PushNotNull(value);

                            return;

                        case LogType.Delete:
                            MemberMapValueLinkPool <valueType> .PushNotNull(value);

                            Monitor.Enter(logLock);
                            queue.Remove(ref key, out queueValue);
                            Monitor.Exit(logLock);
                            return;
                        }
                    }
                    catch (Exception error)
                    {
                        log.add(AutoCSer.Log.LogType.Error, error);
                    }
                }
                this.error();
            }
        }
예제 #10
0
 public IActionResult NewGame()
 {
     RandomKey.GetRandomKey(6);
     return(View());
 }