protected override Dictionary <string, Action <BasicDeliverEventArgs> > GetPaths() { return(new Dictionary <string, Action <BasicDeliverEventArgs> > { [MqKeyword.SpeedsRoutingKey] = ea => { ClientIdIp[] clientIdIps = MinerClientMqBodyUtil.GetClientIdIpsMqReciveBody(ea.Body); string appId = ea.BasicProperties.AppId; VirtualRoot.RaiseEvent(new SpeedDatasMqEvent(appId, clientIdIps, ea.GetTimestamp())); }, [MqKeyword.MinerSignSetedRoutingKey] = ea => { string appId = ea.BasicProperties.AppId; MinerSign minerSign = MinerClientMqBodyUtil.GetMinerSignMqReceiveBody(ea.Body); if (minerSign != null) { VirtualRoot.RaiseEvent(new MinerSignSetedMqEvent(appId, minerSign, ea.GetTimestamp())); } }, [MqKeyword.QueryClientsForWsRoutingKey] = ea => { string appId = ea.BasicProperties.AppId; string mqMessageId = ea.BasicProperties.MessageId; string loginName = ea.BasicProperties.ReadHeaderString(MqKeyword.LoginNameHeaderName); string sessionId = ea.BasicProperties.ReadHeaderString(MqKeyword.SessionIdHeaderName); QueryClientsForWsRequest query = MinerClientMqBodyUtil.GetQueryClientsForWsMqReceiveBody(ea.Body); if (query != null) { VirtualRoot.Execute(new QueryClientsForWsMqCommand(appId, mqMessageId, ea.GetTimestamp(), loginName, sessionId, query)); } } }); }
public QueryClientsForWsMqCommand( string appId, DateTime timestamp, string loginName, string sessionId, QueryClientsForWsRequest query) { this.AppId = appId; this.Timestamp = timestamp; this.LoginName = loginName; this.SessionId = sessionId; this.Query = query; }
public void QueryClientsForWsAsync(QueryClientsForWsRequest query, Action <QueryClientsResponse, Exception> callback) { RpcRoot.JsonRequestBinaryResponseRpcHelper.SignPostAsync( RpcRoot.OfficialServerHost, RpcRoot.OfficialServerPort, _controllerName, nameof(IClientDataBinaryController <HttpResponseMessage> .QueryClientsForWs), data: query, callback, timeountMilliseconds: 3000); }
public void SendQueryClientsForWs(string sessionId, QueryClientsForWsRequest request) { if (string.IsNullOrEmpty(sessionId) || request == null || string.IsNullOrEmpty(request.LoginName)) { return; } var basicProperties = CreateNonePersistentWsBasicProperties(request.LoginName, sessionId); _mq.BasicPublish( routingKey: MqKeyword.QueryClientsForWsRoutingKey, basicProperties: basicProperties, body: MinerClientMqBodyUtil.GetQueryClientsForWsMqSendBody(request)); }
public override bool Go(BasicDeliverEventArgs ea) { switch (ea.RoutingKey) { // 上报的算力放在这里消费,因为只有WebApiServer消费该类型的消息,WsServer不消费该类型的消息 case MqKeyword.SpeedRoutingKey: { Guid clientId = MinerClientMqBodyUtil.GetClientIdMqReciveBody(ea.Body); DateTime timestamp = Timestamp.FromTimestamp(ea.BasicProperties.Timestamp.UnixTime); string appId = ea.BasicProperties.AppId; string minerIp = ea.BasicProperties.ReadHeaderString(MqKeyword.MinerIpHeaderName); VirtualRoot.RaiseEvent(new SpeedDataMqEvent(appId, clientId, minerIp, timestamp)); } break; case MqKeyword.ChangeMinerSignRoutingKey: { string appId = ea.BasicProperties.AppId; MinerSign minerSign = MinerClientMqBodyUtil.GetChangeMinerSignMqReceiveBody(ea.Body); if (minerSign != null) { VirtualRoot.Execute(new ChangeMinerSignMqCommand(appId, minerSign)); } } break; case MqKeyword.QueryClientsForWsRoutingKey: { DateTime timestamp = Timestamp.FromTimestamp(ea.BasicProperties.Timestamp.UnixTime); string appId = ea.BasicProperties.AppId; string mqMessageId = ea.BasicProperties.MessageId; string loginName = ea.BasicProperties.ReadHeaderString(MqKeyword.LoginNameHeaderName); string sessionId = ea.BasicProperties.ReadHeaderString(MqKeyword.SessionIdHeaderName); QueryClientsForWsRequest query = MinerClientMqBodyUtil.GetQueryClientsForWsMqReceiveBody(ea.Body); if (query != null) { VirtualRoot.Execute(new QueryClientsForWsMqCommand(appId, mqMessageId, timestamp, loginName, sessionId, query)); } } break; default: return(false); } return(true); }
public HttpResponseMessage QueryClientsForWs([FromBody] QueryClientsForWsRequest request) { QueryClientsResponse response; if (request == null) { response = ResponseBase.InvalidInput <QueryClientsResponse>("参数错误"); } else { request.PagingTrim(); try { var user = WebApiRoot.UserSet.GetUser(UserId.CreateLoginNameUserId(request.LoginName)); if (user == null) { response = ResponseBase.InvalidInput <QueryClientsResponse>("用户不存在"); } else { var data = WebApiRoot.ClientDataSet.QueryClients( user, request, out int total, out CoinSnapshotData[] latestSnapshots, out int totalOnlineCount, out int totalMiningCount) ?? new List <ClientData>(); response = QueryClientsResponse.Ok(data, total, latestSnapshots, totalMiningCount, totalOnlineCount); } } catch (Exception e) { Logger.ErrorDebugLine(e); response = ResponseBase.ServerError <QueryClientsResponse>(e.Message); } } var httpResponseMessage = new HttpResponseMessage(HttpStatusCode.OK) { Content = new ByteArrayContent(VirtualRoot.BinarySerializer.Serialize(response)) }; httpResponseMessage.Content.Headers.ContentType = WebApiRoot.BinaryContentType; return(httpResponseMessage); }
public override bool Go(BasicDeliverEventArgs ea) { switch (ea.RoutingKey) { case MqKeyword.SpeedsRoutingKey: { ClientIdIp[] clientIdIps = MinerClientMqBodyUtil.GetClientIdIpsMqReciveBody(ea.Body); DateTime timestamp = Timestamp.FromTimestamp(ea.BasicProperties.Timestamp.UnixTime); string appId = ea.BasicProperties.AppId; VirtualRoot.RaiseEvent(new SpeedDatasMqEvent(appId, clientIdIps, timestamp)); } break; case MqKeyword.MinerSignSetedRoutingKey: { string appId = ea.BasicProperties.AppId; DateTime timestamp = Timestamp.FromTimestamp(ea.BasicProperties.Timestamp.UnixTime); MinerSign minerSign = MinerClientMqBodyUtil.GetMinerSignMqReceiveBody(ea.Body); if (minerSign != null) { VirtualRoot.RaiseEvent(new MinerSignSetedMqEvent(appId, minerSign, timestamp)); } } break; case MqKeyword.QueryClientsForWsRoutingKey: { DateTime timestamp = Timestamp.FromTimestamp(ea.BasicProperties.Timestamp.UnixTime); string appId = ea.BasicProperties.AppId; string mqMessageId = ea.BasicProperties.MessageId; string loginName = ea.BasicProperties.ReadHeaderString(MqKeyword.LoginNameHeaderName); string sessionId = ea.BasicProperties.ReadHeaderString(MqKeyword.SessionIdHeaderName); QueryClientsForWsRequest query = MinerClientMqBodyUtil.GetQueryClientsForWsMqReceiveBody(ea.Body); if (query != null) { VirtualRoot.Execute(new QueryClientsForWsMqCommand(appId, mqMessageId, timestamp, loginName, sessionId, query)); } } break; default: return(false); } return(true); }
public static QueryClientsResponse QueryClientsForWs(QueryClientsForWsRequest request) { QueryClientsResponse response; if (request == null) { response = ResponseBase.InvalidInput <QueryClientsResponse>("参数错误"); } else { request.PagingTrim(); try { var user = UserSet.GetUser(UserId.CreateLoginNameUserId(request.LoginName)); if (user == null) { response = ResponseBase.InvalidInput <QueryClientsResponse>("用户不存在"); } else { var data = ClientDataSet.QueryClients( user, request, out int total, out CoinSnapshotData[] latestSnapshots, out int totalOnlineCount, out int totalMiningCount) ?? new List <ClientData>(); response = QueryClientsResponse.Ok(data, total, latestSnapshots, totalMiningCount, totalOnlineCount); } } catch (Exception e) { Logger.ErrorDebugLine(e); response = ResponseBase.ServerError <QueryClientsResponse>(e.Message); } } return(response); }
protected override void OnMessage(MessageEventArgs e) { if (e.IsPing) { WsRoot.MinerStudioSessionSet.ActiveByWsSessionId(base.ID, out _); return; } WsMessage message = e.ToWsMessage <WsMessage>(); if (message == null) { return; } if (!WsRoot.MinerStudioSessionSet.TryGetByWsSessionId(this.ID, out IMinerStudioSession minerSession)) { this.CloseAsync(CloseStatusCode.Normal, "意外,会话不存在,请重新连接"); return; } if (!minerSession.IsValid(message)) { this.CloseAsync(CloseStatusCode.Normal, "意外,签名验证失败,请重新连接"); return; } if (message.Type == WsMessage.QueryClientDatas) { if (message.TryGetData(out QueryClientsRequest query)) { // 走的内网,因为WsServer启动时会设置内网Rpc地址 RpcRoot.OfficialServer.ClientDataBinaryService.QueryClientsForWsAsync(QueryClientsForWsRequest.Create(query, minerSession.LoginName), (QueryClientsResponse response, Exception ex) => { if (response.IsSuccess()) { var userData = WsRoot.ReadOnlyUserSet.GetUser(UserId.CreateLoginNameUserId(minerSession.LoginName)); if (userData != null) { this.SendAsync(new WsMessage(Guid.NewGuid(), WsMessage.ClientDatas) { Data = response }.SignToBytes(userData.Password), completed: null); } } }); } return; } if (MinerStudioWsMessageHandler.TryGetHandler(message.Type, out Action <string, WsMessage> handler)) { try { handler.Invoke(minerSession.LoginName, message); } catch (Exception ex) { Logger.ErrorDebugLine(ex); } } else { NTMinerConsole.UserWarn($"{_behaviorName} {nameof(OnMessage)} Received InvalidType {e.Data}"); } }
public static void AutoQueryClientDatas(QueryClientsForWsRequest query) { lock (_lockForAutoQueryClientDatas) { _autoQueryClientDatas.Add(query); } }
public static byte[] GetQueryClientsForWsMqSendBody(QueryClientsForWsRequest request) { return(Encoding.UTF8.GetBytes(VirtualRoot.JsonSerializer.Serialize(request))); }
public static void HandleMinerStudioMessage(IWsSessionAdapter session, WsMessage message) { if (message == null) { return; } if (!WsRoot.MinerStudioSessionSet.TryGetByWsSessionId(session.SessionId, out IMinerStudioSession minerSession)) { session.CloseAsync(WsCloseCode.Normal, "意外,会话不存在,请重新连接"); return; } if (!minerSession.IsValid(message)) { session.CloseAsync(WsCloseCode.Normal, "意外,签名验证失败,请重新连接"); return; } if (message.Type == WsMessage.QueryClientDatas) { if (message.TryGetData(out QueryClientsRequest query)) { // 走的内网,因为WsServer启动时会设置内网Rpc地址 RpcRoot.OfficialServer.ClientDataBinaryService.QueryClientsForWsAsync(QueryClientsForWsRequest.Create(query, minerSession.LoginName), (QueryClientsResponse response, Exception ex) => { if (response.IsSuccess()) { var userData = WsRoot.ReadOnlyUserSet.GetUser(UserId.CreateLoginNameUserId(minerSession.LoginName)); if (userData != null) { session.SendAsync(new WsMessage(Guid.NewGuid(), WsMessage.ClientDatas) { Data = response }.SignToBytes(userData.Password)); } } }); } return; } if (MinerStudioWsMessageHandler.TryGetHandler(message.Type, out Action <string, WsMessage> handler)) { try { handler.Invoke(minerSession.LoginName, message); } catch (Exception ex) { Logger.ErrorDebugLine(ex); } } else { NTMinerConsole.UserWarn($"{session.TypeName} {nameof(HandleMinerStudioMessage)} Received InvalidType {message.Type}"); } }