static public void ResetCredentials(string btc = null, string worker = null, string group = null) { // TODO check protocol // send status first and re-set credentials var minerStatusJsonStr = CreateMinerStatusMessage(); var minerStatus = new NHSendMessage(MessageType.SEND_MESSAGE_STATUS, minerStatusJsonStr); _sendQueue.Enqueue(new NHSendMessage[1] { minerStatus }); // TODO check SetCredentials(btc, worker, group); }
static public void SetCredentials(string btc = null, string worker = null, string group = null) { _login.rig = ApplicationStateManager.RigID; if (btc != null) { _login.btc = btc; } if (worker != null) { _login.worker = worker; } if (group != null) { _login.group = group; } // on credentials change always send close websocket message var closeMsg = new NHSendMessage(MessageType.CLOSE_WEBSOCKET, $"Credentials change reconnecting {ApplicationStateManager.Title}."); _sendQueue.Enqueue(new NHSendMessage[1] { closeMsg }); }
// TODO add cancelation token static private async Task NewConnection(CancellationToken stop) { NHLog.Info("NHWebSocket", "STARTING nhmws SESSION"); try { // TODO if we fill the Queue before this we should start fresh afterwards // TODO think if we might want to dump prev data???? // on each new connection clear the ConcurrentQueues, _recieveQueue = new ConcurrentQueue <MessageEventArgs>(); _sendQueue = new ConcurrentQueue <IEnumerable <NHSendMessage> >(); _isNhmwsRestart = false; _notifyMinerStatusAfter.Value = null; NHLog.Info("NHWebSocket", "Creating socket"); using (_webSocket = new WebSocket(_address, true)) { //stop.Register(() => _webSocket.Close(CloseStatusCode.Normal, "Closing CancellationToken")); _webSocket.OnOpen += Login; _webSocket.OnMessage += (s, eMsg) => _recieveQueue.Enqueue(eMsg); _webSocket.OnError += (s, e) => NHLog.Info("NHWebSocket", $"Error occured: {e.Message}"); _webSocket.OnClose += (s, e) => NHLog.Info("NHWebSocket", $"Connection closed code {e.Code}: {e.Reason}");; _webSocket.Log.Level = LogLevel.Debug; _webSocket.Log.Output = (data, s) => NHLog.Info("NHWebSocket", data.ToString()); _webSocket.EnableRedirection = true; NHLog.Info("NHWebSocket", "Connecting"); _webSocket.Connect(); const int minerStatusTickSeconds = 45; var checkWaitTime = TimeSpan.FromMilliseconds(50); var skipMinerStatus = !CredentialValidators.ValidateBitcoinAddress(_login.btc); NHLog.Info("NHWebSocket", "Starting Loop"); while (IsWsAlive && !stop.IsCancellationRequested) { if (IsWsAlive) { HandleSendMessage(); } if (IsWsAlive) { await HandleReceiveMessage(); } // TODO add here the last miner status send check if (IsWsAlive) { await Task.Delay(checkWaitTime); // TODO add cancelation token here } if (skipMinerStatus) { continue; } var elapsedTime = DateTime.UtcNow - _lastSendMinerStatusTimestamp.Value; if (elapsedTime.TotalSeconds > minerStatusTickSeconds) { var minerStatusJsonStr = CreateMinerStatusMessage(); var minerStatus = new NHSendMessage(MessageType.SEND_MESSAGE_STATUS, minerStatusJsonStr); _sendQueue.Enqueue(new NHSendMessage[1] { minerStatus }); } if (_notifyMinerStatusAfter.Value.HasValue && DateTime.UtcNow >= _notifyMinerStatusAfter.Value.Value) { _notifyMinerStatusAfter.Value = null; var minerStatusJsonStr = CreateMinerStatusMessage(); var minerStatus = new NHSendMessage(MessageType.SEND_MESSAGE_STATUS, minerStatusJsonStr); _sendQueue.Enqueue(new NHSendMessage[1] { minerStatus }); } } // Ws closed NHLog.Info("NHWebSocket", "Exited Loop"); } } finally { NHLog.Info("NHWebSocket", "ENDING nhmws SESSION"); } }