Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 30 additions & 13 deletions GamesDat.Demo/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,26 +21,43 @@ static async Task Main(string[] args)
static async Task CaptureWarThunderSession()
{
var cts = new CancellationTokenSource();
Console.CancelKeyPress += (s, e) =>
ConsoleCancelEventHandler handler = (s, e) =>
{
e.Cancel = true;
cts.Cancel();
};
Console.CancelKeyPress += handler;

var session = new GameSession()
.AddSource(
WarThunderSources.CreateStateSource()
.UseWriter(new BinarySessionWriter())
.OutputTo($"./sessions/warthunder_{DateTime.UtcNow:yyyyMMdd_HHmmss}.bin"))
.OnData<StateData>(data =>
{
Console.WriteLine($"Altitude: {data.Altitude} | Speed {data.TrueAirspeed} | Throttle {data.Throttle}");
});
try
{
var session = new GameSession()
.AddSource(
WarThunderSources.CreateStateSource()
.UseWriter(new BinarySessionWriter())
.OutputTo($"./sessions/warthunder_{DateTime.UtcNow:yyyyMMdd_HHmmss}.bin"))
.OnData<StateData>(data =>
{
Console.WriteLine($"Altitude: {data.Altitude} | Speed {data.TrueAirspeed} | Throttle {data.Throttle}");
});

await session.StartAsync(cts.Token);
await session.StartAsync(cts.Token);

// Session is now running, wait for cancellation
await Task.Delay(Timeout.Infinite, cts.Token);
// Session is now running, wait for cancellation
await Task.Delay(Timeout.Infinite, cts.Token);
}
catch (FileNotFoundException)
{
Console.WriteLine("\nError: War Thunder is not running. Start War Thunder and try again.");
}
catch (OperationCanceledException)
{
Console.WriteLine("\nCapture stopped by user.");
}
finally
{
Console.CancelKeyPress -= handler;
cts.Dispose();
}
}
#endregion

Expand Down
29 changes: 25 additions & 4 deletions GamesDat/Telemetry/Sources/HttpPollingSourceBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,33 @@ public override async IAsyncEnumerable<T> ReadContinuousAsync(
// JSON parse errors are logged but don't trigger aggressive retry
Console.WriteLine($"[{GetType().Name}] JSON parse error (skipping frame): {ex.Message}");
}
catch (OperationCanceledException)
catch (OperationCanceledException ex)
{
// Clean cancellation
yield break;
if (cancellationToken.IsCancellationRequested)
{
// Clean cancellation requested by caller
yield break;
}

// Timeout cancellation from linkedCts (request timeout) - treat as connection error
_consecutiveErrors++;

if (firstError)
{
Console.WriteLine($"[{GetType().Name}] Connection error: {ex.Message}");
Console.WriteLine($"[{GetType().Name}] Retrying with exponential backoff...");
firstError = false;
}

if (_consecutiveErrors >= _options.MaxConsecutiveErrors)
{
errorToThrow = new InvalidOperationException(
$"Failed to connect after {_consecutiveErrors} consecutive attempts. " +
$"Ensure the game is running and the API is accessible at {fullUrl}",
ex);
}
}
catch (Exception ex) when (ex is HttpRequestException or TaskCanceledException)
catch (HttpRequestException ex)
{
// Connection/timeout errors - use exponential backoff
_consecutiveErrors++;
Expand Down
4 changes: 4 additions & 0 deletions GamesDat/Telemetry/Sources/HttpPollingSourceOptions.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;

namespace GamesDat.Core.Telemetry.Sources;

/// <summary>
Expand Down
4 changes: 2 additions & 2 deletions docs/WarThunder.md
Original file line number Diff line number Diff line change
Expand Up @@ -370,5 +370,5 @@ War Thunder API documentation and community tools:
## See Also

- [Creating Custom Sources](CREATING_SOURCES.md)
- [GameSession API Reference](API_REFERENCE.md)
- [Performance Tuning Guide](PERFORMANCE.md)
- GameSession API Reference (coming soon)
- Performance Tuning Guide (coming soon)