Exemplo n.º 1
0
        private IEnumerable <UsermodeRecord> LookupCachedUsermodes(UsermodeLookup lookup)
        {
            var             result = new List <UsermodeRecord>();
            var             db     = peerChatCacheDb.GetDatabase();
            IScanningCursor cursor = null;
            IEnumerable <SortedSetEntry> entries;

            do
            {
                entries = db.SortedSetScan("usermodes", cursor: cursor?.Cursor ?? 0);
                foreach (var entry in entries)
                {
                    int id = int.Parse(entry.Element.ToString());
                    if (id > 0)
                    {
                        continue;
                    }
                    var usermode = LookupTemporaryUsermode(id);
                    if (usermode != null && TestUsermodeMatches(lookup, usermode))
                    {
                        result.Add(usermode);
                    }
                    else if (usermode == null)
                    {
                        db.SortedSetRemove("usermodes", entry.Element);
                    }
                }

                cursor = (IScanningCursor)entries;
            } while ((cursor?.Cursor ?? 0) != 0);
            return(result);
        }
Exemplo n.º 2
0
        public async Task ResyncChannelUsermodes(string channelMask)
        {
            var                     db     = peerChatCacheDb.GetDatabase();
            IScanningCursor         cursor = null;
            IEnumerable <HashEntry> entries;

            do
            {
                entries = db.HashScan("channels", channelMask, 10, cursor?.Cursor ?? 0);
                foreach (var entry in entries)
                {
                    var channelId = int.Parse(entry.Value);
                    var userIds   = GetChannelUsers(channelId);
                    foreach (var userId in userIds)
                    {
                        var summary         = GetSummaryByUserid(userId);
                        var chanUserSummary = new PeerchatChannelUserSummary();
                        chanUserSummary.ChannelName = entry.Name;
                        chanUserSummary.UserSummary = summary;
                        await ApplyUsermode(chanUserSummary, null);
                    }
                }
                cursor = (IScanningCursor)entries;
            } while ((cursor?.Cursor ?? 0) != 0);
        }
Exemplo n.º 3
0
        private void SendBansUpdate(IEnumerable <UsermodeRecord> usermodes, bool add)
        {
            var                     db     = peerChatCacheDb.GetDatabase();
            IScanningCursor         cursor = null;
            IEnumerable <HashEntry> entries;

            foreach (var usermode in usermodes)
            {
                if (usermode.hostmask != null && usermode.hostmask.Length > 0 && (usermode.modeflags & (int)EUserChannelFlag.EUserChannelFlag_Banned) != 0)
                {
                    do
                    {
                        entries = db.HashScan("channels", usermode.channelmask, 10, cursor?.Cursor ?? 0);
                        foreach (var entry in entries)
                        {
                            ConnectionFactory factory = connectionFactory.Get();
                            using (IConnection connection = factory.CreateConnection())
                            {
                                using (IModel channel = connection.CreateModel())
                                {
                                    var modeString = (add ? "+" : "-") + "b *@" + usermode.hostmask;
                                    SendModeString(entry.Value, modeString);
                                }
                            }
                        }
                        cursor = (IScanningCursor)entries;
                    } while ((cursor?.Cursor ?? 0) != 0);
                }
            }
        }
Exemplo n.º 4
0
        private async Task ApplyUserrecords(UsermodeRecord model)
        {
            var                     db     = peerChatCacheDb.GetDatabase();
            IScanningCursor         cursor = null;
            IEnumerable <HashEntry> entries;

            do
            {
                entries = db.HashScan("channels", model.channelmask, 10, cursor?.Cursor ?? 0);
                foreach (var entry in entries)
                {
                    await ApplyUsermodesToChannel(entry.Name, model);
                }
                cursor = (IScanningCursor)entries;
            } while ((cursor?.Cursor ?? 0) != 0);
        }
