private async Task BroadcastSegmentAsync(EngineOutputSegment segment) { if (segment == null) { return; } string message = JsonConvert.SerializeObject(segment); var removeSocketIds = new List <string>(); var allClients = this.WebSocketConnectionManager?.GetAll(); if (allClients == null) { return; } foreach (KeyValuePair <string, WebSocket> keyValuePair in allClients) { try { KeyValuePair <string, WebSocket> pair = keyValuePair; if (pair.Value.State == WebSocketState.Open) { await this.SendMessageAsync(pair.Value, message); } else { removeSocketIds.Add(pair.Key); } } catch (Exception e) { this.logger.LogWarning($"Failure to send segment to a particular websocket.", e); removeSocketIds.Add(keyValuePair.Key); } } if (removeSocketIds.Count > 0) { this.logger.LogInformation($"Found {removeSocketIds.Count} dead sockets to clean up."); foreach (var socketId in removeSocketIds) { try { this.WebSocketConnectionManager.RemoveSocket(socketId).Wait(); } catch (Exception e) { this.logger.LogWarning($"Failed to remove socket with id {socketId}.", e); } } } }
private void HandleEngineOutputReceived(string outputText) { if (outputText == null || !this.isEval) { return; } EvaluatedLine line = null; lock (this.bestLines) { if (this.PositionFen == null) { throw new ArgumentException($"Cannot handle engine output because PositionFen is null."); } line = this.lineParser.ParseLineText(this.PositionFen, outputText); if (line == null) { return; } bool containsUpdate = false; if (!this.bestLines.ContainsKey(line.LineRank)) { this.bestLines.Add(line.LineRank, line); containsUpdate = true; } else if (line.LineSan.Split().Length >= this.bestLines[line.LineRank].LineSan.Split().Length) { this.bestLines[line.LineRank] = line; containsUpdate = true; } if (!containsUpdate) { return; } } var segment = new EngineOutputSegment() { Output = outputText, Line = line, BestLines = this.bestLines, }; this.ReceivedEvaluationUpdate?.Invoke(this, segment); }
private void ErrorReceived(string errorText) { if (errorText == null) { return; } Console.Error.WriteLine(errorText); var segment = new EngineOutputSegment() { Error = errorText, }; this.BroadcastSegmentAsync(segment).Wait(); }