Exemplo n.º 1
0
 public MinerSignSet(IReadOnlyMinerRedis redis)
 {
     redis.GetAllAsync().ContinueWith(t => {
         _initedOn = DateTime.Now;
         foreach (var item in t.Result)
         {
             Add(MinerSign.Create(item));
         }
         IsReadied = true;
         NTMinerConsole.UserOk("矿机签名集就绪");
         VirtualRoot.RaiseEvent(new MinerSignSetInitedEvent());
     });
     // 收到Mq消息之前一定已经初始化完成,因为Mq消费者在MinerSignSetInitedEvent事件之后才会创建
     VirtualRoot.BuildEventPath <MinerDataRemovedMqMessage>("收到MinerClientRemovedMq消息后移除内存中对应的记录", LogEnum.None, path: message => {
         #region
         if (message.AppId == ServerRoot.HostConfig.ThisServerAddress)
         {
             return;
         }
         if (string.IsNullOrEmpty(message.MinerId))
         {
             return;
         }
         if (IsOldMqMessage(message.Timestamp))
         {
             NTMinerConsole.UserOk(nameof(MinerDataRemovedMqMessage) + ":" + MqKeyword.SafeIgnoreMessage);
             return;
         }
         if (_dicByMinerId.TryGetValue(message.MinerId, out MinerSign minerSign))
         {
             if (AppRoot.MinerClientSessionSet.TryGetByClientId(minerSign.ClientId, out IMinerClientSession ntminerSession))
             {
                 ntminerSession.CloseAsync(WsCloseCode.Normal, "服务端移除了该矿机");
             }
             _dicByMinerId.Remove(message.MinerId);
             _dicByClientId.Remove(minerSign.ClientId);
         }
         #endregion
     }, this.GetType());
     VirtualRoot.BuildEventPath <MinerDataAddedMqMessage>("收到MinerDataAddedMq消息后更新内存中对应的记录", LogEnum.None, path: message => {
         #region
         if (message.AppId == ServerRoot.HostConfig.ThisServerAddress)
         {
             return;
         }
         if (string.IsNullOrEmpty(message.MinerId))
         {
             return;
         }
         if (IsOldMqMessage(message.Timestamp))
         {
             NTMinerConsole.UserOk(nameof(MinerDataAddedMqMessage) + ":" + MqKeyword.SafeIgnoreMessage);
             return;
         }
         redis.GetByIdAsync(message.MinerId).ContinueWith(t => {
             if (t.Result != null)
             {
                 if (_dicByMinerId.TryGetValue(message.MinerId, out MinerSign minerSign))
                 {
                     minerSign.Update(t.Result);
                 }
                 else
                 {
                     Add(MinerSign.Create(t.Result));
                 }
             }
         });
         #endregion
     }, this.GetType());
     VirtualRoot.BuildEventPath <MinerSignChangedMqMessage>("收到MinerSignChangedMq消息后更新内存中对应的记录", LogEnum.None, path: message => {
         #region
         if (message.AppId == ServerRoot.HostConfig.ThisServerAddress)
         {
             return;
         }
         if (string.IsNullOrEmpty(message.MinerId))
         {
             return;
         }
         if (IsOldMqMessage(message.Timestamp))
         {
             NTMinerConsole.UserOk(nameof(MinerSignChangedMqMessage) + ":" + MqKeyword.SafeIgnoreMessage);
             return;
         }
         redis.GetByIdAsync(message.MinerId).ContinueWith(t => {
             if (t.Result != null)
             {
                 if (_dicByMinerId.TryGetValue(message.MinerId, out MinerSign minerSign))
                 {
                     minerSign.Update(t.Result);
                 }
                 else
                 {
                     Add(MinerSign.Create(t.Result));
                 }
             }
         });
         #endregion
     }, this.GetType());
 }