Exemplo n.º 5
0
        public void ScansIScanning()
        {
            using (var conn = Create(allowAdmin: true))
            {
                var prefix = Me() + Guid.NewGuid();
                var dbId   = TestConfig.GetDedicatedDB(conn);
                var db     = conn.GetDatabase(dbId);
                var server = GetServer(conn);
                server.FlushDatabase(dbId);
                for (int i = 0; i < 100; i++)
                {
                    db.StringSet(prefix + i, Guid.NewGuid().ToString(), flags: CommandFlags.FireAndForget);
                }
                var seq = server.Keys(dbId, prefix + "*", pageSize: 15);
                using (var iter = seq.GetEnumerator())
                {
                    IScanningCursor s0 = (IScanningCursor)seq, s1 = (IScanningCursor)iter;

                    Assert.Equal(15, s0.PageSize);
                    Assert.Equal(15, s1.PageSize);

                    // start at zero
                    Assert.Equal(0, s0.Cursor);
                    Assert.Equal(s0.Cursor, s1.Cursor);

                    for (int i = 0; i < 47; i++)
                    {
                        Assert.True(iter.MoveNext());
                    }

                    // non-zero in the middle
                    Assert.NotEqual(0, s0.Cursor);
                    Assert.Equal(s0.Cursor, s1.Cursor);

                    for (int i = 0; i < 53; i++)
                    {
                        Assert.True(iter.MoveNext());
                    }

                    // zero "next" at the end
                    Assert.False(iter.MoveNext());
                    Assert.NotEqual(0, s0.Cursor);
                    Assert.NotEqual(0, s1.Cursor);
                }
            }
        }
        private void DeleteChannel(int channel_id)
        {
            var db             = peerChatCacheDb.GetDatabase();
            var channel_prefix = "channel_" + channel_id;
            var channel_name   = db.StringGet("channelname_" + channel_id);

            db.HashSet(channel_prefix, "modeflags", (int)EChannelBasicModes.EChannelMode_InviteOnly); //set invite flag, so no one can join
            ConnectionFactory factory = connectionFactory.Get();

            using (IConnection connection = factory.CreateConnection())
            {
                using (IModel channel = connection.CreateModel())
                {
                    IScanningCursor cursor = null;
                    IEnumerable <SortedSetEntry> entries;
                    do
                    {
                        entries = db.SortedSetScan(channel_prefix + "_users", cursor: cursor?.Cursor ?? 0);
                        foreach (var entry in entries)
                        {
                            int id = int.Parse(entry.Element.ToString());

                            string chanmodeflags_update = string.Format("\\type\\UPDATE_USER_CHANMODEFLAGS\\to\\{0}\\user_id\\{1}\\modeflags\\0", channel_id, id);
                            byte[] messageBodyBytes     = System.Text.Encoding.UTF8.GetBytes(chanmodeflags_update);

                            IBasicProperties props = channel.CreateBasicProperties();
                            props.ContentType = "text/plain";
                            channel.BasicPublish(PEERCHAT_EXCHANGE, PEERCHAT_KEYUPDATE_KEY, props, messageBodyBytes);


                            var kick_message = System.Text.Encoding.UTF8.GetBytes("Channel Reset");

                            string kick_event_message = string.Format("\\type\\KICK\\toChannelId\\{0}\\fromUserSummary\\{1}\\toUserId\\{2}\\message\\{3}", channel_id, "[email protected]", id, Convert.ToBase64String(kick_message));
                            messageBodyBytes = System.Text.Encoding.UTF8.GetBytes(kick_event_message);
                            channel.BasicPublish(PEERCHAT_EXCHANGE, PEERCAHT_CLIENT_MESSAGE_KEY, props, messageBodyBytes);
                        }

                        cursor = (IScanningCursor)entries;
                    } while ((cursor?.Cursor ?? 0) != 0);
                }
            }
            db.KeyDelete(channel_prefix);
            db.KeyDelete(channel_prefix + "_users");
            db.HashDelete("channels", channel_id.ToString());
        }
