Skip to content
Open
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
21 changes: 18 additions & 3 deletions src/FiveStack.Services/MatchEvents.cs
Original file line number Diff line number Diff line change
Expand Up @@ -206,15 +206,17 @@ await _webSocket.CloseAsync(
{
_logger.LogWarning($"Error closing existing WebSocket: {ex.Message}");
}
_webSocket.Dispose();
_webSocket = null;
}

_connectionCts?.Cancel();
_connectionCts = new CancellationTokenSource();

ClientWebSocket? newWebSocket = null;
try
{
_webSocket = new ClientWebSocket();
newWebSocket = new ClientWebSocket();

_environmentService.Load();

Expand All @@ -224,18 +226,21 @@ await _webSocket.CloseAsync(
if (serverId == null || serverApiPassword == null)
{
_logger.LogWarning("Cannot connect to WebSocket, Missing Server ID / API Password");
newWebSocket.Dispose();
return false;
}

_webSocket.Options.SetRequestHeader(
newWebSocket.Options.SetRequestHeader(
"Authorization",
$"Basic {Convert.ToBase64String(
System.Text.Encoding.UTF8.GetBytes($"{serverId}:{serverApiPassword}")
)}"
);

var uri = new Uri($"{_environmentService.GetWsUrl()}/matches");
await _webSocket.ConnectAsync(uri, _connectionCts.Token);
await newWebSocket.ConnectAsync(uri, _connectionCts.Token);

_webSocket = newWebSocket;

_matchService.GetMatchFromApi();

Expand All @@ -244,11 +249,13 @@ await _webSocket.CloseAsync(
catch (WebSocketException ex)
{
_logger.LogWarning("Failed to connect to WebSocket server: " + ex.Message);
newWebSocket?.Dispose();
return false;
}
catch (Exception ex)
{
_logger.LogCritical("An error occurred: " + ex.Message);
newWebSocket?.Dispose();
return false;
}
}
Expand All @@ -259,6 +266,7 @@ public async Task Disconnect()
_connectionCts?.Cancel();
_isMonitoring = false;
_retryTimer.Stop();
_retryTimer?.Dispose();

if (_webSocket != null && _webSocket.State == WebSocketState.Open)
{
Expand All @@ -267,6 +275,7 @@ await _webSocket.CloseAsync(
"Disconnecting",
CancellationToken.None
);
_webSocket.Dispose();
_webSocket = null;
}
}
Expand Down Expand Up @@ -324,6 +333,12 @@ private async Task Publish<T>(Guid matchId, Guid mapId, EventData<T> data)
if (data is EventData<Dictionary<string, object>> typedData)
{
_pendingMessages[data.messageId] = (typedData, DateTime.UtcNow);

if (_pendingMessages.Count > 1000)
{
var oldest = _pendingMessages.OrderBy(p => p.Value.Timestamp).First();
_pendingMessages.Remove(oldest.Key);
}
}

try
Expand Down
72 changes: 37 additions & 35 deletions src/FiveStack.Services/MatchService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,6 @@ public async void GetMatchFromApi()
return;
}

HttpClient httpClient = new HttpClient();

string? serverId = _environmentService.GetServerId();
string? apiPassword = _environmentService.GetServerApiPassword();

Expand All @@ -97,53 +95,57 @@ public async void GetMatchFromApi()

try
{
string matchUri = $"{_environmentService.GetApiUrl()}/matches/current-match/{serverId}";
using (var httpClient = new HttpClient())
{
string matchUri =
$"{_environmentService.GetApiUrl()}/matches/current-match/{serverId}";

_logger.LogInformation($"Fetching Match Info for server : {serverId}");
_logger.LogInformation($"Fetching Match Info for server : {serverId}");

httpClient.DefaultRequestHeaders.Authorization =
new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", apiPassword);
httpClient.DefaultRequestHeaders.Authorization =
new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", apiPassword);

string? response = await httpClient.GetStringAsync(matchUri);
string? response = await httpClient.GetStringAsync(matchUri);

Server.NextFrame(() =>
{
if (response.Length == 0)
Server.NextFrame(() =>
{
if (_currentMatch != null)
if (response.Length == 0)
{
_currentMatch.Reset();
}
if (_currentMatch != null)
{
_currentMatch.Reset();
}

_currentMatch = null;
_currentMatch = null;

_logger.LogWarning("currenlty no match assigned to server");
return;
}
_logger.LogWarning("currenlty no match assigned to server");
return;
}

MatchData? matchData = JsonSerializer.Deserialize<MatchData>(response);
MatchData? matchData = JsonSerializer.Deserialize<MatchData>(response);

if (matchData == null)
{
return;
}
if (matchData == null)
{
return;
}

if (_currentMatch?.GetMatchData()?.id == matchData.id)
{
_currentMatch.SetupMatch(matchData);
return;
}
if (_currentMatch?.GetMatchData()?.id == matchData.id)
{
_currentMatch.SetupMatch(matchData);
return;
}

if (_currentMatch != null)
{
_currentMatch.Reset();
}
if (_currentMatch != null)
{
_currentMatch.Reset();
}

_currentMatch =
_serviceProvider.GetRequiredService(typeof(MatchManager)) as MatchManager;
_currentMatch =
_serviceProvider.GetRequiredService(typeof(MatchManager)) as MatchManager;

_currentMatch!.SetupMatch(matchData);
});
_currentMatch!.SetupMatch(matchData);
});
}
}
catch (HttpRequestException ex)
{
Expand Down
2 changes: 1 addition & 1 deletion src/FiveStack.Services/SurrenderSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,8 @@ public void Reset()
{
timer?.Kill();
}
_disconnectTimers[team].Clear();
}
_disconnectTimers.Clear();
}

public bool IsSurrendering()
Expand Down
Loading