Exemplo n.º 2
0
 public MinerSignSet(IReadOnlyMinerRedis redis)
 {
     redis.GetAllAsync().ContinueWith(t => {
         _initedOn = DateTime.Now;
         foreach (var item in t.Result)
         {
             Add(MinerSign.Create(item));
         }
         IsReadied = true;
         Write.UserOk("矿机签名集就绪");
         VirtualRoot.RaiseEvent(new MinerSignSetInitedEvent());
     });
     // 收到Mq消息之前一定已经初始化完成,因为Mq消费者在MinerSignSetInitedEvent事件之后才会创建
     VirtualRoot.AddEventPath <MinerDataRemovedMqMessage>("收到MinerClientRemovedMq消息后移除内存中对应的记录", LogEnum.None, action: message => {
         if (message.AppId == ServerRoot.HostConfig.ThisServerAddress)
         {
             return;
         }
         if (string.IsNullOrEmpty(message.MinerId))
         {
             return;
         }
         if (IsOldMqMessage(message.Timestamp))
         {
             Write.UserOk(_safeIgnoreMessage);
             return;
         }
         if (_dicByMinerId.TryGetValue(message.MinerId, out MinerSign minerSign))
         {
             _dicByMinerId.Remove(message.MinerId);
             _dicByClientId.Remove(minerSign.ClientId);
         }
     }, this.GetType());
     VirtualRoot.AddEventPath <MinerDataAddedMqMessage>("收到MinerDataAddedMq消息后更新内存中对应的记录", LogEnum.None, action: message => {
         if (message.AppId == ServerRoot.HostConfig.ThisServerAddress)
         {
             return;
         }
         if (string.IsNullOrEmpty(message.MinerId))
         {
             return;
         }
         if (IsOldMqMessage(message.Timestamp))
         {
             Write.UserOk(_safeIgnoreMessage);
             return;
         }
         redis.GetByIdAsync(message.MinerId).ContinueWith(t => {
             if (t.Result != null)
             {
                 if (_dicByMinerId.TryGetValue(message.MinerId, out MinerSign minerSign))
                 {
                     minerSign.Update(t.Result);
                 }
                 else
                 {
                     Add(MinerSign.Create(t.Result));
                 }
             }
         });
     }, this.GetType());
     VirtualRoot.AddEventPath <MinerSignChangedMqMessage>("收到MinerSignChangedMq消息后更新内存中对应的记录", LogEnum.None, action: message => {
         if (message.AppId == ServerRoot.HostConfig.ThisServerAddress)
         {
             return;
         }
         if (string.IsNullOrEmpty(message.MinerId))
         {
             return;
         }
         if (IsOldMqMessage(message.Timestamp))
         {
             Write.UserOk(_safeIgnoreMessage);
             return;
         }
         redis.GetByIdAsync(message.MinerId).ContinueWith(t => {
             if (t.Result != null)
             {
                 if (_dicByMinerId.TryGetValue(message.MinerId, out MinerSign minerSign))
                 {
                     minerSign.Update(t.Result);
                 }
                 else
                 {
                     Add(MinerSign.Create(t.Result));
                 }
             }
         });
     }, this.GetType());
 }