Exemplo n.º 7
0
        public void ScansIScanning()
        {
            using (var conn = Create(allowAdmin: true))
            {
                const int DB     = 7;
                var       db     = conn.GetDatabase(DB);
                var       server = GetServer(conn);
                server.FlushDatabase(DB);
                for (int i = 0; i < 100; i++)
                {
                    db.StringSet("ScansRepeatable:" + i, Guid.NewGuid().ToString(), flags: CommandFlags.FireAndForget);
                }
                var seq = server.Keys(DB, pageSize: 15);
                using (var iter = seq.GetEnumerator())
                {
                    IScanningCursor s0 = (IScanningCursor)seq, s1 = (IScanningCursor)iter;

                    Assert.AreEqual(15, s0.PageSize);
                    Assert.AreEqual(15, s1.PageSize);

                    // start at zero
                    Assert.AreEqual(0, s0.Cursor);
                    Assert.AreEqual(s0.Cursor, s1.Cursor);

                    for (int i = 0; i < 47; i++)
                    {
                        Assert.IsTrue(iter.MoveNext());
                    }

                    // non-zero in the middle
                    Assert.AreNotEqual(0, s0.Cursor);
                    Assert.AreEqual(s0.Cursor, s1.Cursor);

                    for (int i = 0; i < 53; i++)
                    {
                        Assert.IsTrue(iter.MoveNext());
                    }

                    // zero "next" at the end
                    Assert.IsFalse(iter.MoveNext());
                    Assert.AreNotEqual(0, s0.Cursor);
                    Assert.AreNotEqual(0, s1.Cursor);
                }
            }
        }
Exemplo n.º 8
0
        private async Task ApplyUsermodesToChannel(string channelName, UsermodeRecord model)
        {
            var db = peerChatCacheDb.GetDatabase();
            var id = db.StringGet("channel_" + channelName);
            //scan channel users
            var             channel_users_key = "channel_" + id + "_users";
            IScanningCursor cursor            = null;
            IEnumerable <SortedSetEntry> entries;

            do
            {
                entries = db.SortedSetScan(channel_users_key, cursor: cursor?.Cursor ?? 0);
                foreach (var entry in entries)
                {
                    var userId             = int.Parse(entry.Element);
                    var summary            = GetSummaryByUserid(userId);
                    var channelUserSummary = new PeerchatChannelUserSummary();
                    channelUserSummary.ChannelName = channelName;
                    channelUserSummary.UserSummary = summary;
                    var effectiveUsermode = await GetEffectiveUsermode(channelUserSummary);
                    await ApplyUsermode(channelUserSummary, effectiveUsermode);
                }
            } while ((cursor?.Cursor ?? 0) != 0);

            //
            if (!string.IsNullOrEmpty(model.hostmask) && model.hostmask.Length > 0 && (model.modeflags & (int)EUserChannelFlag.EUserChannelFlag_Banned) != 0)
            {
                ConnectionFactory factory = connectionFactory.Get();
                using (IConnection connection = factory.CreateConnection())
                {
                    using (IModel channel = connection.CreateModel())
                    {
                        var    modeString       = "+b *@" + model.hostmask;
                        var    modeStringBytes  = System.Text.Encoding.UTF8.GetBytes(modeString);
                        String message          = String.Format("\\type\\MODE\\toChannelId\\{0}\\message\\{1}\\fromUserId\\-1\\includeSelf\\1", id, Convert.ToBase64String(modeStringBytes));
                        byte[] messageBodyBytes = System.Text.Encoding.UTF8.GetBytes(message);

                        IBasicProperties props = channel.CreateBasicProperties();
                        props.ContentType = "text/plain";
                        channel.BasicPublish(PEERCHAT_EXCHANGE, PEERCAHT_CLIENT_MESSAGE_KEY, props, messageBodyBytes);
                    }
                }
            }
        }
