private async Task HandleIncomingData(RemotePlayer player, StreamReader reader, CancellationToken cancellationToken) { while (!cancellationToken.IsCancellationRequested) { var line = (await reader.ReadLineAsync(cancellationToken))?.Trim().ToUpper(); switch (line) { case "LEFT": player.SetNextMove(MoveDisposition.TurnLeft); break; case "RIGHT": player.SetNextMove(MoveDisposition.TurnRight); break; case "STRAIGHT": player.SetNextMove(MoveDisposition.GoStraight); break; default: // ignore everything else break; } } }
private async Task HandleOutgoingData(RemotePlayer player, StreamWriter writer, CancellationToken cancellationToken) { var dataToSend = new BufferBlock<string>(); var statusUpdater = new Action<string>(state => dataToSend.Post(state)); try { player.GameStateUpdated += statusUpdater; while (!cancellationToken.IsCancellationRequested) { var data = await dataToSend.ReceiveAsync(cancellationToken); await writer.WriteLineAsync(data, cancellationToken); } } finally { player.GameStateUpdated -= statusUpdater; } }