Exemplo n.º 3
0
        public MinerSignSet(IMinerDataRedis redis)
        {
            _redis = redis;
            Stopwatch stopwatch = new Stopwatch();

            stopwatch.Start();
            redis.GetAllAsync().ContinueWith(t => {
                _initedOn = DateTime.Now;
                foreach (var item in t.Result)
                {
                    _dicByClientId[item.ClientId] = MinerSign.Create(item);
                }
                IsReadied = true;
                stopwatch.Stop();
                NTMinerConsole.UserOk($"矿机签名集就绪,耗时 {stopwatch.GetElapsedSeconds().ToString("f2")} 秒");
                VirtualRoot.RaiseEvent(new MinerSignSetInitedEvent());
            });
            // 收到Mq消息之前一定已经初始化完成,因为Mq消费者在MinerSignSetInitedEvent事件之后才会创建
            VirtualRoot.BuildEventPath <MinerDataRemovedMqEvent>("收到MinerClientRemovedMq消息后移除内存中对应的记录", LogEnum.None, this.GetType(), PathPriority.Normal, path: message => {
                #region
                if (message.AppId == ServerRoot.HostConfig.ThisServerAddress)
                {
                    return;
                }
                if (IsOldMqMessage(message.Timestamp))
                {
                    NTMinerConsole.UserOk(nameof(MinerDataRemovedMqEvent) + ":" + MqKeyword.SafeIgnoreMessage);
                    return;
                }
                if (_dicByClientId.TryRemove(message.ClientId, out MinerSign minerSign))
                {
                    if (AppRoot.MinerClientSessionSet.TryGetByClientId(minerSign.ClientId, out IMinerClientSession ntminerSession))
                    {
                        ntminerSession.CloseAsync(WsCloseCode.Normal, "服务端移除了该矿机");
                    }
                }
                #endregion
            });
            VirtualRoot.BuildEventPath <MinerDatasRemovedMqEvent>("收到MinerClientsRemovedMq消息后移除内存中对应的记录", LogEnum.None, this.GetType(), PathPriority.Normal, path: message => {
                #region
                if (message.AppId == ServerRoot.HostConfig.ThisServerAddress)
                {
                    return;
                }
                if (IsOldMqMessage(message.Timestamp))
                {
                    NTMinerConsole.UserOk(nameof(MinerDataRemovedMqEvent) + ":" + MqKeyword.SafeIgnoreMessage);
                    return;
                }
                foreach (var clientId in message.ClientIds)
                {
                    if (_dicByClientId.TryRemove(clientId, out MinerSign minerSign))
                    {
                        if (AppRoot.MinerClientSessionSet.TryGetByClientId(clientId, out IMinerClientSession ntminerSession))
                        {
                            ntminerSession.CloseAsync(WsCloseCode.Normal, "服务端移除了该矿机");
                        }
                    }
                }
                #endregion
            });
            VirtualRoot.BuildEventPath <Per1SecondEvent>("每秒钟将暂存的新设置的MinerSign发送到Mq", LogEnum.None, this.GetType(), PathPriority.Normal, message => {
                Task.Factory.StartNew(() => {
                    MinerSign[] minerSignsSeted;
                    lock (_lockForMinerSignsSeted) {
                        minerSignsSeted = _minerSignsSeted.ToArray();
                        _minerSignsSeted.Clear();
                    }
                    AppRoot.MinerClientMqSender.SendMinerSignsSeted(minerSignsSeted);
                });
            });
        }
Exemplo n.º 4
0
        public MinerSignSet(IMinerDataRedis redis)
        {
            _redis = redis;
            Stopwatch stopwatch = new Stopwatch();

            stopwatch.Start();
            redis.GetAllAsync().ContinueWith(t => {
                _initedOn = DateTime.Now;
                foreach (var item in t.Result)
                {
                    _dicByClientId[item.ClientId] = MinerSign.Create(item);
                }
                IsReadied = true;
                stopwatch.Stop();
                NTMinerConsole.UserOk($"矿机签名集就绪,耗时 {stopwatch.GetElapsedSeconds().ToString("f2")} 秒");
                VirtualRoot.RaiseEvent(new MinerSignSetInitedEvent());
            });
            // 收到Mq消息之前一定已经初始化完成,因为Mq消费者在MinerSignSetInitedEvent事件之后才会创建
            VirtualRoot.BuildEventPath <MinerDataRemovedMqEvent>("收到MinerClientRemovedMq消息后移除内存中对应的记录", LogEnum.None, path: message => {
                #region
                if (message.AppId == ServerRoot.HostConfig.ThisServerAddress)
                {
                    return;
                }
                if (string.IsNullOrEmpty(message.MinerId))
                {
                    return;
                }
                if (IsOldMqMessage(message.Timestamp))
                {
                    NTMinerConsole.UserOk(nameof(MinerDataRemovedMqEvent) + ":" + MqKeyword.SafeIgnoreMessage);
                    return;
                }
                if (_dicByClientId.TryRemove(message.ClientId, out MinerSign minerSign))
                {
                    if (AppRoot.MinerClientSessionSet.TryGetByClientId(minerSign.ClientId, out IMinerClientSession ntminerSession))
                    {
                        ntminerSession.CloseAsync(WsCloseCode.Normal, "服务端移除了该矿机");
                    }
                }
                #endregion
            }, this.GetType());
            VirtualRoot.BuildEventPath <MinerSignSetedMqEvent>("收到MinerSignSetedMq消息后更新内存中对应的记录", LogEnum.None, path: message => {
                #region
                if (message.AppId == ServerRoot.HostConfig.ThisServerAddress)
                {
                    return;
                }
                if (message.Data == null)
                {
                    return;
                }
                if (IsOldMqMessage(message.Timestamp))
                {
                    NTMinerConsole.UserOk(nameof(MinerSignSetedMqEvent) + ":" + MqKeyword.SafeIgnoreMessage);
                    return;
                }
                _dicByClientId[message.Data.ClientId] = message.Data;
                #endregion
            }, this.GetType());
        }