Exemplo n.º 9
0
        private IEnumerable <int> GetChannelUsers(int channelId)
        {
            var             db        = peerChatCacheDb.GetDatabase();
            var             redis_key = "channel_" + channelId + "_users";
            IScanningCursor cursor    = null;
            IEnumerable <SortedSetEntry> entries;
            List <int> result = new List <int>();

            do
            {
                entries = db.SortedSetScan(redis_key, cursor: cursor?.Cursor ?? 0);
                foreach (var entry in entries)
                {
                    result.Add(int.Parse(entry.Element));
                }
                cursor = (IScanningCursor)entries;
            } while ((cursor?.Cursor ?? 0) != 0);
            return(result);
        }
Exemplo n.º 10
0
        private async Task ResyncChanPropsForChanMask(string channel_mask, bool kickExisting)
        {
            var db = peerChatCacheDb.GetDatabase();


            IScanningCursor         cursor = null;
            IEnumerable <HashEntry> entries;

            do
            {
                entries = db.HashScan("channels", channel_mask, cursor: cursor?.Cursor ?? 0);
                foreach (var entry in entries)
                {
                    var name = db.HashGet("channel_" + entry.Value, "name");
                    await ApplyEffectiveChanprops(name, kickExisting);
                }

                cursor = (IScanningCursor)entries;
            } while ((cursor?.Cursor ?? 0) != 0);
        }
        /// <summary>
        /// 检索指定db 的模式键搜索
        /// </summary>
        /// <param name="db"></param>
        /// <param name="pattern"></param>
        /// <param name="pageSize"></param>
        /// <param name="cursor"></param>
        /// <returns></returns>
        public ScanResult Scan(int db, string pattern, int pageSize = 10, long cursor = 0)
        {
            var allEndPoints = this.GetEndPoints();
            var ep           = allEndPoints[0];//使用默认第一个连接终结点

            var server = GetConnection().GetServer(ep);
            IEnumerable <RedisKey> keys = server.Keys(database: db, pattern: pattern, pageSize: pageSize, cursor: cursor);

            if (keys.IsEmpty())
            {
                return(null);
            }

            ScanResult result = new ScanResult {
                Results = keys.Select(x => x.ToString())
                          .ToList()
            };

            IScanningCursor _cursorObj = keys.GetEnumerator() as IScanningCursor;

            result.Cursor = _cursorObj.Cursor;
            return(result);
        }
Exemplo n.º 12
0
        public List <Models.HashEntry> HashScan(string setKey, string hashFieldPattern)
        {
            _logger.LogDebug("Getting {SetKey} entries matching {HashFieldPattern}", setKey, hashFieldPattern);
            var             entries   = new List <Models.HashEntry>();
            var             db        = _redis.GetDatabase();
            IScanningCursor cursor    = null;
            long            oldCursor = 0;

            while (cursor == null || cursor.Cursor > oldCursor)
            {
                if (cursor != null)
                {
                    oldCursor = cursor.Cursor;
                }
                var results = db.HashScan(setKey, hashFieldPattern, 100);
                foreach (var entry in results)
                {
                    entries.Add(new Models.HashEntry(setKey, entry.Name, entry.Value));
                }
                cursor = (IScanningCursor)results;
            }
            return(entries);
        }
Exemplo n.º 13
0
 private protected virtual Task <ScanResult> GetNextPageAsync(IScanningCursor obj, long cursor, out Message message)
 {
     activeCursor = obj;
     message      = CreateMessage(cursor);
     return(redis.ExecuteAsync(message, Processor, server));
 }
Exemplo n.º 14
0
 private protected override Task <ScanResult> GetNextPageAsync(IScanningCursor obj, long cursor, out Message message)
 {
     message = null;
     return(AwaitedGetNextPageAsync());
 }
Exemplo n.º 15
0
 protected ScanResult GetNextPageSync(IScanningCursor obj, long cursor)
 {
     activeCursor = obj;
     return(redis.ExecuteSync(CreateMessage(cursor), Processor, server));
 }
Exemplo n.º 16
0
 protected Task <ScanResult> GetNextPageAsync(IScanningCursor obj, long cursor)
 {
     this.activeCursor = obj;
     return(redis.ExecuteAsync(CreateMessage(cursor), Processor, server));
 }