-
-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a036d8a
commit 817fcad
Showing
19 changed files
with
379 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 18 additions & 19 deletions
37
src/Common/SI.GameServer.Client/GameServerClientOptions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,26 @@ | ||
using System; | ||
|
||
namespace SI.GameServer.Client | ||
namespace SI.GameServer.Client; | ||
|
||
/// <summary> | ||
/// Provides SIGame server client options. | ||
/// </summary> | ||
public sealed class GameServerClientOptions | ||
{ | ||
public const string ConfigurationSectionName = "GameServerClient"; | ||
|
||
/// <summary> | ||
/// Provides SIGame server client options. | ||
/// SIGame server Uri. | ||
/// </summary> | ||
public sealed class GameServerClientOptions | ||
{ | ||
public const string ConfigurationSectionName = "GameServerClient"; | ||
public string? ServiceUri { get; set; } | ||
|
||
/// <summary> | ||
/// SIGame server Uri. | ||
/// </summary> | ||
public string? ServiceUri { get; set; } | ||
|
||
/// <summary> | ||
/// SIGame service discovery Uri. | ||
/// </summary> | ||
public Uri? ServiceDiscoveryUri { get; set; } = new Uri("https://vladimirkhil.com/api/si/servers"); | ||
/// <summary> | ||
/// SIGame service discovery Uri. | ||
/// </summary> | ||
public Uri? ServiceDiscoveryUri { get; set; } = new Uri("https://vladimirkhil.com/api/si/servers"); | ||
|
||
/// <summary> | ||
/// Client timeout. | ||
/// </summary> | ||
public TimeSpan Timeout { get; set; } = TimeSpan.FromMinutes(6); // Large value for uploading packages | ||
} | ||
/// <summary> | ||
/// Client timeout. | ||
/// </summary> | ||
public TimeSpan Timeout { get; set; } = TimeSpan.FromMinutes(6); // Large value for uploading packages | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
using SIData; | ||
using System; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace SI.GameServer.Client; | ||
|
||
/// <summary> | ||
/// Defines common SI server client. | ||
/// </summary> | ||
public interface IGameClient : IAsyncDisposable | ||
{ | ||
/// <summary> | ||
/// Message received event. | ||
/// </summary> | ||
event Action<Message> IncomingMessage; | ||
|
||
/// <summary> | ||
/// Reconnecting event. | ||
/// </summary> | ||
event Func<Exception?, Task> Reconnecting; | ||
|
||
/// <summary> | ||
/// Reconnected event. | ||
/// </summary> | ||
event Func<string?, Task> Reconnected; | ||
|
||
/// <summary> | ||
/// Connection closed event. | ||
/// </summary> | ||
event Func<Exception?, Task> Closed; | ||
|
||
/// <summary> | ||
/// Sends message to server. | ||
/// </summary> | ||
/// <param name="message">Message to send.</param> | ||
/// <param name="cancellationToken">Cancellation token.</param> | ||
Task SendMessageAsync(Message message, CancellationToken cancellationToken = default); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
using Microsoft.AspNetCore.SignalR.Client; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using SI.GameServer.Client.Properties; | ||
using SI.GameServer.Contract; | ||
using SIData; | ||
using System; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace SI.GameServer.Client; | ||
|
||
/// <summary> | ||
/// Defines SIHost client. | ||
/// </summary> | ||
public sealed class SIHostClient : IGameClient | ||
{ | ||
private readonly HubConnection _connection; | ||
private readonly SIHostClientOptions _options; | ||
|
||
public event Action<Message>? IncomingMessage; | ||
|
||
public event Func<Exception?, Task>? Reconnecting; | ||
public event Func<string?, Task>? Reconnected; | ||
public event Func<Exception?, Task>? Closed; | ||
|
||
public SIHostClient(HubConnection connection, SIHostClientOptions options) | ||
{ | ||
_connection = connection; | ||
_options = options; | ||
|
||
_connection.Reconnecting += async e => | ||
{ | ||
if (Reconnecting != null) | ||
{ | ||
await Reconnecting(e); | ||
} | ||
}; | ||
|
||
_connection.Reconnected += async s => | ||
{ | ||
if (Reconnected != null) | ||
{ | ||
await Reconnected(s); | ||
} | ||
}; | ||
|
||
_connection.Closed += OnConnectionClosedAsync; | ||
|
||
_connection.On<Message>("Receive", (message) => IncomingMessage?.Invoke(message)); | ||
|
||
_connection.On("Disconnect", async () => | ||
{ | ||
IncomingMessage?.Invoke(new Message(Resources.YourWereKicked, "@", isSystem: false)); | ||
|
||
await _connection.StopAsync(); | ||
}); | ||
} | ||
|
||
public static async Task<SIHostClient> CreateAsync(SIHostClientOptions options, CancellationToken cancellationToken) | ||
{ | ||
var serviceUri = options.ServiceUri?.ToString() ?? ""; | ||
|
||
if (!serviceUri.EndsWith('/')) | ||
{ | ||
serviceUri += "/"; | ||
} | ||
|
||
var connection = new HubConnectionBuilder() | ||
.WithUrl($"{serviceUri}sihost") | ||
.WithAutomaticReconnect() | ||
.AddMessagePackProtocol() | ||
.Build(); | ||
|
||
connection.HandshakeTimeout = options.HandshakeTimeout; | ||
|
||
await connection.StartAsync(cancellationToken); | ||
|
||
return new SIHostClient(connection, options); | ||
} | ||
|
||
public Task<JoinGameResponse> JoinGameAsync( | ||
JoinGameRequest joinGameRequest, | ||
CancellationToken cancellationToken = default) => | ||
_connection.InvokeAsync<JoinGameResponse>("JoinGame", joinGameRequest, cancellationToken); | ||
|
||
public Task SendMessageAsync(Message message, CancellationToken cancellationToken = default) => | ||
_connection.InvokeAsync("SendMessage", message, cancellationToken); | ||
|
||
public Task LeaveGameAsync(CancellationToken cancellationToken = default) => | ||
_connection.InvokeAsync("LeaveGame", cancellationToken); | ||
|
||
private Task OnConnectionClosedAsync(Exception? exc) | ||
{ | ||
// TODO: recreate connection and retry | ||
|
||
return Closed != null ? Closed(exc) : Task.CompletedTask; | ||
} | ||
|
||
public ValueTask DisposeAsync() => _connection.DisposeAsync(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
using System; | ||
|
||
namespace SI.GameServer.Client; | ||
|
||
/// <summary> | ||
/// Defines SIHost client options. | ||
/// </summary> | ||
public sealed class SIHostClientOptions | ||
{ | ||
/// <summary> | ||
/// SIHost service Uri. | ||
/// </summary> | ||
public Uri? ServiceUri { get; set; } | ||
|
||
/// <summary> | ||
/// Initial handshake timeout. | ||
/// </summary> | ||
public TimeSpan HandshakeTimeout { get; set; } = TimeSpan.FromMinutes(2); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.